Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 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 | #include <linux/module.h> |
| 20 | #include <linux/file.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/miscdevice.h> |
| 23 | #include <linux/security.h> |
| 24 | #include <linux/mm.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> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 31 | #include <linux/ashmem.h> |
| 32 | #include <asm/cacheflush.h> |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 33 | |
| 34 | #define ASHMEM_NAME_PREFIX "dev/ashmem/" |
| 35 | #define ASHMEM_NAME_PREFIX_LEN (sizeof(ASHMEM_NAME_PREFIX) - 1) |
| 36 | #define ASHMEM_FULL_NAME_LEN (ASHMEM_NAME_LEN + ASHMEM_NAME_PREFIX_LEN) |
| 37 | |
| 38 | /* |
| 39 | * ashmem_area - anonymous shared memory area |
| 40 | * Lifecycle: From our parent file's open() until its release() |
| 41 | * Locking: Protected by `ashmem_mutex' |
| 42 | * Big Note: Mappings do NOT pin this structure; it dies on close() |
| 43 | */ |
| 44 | struct ashmem_area { |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 45 | char name[ASHMEM_FULL_NAME_LEN]; /* optional name in /proc/pid/maps */ |
| 46 | struct list_head unpinned_list; /* list of all ashmem areas */ |
| 47 | struct file *file; /* the shmem-based backing file */ |
| 48 | size_t size; /* size of the mapping, in bytes */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 49 | unsigned long vm_start; /* Start address of vm_area |
| 50 | * which maps this ashmem */ |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 51 | unsigned long prot_mask; /* allowed prot bits, as vm_flags */ |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 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) \ |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 85 | ((range)->pgend - (range)->pgstart + 1) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 86 | |
| 87 | #define range_on_lru(range) \ |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 88 | ((range)->purged == ASHMEM_NOT_PURGED) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 89 | |
| 90 | #define page_range_subsumes_range(range, start, end) \ |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 91 | (((range)->pgstart >= (start)) && ((range)->pgend <= (end))) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 92 | |
| 93 | #define page_range_subsumed_by_range(range, start, end) \ |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 94 | (((range)->pgstart <= (start)) && ((range)->pgend >= (end))) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 95 | |
| 96 | #define page_in_range(range, page) \ |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 97 | (((range)->pgstart <= (page)) && ((range)->pgend >= (page))) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 98 | |
| 99 | #define page_range_in_range(range, start, end) \ |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 100 | (page_in_range(range, start) || page_in_range(range, end) || \ |
| 101 | page_range_subsumes_range(range, start, end)) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 102 | |
| 103 | #define range_before_page(range, page) \ |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 104 | ((range)->pgend < (page)) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 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 | |
Bjorn Bringert | 5154b93 | 2011-12-20 16:49:52 -0800 | [diff] [blame] | 184 | ret = generic_file_open(inode, file); |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 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 | |
Bjorn Bringert | 853ca7a | 2011-12-20 16:49:49 -0800 | [diff] [blame] | 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. */ |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 226 | if (asma->size == 0) |
Todd Poynor | e70b640 | 2013-06-04 17:29:38 -0700 | [diff] [blame] | 227 | goto out_unlock; |
Bjorn Bringert | 853ca7a | 2011-12-20 16:49:49 -0800 | [diff] [blame] | 228 | |
| 229 | if (!asma->file) { |
| 230 | ret = -EBADF; |
Todd Poynor | e70b640 | 2013-06-04 17:29:38 -0700 | [diff] [blame] | 231 | goto out_unlock; |
Bjorn Bringert | 853ca7a | 2011-12-20 16:49:49 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Todd Poynor | e70b640 | 2013-06-04 17:29:38 -0700 | [diff] [blame] | 234 | mutex_unlock(&ashmem_mutex); |
| 235 | |
| 236 | /* |
| 237 | * asma and asma->file are used outside the lock here. We assume |
| 238 | * once asma->file is set it will never be changed, and will not |
| 239 | * be destroyed until all references to the file are dropped and |
| 240 | * ashmem_release is called. |
| 241 | */ |
Bjorn Bringert | 853ca7a | 2011-12-20 16:49:49 -0800 | [diff] [blame] | 242 | ret = asma->file->f_op->read(asma->file, buf, len, pos); |
Todd Poynor | e70b640 | 2013-06-04 17:29:38 -0700 | [diff] [blame] | 243 | if (ret >= 0) { |
| 244 | /** Update backing file pos, since f_ops->read() doesn't */ |
| 245 | asma->file->f_pos = *pos; |
| 246 | } |
| 247 | return ret; |
Bjorn Bringert | 5154b93 | 2011-12-20 16:49:52 -0800 | [diff] [blame] | 248 | |
Todd Poynor | e70b640 | 2013-06-04 17:29:38 -0700 | [diff] [blame] | 249 | out_unlock: |
Bjorn Bringert | 5154b93 | 2011-12-20 16:49:52 -0800 | [diff] [blame] | 250 | mutex_unlock(&ashmem_mutex); |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static loff_t ashmem_llseek(struct file *file, loff_t offset, int origin) |
| 255 | { |
| 256 | struct ashmem_area *asma = file->private_data; |
| 257 | int ret; |
| 258 | |
| 259 | mutex_lock(&ashmem_mutex); |
| 260 | |
| 261 | if (asma->size == 0) { |
| 262 | ret = -EINVAL; |
| 263 | goto out; |
| 264 | } |
| 265 | |
| 266 | if (!asma->file) { |
| 267 | ret = -EBADF; |
| 268 | goto out; |
| 269 | } |
| 270 | |
| 271 | ret = asma->file->f_op->llseek(asma->file, offset, origin); |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 272 | if (ret < 0) |
Bjorn Bringert | 5154b93 | 2011-12-20 16:49:52 -0800 | [diff] [blame] | 273 | goto out; |
Bjorn Bringert | 5154b93 | 2011-12-20 16:49:52 -0800 | [diff] [blame] | 274 | |
| 275 | /** Copy f_pos from backing file, since f_ops->llseek() sets it */ |
| 276 | file->f_pos = asma->file->f_pos; |
Bjorn Bringert | 853ca7a | 2011-12-20 16:49:49 -0800 | [diff] [blame] | 277 | |
| 278 | out: |
| 279 | mutex_unlock(&ashmem_mutex); |
| 280 | return ret; |
| 281 | } |
| 282 | |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 283 | static inline unsigned long calc_vm_may_flags(unsigned long prot) |
Arve Hjønnevåg | 56f76fc | 2011-12-20 16:49:50 -0800 | [diff] [blame] | 284 | { |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 285 | return _calc_vm_trans(prot, PROT_READ, VM_MAYREAD) | |
Arve Hjønnevåg | 56f76fc | 2011-12-20 16:49:50 -0800 | [diff] [blame] | 286 | _calc_vm_trans(prot, PROT_WRITE, VM_MAYWRITE) | |
| 287 | _calc_vm_trans(prot, PROT_EXEC, VM_MAYEXEC); |
| 288 | } |
Bjorn Bringert | 853ca7a | 2011-12-20 16:49:49 -0800 | [diff] [blame] | 289 | |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 290 | static int ashmem_mmap(struct file *file, struct vm_area_struct *vma) |
| 291 | { |
| 292 | struct ashmem_area *asma = file->private_data; |
| 293 | int ret = 0; |
| 294 | |
| 295 | mutex_lock(&ashmem_mutex); |
| 296 | |
| 297 | /* user needs to SET_SIZE before mapping */ |
| 298 | if (unlikely(!asma->size)) { |
| 299 | ret = -EINVAL; |
| 300 | goto out; |
| 301 | } |
| 302 | |
| 303 | /* requested protection bits must match our allowed protection mask */ |
Arve Hjønnevåg | 56f76fc | 2011-12-20 16:49:50 -0800 | [diff] [blame] | 304 | if (unlikely((vma->vm_flags & ~calc_vm_prot_bits(asma->prot_mask)) & |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 305 | calc_vm_prot_bits(PROT_MASK))) { |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 306 | ret = -EPERM; |
| 307 | goto out; |
| 308 | } |
Arve Hjønnevåg | 56f76fc | 2011-12-20 16:49:50 -0800 | [diff] [blame] | 309 | vma->vm_flags &= ~calc_vm_may_flags(~asma->prot_mask); |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 310 | |
| 311 | if (!asma->file) { |
| 312 | char *name = ASHMEM_NAME_DEF; |
| 313 | struct file *vmfile; |
| 314 | |
| 315 | if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0') |
| 316 | name = asma->name; |
| 317 | |
| 318 | /* ... and allocate the backing shmem file */ |
| 319 | vmfile = shmem_file_setup(name, asma->size, vma->vm_flags); |
| 320 | if (unlikely(IS_ERR(vmfile))) { |
| 321 | ret = PTR_ERR(vmfile); |
| 322 | goto out; |
| 323 | } |
| 324 | asma->file = vmfile; |
| 325 | } |
| 326 | get_file(asma->file); |
| 327 | |
John Stultz | d92aa58 | 2011-12-15 18:51:04 -0800 | [diff] [blame] | 328 | if (vma->vm_flags & VM_SHARED) |
| 329 | shmem_set_file(vma, asma->file); |
| 330 | else { |
| 331 | if (vma->vm_file) |
| 332 | fput(vma->vm_file); |
| 333 | vma->vm_file = asma->file; |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 334 | } |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 335 | vma->vm_flags |= VM_CAN_NONLINEAR; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 336 | asma->vm_start = vma->vm_start; |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 337 | |
| 338 | out: |
| 339 | mutex_unlock(&ashmem_mutex); |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * ashmem_shrink - our cache shrinker, called from mm/vmscan.c :: shrink_slab |
| 345 | * |
| 346 | * 'nr_to_scan' is the number of objects (pages) to prune, or 0 to query how |
| 347 | * many objects (pages) we have in total. |
| 348 | * |
| 349 | * 'gfp_mask' is the mask of the allocation that got us into this mess. |
| 350 | * |
| 351 | * Return value is the number of objects (pages) remaining, or -1 if we cannot |
| 352 | * proceed without risk of deadlock (due to gfp_mask). |
| 353 | * |
| 354 | * We approximate LRU via least-recently-unpinned, jettisoning unpinned partial |
| 355 | * chunks of ashmem regions LRU-wise one-at-a-time until we hit 'nr_to_scan' |
| 356 | * pages freed. |
| 357 | */ |
Colin Cross | 33e8fc4 | 2011-12-20 16:49:53 -0800 | [diff] [blame] | 358 | static int ashmem_shrink(struct shrinker *s, struct shrink_control *sc) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 359 | { |
| 360 | struct ashmem_range *range, *next; |
| 361 | |
| 362 | /* We might recurse into filesystem code, so bail out if necessary */ |
Colin Cross | 33e8fc4 | 2011-12-20 16:49:53 -0800 | [diff] [blame] | 363 | if (sc->nr_to_scan && !(sc->gfp_mask & __GFP_FS)) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 364 | return -1; |
Colin Cross | 33e8fc4 | 2011-12-20 16:49:53 -0800 | [diff] [blame] | 365 | if (!sc->nr_to_scan) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 366 | return lru_count; |
| 367 | |
| 368 | mutex_lock(&ashmem_mutex); |
| 369 | list_for_each_entry_safe(range, next, &ashmem_lru_list, lru) { |
| 370 | struct inode *inode = range->asma->file->f_dentry->d_inode; |
| 371 | loff_t start = range->pgstart * PAGE_SIZE; |
| 372 | loff_t end = (range->pgend + 1) * PAGE_SIZE - 1; |
| 373 | |
| 374 | vmtruncate_range(inode, start, end); |
| 375 | range->purged = ASHMEM_WAS_PURGED; |
| 376 | lru_del(range); |
| 377 | |
Colin Cross | 33e8fc4 | 2011-12-20 16:49:53 -0800 | [diff] [blame] | 378 | sc->nr_to_scan -= range_size(range); |
| 379 | if (sc->nr_to_scan <= 0) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 380 | break; |
| 381 | } |
| 382 | mutex_unlock(&ashmem_mutex); |
| 383 | |
| 384 | return lru_count; |
| 385 | } |
| 386 | |
| 387 | static struct shrinker ashmem_shrinker = { |
| 388 | .shrink = ashmem_shrink, |
| 389 | .seeks = DEFAULT_SEEKS * 4, |
| 390 | }; |
| 391 | |
| 392 | static int set_prot_mask(struct ashmem_area *asma, unsigned long prot) |
| 393 | { |
| 394 | int ret = 0; |
| 395 | |
| 396 | mutex_lock(&ashmem_mutex); |
| 397 | |
| 398 | /* the user can only remove, not add, protection bits */ |
| 399 | if (unlikely((asma->prot_mask & prot) != prot)) { |
| 400 | ret = -EINVAL; |
| 401 | goto out; |
| 402 | } |
| 403 | |
| 404 | /* does the application expect PROT_READ to imply PROT_EXEC? */ |
| 405 | if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) |
| 406 | prot |= PROT_EXEC; |
| 407 | |
| 408 | asma->prot_mask = prot; |
| 409 | |
| 410 | out: |
| 411 | mutex_unlock(&ashmem_mutex); |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | static int set_name(struct ashmem_area *asma, void __user *name) |
| 416 | { |
Todd Poynor | e70b640 | 2013-06-04 17:29:38 -0700 | [diff] [blame] | 417 | int len; |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 418 | int ret = 0; |
Shankar Brahadeeswaran | c1d5bd6 | 2013-02-20 23:41:26 +0530 | [diff] [blame] | 419 | char local_name[ASHMEM_NAME_LEN]; |
| 420 | |
| 421 | /* |
| 422 | * Holding the ashmem_mutex while doing a copy_from_user might cause |
| 423 | * an data abort which would try to access mmap_sem. If another |
| 424 | * thread has invoked ashmem_mmap then it will be holding the |
| 425 | * semaphore and will be waiting for ashmem_mutex, there by leading to |
| 426 | * deadlock. We'll release the mutex and take the name to a local |
| 427 | * variable that does not need protection and later copy the local |
| 428 | * variable to the structure member with lock held. |
| 429 | */ |
Todd Poynor | e70b640 | 2013-06-04 17:29:38 -0700 | [diff] [blame] | 430 | len = strncpy_from_user(local_name, name, ASHMEM_NAME_LEN); |
| 431 | if (len < 0) |
| 432 | return len; |
| 433 | if (len == ASHMEM_NAME_LEN) |
| 434 | local_name[ASHMEM_NAME_LEN - 1] = '\0'; |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 435 | mutex_lock(&ashmem_mutex); |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 436 | /* cannot change an existing mapping's name */ |
Todd Poynor | e70b640 | 2013-06-04 17:29:38 -0700 | [diff] [blame] | 437 | if (unlikely(asma->file)) |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 438 | ret = -EINVAL; |
Todd Poynor | e70b640 | 2013-06-04 17:29:38 -0700 | [diff] [blame] | 439 | else |
| 440 | strcpy(asma->name + ASHMEM_NAME_PREFIX_LEN, local_name); |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 441 | |
Todd Poynor | e70b640 | 2013-06-04 17:29:38 -0700 | [diff] [blame] | 442 | mutex_unlock(&ashmem_mutex); |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | static int get_name(struct ashmem_area *asma, void __user *name) |
| 447 | { |
| 448 | int ret = 0; |
Shankar Brahadeeswaran | c1d5bd6 | 2013-02-20 23:41:26 +0530 | [diff] [blame] | 449 | size_t len; |
| 450 | /* |
| 451 | * Have a local variable to which we'll copy the content |
| 452 | * from asma with the lock held. Later we can copy this to the user |
| 453 | * space safely without holding any locks. So even if we proceed to |
| 454 | * wait for mmap_sem, it won't lead to deadlock. |
| 455 | */ |
| 456 | char local_name[ASHMEM_NAME_LEN]; |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 457 | |
| 458 | mutex_lock(&ashmem_mutex); |
| 459 | if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0') { |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 460 | |
| 461 | /* |
| 462 | * Copying only `len', instead of ASHMEM_NAME_LEN, bytes |
| 463 | * prevents us from revealing one user's stack to another. |
| 464 | */ |
| 465 | len = strlen(asma->name + ASHMEM_NAME_PREFIX_LEN) + 1; |
Shankar Brahadeeswaran | c1d5bd6 | 2013-02-20 23:41:26 +0530 | [diff] [blame] | 466 | memcpy(local_name, asma->name + ASHMEM_NAME_PREFIX_LEN, len); |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 467 | } else { |
Shankar Brahadeeswaran | c1d5bd6 | 2013-02-20 23:41:26 +0530 | [diff] [blame] | 468 | len = sizeof(ASHMEM_NAME_DEF); |
| 469 | memcpy(local_name, ASHMEM_NAME_DEF, len); |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 470 | } |
| 471 | mutex_unlock(&ashmem_mutex); |
| 472 | |
Shankar Brahadeeswaran | c1d5bd6 | 2013-02-20 23:41:26 +0530 | [diff] [blame] | 473 | /* |
| 474 | * Now we are just copying from the stack variable to userland |
| 475 | * No lock held |
| 476 | */ |
| 477 | if (unlikely(copy_to_user(name, local_name, len))) |
| 478 | ret = -EFAULT; |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * ashmem_pin - pin the given ashmem region, returning whether it was |
| 484 | * previously purged (ASHMEM_WAS_PURGED) or not (ASHMEM_NOT_PURGED). |
| 485 | * |
| 486 | * Caller must hold ashmem_mutex. |
| 487 | */ |
| 488 | static int ashmem_pin(struct ashmem_area *asma, size_t pgstart, size_t pgend) |
| 489 | { |
| 490 | struct ashmem_range *range, *next; |
| 491 | int ret = ASHMEM_NOT_PURGED; |
| 492 | |
| 493 | list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) { |
| 494 | /* moved past last applicable page; we can short circuit */ |
| 495 | if (range_before_page(range, pgstart)) |
| 496 | break; |
| 497 | |
| 498 | /* |
| 499 | * The user can ask us to pin pages that span multiple ranges, |
| 500 | * or to pin pages that aren't even unpinned, so this is messy. |
| 501 | * |
| 502 | * Four cases: |
| 503 | * 1. The requested range subsumes an existing range, so we |
| 504 | * just remove the entire matching range. |
| 505 | * 2. The requested range overlaps the start of an existing |
| 506 | * range, so we just update that range. |
| 507 | * 3. The requested range overlaps the end of an existing |
| 508 | * range, so we just update that range. |
| 509 | * 4. The requested range punches a hole in an existing range, |
| 510 | * so we have to update one side of the range and then |
| 511 | * create a new range for the other side. |
| 512 | */ |
| 513 | if (page_range_in_range(range, pgstart, pgend)) { |
| 514 | ret |= range->purged; |
| 515 | |
| 516 | /* Case #1: Easy. Just nuke the whole thing. */ |
| 517 | if (page_range_subsumes_range(range, pgstart, pgend)) { |
| 518 | range_del(range); |
| 519 | continue; |
| 520 | } |
| 521 | |
| 522 | /* Case #2: We overlap from the start, so adjust it */ |
| 523 | if (range->pgstart >= pgstart) { |
| 524 | range_shrink(range, pgend + 1, range->pgend); |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | /* Case #3: We overlap from the rear, so adjust it */ |
| 529 | if (range->pgend <= pgend) { |
| 530 | range_shrink(range, range->pgstart, pgstart-1); |
| 531 | continue; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Case #4: We eat a chunk out of the middle. A bit |
| 536 | * more complicated, we allocate a new range for the |
| 537 | * second half and adjust the first chunk's endpoint. |
| 538 | */ |
| 539 | range_alloc(asma, range, range->purged, |
| 540 | pgend + 1, range->pgend); |
| 541 | range_shrink(range, range->pgstart, pgstart - 1); |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * ashmem_unpin - unpin the given range of pages. Returns zero on success. |
| 551 | * |
| 552 | * Caller must hold ashmem_mutex. |
| 553 | */ |
| 554 | static int ashmem_unpin(struct ashmem_area *asma, size_t pgstart, size_t pgend) |
| 555 | { |
| 556 | struct ashmem_range *range, *next; |
| 557 | unsigned int purged = ASHMEM_NOT_PURGED; |
| 558 | |
| 559 | restart: |
| 560 | list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) { |
| 561 | /* short circuit: this is our insertion point */ |
| 562 | if (range_before_page(range, pgstart)) |
| 563 | break; |
| 564 | |
| 565 | /* |
| 566 | * The user can ask us to unpin pages that are already entirely |
| 567 | * or partially pinned. We handle those two cases here. |
| 568 | */ |
| 569 | if (page_range_subsumed_by_range(range, pgstart, pgend)) |
| 570 | return 0; |
| 571 | if (page_range_in_range(range, pgstart, pgend)) { |
| 572 | pgstart = min_t(size_t, range->pgstart, pgstart), |
| 573 | pgend = max_t(size_t, range->pgend, pgend); |
| 574 | purged |= range->purged; |
| 575 | range_del(range); |
| 576 | goto restart; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | return range_alloc(asma, range, purged, pgstart, pgend); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * ashmem_get_pin_status - Returns ASHMEM_IS_UNPINNED if _any_ pages in the |
| 585 | * given interval are unpinned and ASHMEM_IS_PINNED otherwise. |
| 586 | * |
| 587 | * Caller must hold ashmem_mutex. |
| 588 | */ |
| 589 | static int ashmem_get_pin_status(struct ashmem_area *asma, size_t pgstart, |
| 590 | size_t pgend) |
| 591 | { |
| 592 | struct ashmem_range *range; |
| 593 | int ret = ASHMEM_IS_PINNED; |
| 594 | |
| 595 | list_for_each_entry(range, &asma->unpinned_list, unpinned) { |
| 596 | if (range_before_page(range, pgstart)) |
| 597 | break; |
| 598 | if (page_range_in_range(range, pgstart, pgend)) { |
| 599 | ret = ASHMEM_IS_UNPINNED; |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | return ret; |
| 605 | } |
| 606 | |
| 607 | static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd, |
| 608 | void __user *p) |
| 609 | { |
| 610 | struct ashmem_pin pin; |
| 611 | size_t pgstart, pgend; |
| 612 | int ret = -EINVAL; |
| 613 | |
| 614 | if (unlikely(!asma->file)) |
| 615 | return -EINVAL; |
| 616 | |
| 617 | if (unlikely(copy_from_user(&pin, p, sizeof(pin)))) |
| 618 | return -EFAULT; |
| 619 | |
| 620 | /* per custom, you can pass zero for len to mean "everything onward" */ |
| 621 | if (!pin.len) |
| 622 | pin.len = PAGE_ALIGN(asma->size) - pin.offset; |
| 623 | |
| 624 | if (unlikely((pin.offset | pin.len) & ~PAGE_MASK)) |
| 625 | return -EINVAL; |
| 626 | |
| 627 | if (unlikely(((__u32) -1) - pin.offset < pin.len)) |
| 628 | return -EINVAL; |
| 629 | |
| 630 | if (unlikely(PAGE_ALIGN(asma->size) < pin.offset + pin.len)) |
| 631 | return -EINVAL; |
| 632 | |
| 633 | pgstart = pin.offset / PAGE_SIZE; |
| 634 | pgend = pgstart + (pin.len / PAGE_SIZE) - 1; |
| 635 | |
| 636 | mutex_lock(&ashmem_mutex); |
| 637 | |
| 638 | switch (cmd) { |
| 639 | case ASHMEM_PIN: |
| 640 | ret = ashmem_pin(asma, pgstart, pgend); |
| 641 | break; |
| 642 | case ASHMEM_UNPIN: |
| 643 | ret = ashmem_unpin(asma, pgstart, pgend); |
| 644 | break; |
| 645 | case ASHMEM_GET_PIN_STATUS: |
| 646 | ret = ashmem_get_pin_status(asma, pgstart, pgend); |
| 647 | break; |
| 648 | } |
| 649 | |
| 650 | mutex_unlock(&ashmem_mutex); |
| 651 | |
| 652 | return ret; |
| 653 | } |
| 654 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 655 | #ifdef CONFIG_OUTER_CACHE |
| 656 | static unsigned int virtaddr_to_physaddr(unsigned int virtaddr) |
| 657 | { |
| 658 | unsigned int physaddr = 0; |
| 659 | pgd_t *pgd_ptr = NULL; |
Trilok Soni | 318763f | 2012-06-26 14:30:15 +0530 | [diff] [blame] | 660 | pud_t *pud_ptr = NULL; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 661 | pmd_t *pmd_ptr = NULL; |
| 662 | pte_t *pte_ptr = NULL, pte; |
| 663 | |
| 664 | spin_lock(¤t->mm->page_table_lock); |
| 665 | pgd_ptr = pgd_offset(current->mm, virtaddr); |
Trilok Soni | 318763f | 2012-06-26 14:30:15 +0530 | [diff] [blame] | 666 | if (pgd_none(*pgd_ptr) || pgd_bad(*pgd_ptr)) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 667 | pr_err("Failed to convert virtaddr %x to pgd_ptr\n", |
| 668 | virtaddr); |
| 669 | goto done; |
| 670 | } |
| 671 | |
Trilok Soni | 318763f | 2012-06-26 14:30:15 +0530 | [diff] [blame] | 672 | pud_ptr = pud_offset(pgd_ptr, virtaddr); |
| 673 | if (pud_none(*pud_ptr) || pud_bad(*pud_ptr)) { |
| 674 | pr_err("Failed to convert pgd_ptr %p to pud_ptr\n", |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 675 | (void *)pgd_ptr); |
| 676 | goto done; |
| 677 | } |
| 678 | |
Trilok Soni | 318763f | 2012-06-26 14:30:15 +0530 | [diff] [blame] | 679 | pmd_ptr = pmd_offset(pud_ptr, virtaddr); |
| 680 | if (pmd_none(*pmd_ptr) || pmd_bad(*pmd_ptr)) { |
| 681 | pr_err("Failed to convert pud_ptr %p to pmd_ptr\n", |
| 682 | (void *)pud_ptr); |
| 683 | goto done; |
| 684 | } |
| 685 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 686 | pte_ptr = pte_offset_map(pmd_ptr, virtaddr); |
| 687 | if (!pte_ptr) { |
| 688 | pr_err("Failed to convert pmd_ptr %p to pte_ptr\n", |
| 689 | (void *)pmd_ptr); |
| 690 | goto done; |
| 691 | } |
| 692 | pte = *pte_ptr; |
| 693 | physaddr = pte_pfn(pte); |
| 694 | pte_unmap(pte_ptr); |
| 695 | done: |
| 696 | spin_unlock(¤t->mm->page_table_lock); |
| 697 | physaddr <<= PAGE_SHIFT; |
| 698 | return physaddr; |
| 699 | } |
| 700 | #endif |
| 701 | |
| 702 | static int ashmem_cache_op(struct ashmem_area *asma, |
| 703 | void (*cache_func)(unsigned long vstart, unsigned long length, |
| 704 | unsigned long pstart)) |
| 705 | { |
| 706 | int ret = 0; |
| 707 | struct vm_area_struct *vma; |
| 708 | #ifdef CONFIG_OUTER_CACHE |
| 709 | unsigned long vaddr; |
| 710 | #endif |
| 711 | if (!asma->vm_start) |
| 712 | return -EINVAL; |
| 713 | |
| 714 | down_read(¤t->mm->mmap_sem); |
| 715 | vma = find_vma(current->mm, asma->vm_start); |
| 716 | if (!vma) { |
| 717 | ret = -EINVAL; |
| 718 | goto done; |
| 719 | } |
| 720 | if (vma->vm_file != asma->file) { |
| 721 | ret = -EINVAL; |
| 722 | goto done; |
| 723 | } |
Matthew Trusten | 03fd16d | 2012-08-07 13:55:03 -0700 | [diff] [blame] | 724 | if ((asma->vm_start + asma->size) > vma->vm_end) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 725 | ret = -EINVAL; |
| 726 | goto done; |
| 727 | } |
| 728 | #ifndef CONFIG_OUTER_CACHE |
| 729 | cache_func(asma->vm_start, asma->size, 0); |
| 730 | #else |
| 731 | for (vaddr = asma->vm_start; vaddr < asma->vm_start + asma->size; |
| 732 | vaddr += PAGE_SIZE) { |
| 733 | unsigned long physaddr; |
| 734 | physaddr = virtaddr_to_physaddr(vaddr); |
| 735 | if (!physaddr) |
| 736 | return -EINVAL; |
| 737 | cache_func(vaddr, PAGE_SIZE, physaddr); |
| 738 | } |
| 739 | #endif |
| 740 | done: |
| 741 | up_read(¤t->mm->mmap_sem); |
| 742 | if (ret) |
| 743 | asma->vm_start = 0; |
| 744 | return ret; |
| 745 | } |
| 746 | |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 747 | static long ashmem_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 748 | { |
| 749 | struct ashmem_area *asma = file->private_data; |
| 750 | long ret = -ENOTTY; |
| 751 | |
| 752 | switch (cmd) { |
| 753 | case ASHMEM_SET_NAME: |
| 754 | ret = set_name(asma, (void __user *) arg); |
| 755 | break; |
| 756 | case ASHMEM_GET_NAME: |
| 757 | ret = get_name(asma, (void __user *) arg); |
| 758 | break; |
| 759 | case ASHMEM_SET_SIZE: |
| 760 | ret = -EINVAL; |
| 761 | if (!asma->file) { |
| 762 | ret = 0; |
| 763 | asma->size = (size_t) arg; |
| 764 | } |
| 765 | break; |
| 766 | case ASHMEM_GET_SIZE: |
| 767 | ret = asma->size; |
| 768 | break; |
| 769 | case ASHMEM_SET_PROT_MASK: |
| 770 | ret = set_prot_mask(asma, arg); |
| 771 | break; |
| 772 | case ASHMEM_GET_PROT_MASK: |
| 773 | ret = asma->prot_mask; |
| 774 | break; |
| 775 | case ASHMEM_PIN: |
| 776 | case ASHMEM_UNPIN: |
| 777 | case ASHMEM_GET_PIN_STATUS: |
| 778 | ret = ashmem_pin_unpin(asma, cmd, (void __user *) arg); |
| 779 | break; |
| 780 | case ASHMEM_PURGE_ALL_CACHES: |
| 781 | ret = -EPERM; |
| 782 | if (capable(CAP_SYS_ADMIN)) { |
Colin Cross | 33e8fc4 | 2011-12-20 16:49:53 -0800 | [diff] [blame] | 783 | struct shrink_control sc = { |
| 784 | .gfp_mask = GFP_KERNEL, |
| 785 | .nr_to_scan = 0, |
| 786 | }; |
| 787 | ret = ashmem_shrink(&ashmem_shrinker, &sc); |
| 788 | sc.nr_to_scan = ret; |
| 789 | ashmem_shrink(&ashmem_shrinker, &sc); |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 790 | } |
| 791 | break; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 792 | case ASHMEM_CACHE_FLUSH_RANGE: |
| 793 | ret = ashmem_cache_op(asma, &clean_and_invalidate_caches); |
| 794 | break; |
| 795 | case ASHMEM_CACHE_CLEAN_RANGE: |
| 796 | ret = ashmem_cache_op(asma, &clean_caches); |
| 797 | break; |
| 798 | case ASHMEM_CACHE_INV_RANGE: |
| 799 | ret = ashmem_cache_op(asma, &invalidate_caches); |
| 800 | break; |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | return ret; |
| 804 | } |
| 805 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 806 | static int is_ashmem_file(struct file *file) |
| 807 | { |
| 808 | char fname[256], *name; |
| 809 | name = dentry_path(file->f_dentry, fname, 256); |
| 810 | return strcmp(name, "/ashmem") ? 0 : 1; |
| 811 | } |
| 812 | |
| 813 | int get_ashmem_file(int fd, struct file **filp, struct file **vm_file, |
| 814 | unsigned long *len) |
| 815 | { |
| 816 | int ret = -1; |
| 817 | struct file *file = fget(fd); |
| 818 | *filp = NULL; |
| 819 | *vm_file = NULL; |
| 820 | if (unlikely(file == NULL)) { |
| 821 | pr_err("ashmem: %s: requested data from file " |
| 822 | "descriptor that doesn't exist.\n", __func__); |
| 823 | } else { |
| 824 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 825 | pr_debug("filp %p rdev %d pid %u(%s) file %p(%ld)" |
| 826 | " dev id: %d\n", filp, |
| 827 | file->f_dentry->d_inode->i_rdev, |
| 828 | current->pid, get_task_comm(currtask_name, current), |
| 829 | file, file_count(file), |
| 830 | MINOR(file->f_dentry->d_inode->i_rdev)); |
| 831 | if (is_ashmem_file(file)) { |
| 832 | struct ashmem_area *asma = file->private_data; |
| 833 | *filp = file; |
| 834 | *vm_file = asma->file; |
| 835 | *len = asma->size; |
| 836 | ret = 0; |
| 837 | } else { |
| 838 | pr_err("file descriptor is not an ashmem " |
| 839 | "region fd: %d\n", fd); |
| 840 | fput(file); |
| 841 | } |
| 842 | } |
| 843 | return ret; |
| 844 | } |
| 845 | EXPORT_SYMBOL(get_ashmem_file); |
| 846 | |
| 847 | void put_ashmem_file(struct file *file) |
| 848 | { |
| 849 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 850 | pr_debug("rdev %d pid %u(%s) file %p(%ld)" " dev id: %d\n", |
| 851 | file->f_dentry->d_inode->i_rdev, current->pid, |
| 852 | get_task_comm(currtask_name, current), file, |
| 853 | file_count(file), MINOR(file->f_dentry->d_inode->i_rdev)); |
| 854 | if (file && is_ashmem_file(file)) |
| 855 | fput(file); |
| 856 | } |
| 857 | EXPORT_SYMBOL(put_ashmem_file); |
| 858 | |
Tracey Dent | aa5af97 | 2012-01-22 21:28:44 -0500 | [diff] [blame] | 859 | static const struct file_operations ashmem_fops = { |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 860 | .owner = THIS_MODULE, |
| 861 | .open = ashmem_open, |
| 862 | .release = ashmem_release, |
John Stultz | 1efb343 | 2011-12-20 16:49:54 -0800 | [diff] [blame] | 863 | .read = ashmem_read, |
| 864 | .llseek = ashmem_llseek, |
Robert Love | 11980c2 | 2011-12-20 16:49:48 -0800 | [diff] [blame] | 865 | .mmap = ashmem_mmap, |
| 866 | .unlocked_ioctl = ashmem_ioctl, |
| 867 | .compat_ioctl = ashmem_ioctl, |
| 868 | }; |
| 869 | |
| 870 | static struct miscdevice ashmem_misc = { |
| 871 | .minor = MISC_DYNAMIC_MINOR, |
| 872 | .name = "ashmem", |
| 873 | .fops = &ashmem_fops, |
| 874 | }; |
| 875 | |
| 876 | static int __init ashmem_init(void) |
| 877 | { |
| 878 | int ret; |
| 879 | |
| 880 | ashmem_area_cachep = kmem_cache_create("ashmem_area_cache", |
| 881 | sizeof(struct ashmem_area), |
| 882 | 0, 0, NULL); |
| 883 | if (unlikely(!ashmem_area_cachep)) { |
| 884 | printk(KERN_ERR "ashmem: failed to create slab cache\n"); |
| 885 | return -ENOMEM; |
| 886 | } |
| 887 | |
| 888 | ashmem_range_cachep = kmem_cache_create("ashmem_range_cache", |
| 889 | sizeof(struct ashmem_range), |
| 890 | 0, 0, NULL); |
| 891 | if (unlikely(!ashmem_range_cachep)) { |
| 892 | printk(KERN_ERR "ashmem: failed to create slab cache\n"); |
| 893 | return -ENOMEM; |
| 894 | } |
| 895 | |
| 896 | ret = misc_register(&ashmem_misc); |
| 897 | if (unlikely(ret)) { |
| 898 | printk(KERN_ERR "ashmem: failed to register misc device!\n"); |
| 899 | return ret; |
| 900 | } |
| 901 | |
| 902 | register_shrinker(&ashmem_shrinker); |
| 903 | |
| 904 | printk(KERN_INFO "ashmem: initialized\n"); |
| 905 | |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | static void __exit ashmem_exit(void) |
| 910 | { |
| 911 | int ret; |
| 912 | |
| 913 | unregister_shrinker(&ashmem_shrinker); |
| 914 | |
| 915 | ret = misc_deregister(&ashmem_misc); |
| 916 | if (unlikely(ret)) |
| 917 | printk(KERN_ERR "ashmem: failed to unregister misc device!\n"); |
| 918 | |
| 919 | kmem_cache_destroy(ashmem_range_cachep); |
| 920 | kmem_cache_destroy(ashmem_area_cachep); |
| 921 | |
| 922 | printk(KERN_INFO "ashmem: unloaded\n"); |
| 923 | } |
| 924 | |
| 925 | module_init(ashmem_init); |
| 926 | module_exit(ashmem_exit); |
| 927 | |
| 928 | MODULE_LICENSE("GPL"); |