blob: 7ca247af034a0e9a40044b9f8d8ba4c21498103d [file] [log] [blame]
Robert Love11980c22011-12-20 16:49:48 -08001/* 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 Mucklef132c6c2012-06-06 18:30:57 -070031#include <linux/ashmem.h>
32#include <asm/cacheflush.h>
Robert Love11980c22011-12-20 16:49:48 -080033
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 */
44struct ashmem_area {
John Stultz1efb3432011-12-20 16:49:54 -080045 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 Mucklef132c6c2012-06-06 18:30:57 -070049 unsigned long vm_start; /* Start address of vm_area
50 * which maps this ashmem */
John Stultz1efb3432011-12-20 16:49:54 -080051 unsigned long prot_mask; /* allowed prot bits, as vm_flags */
Robert Love11980c22011-12-20 16:49:48 -080052};
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 */
59struct 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 */
69static LIST_HEAD(ashmem_lru_list);
70
71/* Count of pages on our LRU list, protected by ashmem_mutex */
72static 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 */
79static DEFINE_MUTEX(ashmem_mutex);
80
81static struct kmem_cache *ashmem_area_cachep __read_mostly;
82static struct kmem_cache *ashmem_range_cachep __read_mostly;
83
84#define range_size(range) \
John Stultz1efb3432011-12-20 16:49:54 -080085 ((range)->pgend - (range)->pgstart + 1)
Robert Love11980c22011-12-20 16:49:48 -080086
87#define range_on_lru(range) \
John Stultz1efb3432011-12-20 16:49:54 -080088 ((range)->purged == ASHMEM_NOT_PURGED)
Robert Love11980c22011-12-20 16:49:48 -080089
90#define page_range_subsumes_range(range, start, end) \
John Stultz1efb3432011-12-20 16:49:54 -080091 (((range)->pgstart >= (start)) && ((range)->pgend <= (end)))
Robert Love11980c22011-12-20 16:49:48 -080092
93#define page_range_subsumed_by_range(range, start, end) \
John Stultz1efb3432011-12-20 16:49:54 -080094 (((range)->pgstart <= (start)) && ((range)->pgend >= (end)))
Robert Love11980c22011-12-20 16:49:48 -080095
96#define page_in_range(range, page) \
John Stultz1efb3432011-12-20 16:49:54 -080097 (((range)->pgstart <= (page)) && ((range)->pgend >= (page)))
Robert Love11980c22011-12-20 16:49:48 -080098
99#define page_range_in_range(range, start, end) \
John Stultz1efb3432011-12-20 16:49:54 -0800100 (page_in_range(range, start) || page_in_range(range, end) || \
101 page_range_subsumes_range(range, start, end))
Robert Love11980c22011-12-20 16:49:48 -0800102
103#define range_before_page(range, page) \
John Stultz1efb3432011-12-20 16:49:54 -0800104 ((range)->pgend < (page))
Robert Love11980c22011-12-20 16:49:48 -0800105
106#define PROT_MASK (PROT_EXEC | PROT_READ | PROT_WRITE)
107
108static 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
114static 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 */
131static 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
154static 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 */
167static 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
179static int ashmem_open(struct inode *inode, struct file *file)
180{
181 struct ashmem_area *asma;
182 int ret;
183
Bjorn Bringert5154b932011-12-20 16:49:52 -0800184 ret = generic_file_open(inode, file);
Robert Love11980c22011-12-20 16:49:48 -0800185 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
200static 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 Bringert853ca7a2011-12-20 16:49:49 -0800217static 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 Stultz1efb3432011-12-20 16:49:54 -0800226 if (asma->size == 0)
Todd Poynore70b6402013-06-04 17:29:38 -0700227 goto out_unlock;
Bjorn Bringert853ca7a2011-12-20 16:49:49 -0800228
229 if (!asma->file) {
230 ret = -EBADF;
Todd Poynore70b6402013-06-04 17:29:38 -0700231 goto out_unlock;
Bjorn Bringert853ca7a2011-12-20 16:49:49 -0800232 }
233
Todd Poynore70b6402013-06-04 17:29:38 -0700234 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 Bringert853ca7a2011-12-20 16:49:49 -0800242 ret = asma->file->f_op->read(asma->file, buf, len, pos);
Todd Poynore70b6402013-06-04 17:29:38 -0700243 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 Bringert5154b932011-12-20 16:49:52 -0800248
Todd Poynore70b6402013-06-04 17:29:38 -0700249out_unlock:
Bjorn Bringert5154b932011-12-20 16:49:52 -0800250 mutex_unlock(&ashmem_mutex);
251 return ret;
252}
253
254static 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 Stultz1efb3432011-12-20 16:49:54 -0800272 if (ret < 0)
Bjorn Bringert5154b932011-12-20 16:49:52 -0800273 goto out;
Bjorn Bringert5154b932011-12-20 16:49:52 -0800274
275 /** Copy f_pos from backing file, since f_ops->llseek() sets it */
276 file->f_pos = asma->file->f_pos;
Bjorn Bringert853ca7a2011-12-20 16:49:49 -0800277
278out:
279 mutex_unlock(&ashmem_mutex);
280 return ret;
281}
282
John Stultz1efb3432011-12-20 16:49:54 -0800283static inline unsigned long calc_vm_may_flags(unsigned long prot)
Arve Hjønnevåg56f76fc2011-12-20 16:49:50 -0800284{
John Stultz1efb3432011-12-20 16:49:54 -0800285 return _calc_vm_trans(prot, PROT_READ, VM_MAYREAD) |
Arve Hjønnevåg56f76fc2011-12-20 16:49:50 -0800286 _calc_vm_trans(prot, PROT_WRITE, VM_MAYWRITE) |
287 _calc_vm_trans(prot, PROT_EXEC, VM_MAYEXEC);
288}
Bjorn Bringert853ca7a2011-12-20 16:49:49 -0800289
Robert Love11980c22011-12-20 16:49:48 -0800290static 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åg56f76fc2011-12-20 16:49:50 -0800304 if (unlikely((vma->vm_flags & ~calc_vm_prot_bits(asma->prot_mask)) &
John Stultz1efb3432011-12-20 16:49:54 -0800305 calc_vm_prot_bits(PROT_MASK))) {
Robert Love11980c22011-12-20 16:49:48 -0800306 ret = -EPERM;
307 goto out;
308 }
Arve Hjønnevåg56f76fc2011-12-20 16:49:50 -0800309 vma->vm_flags &= ~calc_vm_may_flags(~asma->prot_mask);
Robert Love11980c22011-12-20 16:49:48 -0800310
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 Stultzd92aa582011-12-15 18:51:04 -0800328 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 Love11980c22011-12-20 16:49:48 -0800334 }
Robert Love11980c22011-12-20 16:49:48 -0800335 vma->vm_flags |= VM_CAN_NONLINEAR;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700336 asma->vm_start = vma->vm_start;
Robert Love11980c22011-12-20 16:49:48 -0800337
338out:
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 Cross33e8fc42011-12-20 16:49:53 -0800358static int ashmem_shrink(struct shrinker *s, struct shrink_control *sc)
Robert Love11980c22011-12-20 16:49:48 -0800359{
360 struct ashmem_range *range, *next;
361
362 /* We might recurse into filesystem code, so bail out if necessary */
Colin Cross33e8fc42011-12-20 16:49:53 -0800363 if (sc->nr_to_scan && !(sc->gfp_mask & __GFP_FS))
Robert Love11980c22011-12-20 16:49:48 -0800364 return -1;
Colin Cross33e8fc42011-12-20 16:49:53 -0800365 if (!sc->nr_to_scan)
Robert Love11980c22011-12-20 16:49:48 -0800366 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 Cross33e8fc42011-12-20 16:49:53 -0800378 sc->nr_to_scan -= range_size(range);
379 if (sc->nr_to_scan <= 0)
Robert Love11980c22011-12-20 16:49:48 -0800380 break;
381 }
382 mutex_unlock(&ashmem_mutex);
383
384 return lru_count;
385}
386
387static struct shrinker ashmem_shrinker = {
388 .shrink = ashmem_shrink,
389 .seeks = DEFAULT_SEEKS * 4,
390};
391
392static 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
410out:
411 mutex_unlock(&ashmem_mutex);
412 return ret;
413}
414
415static int set_name(struct ashmem_area *asma, void __user *name)
416{
Todd Poynore70b6402013-06-04 17:29:38 -0700417 int len;
Robert Love11980c22011-12-20 16:49:48 -0800418 int ret = 0;
Shankar Brahadeeswaranc1d5bd62013-02-20 23:41:26 +0530419 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 Poynore70b6402013-06-04 17:29:38 -0700430 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 Love11980c22011-12-20 16:49:48 -0800435 mutex_lock(&ashmem_mutex);
Robert Love11980c22011-12-20 16:49:48 -0800436 /* cannot change an existing mapping's name */
Todd Poynore70b6402013-06-04 17:29:38 -0700437 if (unlikely(asma->file))
Robert Love11980c22011-12-20 16:49:48 -0800438 ret = -EINVAL;
Todd Poynore70b6402013-06-04 17:29:38 -0700439 else
440 strcpy(asma->name + ASHMEM_NAME_PREFIX_LEN, local_name);
Robert Love11980c22011-12-20 16:49:48 -0800441
Todd Poynore70b6402013-06-04 17:29:38 -0700442 mutex_unlock(&ashmem_mutex);
Robert Love11980c22011-12-20 16:49:48 -0800443 return ret;
444}
445
446static int get_name(struct ashmem_area *asma, void __user *name)
447{
448 int ret = 0;
Shankar Brahadeeswaranc1d5bd62013-02-20 23:41:26 +0530449 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 Love11980c22011-12-20 16:49:48 -0800457
458 mutex_lock(&ashmem_mutex);
459 if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0') {
Robert Love11980c22011-12-20 16:49:48 -0800460
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 Brahadeeswaranc1d5bd62013-02-20 23:41:26 +0530466 memcpy(local_name, asma->name + ASHMEM_NAME_PREFIX_LEN, len);
Robert Love11980c22011-12-20 16:49:48 -0800467 } else {
Shankar Brahadeeswaranc1d5bd62013-02-20 23:41:26 +0530468 len = sizeof(ASHMEM_NAME_DEF);
469 memcpy(local_name, ASHMEM_NAME_DEF, len);
Robert Love11980c22011-12-20 16:49:48 -0800470 }
471 mutex_unlock(&ashmem_mutex);
472
Shankar Brahadeeswaranc1d5bd62013-02-20 23:41:26 +0530473 /*
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 Love11980c22011-12-20 16:49:48 -0800479 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 */
488static 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 */
554static 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
559restart:
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 */
589static 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
607static 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 Mucklef132c6c2012-06-06 18:30:57 -0700655#ifdef CONFIG_OUTER_CACHE
656static unsigned int virtaddr_to_physaddr(unsigned int virtaddr)
657{
658 unsigned int physaddr = 0;
659 pgd_t *pgd_ptr = NULL;
Trilok Soni318763f2012-06-26 14:30:15 +0530660 pud_t *pud_ptr = NULL;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700661 pmd_t *pmd_ptr = NULL;
662 pte_t *pte_ptr = NULL, pte;
663
664 spin_lock(&current->mm->page_table_lock);
665 pgd_ptr = pgd_offset(current->mm, virtaddr);
Trilok Soni318763f2012-06-26 14:30:15 +0530666 if (pgd_none(*pgd_ptr) || pgd_bad(*pgd_ptr)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700667 pr_err("Failed to convert virtaddr %x to pgd_ptr\n",
668 virtaddr);
669 goto done;
670 }
671
Trilok Soni318763f2012-06-26 14:30:15 +0530672 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 Mucklef132c6c2012-06-06 18:30:57 -0700675 (void *)pgd_ptr);
676 goto done;
677 }
678
Trilok Soni318763f2012-06-26 14:30:15 +0530679 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 Mucklef132c6c2012-06-06 18:30:57 -0700686 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);
695done:
696 spin_unlock(&current->mm->page_table_lock);
697 physaddr <<= PAGE_SHIFT;
698 return physaddr;
699}
700#endif
701
702static 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(&current->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 Trusten03fd16d2012-08-07 13:55:03 -0700724 if ((asma->vm_start + asma->size) > vma->vm_end) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700725 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
740done:
741 up_read(&current->mm->mmap_sem);
742 if (ret)
743 asma->vm_start = 0;
744 return ret;
745}
746
Robert Love11980c22011-12-20 16:49:48 -0800747static 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 Cross33e8fc42011-12-20 16:49:53 -0800783 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 Love11980c22011-12-20 16:49:48 -0800790 }
791 break;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700792 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 Love11980c22011-12-20 16:49:48 -0800801 }
802
803 return ret;
804}
805
Steve Mucklef132c6c2012-06-06 18:30:57 -0700806static 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
813int 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}
845EXPORT_SYMBOL(get_ashmem_file);
846
847void 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}
857EXPORT_SYMBOL(put_ashmem_file);
858
Tracey Dentaa5af972012-01-22 21:28:44 -0500859static const struct file_operations ashmem_fops = {
Robert Love11980c22011-12-20 16:49:48 -0800860 .owner = THIS_MODULE,
861 .open = ashmem_open,
862 .release = ashmem_release,
John Stultz1efb3432011-12-20 16:49:54 -0800863 .read = ashmem_read,
864 .llseek = ashmem_llseek,
Robert Love11980c22011-12-20 16:49:48 -0800865 .mmap = ashmem_mmap,
866 .unlocked_ioctl = ashmem_ioctl,
867 .compat_ioctl = ashmem_ioctl,
868};
869
870static struct miscdevice ashmem_misc = {
871 .minor = MISC_DYNAMIC_MINOR,
872 .name = "ashmem",
873 .fops = &ashmem_fops,
874};
875
876static 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
909static 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
925module_init(ashmem_init);
926module_exit(ashmem_exit);
927
928MODULE_LICENSE("GPL");