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 */ |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 70 | struct mutex lock; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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) |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 122 | gnttab_free_pages(map->count, map->pages); |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 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 | |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 155 | if (gnttab_alloc_pages(count, add->pages)) |
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) { |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 224 | mutex_lock(&priv->lock); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 225 | list_del(&map->next); |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 226 | mutex_unlock(&priv->lock); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 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 | |
David Vrabel | 923b291 | 2014-12-18 14:56:54 +0000 | [diff] [blame^] | 247 | /* |
| 248 | * Set the PTE as special to force get_user_pages_fast() fall |
| 249 | * back to the slow path. If this is not supported as part of |
| 250 | * the grant map, it will be done afterwards. |
| 251 | */ |
| 252 | if (xen_feature(XENFEAT_gnttab_map_avail_bits)) |
| 253 | flags |= (1 << _GNTMAP_guest_avail0); |
| 254 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 255 | gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 256 | map->grants[pgnr].ref, |
| 257 | map->grants[pgnr].domid); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 258 | gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 259 | -1 /* handle */); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
David Vrabel | 923b291 | 2014-12-18 14:56:54 +0000 | [diff] [blame^] | 263 | #ifdef CONFIG_X86 |
| 264 | static int set_grant_ptes_as_special(pte_t *pte, pgtable_t token, |
| 265 | unsigned long addr, void *data) |
| 266 | { |
| 267 | set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte)); |
| 268 | return 0; |
| 269 | } |
| 270 | #endif |
| 271 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 272 | static int map_grant_pages(struct grant_map *map) |
| 273 | { |
| 274 | int i, err = 0; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 275 | |
| 276 | if (!use_ptemod) { |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 277 | /* Note: it could already be mapped */ |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 278 | if (map->map_ops[0].handle != -1) |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 279 | return 0; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 280 | for (i = 0; i < map->count; i++) { |
Ian Campbell | 38eaeb0 | 2011-03-08 16:56:43 +0000 | [diff] [blame] | 281 | unsigned long addr = (unsigned long) |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 282 | pfn_to_kaddr(page_to_pfn(map->pages[i])); |
| 283 | gnttab_set_map_op(&map->map_ops[i], addr, map->flags, |
| 284 | map->grants[i].ref, |
| 285 | map->grants[i].domid); |
| 286 | gnttab_set_unmap_op(&map->unmap_ops[i], addr, |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 287 | map->flags, -1 /* handle */); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 288 | } |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 289 | } else { |
| 290 | /* |
| 291 | * Setup the map_ops corresponding to the pte entries pointing |
| 292 | * to the kernel linear addresses of the struct pages. |
| 293 | * These ptes are completely different from the user ptes dealt |
| 294 | * with find_grant_ptes. |
| 295 | */ |
| 296 | for (i = 0; i < map->count; i++) { |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 297 | unsigned long address = (unsigned long) |
| 298 | pfn_to_kaddr(page_to_pfn(map->pages[i])); |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 299 | BUG_ON(PageHighMem(map->pages[i])); |
| 300 | |
Stefano Stabellini | ee07264 | 2013-07-23 17:23:54 +0000 | [diff] [blame] | 301 | gnttab_set_map_op(&map->kmap_ops[i], address, |
| 302 | map->flags | GNTMAP_host_map, |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 303 | map->grants[i].ref, |
| 304 | map->grants[i].domid); |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 305 | gnttab_set_unmap_op(&map->kunmap_ops[i], address, |
| 306 | map->flags | GNTMAP_host_map, -1); |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 307 | } |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 308 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 309 | |
| 310 | pr_debug("map %d+%d\n", map->index, map->count); |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 311 | err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL, |
| 312 | map->pages, map->count); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 313 | if (err) |
| 314 | return err; |
| 315 | |
| 316 | for (i = 0; i < map->count; i++) { |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 317 | if (map->map_ops[i].status) { |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 318 | err = -EINVAL; |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 319 | continue; |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 320 | } |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 321 | |
| 322 | map->unmap_ops[i].handle = map->map_ops[i].handle; |
| 323 | if (use_ptemod) |
| 324 | map->kunmap_ops[i].handle = map->kmap_ops[i].handle; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 325 | } |
| 326 | return err; |
| 327 | } |
| 328 | |
Jennifer Herbert | 7452822 | 2015-01-05 15:07:46 +0000 | [diff] [blame] | 329 | struct unmap_grant_pages_callback_data |
| 330 | { |
| 331 | struct completion completion; |
| 332 | int result; |
| 333 | }; |
| 334 | |
| 335 | static void unmap_grant_callback(int result, |
| 336 | struct gntab_unmap_queue_data *data) |
| 337 | { |
| 338 | struct unmap_grant_pages_callback_data* d = data->data; |
| 339 | |
| 340 | d->result = result; |
| 341 | complete(&d->completion); |
| 342 | } |
| 343 | |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 344 | 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] | 345 | { |
| 346 | int i, err = 0; |
Jennifer Herbert | 7452822 | 2015-01-05 15:07:46 +0000 | [diff] [blame] | 347 | struct gntab_unmap_queue_data unmap_data; |
| 348 | struct unmap_grant_pages_callback_data data; |
| 349 | |
| 350 | init_completion(&data.completion); |
| 351 | unmap_data.data = &data; |
| 352 | unmap_data.done= &unmap_grant_callback; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 353 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 354 | if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { |
| 355 | int pgno = (map->notify.addr >> PAGE_SHIFT); |
Daniel De Graaf | 1affa98 | 2013-01-02 17:57:13 -0500 | [diff] [blame] | 356 | if (pgno >= offset && pgno < offset + pages) { |
| 357 | /* No need for kmap, pages are in lowmem */ |
| 358 | 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] | 359 | tmp[map->notify.addr & (PAGE_SIZE-1)] = 0; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 360 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; |
| 361 | } |
| 362 | } |
| 363 | |
Jennifer Herbert | 7452822 | 2015-01-05 15:07:46 +0000 | [diff] [blame] | 364 | unmap_data.unmap_ops = map->unmap_ops + offset; |
| 365 | unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL; |
| 366 | unmap_data.pages = map->pages + offset; |
| 367 | unmap_data.count = pages; |
| 368 | |
| 369 | gnttab_unmap_refs_async(&unmap_data); |
| 370 | |
| 371 | wait_for_completion(&data.completion); |
| 372 | if (data.result) |
| 373 | return data.result; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 374 | |
| 375 | for (i = 0; i < pages; i++) { |
| 376 | if (map->unmap_ops[offset+i].status) |
| 377 | err = -EINVAL; |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 378 | pr_debug("unmap handle=%d st=%d\n", |
| 379 | map->unmap_ops[offset+i].handle, |
| 380 | map->unmap_ops[offset+i].status); |
| 381 | map->unmap_ops[offset+i].handle = -1; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 382 | } |
| 383 | return err; |
| 384 | } |
| 385 | |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 386 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages) |
| 387 | { |
| 388 | int range, err = 0; |
| 389 | |
| 390 | pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages); |
| 391 | |
| 392 | /* It is possible the requested range will have a "hole" where we |
| 393 | * already unmapped some of the grants. Only unmap valid ranges. |
| 394 | */ |
| 395 | while (pages && !err) { |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 396 | while (pages && map->unmap_ops[offset].handle == -1) { |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 397 | offset++; |
| 398 | pages--; |
| 399 | } |
| 400 | range = 0; |
| 401 | while (range < pages) { |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 402 | if (map->unmap_ops[offset+range].handle == -1) { |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 403 | range--; |
| 404 | break; |
| 405 | } |
| 406 | range++; |
| 407 | } |
| 408 | err = __unmap_grant_pages(map, offset, range); |
| 409 | offset += range; |
| 410 | pages -= range; |
| 411 | } |
| 412 | |
| 413 | return err; |
| 414 | } |
| 415 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 416 | /* ------------------------------------------------------------------ */ |
| 417 | |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 418 | static void gntdev_vma_open(struct vm_area_struct *vma) |
| 419 | { |
| 420 | struct grant_map *map = vma->vm_private_data; |
| 421 | |
| 422 | pr_debug("gntdev_vma_open %p\n", vma); |
| 423 | atomic_inc(&map->users); |
| 424 | } |
| 425 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 426 | static void gntdev_vma_close(struct vm_area_struct *vma) |
| 427 | { |
| 428 | struct grant_map *map = vma->vm_private_data; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 429 | struct file *file = vma->vm_file; |
| 430 | struct gntdev_priv *priv = file->private_data; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 431 | |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 432 | pr_debug("gntdev_vma_close %p\n", vma); |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 433 | if (use_ptemod) { |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 434 | /* It is possible that an mmu notifier could be running |
| 435 | * concurrently, so take priv->lock to ensure that the vma won't |
| 436 | * vanishing during the unmap_grant_pages call, since we will |
| 437 | * spin here until that completes. Such a concurrent call will |
| 438 | * not do any unmapping, since that has been done prior to |
| 439 | * closing the vma, but it may still iterate the unmap_ops list. |
| 440 | */ |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 441 | mutex_lock(&priv->lock); |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 442 | map->vma = NULL; |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 443 | mutex_unlock(&priv->lock); |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 444 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 445 | vma->vm_private_data = NULL; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 446 | gntdev_put_map(priv, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 449 | static struct vm_operations_struct gntdev_vmops = { |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 450 | .open = gntdev_vma_open, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 451 | .close = gntdev_vma_close, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
| 454 | /* ------------------------------------------------------------------ */ |
| 455 | |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 456 | static void unmap_if_in_range(struct grant_map *map, |
| 457 | unsigned long start, unsigned long end) |
| 458 | { |
| 459 | unsigned long mstart, mend; |
| 460 | int err; |
| 461 | |
| 462 | if (!map->vma) |
| 463 | return; |
| 464 | if (map->vma->vm_start >= end) |
| 465 | return; |
| 466 | if (map->vma->vm_end <= start) |
| 467 | return; |
| 468 | mstart = max(start, map->vma->vm_start); |
| 469 | mend = min(end, map->vma->vm_end); |
| 470 | pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n", |
| 471 | map->index, map->count, |
| 472 | map->vma->vm_start, map->vma->vm_end, |
| 473 | start, end, mstart, mend); |
| 474 | err = unmap_grant_pages(map, |
| 475 | (mstart - map->vma->vm_start) >> PAGE_SHIFT, |
| 476 | (mend - mstart) >> PAGE_SHIFT); |
| 477 | WARN_ON(err); |
| 478 | } |
| 479 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 480 | static void mn_invl_range_start(struct mmu_notifier *mn, |
| 481 | struct mm_struct *mm, |
| 482 | unsigned long start, unsigned long end) |
| 483 | { |
| 484 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); |
| 485 | struct grant_map *map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 486 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 487 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 488 | list_for_each_entry(map, &priv->maps, next) { |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 489 | unmap_if_in_range(map, start, end); |
| 490 | } |
| 491 | list_for_each_entry(map, &priv->freeable_maps, next) { |
| 492 | unmap_if_in_range(map, start, end); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 493 | } |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 494 | mutex_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static void mn_invl_page(struct mmu_notifier *mn, |
| 498 | struct mm_struct *mm, |
| 499 | unsigned long address) |
| 500 | { |
| 501 | mn_invl_range_start(mn, mm, address, address + PAGE_SIZE); |
| 502 | } |
| 503 | |
| 504 | static void mn_release(struct mmu_notifier *mn, |
| 505 | struct mm_struct *mm) |
| 506 | { |
| 507 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); |
| 508 | struct grant_map *map; |
| 509 | int err; |
| 510 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 511 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 512 | list_for_each_entry(map, &priv->maps, next) { |
| 513 | if (!map->vma) |
| 514 | continue; |
| 515 | pr_debug("map %d+%d (%lx %lx)\n", |
| 516 | map->index, map->count, |
| 517 | map->vma->vm_start, map->vma->vm_end); |
| 518 | err = unmap_grant_pages(map, /* offset */ 0, map->count); |
| 519 | WARN_ON(err); |
| 520 | } |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 521 | list_for_each_entry(map, &priv->freeable_maps, next) { |
| 522 | if (!map->vma) |
| 523 | continue; |
| 524 | pr_debug("map %d+%d (%lx %lx)\n", |
| 525 | map->index, map->count, |
| 526 | map->vma->vm_start, map->vma->vm_end); |
| 527 | err = unmap_grant_pages(map, /* offset */ 0, map->count); |
| 528 | WARN_ON(err); |
| 529 | } |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 530 | mutex_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Konrad Rzeszutek Wilk | b8b0f55 | 2012-08-21 14:49:34 -0400 | [diff] [blame] | 533 | static struct mmu_notifier_ops gntdev_mmu_ops = { |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 534 | .release = mn_release, |
| 535 | .invalidate_page = mn_invl_page, |
| 536 | .invalidate_range_start = mn_invl_range_start, |
| 537 | }; |
| 538 | |
| 539 | /* ------------------------------------------------------------------ */ |
| 540 | |
| 541 | static int gntdev_open(struct inode *inode, struct file *flip) |
| 542 | { |
| 543 | struct gntdev_priv *priv; |
| 544 | int ret = 0; |
| 545 | |
| 546 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 547 | if (!priv) |
| 548 | return -ENOMEM; |
| 549 | |
| 550 | INIT_LIST_HEAD(&priv->maps); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 551 | INIT_LIST_HEAD(&priv->freeable_maps); |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 552 | mutex_init(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 553 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 554 | if (use_ptemod) { |
| 555 | priv->mm = get_task_mm(current); |
| 556 | if (!priv->mm) { |
| 557 | kfree(priv); |
| 558 | return -ENOMEM; |
| 559 | } |
| 560 | priv->mn.ops = &gntdev_mmu_ops; |
| 561 | ret = mmu_notifier_register(&priv->mn, priv->mm); |
| 562 | mmput(priv->mm); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 563 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 564 | |
| 565 | if (ret) { |
| 566 | kfree(priv); |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | flip->private_data = priv; |
| 571 | pr_debug("priv %p\n", priv); |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int gntdev_release(struct inode *inode, struct file *flip) |
| 577 | { |
| 578 | struct gntdev_priv *priv = flip->private_data; |
| 579 | struct grant_map *map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 580 | |
| 581 | pr_debug("priv %p\n", priv); |
| 582 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 583 | while (!list_empty(&priv->maps)) { |
| 584 | map = list_entry(priv->maps.next, struct grant_map, next); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 585 | list_del(&map->next); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 586 | gntdev_put_map(NULL /* already removed */, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 587 | } |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 588 | WARN_ON(!list_empty(&priv->freeable_maps)); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 589 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 590 | if (use_ptemod) |
| 591 | mmu_notifier_unregister(&priv->mn, priv->mm); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 592 | kfree(priv); |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, |
| 597 | struct ioctl_gntdev_map_grant_ref __user *u) |
| 598 | { |
| 599 | struct ioctl_gntdev_map_grant_ref op; |
| 600 | struct grant_map *map; |
| 601 | int err; |
| 602 | |
| 603 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 604 | return -EFAULT; |
| 605 | pr_debug("priv %p, add %d\n", priv, op.count); |
| 606 | if (unlikely(op.count <= 0)) |
| 607 | return -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 608 | |
| 609 | err = -ENOMEM; |
| 610 | map = gntdev_alloc_map(priv, op.count); |
| 611 | if (!map) |
| 612 | return err; |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 613 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 614 | if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) { |
| 615 | pr_debug("can't map: over limit\n"); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 616 | gntdev_put_map(NULL, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 617 | return err; |
| 618 | } |
| 619 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 620 | if (copy_from_user(map->grants, &u->refs, |
| 621 | sizeof(map->grants[0]) * op.count) != 0) { |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 622 | gntdev_put_map(NULL, map); |
| 623 | return -EFAULT; |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 624 | } |
| 625 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 626 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 627 | gntdev_add_map(priv, map); |
| 628 | op.index = map->index << PAGE_SHIFT; |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 629 | mutex_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 630 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 631 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
| 632 | return -EFAULT; |
| 633 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv, |
| 638 | struct ioctl_gntdev_unmap_grant_ref __user *u) |
| 639 | { |
| 640 | struct ioctl_gntdev_unmap_grant_ref op; |
| 641 | struct grant_map *map; |
| 642 | int err = -ENOENT; |
| 643 | |
| 644 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 645 | return -EFAULT; |
| 646 | pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count); |
| 647 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 648 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 649 | 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] | 650 | if (map) { |
| 651 | list_del(&map->next); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 652 | if (populate_freeable_maps) |
| 653 | list_add_tail(&map->next, &priv->freeable_maps); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 654 | err = 0; |
| 655 | } |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 656 | mutex_unlock(&priv->lock); |
Daniel De Graaf | 1f1503b | 2011-10-11 15:16:06 -0400 | [diff] [blame] | 657 | if (map) |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 658 | gntdev_put_map(priv, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 659 | return err; |
| 660 | } |
| 661 | |
| 662 | static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv, |
| 663 | struct ioctl_gntdev_get_offset_for_vaddr __user *u) |
| 664 | { |
| 665 | struct ioctl_gntdev_get_offset_for_vaddr op; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 666 | struct vm_area_struct *vma; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 667 | struct grant_map *map; |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 668 | int rv = -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 669 | |
| 670 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 671 | return -EFAULT; |
| 672 | pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr); |
| 673 | |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 674 | down_read(¤t->mm->mmap_sem); |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 675 | vma = find_vma(current->mm, op.vaddr); |
| 676 | if (!vma || vma->vm_ops != &gntdev_vmops) |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 677 | goto out_unlock; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 678 | |
| 679 | map = vma->vm_private_data; |
| 680 | if (!map) |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 681 | goto out_unlock; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 682 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 683 | op.offset = map->index << PAGE_SHIFT; |
| 684 | op.count = map->count; |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 685 | rv = 0; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 686 | |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 687 | out_unlock: |
| 688 | up_read(¤t->mm->mmap_sem); |
| 689 | |
| 690 | if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 691 | return -EFAULT; |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 692 | return rv; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 695 | static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) |
| 696 | { |
| 697 | struct ioctl_gntdev_unmap_notify op; |
| 698 | struct grant_map *map; |
| 699 | int rc; |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 700 | int out_flags; |
| 701 | unsigned int out_event; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 702 | |
| 703 | if (copy_from_user(&op, u, sizeof(op))) |
| 704 | return -EFAULT; |
| 705 | |
| 706 | if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) |
| 707 | return -EINVAL; |
| 708 | |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 709 | /* We need to grab a reference to the event channel we are going to use |
| 710 | * to send the notify before releasing the reference we may already have |
| 711 | * (if someone has called this ioctl twice). This is required so that |
| 712 | * it is possible to change the clear_byte part of the notification |
| 713 | * without disturbing the event channel part, which may now be the last |
| 714 | * reference to that event channel. |
| 715 | */ |
| 716 | if (op.action & UNMAP_NOTIFY_SEND_EVENT) { |
| 717 | if (evtchn_get(op.event_channel_port)) |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | |
| 721 | out_flags = op.action; |
| 722 | out_event = op.event_channel_port; |
| 723 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 724 | mutex_lock(&priv->lock); |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 725 | |
| 726 | list_for_each_entry(map, &priv->maps, next) { |
| 727 | uint64_t begin = map->index << PAGE_SHIFT; |
| 728 | uint64_t end = (map->index + map->count) << PAGE_SHIFT; |
| 729 | if (op.index >= begin && op.index < end) |
| 730 | goto found; |
| 731 | } |
| 732 | rc = -ENOENT; |
| 733 | goto unlock_out; |
| 734 | |
| 735 | found: |
Daniel De Graaf | 9960be9 | 2011-02-09 18:15:50 -0500 | [diff] [blame] | 736 | if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) && |
| 737 | (map->flags & GNTMAP_readonly)) { |
| 738 | rc = -EINVAL; |
| 739 | goto unlock_out; |
| 740 | } |
| 741 | |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 742 | out_flags = map->notify.flags; |
| 743 | out_event = map->notify.event; |
| 744 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 745 | map->notify.flags = op.action; |
| 746 | map->notify.addr = op.index - (map->index << PAGE_SHIFT); |
| 747 | map->notify.event = op.event_channel_port; |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 748 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 749 | rc = 0; |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 750 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 751 | unlock_out: |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 752 | mutex_unlock(&priv->lock); |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 753 | |
| 754 | /* Drop the reference to the event channel we did not save in the map */ |
| 755 | if (out_flags & UNMAP_NOTIFY_SEND_EVENT) |
| 756 | evtchn_put(out_event); |
| 757 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 758 | return rc; |
| 759 | } |
| 760 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 761 | static long gntdev_ioctl(struct file *flip, |
| 762 | unsigned int cmd, unsigned long arg) |
| 763 | { |
| 764 | struct gntdev_priv *priv = flip->private_data; |
| 765 | void __user *ptr = (void __user *)arg; |
| 766 | |
| 767 | switch (cmd) { |
| 768 | case IOCTL_GNTDEV_MAP_GRANT_REF: |
| 769 | return gntdev_ioctl_map_grant_ref(priv, ptr); |
| 770 | |
| 771 | case IOCTL_GNTDEV_UNMAP_GRANT_REF: |
| 772 | return gntdev_ioctl_unmap_grant_ref(priv, ptr); |
| 773 | |
| 774 | case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR: |
| 775 | return gntdev_ioctl_get_offset_for_vaddr(priv, ptr); |
| 776 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 777 | case IOCTL_GNTDEV_SET_UNMAP_NOTIFY: |
| 778 | return gntdev_ioctl_notify(priv, ptr); |
| 779 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 780 | default: |
| 781 | pr_debug("priv %p, unknown cmd %x\n", priv, cmd); |
| 782 | return -ENOIOCTLCMD; |
| 783 | } |
| 784 | |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) |
| 789 | { |
| 790 | struct gntdev_priv *priv = flip->private_data; |
| 791 | int index = vma->vm_pgoff; |
| 792 | int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; |
| 793 | struct grant_map *map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 794 | int i, err = -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 795 | |
| 796 | if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED)) |
| 797 | return -EINVAL; |
| 798 | |
| 799 | pr_debug("map %d+%d at %lx (pgoff %lx)\n", |
| 800 | index, count, vma->vm_start, vma->vm_pgoff); |
| 801 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 802 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 803 | map = gntdev_find_map_index(priv, index, count); |
| 804 | if (!map) |
| 805 | goto unlock_out; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 806 | if (use_ptemod && map->vma) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 807 | goto unlock_out; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 808 | if (use_ptemod && priv->mm != vma->vm_mm) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 809 | pr_warn("Huh? Other mm?\n"); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 810 | goto unlock_out; |
| 811 | } |
| 812 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 813 | atomic_inc(&map->users); |
| 814 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 815 | vma->vm_ops = &gntdev_vmops; |
| 816 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 817 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 818 | |
| 819 | if (use_ptemod) |
Stefano Stabellini | e8e937b | 2012-04-03 18:05:47 +0100 | [diff] [blame] | 820 | vma->vm_flags |= VM_DONTCOPY; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 821 | |
| 822 | vma->vm_private_data = map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 823 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 824 | if (use_ptemod) |
| 825 | map->vma = vma; |
| 826 | |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 827 | if (map->flags) { |
| 828 | if ((vma->vm_flags & VM_WRITE) && |
| 829 | (map->flags & GNTMAP_readonly)) |
Dan Carpenter | a93e20a | 2011-03-19 08:45:43 +0300 | [diff] [blame] | 830 | goto out_unlock_put; |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 831 | } else { |
| 832 | map->flags = GNTMAP_host_map; |
| 833 | if (!(vma->vm_flags & VM_WRITE)) |
| 834 | map->flags |= GNTMAP_readonly; |
| 835 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 836 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 837 | mutex_unlock(&priv->lock); |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 838 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 839 | if (use_ptemod) { |
| 840 | err = apply_to_page_range(vma->vm_mm, vma->vm_start, |
| 841 | vma->vm_end - vma->vm_start, |
| 842 | find_grant_ptes, map); |
| 843 | if (err) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 844 | pr_warn("find_grant_ptes() failure.\n"); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 845 | goto out_put_map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 846 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | err = map_grant_pages(map); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 850 | if (err) |
| 851 | goto out_put_map; |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 852 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 853 | if (!use_ptemod) { |
| 854 | for (i = 0; i < count; i++) { |
| 855 | err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE, |
| 856 | map->pages[i]); |
| 857 | if (err) |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 858 | goto out_put_map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 859 | } |
David Vrabel | 923b291 | 2014-12-18 14:56:54 +0000 | [diff] [blame^] | 860 | } else { |
| 861 | #ifdef CONFIG_X86 |
| 862 | /* |
| 863 | * If the PTEs were not made special by the grant map |
| 864 | * hypercall, do so here. |
| 865 | * |
| 866 | * This is racy since the mapping is already visible |
| 867 | * to userspace but userspace should be well-behaved |
| 868 | * enough to not touch it until the mmap() call |
| 869 | * returns. |
| 870 | */ |
| 871 | if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) { |
| 872 | apply_to_page_range(vma->vm_mm, vma->vm_start, |
| 873 | vma->vm_end - vma->vm_start, |
| 874 | set_grant_ptes_as_special, NULL); |
| 875 | } |
| 876 | #endif |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 877 | } |
| 878 | |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 879 | return 0; |
| 880 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 881 | unlock_out: |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 882 | mutex_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 883 | return err; |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 884 | |
Dan Carpenter | a93e20a | 2011-03-19 08:45:43 +0300 | [diff] [blame] | 885 | out_unlock_put: |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 886 | mutex_unlock(&priv->lock); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 887 | out_put_map: |
Daniel De Graaf | 84e4075 | 2011-02-09 15:11:59 -0500 | [diff] [blame] | 888 | if (use_ptemod) |
| 889 | map->vma = NULL; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 890 | gntdev_put_map(priv, map); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 891 | return err; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | static const struct file_operations gntdev_fops = { |
| 895 | .owner = THIS_MODULE, |
| 896 | .open = gntdev_open, |
| 897 | .release = gntdev_release, |
| 898 | .mmap = gntdev_mmap, |
| 899 | .unlocked_ioctl = gntdev_ioctl |
| 900 | }; |
| 901 | |
| 902 | static struct miscdevice gntdev_miscdev = { |
| 903 | .minor = MISC_DYNAMIC_MINOR, |
| 904 | .name = "xen/gntdev", |
| 905 | .fops = &gntdev_fops, |
| 906 | }; |
| 907 | |
| 908 | /* ------------------------------------------------------------------ */ |
| 909 | |
| 910 | static int __init gntdev_init(void) |
| 911 | { |
| 912 | int err; |
| 913 | |
| 914 | if (!xen_domain()) |
| 915 | return -ENODEV; |
| 916 | |
Konrad Rzeszutek Wilk | 6926f6d | 2014-01-03 10:20:18 -0500 | [diff] [blame] | 917 | use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 918 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 919 | err = misc_register(&gntdev_miscdev); |
| 920 | if (err != 0) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 921 | pr_err("Could not register gntdev device\n"); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 922 | return err; |
| 923 | } |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | static void __exit gntdev_exit(void) |
| 928 | { |
| 929 | misc_deregister(&gntdev_miscdev); |
| 930 | } |
| 931 | |
| 932 | module_init(gntdev_init); |
| 933 | module_exit(gntdev_exit); |
| 934 | |
| 935 | /* ------------------------------------------------------------------ */ |