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