]> Pileus Git - ~andy/linux/blob - drivers/staging/android/ashmem.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[~andy/linux] / drivers / staging / android / ashmem.c
1 /* mm/ashmem.c
2  *
3  * Anonymous Shared Memory Subsystem, ashmem
4  *
5  * Copyright (C) 2008 Google, Inc.
6  *
7  * Robert Love <rlove@google.com>
8  *
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.
12  *
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.
17  */
18
19 #define pr_fmt(fmt) "ashmem: " fmt
20
21 #include <linux/module.h>
22 #include <linux/file.h>
23 #include <linux/fs.h>
24 #include <linux/falloc.h>
25 #include <linux/miscdevice.h>
26 #include <linux/security.h>
27 #include <linux/mm.h>
28 #include <linux/mman.h>
29 #include <linux/uaccess.h>
30 #include <linux/personality.h>
31 #include <linux/bitops.h>
32 #include <linux/mutex.h>
33 #include <linux/shmem_fs.h>
34 #include "ashmem.h"
35
36 #define ASHMEM_NAME_PREFIX "dev/ashmem/"
37 #define ASHMEM_NAME_PREFIX_LEN (sizeof(ASHMEM_NAME_PREFIX) - 1)
38 #define ASHMEM_FULL_NAME_LEN (ASHMEM_NAME_LEN + ASHMEM_NAME_PREFIX_LEN)
39
40 /*
41  * ashmem_area - anonymous shared memory area
42  * Lifecycle: From our parent file's open() until its release()
43  * Locking: Protected by `ashmem_mutex'
44  * Big Note: Mappings do NOT pin this structure; it dies on close()
45  */
46 struct ashmem_area {
47         char name[ASHMEM_FULL_NAME_LEN]; /* optional name in /proc/pid/maps */
48         struct list_head unpinned_list;  /* list of all ashmem areas */
49         struct file *file;               /* the shmem-based backing file */
50         size_t size;                     /* size of the mapping, in bytes */
51         unsigned long prot_mask;         /* allowed prot bits, as vm_flags */
52 };
53
54 /*
55  * ashmem_range - represents an interval of unpinned (evictable) pages
56  * Lifecycle: From unpin to pin
57  * Locking: Protected by `ashmem_mutex'
58  */
59 struct ashmem_range {
60         struct list_head lru;           /* entry in LRU list */
61         struct list_head unpinned;      /* entry in its area's unpinned list */
62         struct ashmem_area *asma;       /* associated area */
63         size_t pgstart;                 /* starting page, inclusive */
64         size_t pgend;                   /* ending page, inclusive */
65         unsigned int purged;            /* ASHMEM_NOT or ASHMEM_WAS_PURGED */
66 };
67
68 /* LRU list of unpinned pages, protected by ashmem_mutex */
69 static LIST_HEAD(ashmem_lru_list);
70
71 /* Count of pages on our LRU list, protected by ashmem_mutex */
72 static unsigned long lru_count;
73
74 /*
75  * ashmem_mutex - protects the list of and each individual ashmem_area
76  *
77  * Lock Ordering: ashmex_mutex -> i_mutex -> i_alloc_sem
78  */
79 static DEFINE_MUTEX(ashmem_mutex);
80
81 static struct kmem_cache *ashmem_area_cachep __read_mostly;
82 static struct kmem_cache *ashmem_range_cachep __read_mostly;
83
84 #define range_size(range) \
85         ((range)->pgend - (range)->pgstart + 1)
86
87 #define range_on_lru(range) \
88         ((range)->purged == ASHMEM_NOT_PURGED)
89
90 #define page_range_subsumes_range(range, start, end) \
91         (((range)->pgstart >= (start)) && ((range)->pgend <= (end)))
92
93 #define page_range_subsumed_by_range(range, start, end) \
94         (((range)->pgstart <= (start)) && ((range)->pgend >= (end)))
95
96 #define page_in_range(range, page) \
97         (((range)->pgstart <= (page)) && ((range)->pgend >= (page)))
98
99 #define page_range_in_range(range, start, end) \
100         (page_in_range(range, start) || page_in_range(range, end) || \
101                 page_range_subsumes_range(range, start, end))
102
103 #define range_before_page(range, page) \
104         ((range)->pgend < (page))
105
106 #define PROT_MASK               (PROT_EXEC | PROT_READ | PROT_WRITE)
107
108 static inline void lru_add(struct ashmem_range *range)
109 {
110         list_add_tail(&range->lru, &ashmem_lru_list);
111         lru_count += range_size(range);
112 }
113
114 static inline void lru_del(struct ashmem_range *range)
115 {
116         list_del(&range->lru);
117         lru_count -= range_size(range);
118 }
119
120 /*
121  * range_alloc - allocate and initialize a new ashmem_range structure
122  *
123  * 'asma' - associated ashmem_area
124  * 'prev_range' - the previous ashmem_range in the sorted asma->unpinned list
125  * 'purged' - initial purge value (ASMEM_NOT_PURGED or ASHMEM_WAS_PURGED)
126  * 'start' - starting page, inclusive
127  * 'end' - ending page, inclusive
128  *
129  * Caller must hold ashmem_mutex.
130  */
131 static int range_alloc(struct ashmem_area *asma,
132                        struct ashmem_range *prev_range, unsigned int purged,
133                        size_t start, size_t end)
134 {
135         struct ashmem_range *range;
136
137         range = kmem_cache_zalloc(ashmem_range_cachep, GFP_KERNEL);
138         if (unlikely(!range))
139                 return -ENOMEM;
140
141         range->asma = asma;
142         range->pgstart = start;
143         range->pgend = end;
144         range->purged = purged;
145
146         list_add_tail(&range->unpinned, &prev_range->unpinned);
147
148         if (range_on_lru(range))
149                 lru_add(range);
150
151         return 0;
152 }
153
154 static void range_del(struct ashmem_range *range)
155 {
156         list_del(&range->unpinned);
157         if (range_on_lru(range))
158                 lru_del(range);
159         kmem_cache_free(ashmem_range_cachep, range);
160 }
161
162 /*
163  * range_shrink - shrinks a range
164  *
165  * Caller must hold ashmem_mutex.
166  */
167 static inline void range_shrink(struct ashmem_range *range,
168                                 size_t start, size_t end)
169 {
170         size_t pre = range_size(range);
171
172         range->pgstart = start;
173         range->pgend = end;
174
175         if (range_on_lru(range))
176                 lru_count -= pre - range_size(range);
177 }
178
179 static int ashmem_open(struct inode *inode, struct file *file)
180 {
181         struct ashmem_area *asma;
182         int ret;
183
184         ret = generic_file_open(inode, file);
185         if (unlikely(ret))
186                 return ret;
187
188         asma = kmem_cache_zalloc(ashmem_area_cachep, GFP_KERNEL);
189         if (unlikely(!asma))
190                 return -ENOMEM;
191
192         INIT_LIST_HEAD(&asma->unpinned_list);
193         memcpy(asma->name, ASHMEM_NAME_PREFIX, ASHMEM_NAME_PREFIX_LEN);
194         asma->prot_mask = PROT_MASK;
195         file->private_data = asma;
196
197         return 0;
198 }
199
200 static int ashmem_release(struct inode *ignored, struct file *file)
201 {
202         struct ashmem_area *asma = file->private_data;
203         struct ashmem_range *range, *next;
204
205         mutex_lock(&ashmem_mutex);
206         list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned)
207                 range_del(range);
208         mutex_unlock(&ashmem_mutex);
209
210         if (asma->file)
211                 fput(asma->file);
212         kmem_cache_free(ashmem_area_cachep, asma);
213
214         return 0;
215 }
216
217 static ssize_t ashmem_read(struct file *file, char __user *buf,
218                            size_t len, loff_t *pos)
219 {
220         struct ashmem_area *asma = file->private_data;
221         int ret = 0;
222
223         mutex_lock(&ashmem_mutex);
224
225         /* If size is not set, or set to 0, always return EOF. */
226         if (asma->size == 0)
227                 goto out;
228
229         if (!asma->file) {
230                 ret = -EBADF;
231                 goto out;
232         }
233
234         ret = asma->file->f_op->read(asma->file, buf, len, pos);
235         if (ret < 0)
236                 goto out;
237
238         /** Update backing file pos, since f_ops->read() doesn't */
239         asma->file->f_pos = *pos;
240
241 out:
242         mutex_unlock(&ashmem_mutex);
243         return ret;
244 }
245
246 static loff_t ashmem_llseek(struct file *file, loff_t offset, int origin)
247 {
248         struct ashmem_area *asma = file->private_data;
249         int ret;
250
251         mutex_lock(&ashmem_mutex);
252
253         if (asma->size == 0) {
254                 ret = -EINVAL;
255                 goto out;
256         }
257
258         if (!asma->file) {
259                 ret = -EBADF;
260                 goto out;
261         }
262
263         ret = asma->file->f_op->llseek(asma->file, offset, origin);
264         if (ret < 0)
265                 goto out;
266
267         /** Copy f_pos from backing file, since f_ops->llseek() sets it */
268         file->f_pos = asma->file->f_pos;
269
270 out:
271         mutex_unlock(&ashmem_mutex);
272         return ret;
273 }
274
275 static inline vm_flags_t calc_vm_may_flags(unsigned long prot)
276 {
277         return _calc_vm_trans(prot, PROT_READ,  VM_MAYREAD) |
278                _calc_vm_trans(prot, PROT_WRITE, VM_MAYWRITE) |
279                _calc_vm_trans(prot, PROT_EXEC,  VM_MAYEXEC);
280 }
281
282 static int ashmem_mmap(struct file *file, struct vm_area_struct *vma)
283 {
284         struct ashmem_area *asma = file->private_data;
285         int ret = 0;
286
287         mutex_lock(&ashmem_mutex);
288
289         /* user needs to SET_SIZE before mapping */
290         if (unlikely(!asma->size)) {
291                 ret = -EINVAL;
292                 goto out;
293         }
294
295         /* requested protection bits must match our allowed protection mask */
296         if (unlikely((vma->vm_flags & ~calc_vm_prot_bits(asma->prot_mask)) &
297                      calc_vm_prot_bits(PROT_MASK))) {
298                 ret = -EPERM;
299                 goto out;
300         }
301         vma->vm_flags &= ~calc_vm_may_flags(~asma->prot_mask);
302
303         if (!asma->file) {
304                 char *name = ASHMEM_NAME_DEF;
305                 struct file *vmfile;
306
307                 if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0')
308                         name = asma->name;
309
310                 /* ... and allocate the backing shmem file */
311                 vmfile = shmem_file_setup(name, asma->size, vma->vm_flags);
312                 if (unlikely(IS_ERR(vmfile))) {
313                         ret = PTR_ERR(vmfile);
314                         goto out;
315                 }
316                 asma->file = vmfile;
317         }
318         get_file(asma->file);
319
320         /*
321          * XXX - Reworked to use shmem_zero_setup() instead of
322          * shmem_set_file while we're in staging. -jstultz
323          */
324         if (vma->vm_flags & VM_SHARED) {
325                 ret = shmem_zero_setup(vma);
326                 if (ret) {
327                         fput(asma->file);
328                         goto out;
329                 }
330         }
331
332         if (vma->vm_file)
333                 fput(vma->vm_file);
334         vma->vm_file = asma->file;
335
336 out:
337         mutex_unlock(&ashmem_mutex);
338         return ret;
339 }
340
341 /*
342  * ashmem_shrink - our cache shrinker, called from mm/vmscan.c :: shrink_slab
343  *
344  * 'nr_to_scan' is the number of objects to scan for freeing.
345  *
346  * 'gfp_mask' is the mask of the allocation that got us into this mess.
347  *
348  * Return value is the number of objects freed or -1 if we cannot
349  * proceed without risk of deadlock (due to gfp_mask).
350  *
351  * We approximate LRU via least-recently-unpinned, jettisoning unpinned partial
352  * chunks of ashmem regions LRU-wise one-at-a-time until we hit 'nr_to_scan'
353  * pages freed.
354  */
355 static unsigned long
356 ashmem_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
357 {
358         struct ashmem_range *range, *next;
359         unsigned long freed = 0;
360
361         /* We might recurse into filesystem code, so bail out if necessary */
362         if (!(sc->gfp_mask & __GFP_FS))
363                 return SHRINK_STOP;
364
365         mutex_lock(&ashmem_mutex);
366         list_for_each_entry_safe(range, next, &ashmem_lru_list, lru) {
367                 loff_t start = range->pgstart * PAGE_SIZE;
368                 loff_t end = (range->pgend + 1) * PAGE_SIZE;
369
370                 do_fallocate(range->asma->file,
371                                 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
372                                 start, end - start);
373                 range->purged = ASHMEM_WAS_PURGED;
374                 lru_del(range);
375
376                 freed += range_size(range);
377                 if (--sc->nr_to_scan <= 0)
378                         break;
379         }
380         mutex_unlock(&ashmem_mutex);
381         return freed;
382 }
383
384 static unsigned long
385 ashmem_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
386 {
387         /*
388          * note that lru_count is count of pages on the lru, not a count of
389          * objects on the list. This means the scan function needs to return the
390          * number of pages freed, not the number of objects scanned.
391          */
392         return lru_count;
393 }
394
395 static struct shrinker ashmem_shrinker = {
396         .count_objects = ashmem_shrink_count,
397         .scan_objects = ashmem_shrink_scan,
398         /*
399          * XXX (dchinner): I wish people would comment on why they need on
400          * significant changes to the default value here
401          */
402         .seeks = DEFAULT_SEEKS * 4,
403 };
404
405 static int set_prot_mask(struct ashmem_area *asma, unsigned long prot)
406 {
407         int ret = 0;
408
409         mutex_lock(&ashmem_mutex);
410
411         /* the user can only remove, not add, protection bits */
412         if (unlikely((asma->prot_mask & prot) != prot)) {
413                 ret = -EINVAL;
414                 goto out;
415         }
416
417         /* does the application expect PROT_READ to imply PROT_EXEC? */
418         if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
419                 prot |= PROT_EXEC;
420
421         asma->prot_mask = prot;
422
423 out:
424         mutex_unlock(&ashmem_mutex);
425         return ret;
426 }
427
428 static int set_name(struct ashmem_area *asma, void __user *name)
429 {
430         int ret = 0;
431         char local_name[ASHMEM_NAME_LEN];
432
433         /*
434          * Holding the ashmem_mutex while doing a copy_from_user might cause
435          * an data abort which would try to access mmap_sem. If another
436          * thread has invoked ashmem_mmap then it will be holding the
437          * semaphore and will be waiting for ashmem_mutex, there by leading to
438          * deadlock. We'll release the mutex  and take the name to a local
439          * variable that does not need protection and later copy the local
440          * variable to the structure member with lock held.
441          */
442         if (copy_from_user(local_name, name, ASHMEM_NAME_LEN))
443                 return -EFAULT;
444
445         mutex_lock(&ashmem_mutex);
446         /* cannot change an existing mapping's name */
447         if (unlikely(asma->file)) {
448                 ret = -EINVAL;
449                 goto out;
450         }
451         memcpy(asma->name + ASHMEM_NAME_PREFIX_LEN,
452                 local_name, ASHMEM_NAME_LEN);
453         asma->name[ASHMEM_FULL_NAME_LEN-1] = '\0';
454 out:
455         mutex_unlock(&ashmem_mutex);
456
457         return ret;
458 }
459
460 static int get_name(struct ashmem_area *asma, void __user *name)
461 {
462         int ret = 0;
463         size_t len;
464         /*
465          * Have a local variable to which we'll copy the content
466          * from asma with the lock held. Later we can copy this to the user
467          * space safely without holding any locks. So even if we proceed to
468          * wait for mmap_sem, it won't lead to deadlock.
469          */
470         char local_name[ASHMEM_NAME_LEN];
471
472         mutex_lock(&ashmem_mutex);
473         if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0') {
474
475                 /*
476                  * Copying only `len', instead of ASHMEM_NAME_LEN, bytes
477                  * prevents us from revealing one user's stack to another.
478                  */
479                 len = strlen(asma->name + ASHMEM_NAME_PREFIX_LEN) + 1;
480                 memcpy(local_name, asma->name + ASHMEM_NAME_PREFIX_LEN, len);
481         } else {
482                 len = sizeof(ASHMEM_NAME_DEF);
483                 memcpy(local_name, ASHMEM_NAME_DEF, len);
484         }
485         mutex_unlock(&ashmem_mutex);
486
487         /*
488          * Now we are just copying from the stack variable to userland
489          * No lock held
490          */
491         if (unlikely(copy_to_user(name, local_name, len)))
492                 ret = -EFAULT;
493         return ret;
494 }
495
496 /*
497  * ashmem_pin - pin the given ashmem region, returning whether it was
498  * previously purged (ASHMEM_WAS_PURGED) or not (ASHMEM_NOT_PURGED).
499  *
500  * Caller must hold ashmem_mutex.
501  */
502 static int ashmem_pin(struct ashmem_area *asma, size_t pgstart, size_t pgend)
503 {
504         struct ashmem_range *range, *next;
505         int ret = ASHMEM_NOT_PURGED;
506
507         list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) {
508                 /* moved past last applicable page; we can short circuit */
509                 if (range_before_page(range, pgstart))
510                         break;
511
512                 /*
513                  * The user can ask us to pin pages that span multiple ranges,
514                  * or to pin pages that aren't even unpinned, so this is messy.
515                  *
516                  * Four cases:
517                  * 1. The requested range subsumes an existing range, so we
518                  *    just remove the entire matching range.
519                  * 2. The requested range overlaps the start of an existing
520                  *    range, so we just update that range.
521                  * 3. The requested range overlaps the end of an existing
522                  *    range, so we just update that range.
523                  * 4. The requested range punches a hole in an existing range,
524                  *    so we have to update one side of the range and then
525                  *    create a new range for the other side.
526                  */
527                 if (page_range_in_range(range, pgstart, pgend)) {
528                         ret |= range->purged;
529
530                         /* Case #1: Easy. Just nuke the whole thing. */
531                         if (page_range_subsumes_range(range, pgstart, pgend)) {
532                                 range_del(range);
533                                 continue;
534                         }
535
536                         /* Case #2: We overlap from the start, so adjust it */
537                         if (range->pgstart >= pgstart) {
538                                 range_shrink(range, pgend + 1, range->pgend);
539                                 continue;
540                         }
541
542                         /* Case #3: We overlap from the rear, so adjust it */
543                         if (range->pgend <= pgend) {
544                                 range_shrink(range, range->pgstart, pgstart-1);
545                                 continue;
546                         }
547
548                         /*
549                          * Case #4: We eat a chunk out of the middle. A bit
550                          * more complicated, we allocate a new range for the
551                          * second half and adjust the first chunk's endpoint.
552                          */
553                         range_alloc(asma, range, range->purged,
554                                     pgend + 1, range->pgend);
555                         range_shrink(range, range->pgstart, pgstart - 1);
556                         break;
557                 }
558         }
559
560         return ret;
561 }
562
563 /*
564  * ashmem_unpin - unpin the given range of pages. Returns zero on success.
565  *
566  * Caller must hold ashmem_mutex.
567  */
568 static int ashmem_unpin(struct ashmem_area *asma, size_t pgstart, size_t pgend)
569 {
570         struct ashmem_range *range, *next;
571         unsigned int purged = ASHMEM_NOT_PURGED;
572
573 restart:
574         list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) {
575                 /* short circuit: this is our insertion point */
576                 if (range_before_page(range, pgstart))
577                         break;
578
579                 /*
580                  * The user can ask us to unpin pages that are already entirely
581                  * or partially pinned. We handle those two cases here.
582                  */
583                 if (page_range_subsumed_by_range(range, pgstart, pgend))
584                         return 0;
585                 if (page_range_in_range(range, pgstart, pgend)) {
586                         pgstart = min_t(size_t, range->pgstart, pgstart),
587                         pgend = max_t(size_t, range->pgend, pgend);
588                         purged |= range->purged;
589                         range_del(range);
590                         goto restart;
591                 }
592         }
593
594         return range_alloc(asma, range, purged, pgstart, pgend);
595 }
596
597 /*
598  * ashmem_get_pin_status - Returns ASHMEM_IS_UNPINNED if _any_ pages in the
599  * given interval are unpinned and ASHMEM_IS_PINNED otherwise.
600  *
601  * Caller must hold ashmem_mutex.
602  */
603 static int ashmem_get_pin_status(struct ashmem_area *asma, size_t pgstart,
604                                  size_t pgend)
605 {
606         struct ashmem_range *range;
607         int ret = ASHMEM_IS_PINNED;
608
609         list_for_each_entry(range, &asma->unpinned_list, unpinned) {
610                 if (range_before_page(range, pgstart))
611                         break;
612                 if (page_range_in_range(range, pgstart, pgend)) {
613                         ret = ASHMEM_IS_UNPINNED;
614                         break;
615                 }
616         }
617
618         return ret;
619 }
620
621 static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd,
622                             void __user *p)
623 {
624         struct ashmem_pin pin;
625         size_t pgstart, pgend;
626         int ret = -EINVAL;
627
628         if (unlikely(!asma->file))
629                 return -EINVAL;
630
631         if (unlikely(copy_from_user(&pin, p, sizeof(pin))))
632                 return -EFAULT;
633
634         /* per custom, you can pass zero for len to mean "everything onward" */
635         if (!pin.len)
636                 pin.len = PAGE_ALIGN(asma->size) - pin.offset;
637
638         if (unlikely((pin.offset | pin.len) & ~PAGE_MASK))
639                 return -EINVAL;
640
641         if (unlikely(((__u32) -1) - pin.offset < pin.len))
642                 return -EINVAL;
643
644         if (unlikely(PAGE_ALIGN(asma->size) < pin.offset + pin.len))
645                 return -EINVAL;
646
647         pgstart = pin.offset / PAGE_SIZE;
648         pgend = pgstart + (pin.len / PAGE_SIZE) - 1;
649
650         mutex_lock(&ashmem_mutex);
651
652         switch (cmd) {
653         case ASHMEM_PIN:
654                 ret = ashmem_pin(asma, pgstart, pgend);
655                 break;
656         case ASHMEM_UNPIN:
657                 ret = ashmem_unpin(asma, pgstart, pgend);
658                 break;
659         case ASHMEM_GET_PIN_STATUS:
660                 ret = ashmem_get_pin_status(asma, pgstart, pgend);
661                 break;
662         }
663
664         mutex_unlock(&ashmem_mutex);
665
666         return ret;
667 }
668
669 static long ashmem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
670 {
671         struct ashmem_area *asma = file->private_data;
672         long ret = -ENOTTY;
673
674         switch (cmd) {
675         case ASHMEM_SET_NAME:
676                 ret = set_name(asma, (void __user *) arg);
677                 break;
678         case ASHMEM_GET_NAME:
679                 ret = get_name(asma, (void __user *) arg);
680                 break;
681         case ASHMEM_SET_SIZE:
682                 ret = -EINVAL;
683                 if (!asma->file) {
684                         ret = 0;
685                         asma->size = (size_t) arg;
686                 }
687                 break;
688         case ASHMEM_GET_SIZE:
689                 ret = asma->size;
690                 break;
691         case ASHMEM_SET_PROT_MASK:
692                 ret = set_prot_mask(asma, arg);
693                 break;
694         case ASHMEM_GET_PROT_MASK:
695                 ret = asma->prot_mask;
696                 break;
697         case ASHMEM_PIN:
698         case ASHMEM_UNPIN:
699         case ASHMEM_GET_PIN_STATUS:
700                 ret = ashmem_pin_unpin(asma, cmd, (void __user *) arg);
701                 break;
702         case ASHMEM_PURGE_ALL_CACHES:
703                 ret = -EPERM;
704                 if (capable(CAP_SYS_ADMIN)) {
705                         struct shrink_control sc = {
706                                 .gfp_mask = GFP_KERNEL,
707                                 .nr_to_scan = LONG_MAX,
708                         };
709
710                         nodes_setall(sc.nodes_to_scan);
711                         ashmem_shrink_scan(&ashmem_shrinker, &sc);
712                 }
713                 break;
714         }
715
716         return ret;
717 }
718
719 /* support of 32bit userspace on 64bit platforms */
720 #ifdef CONFIG_COMPAT
721 static long compat_ashmem_ioctl(struct file *file, unsigned int cmd,
722                                 unsigned long arg)
723 {
724
725         switch (cmd) {
726         case COMPAT_ASHMEM_SET_SIZE:
727                 cmd = ASHMEM_SET_SIZE;
728                 break;
729         case COMPAT_ASHMEM_SET_PROT_MASK:
730                 cmd = ASHMEM_SET_PROT_MASK;
731                 break;
732         }
733         return ashmem_ioctl(file, cmd, arg);
734 }
735 #endif
736
737 static const struct file_operations ashmem_fops = {
738         .owner = THIS_MODULE,
739         .open = ashmem_open,
740         .release = ashmem_release,
741         .read = ashmem_read,
742         .llseek = ashmem_llseek,
743         .mmap = ashmem_mmap,
744         .unlocked_ioctl = ashmem_ioctl,
745 #ifdef CONFIG_COMPAT
746         .compat_ioctl = compat_ashmem_ioctl,
747 #endif
748 };
749
750 static struct miscdevice ashmem_misc = {
751         .minor = MISC_DYNAMIC_MINOR,
752         .name = "ashmem",
753         .fops = &ashmem_fops,
754 };
755
756 static int __init ashmem_init(void)
757 {
758         int ret;
759
760         ashmem_area_cachep = kmem_cache_create("ashmem_area_cache",
761                                           sizeof(struct ashmem_area),
762                                           0, 0, NULL);
763         if (unlikely(!ashmem_area_cachep)) {
764                 pr_err("failed to create slab cache\n");
765                 return -ENOMEM;
766         }
767
768         ashmem_range_cachep = kmem_cache_create("ashmem_range_cache",
769                                           sizeof(struct ashmem_range),
770                                           0, 0, NULL);
771         if (unlikely(!ashmem_range_cachep)) {
772                 pr_err("failed to create slab cache\n");
773                 return -ENOMEM;
774         }
775
776         ret = misc_register(&ashmem_misc);
777         if (unlikely(ret)) {
778                 pr_err("failed to register misc device!\n");
779                 return ret;
780         }
781
782         register_shrinker(&ashmem_shrinker);
783
784         pr_info("initialized\n");
785
786         return 0;
787 }
788
789 static void __exit ashmem_exit(void)
790 {
791         int ret;
792
793         unregister_shrinker(&ashmem_shrinker);
794
795         ret = misc_deregister(&ashmem_misc);
796         if (unlikely(ret))
797                 pr_err("failed to unregister misc device!\n");
798
799         kmem_cache_destroy(ashmem_range_cachep);
800         kmem_cache_destroy(ashmem_area_cachep);
801
802         pr_info("unloaded\n");
803 }
804
805 module_init(ashmem_init);
806 module_exit(ashmem_exit);
807
808 MODULE_LICENSE("GPL");