Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * gntdev.c |
| 3 | * |
| 4 | * Device for accessing (in user-space) pages that have been granted by other |
| 5 | * domains. |
| 6 | * |
| 7 | * Copyright (c) 2006-2007, D G Murray. |
| 8 | * (c) 2009 Gerd Hoffmann <kraxel@redhat.com> |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #undef DEBUG |
| 21 | |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 22 | #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt |
| 23 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/miscdevice.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/mman.h> |
| 31 | #include <linux/mmu_notifier.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/sched.h> |
| 35 | #include <linux/spinlock.h> |
| 36 | #include <linux/slab.h> |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 37 | #include <linux/highmem.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 38 | |
| 39 | #include <xen/xen.h> |
| 40 | #include <xen/grant_table.h> |
Daniel De Graaf | ca47cea | 2011-03-09 18:07:34 -0500 | [diff] [blame] | 41 | #include <xen/balloon.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 42 | #include <xen/gntdev.h> |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 43 | #include <xen/events.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 44 | #include <asm/xen/hypervisor.h> |
| 45 | #include <asm/xen/hypercall.h> |
| 46 | #include <asm/xen/page.h> |
| 47 | |
| 48 | MODULE_LICENSE("GPL"); |
| 49 | MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, " |
| 50 | "Gerd Hoffmann <kraxel@redhat.com>"); |
| 51 | MODULE_DESCRIPTION("User-space granted page access driver"); |
| 52 | |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 53 | static int limit = 1024*1024; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 54 | module_param(limit, int, 0644); |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 55 | MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by " |
| 56 | "the gntdev device"); |
| 57 | |
| 58 | static atomic_t pages_mapped = ATOMIC_INIT(0); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 59 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 60 | static int use_ptemod; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 61 | #define populate_freeable_maps use_ptemod |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 62 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 63 | struct gntdev_priv { |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 64 | /* maps with visible offsets in the file descriptor */ |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 65 | struct list_head maps; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 66 | /* maps that are not visible; will be freed on munmap. |
| 67 | * Only populated if populate_freeable_maps == 1 */ |
| 68 | struct list_head freeable_maps; |
| 69 | /* lock protects maps and freeable_maps */ |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 70 | spinlock_t lock; |
| 71 | struct mm_struct *mm; |
| 72 | struct mmu_notifier mn; |
| 73 | }; |
| 74 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 75 | struct unmap_notify { |
| 76 | int flags; |
| 77 | /* Address relative to the start of the grant_map */ |
| 78 | int addr; |
| 79 | int event; |
| 80 | }; |
| 81 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 82 | struct grant_map { |
| 83 | struct list_head next; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 84 | struct vm_area_struct *vma; |
| 85 | int index; |
| 86 | int count; |
| 87 | int flags; |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 88 | atomic_t users; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 89 | struct unmap_notify notify; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 90 | struct ioctl_gntdev_grant_ref *grants; |
| 91 | struct gnttab_map_grant_ref *map_ops; |
| 92 | struct gnttab_unmap_grant_ref *unmap_ops; |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 93 | struct gnttab_map_grant_ref *kmap_ops; |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame^] | 94 | struct gnttab_unmap_grant_ref *kunmap_ops; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 95 | struct page **pages; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 98 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages); |
| 99 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 100 | /* ------------------------------------------------------------------ */ |
| 101 | |
| 102 | static void gntdev_print_maps(struct gntdev_priv *priv, |
| 103 | char *text, int text_index) |
| 104 | { |
| 105 | #ifdef DEBUG |
| 106 | struct grant_map *map; |
| 107 | |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 108 | pr_debug("%s: maps list (priv %p)\n", __func__, priv); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 109 | list_for_each_entry(map, &priv->maps, next) |
| 110 | pr_debug(" index %2d, count %2d %s\n", |
| 111 | map->index, map->count, |
| 112 | map->index == text_index && text ? text : ""); |
| 113 | #endif |
| 114 | } |
| 115 | |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 116 | static void gntdev_free_map(struct grant_map *map) |
| 117 | { |
| 118 | if (map == NULL) |
| 119 | return; |
| 120 | |
| 121 | if (map->pages) |
| 122 | free_xenballooned_pages(map->count, map->pages); |
| 123 | kfree(map->pages); |
| 124 | kfree(map->grants); |
| 125 | kfree(map->map_ops); |
| 126 | kfree(map->unmap_ops); |
| 127 | kfree(map->kmap_ops); |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame^] | 128 | kfree(map->kunmap_ops); |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 129 | kfree(map); |
| 130 | } |
| 131 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 132 | static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count) |
| 133 | { |
| 134 | struct grant_map *add; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 135 | int i; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 136 | |
| 137 | add = kzalloc(sizeof(struct grant_map), GFP_KERNEL); |
| 138 | if (NULL == add) |
| 139 | return NULL; |
| 140 | |
Dan Carpenter | fc6e0c3 | 2011-11-04 21:23:32 +0300 | [diff] [blame] | 141 | add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL); |
| 142 | add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL); |
| 143 | add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL); |
| 144 | add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL); |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame^] | 145 | add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL); |
Dan Carpenter | fc6e0c3 | 2011-11-04 21:23:32 +0300 | [diff] [blame] | 146 | add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL); |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 147 | if (NULL == add->grants || |
| 148 | NULL == add->map_ops || |
| 149 | NULL == add->unmap_ops || |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 150 | NULL == add->kmap_ops || |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame^] | 151 | NULL == add->kunmap_ops || |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 152 | NULL == add->pages) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 153 | goto err; |
| 154 | |
Stefano Stabellini | 693394b | 2011-09-29 11:57:55 +0100 | [diff] [blame] | 155 | if (alloc_xenballooned_pages(count, add->pages, false /* lowmem */)) |
Daniel De Graaf | ca47cea | 2011-03-09 18:07:34 -0500 | [diff] [blame] | 156 | goto err; |
| 157 | |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 158 | for (i = 0; i < count; i++) { |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 159 | add->map_ops[i].handle = -1; |
| 160 | add->unmap_ops[i].handle = -1; |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 161 | add->kmap_ops[i].handle = -1; |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame^] | 162 | add->kunmap_ops[i].handle = -1; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 165 | add->index = 0; |
| 166 | add->count = count; |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 167 | atomic_set(&add->users, 1); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 168 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 169 | return add; |
| 170 | |
| 171 | err: |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 172 | gntdev_free_map(add); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add) |
| 177 | { |
| 178 | struct grant_map *map; |
| 179 | |
| 180 | list_for_each_entry(map, &priv->maps, next) { |
| 181 | if (add->index + add->count < map->index) { |
| 182 | list_add_tail(&add->next, &map->next); |
| 183 | goto done; |
| 184 | } |
| 185 | add->index = map->index + map->count; |
| 186 | } |
| 187 | list_add_tail(&add->next, &priv->maps); |
| 188 | |
| 189 | done: |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 190 | gntdev_print_maps(priv, "[new]", add->index); |
| 191 | } |
| 192 | |
| 193 | static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, |
| 194 | int index, int count) |
| 195 | { |
| 196 | struct grant_map *map; |
| 197 | |
| 198 | list_for_each_entry(map, &priv->maps, next) { |
| 199 | if (map->index != index) |
| 200 | continue; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 201 | if (count && map->count != count) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 202 | continue; |
| 203 | return map; |
| 204 | } |
| 205 | return NULL; |
| 206 | } |
| 207 | |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 208 | static void gntdev_put_map(struct gntdev_priv *priv, struct grant_map *map) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 209 | { |
| 210 | if (!map) |
| 211 | return; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 212 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 213 | if (!atomic_dec_and_test(&map->users)) |
| 214 | return; |
| 215 | |
| 216 | atomic_sub(map->count, &pages_mapped); |
| 217 | |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 218 | if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 219 | notify_remote_via_evtchn(map->notify.event); |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 220 | evtchn_put(map->notify.event); |
| 221 | } |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 222 | |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 223 | if (populate_freeable_maps && priv) { |
| 224 | spin_lock(&priv->lock); |
| 225 | list_del(&map->next); |
| 226 | spin_unlock(&priv->lock); |
| 227 | } |
| 228 | |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 229 | if (map->pages && !use_ptemod) |
| 230 | unmap_grant_pages(map, 0, map->count); |
| 231 | gntdev_free_map(map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* ------------------------------------------------------------------ */ |
| 235 | |
| 236 | static int find_grant_ptes(pte_t *pte, pgtable_t token, |
| 237 | unsigned long addr, void *data) |
| 238 | { |
| 239 | struct grant_map *map = data; |
| 240 | unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 241 | int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 242 | u64 pte_maddr; |
| 243 | |
| 244 | BUG_ON(pgnr >= map->count); |
Jeremy Fitzhardinge | ba5d101 | 2010-12-08 10:54:32 -0800 | [diff] [blame] | 245 | pte_maddr = arbitrary_virt_to_machine(pte).maddr; |
| 246 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 247 | gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 248 | map->grants[pgnr].ref, |
| 249 | map->grants[pgnr].domid); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 250 | gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 251 | -1 /* handle */); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static int map_grant_pages(struct grant_map *map) |
| 256 | { |
| 257 | int i, err = 0; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 258 | |
| 259 | if (!use_ptemod) { |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 260 | /* Note: it could already be mapped */ |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 261 | if (map->map_ops[0].handle != -1) |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 262 | return 0; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 263 | for (i = 0; i < map->count; i++) { |
Ian Campbell | 38eaeb0 | 2011-03-08 16:56:43 +0000 | [diff] [blame] | 264 | unsigned long addr = (unsigned long) |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 265 | pfn_to_kaddr(page_to_pfn(map->pages[i])); |
| 266 | gnttab_set_map_op(&map->map_ops[i], addr, map->flags, |
| 267 | map->grants[i].ref, |
| 268 | map->grants[i].domid); |
| 269 | gnttab_set_unmap_op(&map->unmap_ops[i], addr, |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 270 | map->flags, -1 /* handle */); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 271 | } |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 272 | } else { |
| 273 | /* |
| 274 | * Setup the map_ops corresponding to the pte entries pointing |
| 275 | * to the kernel linear addresses of the struct pages. |
| 276 | * These ptes are completely different from the user ptes dealt |
| 277 | * with find_grant_ptes. |
| 278 | */ |
| 279 | for (i = 0; i < map->count; i++) { |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 280 | unsigned long address = (unsigned long) |
| 281 | pfn_to_kaddr(page_to_pfn(map->pages[i])); |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 282 | BUG_ON(PageHighMem(map->pages[i])); |
| 283 | |
Stefano Stabellini | ee07264 | 2013-07-23 17:23:54 +0000 | [diff] [blame] | 284 | gnttab_set_map_op(&map->kmap_ops[i], address, |
| 285 | map->flags | GNTMAP_host_map, |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 286 | map->grants[i].ref, |
| 287 | map->grants[i].domid); |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame^] | 288 | gnttab_set_unmap_op(&map->kunmap_ops[i], address, |
| 289 | map->flags | GNTMAP_host_map, -1); |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 290 | } |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 291 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 292 | |
| 293 | pr_debug("map %d+%d\n", map->index, map->count); |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 294 | err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL, |
| 295 | map->pages, map->count); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 296 | if (err) |
| 297 | return err; |
| 298 | |
| 299 | for (i = 0; i < map->count; i++) { |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame^] | 300 | if (map->map_ops[i].status) { |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 301 | err = -EINVAL; |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame^] | 302 | continue; |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 303 | } |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame^] | 304 | |
| 305 | map->unmap_ops[i].handle = map->map_ops[i].handle; |
| 306 | if (use_ptemod) |
| 307 | map->kunmap_ops[i].handle = map->kmap_ops[i].handle; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 308 | } |
| 309 | return err; |
| 310 | } |
| 311 | |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 312 | static int __unmap_grant_pages(struct grant_map *map, int offset, int pages) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 313 | { |
| 314 | int i, err = 0; |
| 315 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 316 | if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { |
| 317 | int pgno = (map->notify.addr >> PAGE_SHIFT); |
Daniel De Graaf | 1affa98 | 2013-01-02 17:57:13 -0500 | [diff] [blame] | 318 | if (pgno >= offset && pgno < offset + pages) { |
| 319 | /* No need for kmap, pages are in lowmem */ |
| 320 | uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno])); |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 321 | tmp[map->notify.addr & (PAGE_SIZE-1)] = 0; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 322 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; |
| 323 | } |
| 324 | } |
| 325 | |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 326 | err = gnttab_unmap_refs(map->unmap_ops + offset, |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame^] | 327 | use_ptemod ? map->kunmap_ops + offset : NULL, map->pages + offset, |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 328 | pages); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 329 | if (err) |
| 330 | return err; |
| 331 | |
| 332 | for (i = 0; i < pages; i++) { |
| 333 | if (map->unmap_ops[offset+i].status) |
| 334 | err = -EINVAL; |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 335 | pr_debug("unmap handle=%d st=%d\n", |
| 336 | map->unmap_ops[offset+i].handle, |
| 337 | map->unmap_ops[offset+i].status); |
| 338 | map->unmap_ops[offset+i].handle = -1; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 339 | } |
| 340 | return err; |
| 341 | } |
| 342 | |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 343 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages) |
| 344 | { |
| 345 | int range, err = 0; |
| 346 | |
| 347 | pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages); |
| 348 | |
| 349 | /* It is possible the requested range will have a "hole" where we |
| 350 | * already unmapped some of the grants. Only unmap valid ranges. |
| 351 | */ |
| 352 | while (pages && !err) { |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 353 | while (pages && map->unmap_ops[offset].handle == -1) { |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 354 | offset++; |
| 355 | pages--; |
| 356 | } |
| 357 | range = 0; |
| 358 | while (range < pages) { |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 359 | if (map->unmap_ops[offset+range].handle == -1) { |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 360 | range--; |
| 361 | break; |
| 362 | } |
| 363 | range++; |
| 364 | } |
| 365 | err = __unmap_grant_pages(map, offset, range); |
| 366 | offset += range; |
| 367 | pages -= range; |
| 368 | } |
| 369 | |
| 370 | return err; |
| 371 | } |
| 372 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 373 | /* ------------------------------------------------------------------ */ |
| 374 | |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 375 | static void gntdev_vma_open(struct vm_area_struct *vma) |
| 376 | { |
| 377 | struct grant_map *map = vma->vm_private_data; |
| 378 | |
| 379 | pr_debug("gntdev_vma_open %p\n", vma); |
| 380 | atomic_inc(&map->users); |
| 381 | } |
| 382 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 383 | static void gntdev_vma_close(struct vm_area_struct *vma) |
| 384 | { |
| 385 | struct grant_map *map = vma->vm_private_data; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 386 | struct file *file = vma->vm_file; |
| 387 | struct gntdev_priv *priv = file->private_data; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 388 | |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 389 | pr_debug("gntdev_vma_close %p\n", vma); |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 390 | if (use_ptemod) { |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 391 | /* It is possible that an mmu notifier could be running |
| 392 | * concurrently, so take priv->lock to ensure that the vma won't |
| 393 | * vanishing during the unmap_grant_pages call, since we will |
| 394 | * spin here until that completes. Such a concurrent call will |
| 395 | * not do any unmapping, since that has been done prior to |
| 396 | * closing the vma, but it may still iterate the unmap_ops list. |
| 397 | */ |
| 398 | spin_lock(&priv->lock); |
| 399 | map->vma = NULL; |
| 400 | spin_unlock(&priv->lock); |
| 401 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 402 | vma->vm_private_data = NULL; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 403 | gntdev_put_map(priv, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 406 | static struct vm_operations_struct gntdev_vmops = { |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 407 | .open = gntdev_vma_open, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 408 | .close = gntdev_vma_close, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | /* ------------------------------------------------------------------ */ |
| 412 | |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 413 | static void unmap_if_in_range(struct grant_map *map, |
| 414 | unsigned long start, unsigned long end) |
| 415 | { |
| 416 | unsigned long mstart, mend; |
| 417 | int err; |
| 418 | |
| 419 | if (!map->vma) |
| 420 | return; |
| 421 | if (map->vma->vm_start >= end) |
| 422 | return; |
| 423 | if (map->vma->vm_end <= start) |
| 424 | return; |
| 425 | mstart = max(start, map->vma->vm_start); |
| 426 | mend = min(end, map->vma->vm_end); |
| 427 | pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n", |
| 428 | map->index, map->count, |
| 429 | map->vma->vm_start, map->vma->vm_end, |
| 430 | start, end, mstart, mend); |
| 431 | err = unmap_grant_pages(map, |
| 432 | (mstart - map->vma->vm_start) >> PAGE_SHIFT, |
| 433 | (mend - mstart) >> PAGE_SHIFT); |
| 434 | WARN_ON(err); |
| 435 | } |
| 436 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 437 | static void mn_invl_range_start(struct mmu_notifier *mn, |
| 438 | struct mm_struct *mm, |
| 439 | unsigned long start, unsigned long end) |
| 440 | { |
| 441 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); |
| 442 | struct grant_map *map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 443 | |
| 444 | spin_lock(&priv->lock); |
| 445 | list_for_each_entry(map, &priv->maps, next) { |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 446 | unmap_if_in_range(map, start, end); |
| 447 | } |
| 448 | list_for_each_entry(map, &priv->freeable_maps, next) { |
| 449 | unmap_if_in_range(map, start, end); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 450 | } |
| 451 | spin_unlock(&priv->lock); |
| 452 | } |
| 453 | |
| 454 | static void mn_invl_page(struct mmu_notifier *mn, |
| 455 | struct mm_struct *mm, |
| 456 | unsigned long address) |
| 457 | { |
| 458 | mn_invl_range_start(mn, mm, address, address + PAGE_SIZE); |
| 459 | } |
| 460 | |
| 461 | static void mn_release(struct mmu_notifier *mn, |
| 462 | struct mm_struct *mm) |
| 463 | { |
| 464 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); |
| 465 | struct grant_map *map; |
| 466 | int err; |
| 467 | |
| 468 | spin_lock(&priv->lock); |
| 469 | list_for_each_entry(map, &priv->maps, next) { |
| 470 | if (!map->vma) |
| 471 | continue; |
| 472 | pr_debug("map %d+%d (%lx %lx)\n", |
| 473 | map->index, map->count, |
| 474 | map->vma->vm_start, map->vma->vm_end); |
| 475 | err = unmap_grant_pages(map, /* offset */ 0, map->count); |
| 476 | WARN_ON(err); |
| 477 | } |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 478 | list_for_each_entry(map, &priv->freeable_maps, next) { |
| 479 | if (!map->vma) |
| 480 | continue; |
| 481 | pr_debug("map %d+%d (%lx %lx)\n", |
| 482 | map->index, map->count, |
| 483 | map->vma->vm_start, map->vma->vm_end); |
| 484 | err = unmap_grant_pages(map, /* offset */ 0, map->count); |
| 485 | WARN_ON(err); |
| 486 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 487 | spin_unlock(&priv->lock); |
| 488 | } |
| 489 | |
Konrad Rzeszutek Wilk | b8b0f55 | 2012-08-21 14:49:34 -0400 | [diff] [blame] | 490 | static struct mmu_notifier_ops gntdev_mmu_ops = { |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 491 | .release = mn_release, |
| 492 | .invalidate_page = mn_invl_page, |
| 493 | .invalidate_range_start = mn_invl_range_start, |
| 494 | }; |
| 495 | |
| 496 | /* ------------------------------------------------------------------ */ |
| 497 | |
| 498 | static int gntdev_open(struct inode *inode, struct file *flip) |
| 499 | { |
| 500 | struct gntdev_priv *priv; |
| 501 | int ret = 0; |
| 502 | |
| 503 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 504 | if (!priv) |
| 505 | return -ENOMEM; |
| 506 | |
| 507 | INIT_LIST_HEAD(&priv->maps); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 508 | INIT_LIST_HEAD(&priv->freeable_maps); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 509 | spin_lock_init(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 510 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 511 | if (use_ptemod) { |
| 512 | priv->mm = get_task_mm(current); |
| 513 | if (!priv->mm) { |
| 514 | kfree(priv); |
| 515 | return -ENOMEM; |
| 516 | } |
| 517 | priv->mn.ops = &gntdev_mmu_ops; |
| 518 | ret = mmu_notifier_register(&priv->mn, priv->mm); |
| 519 | mmput(priv->mm); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 520 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 521 | |
| 522 | if (ret) { |
| 523 | kfree(priv); |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | flip->private_data = priv; |
| 528 | pr_debug("priv %p\n", priv); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static int gntdev_release(struct inode *inode, struct file *flip) |
| 534 | { |
| 535 | struct gntdev_priv *priv = flip->private_data; |
| 536 | struct grant_map *map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 537 | |
| 538 | pr_debug("priv %p\n", priv); |
| 539 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 540 | while (!list_empty(&priv->maps)) { |
| 541 | map = list_entry(priv->maps.next, struct grant_map, next); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 542 | list_del(&map->next); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 543 | gntdev_put_map(NULL /* already removed */, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 544 | } |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 545 | WARN_ON(!list_empty(&priv->freeable_maps)); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 546 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 547 | if (use_ptemod) |
| 548 | mmu_notifier_unregister(&priv->mn, priv->mm); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 549 | kfree(priv); |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, |
| 554 | struct ioctl_gntdev_map_grant_ref __user *u) |
| 555 | { |
| 556 | struct ioctl_gntdev_map_grant_ref op; |
| 557 | struct grant_map *map; |
| 558 | int err; |
| 559 | |
| 560 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 561 | return -EFAULT; |
| 562 | pr_debug("priv %p, add %d\n", priv, op.count); |
| 563 | if (unlikely(op.count <= 0)) |
| 564 | return -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 565 | |
| 566 | err = -ENOMEM; |
| 567 | map = gntdev_alloc_map(priv, op.count); |
| 568 | if (!map) |
| 569 | return err; |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 570 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 571 | if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) { |
| 572 | pr_debug("can't map: over limit\n"); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 573 | gntdev_put_map(NULL, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 574 | return err; |
| 575 | } |
| 576 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 577 | if (copy_from_user(map->grants, &u->refs, |
| 578 | sizeof(map->grants[0]) * op.count) != 0) { |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 579 | gntdev_put_map(NULL, map); |
| 580 | return -EFAULT; |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 581 | } |
| 582 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 583 | spin_lock(&priv->lock); |
| 584 | gntdev_add_map(priv, map); |
| 585 | op.index = map->index << PAGE_SHIFT; |
| 586 | spin_unlock(&priv->lock); |
| 587 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 588 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
| 589 | return -EFAULT; |
| 590 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv, |
| 595 | struct ioctl_gntdev_unmap_grant_ref __user *u) |
| 596 | { |
| 597 | struct ioctl_gntdev_unmap_grant_ref op; |
| 598 | struct grant_map *map; |
| 599 | int err = -ENOENT; |
| 600 | |
| 601 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 602 | return -EFAULT; |
| 603 | pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count); |
| 604 | |
| 605 | spin_lock(&priv->lock); |
| 606 | map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 607 | if (map) { |
| 608 | list_del(&map->next); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 609 | if (populate_freeable_maps) |
| 610 | list_add_tail(&map->next, &priv->freeable_maps); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 611 | err = 0; |
| 612 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 613 | spin_unlock(&priv->lock); |
Daniel De Graaf | 1f1503b | 2011-10-11 15:16:06 -0400 | [diff] [blame] | 614 | if (map) |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 615 | gntdev_put_map(priv, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 616 | return err; |
| 617 | } |
| 618 | |
| 619 | static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv, |
| 620 | struct ioctl_gntdev_get_offset_for_vaddr __user *u) |
| 621 | { |
| 622 | struct ioctl_gntdev_get_offset_for_vaddr op; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 623 | struct vm_area_struct *vma; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 624 | struct grant_map *map; |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 625 | int rv = -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 626 | |
| 627 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 628 | return -EFAULT; |
| 629 | pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr); |
| 630 | |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 631 | down_read(¤t->mm->mmap_sem); |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 632 | vma = find_vma(current->mm, op.vaddr); |
| 633 | if (!vma || vma->vm_ops != &gntdev_vmops) |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 634 | goto out_unlock; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 635 | |
| 636 | map = vma->vm_private_data; |
| 637 | if (!map) |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 638 | goto out_unlock; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 639 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 640 | op.offset = map->index << PAGE_SHIFT; |
| 641 | op.count = map->count; |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 642 | rv = 0; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 643 | |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 644 | out_unlock: |
| 645 | up_read(¤t->mm->mmap_sem); |
| 646 | |
| 647 | if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 648 | return -EFAULT; |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 649 | return rv; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 652 | static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) |
| 653 | { |
| 654 | struct ioctl_gntdev_unmap_notify op; |
| 655 | struct grant_map *map; |
| 656 | int rc; |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 657 | int out_flags; |
| 658 | unsigned int out_event; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 659 | |
| 660 | if (copy_from_user(&op, u, sizeof(op))) |
| 661 | return -EFAULT; |
| 662 | |
| 663 | if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) |
| 664 | return -EINVAL; |
| 665 | |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 666 | /* We need to grab a reference to the event channel we are going to use |
| 667 | * to send the notify before releasing the reference we may already have |
| 668 | * (if someone has called this ioctl twice). This is required so that |
| 669 | * it is possible to change the clear_byte part of the notification |
| 670 | * without disturbing the event channel part, which may now be the last |
| 671 | * reference to that event channel. |
| 672 | */ |
| 673 | if (op.action & UNMAP_NOTIFY_SEND_EVENT) { |
| 674 | if (evtchn_get(op.event_channel_port)) |
| 675 | return -EINVAL; |
| 676 | } |
| 677 | |
| 678 | out_flags = op.action; |
| 679 | out_event = op.event_channel_port; |
| 680 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 681 | spin_lock(&priv->lock); |
| 682 | |
| 683 | list_for_each_entry(map, &priv->maps, next) { |
| 684 | uint64_t begin = map->index << PAGE_SHIFT; |
| 685 | uint64_t end = (map->index + map->count) << PAGE_SHIFT; |
| 686 | if (op.index >= begin && op.index < end) |
| 687 | goto found; |
| 688 | } |
| 689 | rc = -ENOENT; |
| 690 | goto unlock_out; |
| 691 | |
| 692 | found: |
Daniel De Graaf | 9960be9 | 2011-02-09 18:15:50 -0500 | [diff] [blame] | 693 | if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) && |
| 694 | (map->flags & GNTMAP_readonly)) { |
| 695 | rc = -EINVAL; |
| 696 | goto unlock_out; |
| 697 | } |
| 698 | |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 699 | out_flags = map->notify.flags; |
| 700 | out_event = map->notify.event; |
| 701 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 702 | map->notify.flags = op.action; |
| 703 | map->notify.addr = op.index - (map->index << PAGE_SHIFT); |
| 704 | map->notify.event = op.event_channel_port; |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 705 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 706 | rc = 0; |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 707 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 708 | unlock_out: |
| 709 | spin_unlock(&priv->lock); |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 710 | |
| 711 | /* Drop the reference to the event channel we did not save in the map */ |
| 712 | if (out_flags & UNMAP_NOTIFY_SEND_EVENT) |
| 713 | evtchn_put(out_event); |
| 714 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 715 | return rc; |
| 716 | } |
| 717 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 718 | static long gntdev_ioctl(struct file *flip, |
| 719 | unsigned int cmd, unsigned long arg) |
| 720 | { |
| 721 | struct gntdev_priv *priv = flip->private_data; |
| 722 | void __user *ptr = (void __user *)arg; |
| 723 | |
| 724 | switch (cmd) { |
| 725 | case IOCTL_GNTDEV_MAP_GRANT_REF: |
| 726 | return gntdev_ioctl_map_grant_ref(priv, ptr); |
| 727 | |
| 728 | case IOCTL_GNTDEV_UNMAP_GRANT_REF: |
| 729 | return gntdev_ioctl_unmap_grant_ref(priv, ptr); |
| 730 | |
| 731 | case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR: |
| 732 | return gntdev_ioctl_get_offset_for_vaddr(priv, ptr); |
| 733 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 734 | case IOCTL_GNTDEV_SET_UNMAP_NOTIFY: |
| 735 | return gntdev_ioctl_notify(priv, ptr); |
| 736 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 737 | default: |
| 738 | pr_debug("priv %p, unknown cmd %x\n", priv, cmd); |
| 739 | return -ENOIOCTLCMD; |
| 740 | } |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) |
| 746 | { |
| 747 | struct gntdev_priv *priv = flip->private_data; |
| 748 | int index = vma->vm_pgoff; |
| 749 | int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; |
| 750 | struct grant_map *map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 751 | int i, err = -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 752 | |
| 753 | if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED)) |
| 754 | return -EINVAL; |
| 755 | |
| 756 | pr_debug("map %d+%d at %lx (pgoff %lx)\n", |
| 757 | index, count, vma->vm_start, vma->vm_pgoff); |
| 758 | |
| 759 | spin_lock(&priv->lock); |
| 760 | map = gntdev_find_map_index(priv, index, count); |
| 761 | if (!map) |
| 762 | goto unlock_out; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 763 | if (use_ptemod && map->vma) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 764 | goto unlock_out; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 765 | if (use_ptemod && priv->mm != vma->vm_mm) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 766 | pr_warn("Huh? Other mm?\n"); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 767 | goto unlock_out; |
| 768 | } |
| 769 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 770 | atomic_inc(&map->users); |
| 771 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 772 | vma->vm_ops = &gntdev_vmops; |
| 773 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 774 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 775 | |
| 776 | if (use_ptemod) |
Stefano Stabellini | e8e937b | 2012-04-03 18:05:47 +0100 | [diff] [blame] | 777 | vma->vm_flags |= VM_DONTCOPY; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 778 | |
| 779 | vma->vm_private_data = map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 780 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 781 | if (use_ptemod) |
| 782 | map->vma = vma; |
| 783 | |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 784 | if (map->flags) { |
| 785 | if ((vma->vm_flags & VM_WRITE) && |
| 786 | (map->flags & GNTMAP_readonly)) |
Dan Carpenter | a93e20a | 2011-03-19 08:45:43 +0300 | [diff] [blame] | 787 | goto out_unlock_put; |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 788 | } else { |
| 789 | map->flags = GNTMAP_host_map; |
| 790 | if (!(vma->vm_flags & VM_WRITE)) |
| 791 | map->flags |= GNTMAP_readonly; |
| 792 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 793 | |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 794 | spin_unlock(&priv->lock); |
| 795 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 796 | if (use_ptemod) { |
| 797 | err = apply_to_page_range(vma->vm_mm, vma->vm_start, |
| 798 | vma->vm_end - vma->vm_start, |
| 799 | find_grant_ptes, map); |
| 800 | if (err) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 801 | pr_warn("find_grant_ptes() failure.\n"); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 802 | goto out_put_map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 803 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | err = map_grant_pages(map); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 807 | if (err) |
| 808 | goto out_put_map; |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 809 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 810 | if (!use_ptemod) { |
| 811 | for (i = 0; i < count; i++) { |
| 812 | err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE, |
| 813 | map->pages[i]); |
| 814 | if (err) |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 815 | goto out_put_map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 819 | return 0; |
| 820 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 821 | unlock_out: |
| 822 | spin_unlock(&priv->lock); |
| 823 | return err; |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 824 | |
Dan Carpenter | a93e20a | 2011-03-19 08:45:43 +0300 | [diff] [blame] | 825 | out_unlock_put: |
| 826 | spin_unlock(&priv->lock); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 827 | out_put_map: |
Daniel De Graaf | 84e4075 | 2011-02-09 15:11:59 -0500 | [diff] [blame] | 828 | if (use_ptemod) |
| 829 | map->vma = NULL; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 830 | gntdev_put_map(priv, map); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 831 | return err; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static const struct file_operations gntdev_fops = { |
| 835 | .owner = THIS_MODULE, |
| 836 | .open = gntdev_open, |
| 837 | .release = gntdev_release, |
| 838 | .mmap = gntdev_mmap, |
| 839 | .unlocked_ioctl = gntdev_ioctl |
| 840 | }; |
| 841 | |
| 842 | static struct miscdevice gntdev_miscdev = { |
| 843 | .minor = MISC_DYNAMIC_MINOR, |
| 844 | .name = "xen/gntdev", |
| 845 | .fops = &gntdev_fops, |
| 846 | }; |
| 847 | |
| 848 | /* ------------------------------------------------------------------ */ |
| 849 | |
| 850 | static int __init gntdev_init(void) |
| 851 | { |
| 852 | int err; |
| 853 | |
| 854 | if (!xen_domain()) |
| 855 | return -ENODEV; |
| 856 | |
Konrad Rzeszutek Wilk | 6926f6d | 2014-01-03 10:20:18 -0500 | [diff] [blame] | 857 | use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 858 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 859 | err = misc_register(&gntdev_miscdev); |
| 860 | if (err != 0) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 861 | pr_err("Could not register gntdev device\n"); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 862 | return err; |
| 863 | } |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | static void __exit gntdev_exit(void) |
| 868 | { |
| 869 | misc_deregister(&gntdev_miscdev); |
| 870 | } |
| 871 | |
| 872 | module_init(gntdev_init); |
| 873 | module_exit(gntdev_exit); |
| 874 | |
| 875 | /* ------------------------------------------------------------------ */ |