diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
| commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
| tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /mm | |
| parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) | |
Diffstat (limited to 'mm')
| -rw-r--r-- | mm/ashmem.c | 748 | ||||
| -rw-r--r-- | mm/prio_tree.c | 208 | ||||
| -rw-r--r-- | mm/thrash.c | 155 |
3 files changed, 1111 insertions, 0 deletions
diff --git a/mm/ashmem.c b/mm/ashmem.c new file mode 100644 index 00000000000..66e3f23ee33 --- /dev/null +++ b/mm/ashmem.c | |||
| @@ -0,0 +1,748 @@ | |||
| 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> | ||
| 32 | |||
| 33 | #define ASHMEM_NAME_PREFIX "dev/ashmem/" | ||
| 34 | #define ASHMEM_NAME_PREFIX_LEN (sizeof(ASHMEM_NAME_PREFIX) - 1) | ||
| 35 | #define ASHMEM_FULL_NAME_LEN (ASHMEM_NAME_LEN + ASHMEM_NAME_PREFIX_LEN) | ||
| 36 | |||
| 37 | /* | ||
| 38 | * ashmem_area - anonymous shared memory area | ||
| 39 | * Lifecycle: From our parent file's open() until its release() | ||
| 40 | * Locking: Protected by `ashmem_mutex' | ||
| 41 | * Big Note: Mappings do NOT pin this structure; it dies on close() | ||
| 42 | */ | ||
| 43 | struct ashmem_area { | ||
| 44 | char name[ASHMEM_FULL_NAME_LEN];/* optional name for /proc/pid/maps */ | ||
| 45 | struct list_head unpinned_list; /* list of all ashmem areas */ | ||
| 46 | struct file *file; /* the shmem-based backing file */ | ||
| 47 | size_t size; /* size of the mapping, in bytes */ | ||
| 48 | unsigned long prot_mask; /* allowed prot bits, as vm_flags */ | ||
| 49 | }; | ||
| 50 | |||
| 51 | /* | ||
| 52 | * ashmem_range - represents an interval of unpinned (evictable) pages | ||
| 53 | * Lifecycle: From unpin to pin | ||
| 54 | * Locking: Protected by `ashmem_mutex' | ||
| 55 | */ | ||
| 56 | struct ashmem_range { | ||
| 57 | struct list_head lru; /* entry in LRU list */ | ||
| 58 | struct list_head unpinned; /* entry in its area's unpinned list */ | ||
| 59 | struct ashmem_area *asma; /* associated area */ | ||
| 60 | size_t pgstart; /* starting page, inclusive */ | ||
| 61 | size_t pgend; /* ending page, inclusive */ | ||
| 62 | unsigned int purged; /* ASHMEM_NOT or ASHMEM_WAS_PURGED */ | ||
| 63 | }; | ||
| 64 | |||
| 65 | /* LRU list of unpinned pages, protected by ashmem_mutex */ | ||
| 66 | static LIST_HEAD(ashmem_lru_list); | ||
| 67 | |||
| 68 | /* Count of pages on our LRU list, protected by ashmem_mutex */ | ||
| 69 | static unsigned long lru_count; | ||
| 70 | |||
| 71 | /* | ||
| 72 | * ashmem_mutex - protects the list of and each individual ashmem_area | ||
| 73 | * | ||
| 74 | * Lock Ordering: ashmex_mutex -> i_mutex -> i_alloc_sem | ||
| 75 | */ | ||
| 76 | static DEFINE_MUTEX(ashmem_mutex); | ||
| 77 | |||
| 78 | static struct kmem_cache *ashmem_area_cachep __read_mostly; | ||
| 79 | static struct kmem_cache *ashmem_range_cachep __read_mostly; | ||
| 80 | |||
| 81 | #define range_size(range) \ | ||
| 82 | ((range)->pgend - (range)->pgstart + 1) | ||
| 83 | |||
| 84 | #define range_on_lru(range) \ | ||
| 85 | ((range)->purged == ASHMEM_NOT_PURGED) | ||
| 86 | |||
| 87 | #define page_range_subsumes_range(range, start, end) \ | ||
| 88 | (((range)->pgstart >= (start)) && ((range)->pgend <= (end))) | ||
| 89 | |||
| 90 | #define page_range_subsumed_by_range(range, start, end) \ | ||
| 91 | (((range)->pgstart <= (start)) && ((range)->pgend >= (end))) | ||
| 92 | |||
| 93 | #define page_in_range(range, page) \ | ||
| 94 | (((range)->pgstart <= (page)) && ((range)->pgend >= (page))) | ||
| 95 | |||
| 96 | #define page_range_in_range(range, start, end) \ | ||
| 97 | (page_in_range(range, start) || page_in_range(range, end) || \ | ||
| 98 | page_range_subsumes_range(range, start, end)) | ||
| 99 | |||
| 100 | #define range_before_page(range, page) \ | ||
| 101 | ((range)->pgend < (page)) | ||
| 102 | |||
| 103 | #define PROT_MASK (PROT_EXEC | PROT_READ | PROT_WRITE) | ||
| 104 | |||
| 105 | static inline void lru_add(struct ashmem_range *range) | ||
| 106 | { | ||
| 107 | list_add_tail(&range->lru, &ashmem_lru_list); | ||
| 108 | lru_count += range_size(range); | ||
| 109 | } | ||
| 110 | |||
| 111 | static inline void lru_del(struct ashmem_range *range) | ||
| 112 | { | ||
| 113 | list_del(&range->lru); | ||
| 114 | lru_count -= range_size(range); | ||
| 115 | } | ||
| 116 | |||
| 117 | /* | ||
| 118 | * range_alloc - allocate and initialize a new ashmem_range structure | ||
| 119 | * | ||
| 120 | * 'asma' - associated ashmem_area | ||
| 121 | * 'prev_range' - the previous ashmem_range in the sorted asma->unpinned list | ||
| 122 | * 'purged' - initial purge value (ASMEM_NOT_PURGED or ASHMEM_WAS_PURGED) | ||
| 123 | * 'start' - starting page, inclusive | ||
| 124 | * 'end' - ending page, inclusive | ||
| 125 | * | ||
| 126 | * Caller must hold ashmem_mutex. | ||
| 127 | */ | ||
| 128 | static int range_alloc(struct ashmem_area *asma, | ||
| 129 | struct ashmem_range *prev_range, unsigned int purged, | ||
| 130 | size_t start, size_t end) | ||
| 131 | { | ||
| 132 | struct ashmem_range *range; | ||
| 133 | |||
| 134 | range = kmem_cache_zalloc(ashmem_range_cachep, GFP_KERNEL); | ||
| 135 | if (unlikely(!range)) | ||
| 136 | return -ENOMEM; | ||
| 137 | |||
| 138 | range->asma = asma; | ||
| 139 | range->pgstart = start; | ||
| 140 | range->pgend = end; | ||
| 141 | range->purged = purged; | ||
| 142 | |||
| 143 | list_add_tail(&range->unpinned, &prev_range->unpinned); | ||
| 144 | |||
| 145 | if (range_on_lru(range)) | ||
| 146 | lru_add(range); | ||
| 147 | |||
| 148 | return 0; | ||
| 149 | } | ||
| 150 | |||
| 151 | static void range_del(struct ashmem_range *range) | ||
| 152 | { | ||
| 153 | list_del(&range->unpinned); | ||
| 154 | if (range_on_lru(range)) | ||
| 155 | lru_del(range); | ||
| 156 | kmem_cache_free(ashmem_range_cachep, range); | ||
| 157 | } | ||
| 158 | |||
| 159 | /* | ||
| 160 | * range_shrink - shrinks a range | ||
| 161 | * | ||
| 162 | * Caller must hold ashmem_mutex. | ||
| 163 | */ | ||
| 164 | static inline void range_shrink(struct ashmem_range *range, | ||
| 165 | size_t start, size_t end) | ||
| 166 | { | ||
| 167 | size_t pre = range_size(range); | ||
| 168 | |||
| 169 | range->pgstart = start; | ||
| 170 | range->pgend = end; | ||
| 171 | |||
| 172 | if (range_on_lru(range)) | ||
| 173 | lru_count -= pre - range_size(range); | ||
| 174 | } | ||
| 175 | |||
| 176 | static int ashmem_open(struct inode *inode, struct file *file) | ||
| 177 | { | ||
| 178 | struct ashmem_area *asma; | ||
| 179 | int ret; | ||
| 180 | |||
| 181 | ret = generic_file_open(inode, file); | ||
| 182 | if (unlikely(ret)) | ||
| 183 | return ret; | ||
| 184 | |||
| 185 | asma = kmem_cache_zalloc(ashmem_area_cachep, GFP_KERNEL); | ||
| 186 | if (unlikely(!asma)) | ||
| 187 | return -ENOMEM; | ||
| 188 | |||
| 189 | INIT_LIST_HEAD(&asma->unpinned_list); | ||
| 190 | memcpy(asma->name, ASHMEM_NAME_PREFIX, ASHMEM_NAME_PREFIX_LEN); | ||
| 191 | asma->prot_mask = PROT_MASK; | ||
| 192 | file->private_data = asma; | ||
| 193 | |||
| 194 | return 0; | ||
| 195 | } | ||
| 196 | |||
| 197 | static int ashmem_release(struct inode *ignored, struct file *file) | ||
| 198 | { | ||
| 199 | struct ashmem_area *asma = file->private_data; | ||
| 200 | struct ashmem_range *range, *next; | ||
| 201 | |||
| 202 | mutex_lock(&ashmem_mutex); | ||
| 203 | list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) | ||
| 204 | range_del(range); | ||
| 205 | mutex_unlock(&ashmem_mutex); | ||
| 206 | |||
| 207 | if (asma->file) | ||
| 208 | fput(asma->file); | ||
| 209 | kmem_cache_free(ashmem_area_cachep, asma); | ||
| 210 | |||
| 211 | return 0; | ||
| 212 | } | ||
| 213 | |||
| 214 | static ssize_t ashmem_read(struct file *file, char __user *buf, | ||
| 215 | size_t len, loff_t *pos) | ||
| 216 | { | ||
| 217 | struct ashmem_area *asma = file->private_data; | ||
| 218 | int ret = 0; | ||
| 219 | |||
| 220 | mutex_lock(&ashmem_mutex); | ||
| 221 | |||
| 222 | /* If size is not set, or set to 0, always return EOF. */ | ||
| 223 | if (asma->size == 0) { | ||
| 224 | goto out; | ||
| 225 | } | ||
| 226 | |||
| 227 | if (!asma->file) { | ||
| 228 | ret = -EBADF; | ||
| 229 | |||
