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> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 35 | #include <linux/sched/mm.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 36 | #include <linux/spinlock.h> |
| 37 | #include <linux/slab.h> |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 38 | #include <linux/highmem.h> |
Elena Reshetova | c5f7c5a | 2017-03-06 16:21:16 +0200 | [diff] [blame] | 39 | #include <linux/refcount.h> |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 40 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC |
| 41 | #include <linux/of_device.h> |
| 42 | #endif |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 43 | |
| 44 | #include <xen/xen.h> |
| 45 | #include <xen/grant_table.h> |
Daniel De Graaf | ca47cea | 2011-03-09 18:07:34 -0500 | [diff] [blame] | 46 | #include <xen/balloon.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 47 | #include <xen/gntdev.h> |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 48 | #include <xen/events.h> |
Julien Grall | a9fd60e | 2015-06-17 15:28:02 +0100 | [diff] [blame] | 49 | #include <xen/page.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 50 | #include <asm/xen/hypervisor.h> |
| 51 | #include <asm/xen/hypercall.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 52 | |
| 53 | MODULE_LICENSE("GPL"); |
| 54 | MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, " |
| 55 | "Gerd Hoffmann <kraxel@redhat.com>"); |
| 56 | MODULE_DESCRIPTION("User-space granted page access driver"); |
| 57 | |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 58 | static int limit = 1024*1024; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 59 | module_param(limit, int, 0644); |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 60 | MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by " |
| 61 | "the gntdev device"); |
| 62 | |
| 63 | static atomic_t pages_mapped = ATOMIC_INIT(0); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 64 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 65 | static int use_ptemod; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 66 | #define populate_freeable_maps use_ptemod |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 67 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 68 | struct gntdev_priv { |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 69 | /* maps with visible offsets in the file descriptor */ |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 70 | struct list_head maps; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 71 | /* maps that are not visible; will be freed on munmap. |
| 72 | * Only populated if populate_freeable_maps == 1 */ |
| 73 | struct list_head freeable_maps; |
| 74 | /* lock protects maps and freeable_maps */ |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 75 | struct mutex lock; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 76 | struct mm_struct *mm; |
| 77 | struct mmu_notifier mn; |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 78 | |
| 79 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC |
| 80 | /* Device for which DMA memory is allocated. */ |
| 81 | struct device *dma_dev; |
| 82 | #endif |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 85 | struct unmap_notify { |
| 86 | int flags; |
| 87 | /* Address relative to the start of the grant_map */ |
| 88 | int addr; |
| 89 | int event; |
| 90 | }; |
| 91 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 92 | struct grant_map { |
| 93 | struct list_head next; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 94 | struct vm_area_struct *vma; |
| 95 | int index; |
| 96 | int count; |
| 97 | int flags; |
Elena Reshetova | c5f7c5a | 2017-03-06 16:21:16 +0200 | [diff] [blame] | 98 | refcount_t users; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 99 | struct unmap_notify notify; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 100 | struct ioctl_gntdev_grant_ref *grants; |
| 101 | struct gnttab_map_grant_ref *map_ops; |
| 102 | struct gnttab_unmap_grant_ref *unmap_ops; |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 103 | struct gnttab_map_grant_ref *kmap_ops; |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 104 | struct gnttab_unmap_grant_ref *kunmap_ops; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 105 | struct page **pages; |
David Vrabel | dab069c | 2014-12-18 14:59:07 +0000 | [diff] [blame] | 106 | unsigned long pages_vm_start; |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 107 | |
| 108 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC |
| 109 | /* |
| 110 | * If dmabuf_vaddr is not NULL then this mapping is backed by DMA |
| 111 | * capable memory. |
| 112 | */ |
| 113 | |
| 114 | struct device *dma_dev; |
| 115 | /* Flags used to create this DMA buffer: GNTDEV_DMA_FLAG_XXX. */ |
| 116 | int dma_flags; |
| 117 | void *dma_vaddr; |
| 118 | dma_addr_t dma_bus_addr; |
| 119 | /* Needed to avoid allocation in gnttab_dma_free_pages(). */ |
| 120 | xen_pfn_t *frames; |
| 121 | #endif |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 124 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages); |
| 125 | |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 126 | static struct miscdevice gntdev_miscdev; |
| 127 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 128 | /* ------------------------------------------------------------------ */ |
| 129 | |
| 130 | static void gntdev_print_maps(struct gntdev_priv *priv, |
| 131 | char *text, int text_index) |
| 132 | { |
| 133 | #ifdef DEBUG |
| 134 | struct grant_map *map; |
| 135 | |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 136 | pr_debug("%s: maps list (priv %p)\n", __func__, priv); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 137 | list_for_each_entry(map, &priv->maps, next) |
| 138 | pr_debug(" index %2d, count %2d %s\n", |
| 139 | map->index, map->count, |
| 140 | map->index == text_index && text ? text : ""); |
| 141 | #endif |
| 142 | } |
| 143 | |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 144 | static void gntdev_free_map(struct grant_map *map) |
| 145 | { |
| 146 | if (map == NULL) |
| 147 | return; |
| 148 | |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 149 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC |
| 150 | if (map->dma_vaddr) { |
| 151 | struct gnttab_dma_alloc_args args; |
| 152 | |
| 153 | args.dev = map->dma_dev; |
| 154 | args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT); |
| 155 | args.nr_pages = map->count; |
| 156 | args.pages = map->pages; |
| 157 | args.frames = map->frames; |
| 158 | args.vaddr = map->dma_vaddr; |
| 159 | args.dev_bus_addr = map->dma_bus_addr; |
| 160 | |
| 161 | gnttab_dma_free_pages(&args); |
| 162 | } else |
| 163 | #endif |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 164 | if (map->pages) |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 165 | gnttab_free_pages(map->count, map->pages); |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 166 | |
| 167 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC |
| 168 | kfree(map->frames); |
| 169 | #endif |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 170 | kfree(map->pages); |
| 171 | kfree(map->grants); |
| 172 | kfree(map->map_ops); |
| 173 | kfree(map->unmap_ops); |
| 174 | kfree(map->kmap_ops); |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 175 | kfree(map->kunmap_ops); |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 176 | kfree(map); |
| 177 | } |
| 178 | |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 179 | static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count, |
| 180 | int dma_flags) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 181 | { |
| 182 | struct grant_map *add; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 183 | int i; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 184 | |
| 185 | add = kzalloc(sizeof(struct grant_map), GFP_KERNEL); |
| 186 | if (NULL == add) |
| 187 | return NULL; |
| 188 | |
Dan Carpenter | fc6e0c3 | 2011-11-04 21:23:32 +0300 | [diff] [blame] | 189 | add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL); |
| 190 | add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL); |
| 191 | add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL); |
| 192 | add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL); |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 193 | add->kunmap_ops = kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL); |
Dan Carpenter | fc6e0c3 | 2011-11-04 21:23:32 +0300 | [diff] [blame] | 194 | add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL); |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 195 | if (NULL == add->grants || |
| 196 | NULL == add->map_ops || |
| 197 | NULL == add->unmap_ops || |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 198 | NULL == add->kmap_ops || |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 199 | NULL == add->kunmap_ops || |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 200 | NULL == add->pages) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 201 | goto err; |
| 202 | |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 203 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC |
| 204 | add->dma_flags = dma_flags; |
| 205 | |
| 206 | /* |
| 207 | * Check if this mapping is requested to be backed |
| 208 | * by a DMA buffer. |
| 209 | */ |
| 210 | if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) { |
| 211 | struct gnttab_dma_alloc_args args; |
| 212 | |
| 213 | add->frames = kcalloc(count, sizeof(add->frames[0]), |
| 214 | GFP_KERNEL); |
| 215 | if (!add->frames) |
| 216 | goto err; |
| 217 | |
| 218 | /* Remember the device, so we can free DMA memory. */ |
| 219 | add->dma_dev = priv->dma_dev; |
| 220 | |
| 221 | args.dev = priv->dma_dev; |
| 222 | args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT); |
| 223 | args.nr_pages = count; |
| 224 | args.pages = add->pages; |
| 225 | args.frames = add->frames; |
| 226 | |
| 227 | if (gnttab_dma_alloc_pages(&args)) |
| 228 | goto err; |
| 229 | |
| 230 | add->dma_vaddr = args.vaddr; |
| 231 | add->dma_bus_addr = args.dev_bus_addr; |
| 232 | } else |
| 233 | #endif |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 234 | if (gnttab_alloc_pages(count, add->pages)) |
Daniel De Graaf | ca47cea | 2011-03-09 18:07:34 -0500 | [diff] [blame] | 235 | goto err; |
| 236 | |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 237 | for (i = 0; i < count; i++) { |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 238 | add->map_ops[i].handle = -1; |
| 239 | add->unmap_ops[i].handle = -1; |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 240 | add->kmap_ops[i].handle = -1; |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 241 | add->kunmap_ops[i].handle = -1; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 244 | add->index = 0; |
| 245 | add->count = count; |
Elena Reshetova | c5f7c5a | 2017-03-06 16:21:16 +0200 | [diff] [blame] | 246 | refcount_set(&add->users, 1); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 247 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 248 | return add; |
| 249 | |
| 250 | err: |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 251 | gntdev_free_map(add); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add) |
| 256 | { |
| 257 | struct grant_map *map; |
| 258 | |
| 259 | list_for_each_entry(map, &priv->maps, next) { |
| 260 | if (add->index + add->count < map->index) { |
| 261 | list_add_tail(&add->next, &map->next); |
| 262 | goto done; |
| 263 | } |
| 264 | add->index = map->index + map->count; |
| 265 | } |
| 266 | list_add_tail(&add->next, &priv->maps); |
| 267 | |
| 268 | done: |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 269 | gntdev_print_maps(priv, "[new]", add->index); |
| 270 | } |
| 271 | |
| 272 | static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, |
| 273 | int index, int count) |
| 274 | { |
| 275 | struct grant_map *map; |
| 276 | |
| 277 | list_for_each_entry(map, &priv->maps, next) { |
| 278 | if (map->index != index) |
| 279 | continue; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 280 | if (count && map->count != count) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 281 | continue; |
| 282 | return map; |
| 283 | } |
| 284 | return NULL; |
| 285 | } |
| 286 | |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 287 | 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] | 288 | { |
| 289 | if (!map) |
| 290 | return; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 291 | |
Elena Reshetova | c5f7c5a | 2017-03-06 16:21:16 +0200 | [diff] [blame] | 292 | if (!refcount_dec_and_test(&map->users)) |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 293 | return; |
| 294 | |
| 295 | atomic_sub(map->count, &pages_mapped); |
| 296 | |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 297 | if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 298 | notify_remote_via_evtchn(map->notify.event); |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 299 | evtchn_put(map->notify.event); |
| 300 | } |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 301 | |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 302 | if (populate_freeable_maps && priv) { |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 303 | mutex_lock(&priv->lock); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 304 | list_del(&map->next); |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 305 | mutex_unlock(&priv->lock); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 306 | } |
| 307 | |
David Vrabel | a67baeb7 | 2012-10-24 12:39:02 +0100 | [diff] [blame] | 308 | if (map->pages && !use_ptemod) |
| 309 | unmap_grant_pages(map, 0, map->count); |
| 310 | gntdev_free_map(map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* ------------------------------------------------------------------ */ |
| 314 | |
| 315 | static int find_grant_ptes(pte_t *pte, pgtable_t token, |
| 316 | unsigned long addr, void *data) |
| 317 | { |
| 318 | struct grant_map *map = data; |
| 319 | unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 320 | int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 321 | u64 pte_maddr; |
| 322 | |
| 323 | BUG_ON(pgnr >= map->count); |
Jeremy Fitzhardinge | ba5d101 | 2010-12-08 10:54:32 -0800 | [diff] [blame] | 324 | pte_maddr = arbitrary_virt_to_machine(pte).maddr; |
| 325 | |
David Vrabel | 923b291 | 2014-12-18 14:56:54 +0000 | [diff] [blame] | 326 | /* |
| 327 | * Set the PTE as special to force get_user_pages_fast() fall |
| 328 | * back to the slow path. If this is not supported as part of |
| 329 | * the grant map, it will be done afterwards. |
| 330 | */ |
| 331 | if (xen_feature(XENFEAT_gnttab_map_avail_bits)) |
| 332 | flags |= (1 << _GNTMAP_guest_avail0); |
| 333 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 334 | gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 335 | map->grants[pgnr].ref, |
| 336 | map->grants[pgnr].domid); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 337 | gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 338 | -1 /* handle */); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
David Vrabel | 923b291 | 2014-12-18 14:56:54 +0000 | [diff] [blame] | 342 | #ifdef CONFIG_X86 |
| 343 | static int set_grant_ptes_as_special(pte_t *pte, pgtable_t token, |
| 344 | unsigned long addr, void *data) |
| 345 | { |
| 346 | set_pte_at(current->mm, addr, pte, pte_mkspecial(*pte)); |
| 347 | return 0; |
| 348 | } |
| 349 | #endif |
| 350 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 351 | static int map_grant_pages(struct grant_map *map) |
| 352 | { |
| 353 | int i, err = 0; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 354 | |
| 355 | if (!use_ptemod) { |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 356 | /* Note: it could already be mapped */ |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 357 | if (map->map_ops[0].handle != -1) |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 358 | return 0; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 359 | for (i = 0; i < map->count; i++) { |
Ian Campbell | 38eaeb0 | 2011-03-08 16:56:43 +0000 | [diff] [blame] | 360 | unsigned long addr = (unsigned long) |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 361 | pfn_to_kaddr(page_to_pfn(map->pages[i])); |
| 362 | gnttab_set_map_op(&map->map_ops[i], addr, map->flags, |
| 363 | map->grants[i].ref, |
| 364 | map->grants[i].domid); |
| 365 | gnttab_set_unmap_op(&map->unmap_ops[i], addr, |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 366 | map->flags, -1 /* handle */); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 367 | } |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 368 | } else { |
| 369 | /* |
| 370 | * Setup the map_ops corresponding to the pte entries pointing |
| 371 | * to the kernel linear addresses of the struct pages. |
| 372 | * These ptes are completely different from the user ptes dealt |
| 373 | * with find_grant_ptes. |
| 374 | */ |
| 375 | for (i = 0; i < map->count; i++) { |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 376 | unsigned long address = (unsigned long) |
| 377 | pfn_to_kaddr(page_to_pfn(map->pages[i])); |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 378 | BUG_ON(PageHighMem(map->pages[i])); |
| 379 | |
Stefano Stabellini | ee07264 | 2013-07-23 17:23:54 +0000 | [diff] [blame] | 380 | gnttab_set_map_op(&map->kmap_ops[i], address, |
| 381 | map->flags | GNTMAP_host_map, |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 382 | map->grants[i].ref, |
| 383 | map->grants[i].domid); |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 384 | gnttab_set_unmap_op(&map->kunmap_ops[i], address, |
| 385 | map->flags | GNTMAP_host_map, -1); |
Stefano Stabellini | 0930bba | 2011-09-29 11:57:56 +0100 | [diff] [blame] | 386 | } |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 387 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 388 | |
| 389 | pr_debug("map %d+%d\n", map->index, map->count); |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 390 | err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL, |
| 391 | map->pages, map->count); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 392 | if (err) |
| 393 | return err; |
| 394 | |
| 395 | for (i = 0; i < map->count; i++) { |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 396 | if (map->map_ops[i].status) { |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 397 | err = -EINVAL; |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 398 | continue; |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 399 | } |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 400 | |
| 401 | map->unmap_ops[i].handle = map->map_ops[i].handle; |
| 402 | if (use_ptemod) |
| 403 | map->kunmap_ops[i].handle = map->kmap_ops[i].handle; |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 404 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC |
| 405 | else if (map->dma_vaddr) { |
| 406 | unsigned long bfn; |
| 407 | |
| 408 | bfn = pfn_to_bfn(page_to_pfn(map->pages[i])); |
| 409 | map->unmap_ops[i].dev_bus_addr = __pfn_to_phys(bfn); |
| 410 | } |
| 411 | #endif |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 412 | } |
| 413 | return err; |
| 414 | } |
| 415 | |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 416 | 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] | 417 | { |
| 418 | int i, err = 0; |
Jennifer Herbert | 7452822 | 2015-01-05 15:07:46 +0000 | [diff] [blame] | 419 | struct gntab_unmap_queue_data unmap_data; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 420 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 421 | if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { |
| 422 | int pgno = (map->notify.addr >> PAGE_SHIFT); |
Daniel De Graaf | 1affa98 | 2013-01-02 17:57:13 -0500 | [diff] [blame] | 423 | if (pgno >= offset && pgno < offset + pages) { |
| 424 | /* No need for kmap, pages are in lowmem */ |
| 425 | 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] | 426 | tmp[map->notify.addr & (PAGE_SIZE-1)] = 0; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 427 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; |
| 428 | } |
| 429 | } |
| 430 | |
Jennifer Herbert | 7452822 | 2015-01-05 15:07:46 +0000 | [diff] [blame] | 431 | unmap_data.unmap_ops = map->unmap_ops + offset; |
| 432 | unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL; |
| 433 | unmap_data.pages = map->pages + offset; |
| 434 | unmap_data.count = pages; |
| 435 | |
Bob Liu | b44166c | 2015-04-03 14:42:59 +0800 | [diff] [blame] | 436 | err = gnttab_unmap_refs_sync(&unmap_data); |
| 437 | if (err) |
| 438 | return err; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 439 | |
| 440 | for (i = 0; i < pages; i++) { |
| 441 | if (map->unmap_ops[offset+i].status) |
| 442 | err = -EINVAL; |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 443 | pr_debug("unmap handle=%d st=%d\n", |
| 444 | map->unmap_ops[offset+i].handle, |
| 445 | map->unmap_ops[offset+i].status); |
| 446 | map->unmap_ops[offset+i].handle = -1; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 447 | } |
| 448 | return err; |
| 449 | } |
| 450 | |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 451 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages) |
| 452 | { |
| 453 | int range, err = 0; |
| 454 | |
| 455 | pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages); |
| 456 | |
| 457 | /* It is possible the requested range will have a "hole" where we |
| 458 | * already unmapped some of the grants. Only unmap valid ranges. |
| 459 | */ |
| 460 | while (pages && !err) { |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame] | 461 | while (pages && map->unmap_ops[offset].handle == -1) { |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 462 | offset++; |
| 463 | pages--; |
| 464 | } |
| 465 | range = 0; |
| 466 | while (range < pages) { |
Ross Lagerwall | 951a010 | 2018-01-09 12:10:21 +0000 | [diff] [blame] | 467 | if (map->unmap_ops[offset+range].handle == -1) |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 468 | break; |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 469 | range++; |
| 470 | } |
| 471 | err = __unmap_grant_pages(map, offset, range); |
| 472 | offset += range; |
| 473 | pages -= range; |
| 474 | } |
| 475 | |
| 476 | return err; |
| 477 | } |
| 478 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 479 | /* ------------------------------------------------------------------ */ |
| 480 | |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 481 | static void gntdev_vma_open(struct vm_area_struct *vma) |
| 482 | { |
| 483 | struct grant_map *map = vma->vm_private_data; |
| 484 | |
| 485 | pr_debug("gntdev_vma_open %p\n", vma); |
Elena Reshetova | c5f7c5a | 2017-03-06 16:21:16 +0200 | [diff] [blame] | 486 | refcount_inc(&map->users); |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 487 | } |
| 488 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 489 | static void gntdev_vma_close(struct vm_area_struct *vma) |
| 490 | { |
| 491 | struct grant_map *map = vma->vm_private_data; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 492 | struct file *file = vma->vm_file; |
| 493 | struct gntdev_priv *priv = file->private_data; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 494 | |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 495 | pr_debug("gntdev_vma_close %p\n", vma); |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 496 | if (use_ptemod) { |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 497 | /* It is possible that an mmu notifier could be running |
| 498 | * concurrently, so take priv->lock to ensure that the vma won't |
| 499 | * vanishing during the unmap_grant_pages call, since we will |
| 500 | * spin here until that completes. Such a concurrent call will |
| 501 | * not do any unmapping, since that has been done prior to |
| 502 | * closing the vma, but it may still iterate the unmap_ops list. |
| 503 | */ |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 504 | mutex_lock(&priv->lock); |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 505 | map->vma = NULL; |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 506 | mutex_unlock(&priv->lock); |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 507 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 508 | vma->vm_private_data = NULL; |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 509 | gntdev_put_map(priv, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 510 | } |
| 511 | |
David Vrabel | dab069c | 2014-12-18 14:59:07 +0000 | [diff] [blame] | 512 | static struct page *gntdev_vma_find_special_page(struct vm_area_struct *vma, |
| 513 | unsigned long addr) |
| 514 | { |
| 515 | struct grant_map *map = vma->vm_private_data; |
| 516 | |
| 517 | return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT]; |
| 518 | } |
| 519 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 520 | static const struct vm_operations_struct gntdev_vmops = { |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 521 | .open = gntdev_vma_open, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 522 | .close = gntdev_vma_close, |
David Vrabel | dab069c | 2014-12-18 14:59:07 +0000 | [diff] [blame] | 523 | .find_special_page = gntdev_vma_find_special_page, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | /* ------------------------------------------------------------------ */ |
| 527 | |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 528 | static void unmap_if_in_range(struct grant_map *map, |
| 529 | unsigned long start, unsigned long end) |
| 530 | { |
| 531 | unsigned long mstart, mend; |
| 532 | int err; |
| 533 | |
| 534 | if (!map->vma) |
| 535 | return; |
| 536 | if (map->vma->vm_start >= end) |
| 537 | return; |
| 538 | if (map->vma->vm_end <= start) |
| 539 | return; |
| 540 | mstart = max(start, map->vma->vm_start); |
| 541 | mend = min(end, map->vma->vm_end); |
| 542 | pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n", |
| 543 | map->index, map->count, |
| 544 | map->vma->vm_start, map->vma->vm_end, |
| 545 | start, end, mstart, mend); |
| 546 | err = unmap_grant_pages(map, |
| 547 | (mstart - map->vma->vm_start) >> PAGE_SHIFT, |
| 548 | (mend - mstart) >> PAGE_SHIFT); |
| 549 | WARN_ON(err); |
| 550 | } |
| 551 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 552 | static void mn_invl_range_start(struct mmu_notifier *mn, |
| 553 | struct mm_struct *mm, |
| 554 | unsigned long start, unsigned long end) |
| 555 | { |
| 556 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); |
| 557 | struct grant_map *map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 558 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 559 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 560 | list_for_each_entry(map, &priv->maps, next) { |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 561 | unmap_if_in_range(map, start, end); |
| 562 | } |
| 563 | list_for_each_entry(map, &priv->freeable_maps, next) { |
| 564 | unmap_if_in_range(map, start, end); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 565 | } |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 566 | mutex_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 569 | static void mn_release(struct mmu_notifier *mn, |
| 570 | struct mm_struct *mm) |
| 571 | { |
| 572 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); |
| 573 | struct grant_map *map; |
| 574 | int err; |
| 575 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 576 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 577 | list_for_each_entry(map, &priv->maps, next) { |
| 578 | if (!map->vma) |
| 579 | continue; |
| 580 | pr_debug("map %d+%d (%lx %lx)\n", |
| 581 | map->index, map->count, |
| 582 | map->vma->vm_start, map->vma->vm_end); |
| 583 | err = unmap_grant_pages(map, /* offset */ 0, map->count); |
| 584 | WARN_ON(err); |
| 585 | } |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 586 | list_for_each_entry(map, &priv->freeable_maps, next) { |
| 587 | if (!map->vma) |
| 588 | continue; |
| 589 | pr_debug("map %d+%d (%lx %lx)\n", |
| 590 | map->index, map->count, |
| 591 | map->vma->vm_start, map->vma->vm_end); |
| 592 | err = unmap_grant_pages(map, /* offset */ 0, map->count); |
| 593 | WARN_ON(err); |
| 594 | } |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 595 | mutex_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Julia Lawall | b9c0a92 | 2015-11-29 23:02:49 +0100 | [diff] [blame] | 598 | static const struct mmu_notifier_ops gntdev_mmu_ops = { |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 599 | .release = mn_release, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 600 | .invalidate_range_start = mn_invl_range_start, |
| 601 | }; |
| 602 | |
| 603 | /* ------------------------------------------------------------------ */ |
| 604 | |
| 605 | static int gntdev_open(struct inode *inode, struct file *flip) |
| 606 | { |
| 607 | struct gntdev_priv *priv; |
| 608 | int ret = 0; |
| 609 | |
| 610 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 611 | if (!priv) |
| 612 | return -ENOMEM; |
| 613 | |
| 614 | INIT_LIST_HEAD(&priv->maps); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 615 | INIT_LIST_HEAD(&priv->freeable_maps); |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 616 | mutex_init(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 617 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 618 | if (use_ptemod) { |
| 619 | priv->mm = get_task_mm(current); |
| 620 | if (!priv->mm) { |
| 621 | kfree(priv); |
| 622 | return -ENOMEM; |
| 623 | } |
| 624 | priv->mn.ops = &gntdev_mmu_ops; |
| 625 | ret = mmu_notifier_register(&priv->mn, priv->mm); |
| 626 | mmput(priv->mm); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 627 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 628 | |
| 629 | if (ret) { |
| 630 | kfree(priv); |
| 631 | return ret; |
| 632 | } |
| 633 | |
| 634 | flip->private_data = priv; |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 635 | #ifdef CONFIG_XEN_GRANT_DMA_ALLOC |
| 636 | priv->dma_dev = gntdev_miscdev.this_device; |
| 637 | |
| 638 | /* |
| 639 | * The device is not spawn from a device tree, so arch_setup_dma_ops |
| 640 | * is not called, thus leaving the device with dummy DMA ops. |
| 641 | * Fix this by calling of_dma_configure() with a NULL node to set |
| 642 | * default DMA ops. |
| 643 | */ |
| 644 | of_dma_configure(priv->dma_dev, NULL, true); |
| 645 | #endif |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 646 | pr_debug("priv %p\n", priv); |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static int gntdev_release(struct inode *inode, struct file *flip) |
| 652 | { |
| 653 | struct gntdev_priv *priv = flip->private_data; |
| 654 | struct grant_map *map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 655 | |
| 656 | pr_debug("priv %p\n", priv); |
| 657 | |
Marek Marczykowski-Górecki | 30b03d0 | 2015-06-26 03:28:24 +0200 | [diff] [blame] | 658 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 659 | while (!list_empty(&priv->maps)) { |
| 660 | map = list_entry(priv->maps.next, struct grant_map, next); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 661 | list_del(&map->next); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 662 | gntdev_put_map(NULL /* already removed */, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 663 | } |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 664 | WARN_ON(!list_empty(&priv->freeable_maps)); |
Marek Marczykowski-Górecki | 30b03d0 | 2015-06-26 03:28:24 +0200 | [diff] [blame] | 665 | mutex_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 666 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 667 | if (use_ptemod) |
| 668 | mmu_notifier_unregister(&priv->mn, priv->mm); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 669 | kfree(priv); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, |
| 674 | struct ioctl_gntdev_map_grant_ref __user *u) |
| 675 | { |
| 676 | struct ioctl_gntdev_map_grant_ref op; |
| 677 | struct grant_map *map; |
| 678 | int err; |
| 679 | |
| 680 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 681 | return -EFAULT; |
| 682 | pr_debug("priv %p, add %d\n", priv, op.count); |
| 683 | if (unlikely(op.count <= 0)) |
| 684 | return -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 685 | |
| 686 | err = -ENOMEM; |
Oleksandr Andrushchenko | 975ef7f | 2018-07-20 12:01:46 +0300 | [diff] [blame^] | 687 | map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 688 | if (!map) |
| 689 | return err; |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 690 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 691 | if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) { |
| 692 | pr_debug("can't map: over limit\n"); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 693 | gntdev_put_map(NULL, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 694 | return err; |
| 695 | } |
| 696 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 697 | if (copy_from_user(map->grants, &u->refs, |
| 698 | sizeof(map->grants[0]) * op.count) != 0) { |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 699 | gntdev_put_map(NULL, map); |
| 700 | return -EFAULT; |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 701 | } |
| 702 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 703 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 704 | gntdev_add_map(priv, map); |
| 705 | op.index = map->index << PAGE_SHIFT; |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 706 | mutex_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 707 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 708 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
| 709 | return -EFAULT; |
| 710 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv, |
| 715 | struct ioctl_gntdev_unmap_grant_ref __user *u) |
| 716 | { |
| 717 | struct ioctl_gntdev_unmap_grant_ref op; |
| 718 | struct grant_map *map; |
| 719 | int err = -ENOENT; |
| 720 | |
| 721 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 722 | return -EFAULT; |
| 723 | pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count); |
| 724 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 725 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 726 | 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] | 727 | if (map) { |
| 728 | list_del(&map->next); |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 729 | if (populate_freeable_maps) |
| 730 | list_add_tail(&map->next, &priv->freeable_maps); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 731 | err = 0; |
| 732 | } |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 733 | mutex_unlock(&priv->lock); |
Daniel De Graaf | 1f1503b | 2011-10-11 15:16:06 -0400 | [diff] [blame] | 734 | if (map) |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 735 | gntdev_put_map(priv, map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 736 | return err; |
| 737 | } |
| 738 | |
| 739 | static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv, |
| 740 | struct ioctl_gntdev_get_offset_for_vaddr __user *u) |
| 741 | { |
| 742 | struct ioctl_gntdev_get_offset_for_vaddr op; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 743 | struct vm_area_struct *vma; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 744 | struct grant_map *map; |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 745 | int rv = -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 746 | |
| 747 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 748 | return -EFAULT; |
| 749 | pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr); |
| 750 | |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 751 | down_read(¤t->mm->mmap_sem); |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 752 | vma = find_vma(current->mm, op.vaddr); |
| 753 | if (!vma || vma->vm_ops != &gntdev_vmops) |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 754 | goto out_unlock; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 755 | |
| 756 | map = vma->vm_private_data; |
| 757 | if (!map) |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 758 | goto out_unlock; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 759 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 760 | op.offset = map->index << PAGE_SHIFT; |
| 761 | op.count = map->count; |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 762 | rv = 0; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 763 | |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 764 | out_unlock: |
| 765 | up_read(¤t->mm->mmap_sem); |
| 766 | |
| 767 | if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 768 | return -EFAULT; |
Daniel De Graaf | 2512f29 | 2013-01-02 22:57:11 +0000 | [diff] [blame] | 769 | return rv; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 772 | static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) |
| 773 | { |
| 774 | struct ioctl_gntdev_unmap_notify op; |
| 775 | struct grant_map *map; |
| 776 | int rc; |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 777 | int out_flags; |
| 778 | unsigned int out_event; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 779 | |
| 780 | if (copy_from_user(&op, u, sizeof(op))) |
| 781 | return -EFAULT; |
| 782 | |
| 783 | if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) |
| 784 | return -EINVAL; |
| 785 | |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 786 | /* We need to grab a reference to the event channel we are going to use |
| 787 | * to send the notify before releasing the reference we may already have |
| 788 | * (if someone has called this ioctl twice). This is required so that |
| 789 | * it is possible to change the clear_byte part of the notification |
| 790 | * without disturbing the event channel part, which may now be the last |
| 791 | * reference to that event channel. |
| 792 | */ |
| 793 | if (op.action & UNMAP_NOTIFY_SEND_EVENT) { |
| 794 | if (evtchn_get(op.event_channel_port)) |
| 795 | return -EINVAL; |
| 796 | } |
| 797 | |
| 798 | out_flags = op.action; |
| 799 | out_event = op.event_channel_port; |
| 800 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 801 | mutex_lock(&priv->lock); |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 802 | |
| 803 | list_for_each_entry(map, &priv->maps, next) { |
| 804 | uint64_t begin = map->index << PAGE_SHIFT; |
| 805 | uint64_t end = (map->index + map->count) << PAGE_SHIFT; |
| 806 | if (op.index >= begin && op.index < end) |
| 807 | goto found; |
| 808 | } |
| 809 | rc = -ENOENT; |
| 810 | goto unlock_out; |
| 811 | |
| 812 | found: |
Daniel De Graaf | 9960be9 | 2011-02-09 18:15:50 -0500 | [diff] [blame] | 813 | if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) && |
| 814 | (map->flags & GNTMAP_readonly)) { |
| 815 | rc = -EINVAL; |
| 816 | goto unlock_out; |
| 817 | } |
| 818 | |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 819 | out_flags = map->notify.flags; |
| 820 | out_event = map->notify.event; |
| 821 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 822 | map->notify.flags = op.action; |
| 823 | map->notify.addr = op.index - (map->index << PAGE_SHIFT); |
| 824 | map->notify.event = op.event_channel_port; |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 825 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 826 | rc = 0; |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 827 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 828 | unlock_out: |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 829 | mutex_unlock(&priv->lock); |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 830 | |
| 831 | /* Drop the reference to the event channel we did not save in the map */ |
| 832 | if (out_flags & UNMAP_NOTIFY_SEND_EVENT) |
| 833 | evtchn_put(out_event); |
| 834 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 835 | return rc; |
| 836 | } |
| 837 | |
David Vrabel | 36ae220 | 2016-05-09 10:59:48 +0100 | [diff] [blame] | 838 | #define GNTDEV_COPY_BATCH 16 |
David Vrabel | a4cdb55 | 2014-12-02 16:13:26 +0000 | [diff] [blame] | 839 | |
| 840 | struct gntdev_copy_batch { |
| 841 | struct gnttab_copy ops[GNTDEV_COPY_BATCH]; |
| 842 | struct page *pages[GNTDEV_COPY_BATCH]; |
| 843 | s16 __user *status[GNTDEV_COPY_BATCH]; |
| 844 | unsigned int nr_ops; |
| 845 | unsigned int nr_pages; |
| 846 | }; |
| 847 | |
| 848 | static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt, |
| 849 | bool writeable, unsigned long *gfn) |
| 850 | { |
| 851 | unsigned long addr = (unsigned long)virt; |
| 852 | struct page *page; |
| 853 | unsigned long xen_pfn; |
| 854 | int ret; |
| 855 | |
| 856 | ret = get_user_pages_fast(addr, 1, writeable, &page); |
| 857 | if (ret < 0) |
| 858 | return ret; |
| 859 | |
| 860 | batch->pages[batch->nr_pages++] = page; |
| 861 | |
| 862 | xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK); |
| 863 | *gfn = pfn_to_gfn(xen_pfn); |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static void gntdev_put_pages(struct gntdev_copy_batch *batch) |
| 869 | { |
| 870 | unsigned int i; |
| 871 | |
| 872 | for (i = 0; i < batch->nr_pages; i++) |
| 873 | put_page(batch->pages[i]); |
| 874 | batch->nr_pages = 0; |
| 875 | } |
| 876 | |
| 877 | static int gntdev_copy(struct gntdev_copy_batch *batch) |
| 878 | { |
| 879 | unsigned int i; |
| 880 | |
| 881 | gnttab_batch_copy(batch->ops, batch->nr_ops); |
| 882 | gntdev_put_pages(batch); |
| 883 | |
| 884 | /* |
| 885 | * For each completed op, update the status if the op failed |
| 886 | * and all previous ops for the segment were successful. |
| 887 | */ |
| 888 | for (i = 0; i < batch->nr_ops; i++) { |
| 889 | s16 status = batch->ops[i].status; |
| 890 | s16 old_status; |
| 891 | |
| 892 | if (status == GNTST_okay) |
| 893 | continue; |
| 894 | |
| 895 | if (__get_user(old_status, batch->status[i])) |
| 896 | return -EFAULT; |
| 897 | |
| 898 | if (old_status != GNTST_okay) |
| 899 | continue; |
| 900 | |
| 901 | if (__put_user(status, batch->status[i])) |
| 902 | return -EFAULT; |
| 903 | } |
| 904 | |
| 905 | batch->nr_ops = 0; |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch, |
| 910 | struct gntdev_grant_copy_segment *seg, |
| 911 | s16 __user *status) |
| 912 | { |
| 913 | uint16_t copied = 0; |
| 914 | |
| 915 | /* |
| 916 | * Disallow local -> local copies since there is only space in |
| 917 | * batch->pages for one page per-op and this would be a very |
| 918 | * expensive memcpy(). |
| 919 | */ |
| 920 | if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref))) |
| 921 | return -EINVAL; |
| 922 | |
| 923 | /* Can't cross page if source/dest is a grant ref. */ |
| 924 | if (seg->flags & GNTCOPY_source_gref) { |
| 925 | if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE) |
| 926 | return -EINVAL; |
| 927 | } |
| 928 | if (seg->flags & GNTCOPY_dest_gref) { |
| 929 | if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE) |
| 930 | return -EINVAL; |
| 931 | } |
| 932 | |
| 933 | if (put_user(GNTST_okay, status)) |
| 934 | return -EFAULT; |
| 935 | |
| 936 | while (copied < seg->len) { |
| 937 | struct gnttab_copy *op; |
| 938 | void __user *virt; |
| 939 | size_t len, off; |
| 940 | unsigned long gfn; |
| 941 | int ret; |
| 942 | |
| 943 | if (batch->nr_ops >= GNTDEV_COPY_BATCH) { |
| 944 | ret = gntdev_copy(batch); |
| 945 | if (ret < 0) |
| 946 | return ret; |
| 947 | } |
| 948 | |
| 949 | len = seg->len - copied; |
| 950 | |
| 951 | op = &batch->ops[batch->nr_ops]; |
| 952 | op->flags = 0; |
| 953 | |
| 954 | if (seg->flags & GNTCOPY_source_gref) { |
| 955 | op->source.u.ref = seg->source.foreign.ref; |
| 956 | op->source.domid = seg->source.foreign.domid; |
| 957 | op->source.offset = seg->source.foreign.offset + copied; |
| 958 | op->flags |= GNTCOPY_source_gref; |
| 959 | } else { |
| 960 | virt = seg->source.virt + copied; |
| 961 | off = (unsigned long)virt & ~XEN_PAGE_MASK; |
| 962 | len = min(len, (size_t)XEN_PAGE_SIZE - off); |
| 963 | |
| 964 | ret = gntdev_get_page(batch, virt, false, &gfn); |
| 965 | if (ret < 0) |
| 966 | return ret; |
| 967 | |
| 968 | op->source.u.gmfn = gfn; |
| 969 | op->source.domid = DOMID_SELF; |
| 970 | op->source.offset = off; |
| 971 | } |
| 972 | |
| 973 | if (seg->flags & GNTCOPY_dest_gref) { |
| 974 | op->dest.u.ref = seg->dest.foreign.ref; |
| 975 | op->dest.domid = seg->dest.foreign.domid; |
| 976 | op->dest.offset = seg->dest.foreign.offset + copied; |
| 977 | op->flags |= GNTCOPY_dest_gref; |
| 978 | } else { |
| 979 | virt = seg->dest.virt + copied; |
| 980 | off = (unsigned long)virt & ~XEN_PAGE_MASK; |
| 981 | len = min(len, (size_t)XEN_PAGE_SIZE - off); |
| 982 | |
| 983 | ret = gntdev_get_page(batch, virt, true, &gfn); |
| 984 | if (ret < 0) |
| 985 | return ret; |
| 986 | |
| 987 | op->dest.u.gmfn = gfn; |
| 988 | op->dest.domid = DOMID_SELF; |
| 989 | op->dest.offset = off; |
| 990 | } |
| 991 | |
| 992 | op->len = len; |
| 993 | copied += len; |
| 994 | |
| 995 | batch->status[batch->nr_ops] = status; |
| 996 | batch->nr_ops++; |
| 997 | } |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u) |
| 1003 | { |
| 1004 | struct ioctl_gntdev_grant_copy copy; |
| 1005 | struct gntdev_copy_batch batch; |
| 1006 | unsigned int i; |
| 1007 | int ret = 0; |
| 1008 | |
| 1009 | if (copy_from_user(©, u, sizeof(copy))) |
| 1010 | return -EFAULT; |
| 1011 | |
| 1012 | batch.nr_ops = 0; |
| 1013 | batch.nr_pages = 0; |
| 1014 | |
| 1015 | for (i = 0; i < copy.count; i++) { |
| 1016 | struct gntdev_grant_copy_segment seg; |
| 1017 | |
| 1018 | if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) { |
| 1019 | ret = -EFAULT; |
| 1020 | goto out; |
| 1021 | } |
| 1022 | |
| 1023 | ret = gntdev_grant_copy_seg(&batch, &seg, ©.segments[i].status); |
| 1024 | if (ret < 0) |
| 1025 | goto out; |
| 1026 | |
| 1027 | cond_resched(); |
| 1028 | } |
| 1029 | if (batch.nr_ops) |
| 1030 | ret = gntdev_copy(&batch); |
| 1031 | return ret; |
| 1032 | |
| 1033 | out: |
| 1034 | gntdev_put_pages(&batch); |
| 1035 | return ret; |
| 1036 | } |
| 1037 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1038 | static long gntdev_ioctl(struct file *flip, |
| 1039 | unsigned int cmd, unsigned long arg) |
| 1040 | { |
| 1041 | struct gntdev_priv *priv = flip->private_data; |
| 1042 | void __user *ptr = (void __user *)arg; |
| 1043 | |
| 1044 | switch (cmd) { |
| 1045 | case IOCTL_GNTDEV_MAP_GRANT_REF: |
| 1046 | return gntdev_ioctl_map_grant_ref(priv, ptr); |
| 1047 | |
| 1048 | case IOCTL_GNTDEV_UNMAP_GRANT_REF: |
| 1049 | return gntdev_ioctl_unmap_grant_ref(priv, ptr); |
| 1050 | |
| 1051 | case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR: |
| 1052 | return gntdev_ioctl_get_offset_for_vaddr(priv, ptr); |
| 1053 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 1054 | case IOCTL_GNTDEV_SET_UNMAP_NOTIFY: |
| 1055 | return gntdev_ioctl_notify(priv, ptr); |
| 1056 | |
David Vrabel | a4cdb55 | 2014-12-02 16:13:26 +0000 | [diff] [blame] | 1057 | case IOCTL_GNTDEV_GRANT_COPY: |
| 1058 | return gntdev_ioctl_grant_copy(priv, ptr); |
| 1059 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1060 | default: |
| 1061 | pr_debug("priv %p, unknown cmd %x\n", priv, cmd); |
| 1062 | return -ENOIOCTLCMD; |
| 1063 | } |
| 1064 | |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) |
| 1069 | { |
| 1070 | struct gntdev_priv *priv = flip->private_data; |
| 1071 | int index = vma->vm_pgoff; |
Muhammad Falak R Wani | c7ebf9d | 2016-05-24 05:34:32 +0530 | [diff] [blame] | 1072 | int count = vma_pages(vma); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1073 | struct grant_map *map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1074 | int i, err = -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1075 | |
| 1076 | if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED)) |
| 1077 | return -EINVAL; |
| 1078 | |
| 1079 | pr_debug("map %d+%d at %lx (pgoff %lx)\n", |
| 1080 | index, count, vma->vm_start, vma->vm_pgoff); |
| 1081 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 1082 | mutex_lock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1083 | map = gntdev_find_map_index(priv, index, count); |
| 1084 | if (!map) |
| 1085 | goto unlock_out; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1086 | if (use_ptemod && map->vma) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1087 | goto unlock_out; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1088 | if (use_ptemod && priv->mm != vma->vm_mm) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 1089 | pr_warn("Huh? Other mm?\n"); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1090 | goto unlock_out; |
| 1091 | } |
| 1092 | |
Elena Reshetova | c5f7c5a | 2017-03-06 16:21:16 +0200 | [diff] [blame] | 1093 | refcount_inc(&map->users); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 1094 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1095 | vma->vm_ops = &gntdev_vmops; |
| 1096 | |
Boris Ostrovsky | 30faaaf | 2016-11-21 09:56:06 -0500 | [diff] [blame] | 1097 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP; |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 1098 | |
| 1099 | if (use_ptemod) |
Stefano Stabellini | e8e937b | 2012-04-03 18:05:47 +0100 | [diff] [blame] | 1100 | vma->vm_flags |= VM_DONTCOPY; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1101 | |
| 1102 | vma->vm_private_data = map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1103 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1104 | if (use_ptemod) |
| 1105 | map->vma = vma; |
| 1106 | |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 1107 | if (map->flags) { |
| 1108 | if ((vma->vm_flags & VM_WRITE) && |
| 1109 | (map->flags & GNTMAP_readonly)) |
Dan Carpenter | a93e20a | 2011-03-19 08:45:43 +0300 | [diff] [blame] | 1110 | goto out_unlock_put; |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 1111 | } else { |
| 1112 | map->flags = GNTMAP_host_map; |
| 1113 | if (!(vma->vm_flags & VM_WRITE)) |
| 1114 | map->flags |= GNTMAP_readonly; |
| 1115 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1116 | |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 1117 | mutex_unlock(&priv->lock); |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 1118 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1119 | if (use_ptemod) { |
Juergen Gross | 298d275 | 2017-10-25 17:08:07 +0200 | [diff] [blame] | 1120 | map->pages_vm_start = vma->vm_start; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1121 | err = apply_to_page_range(vma->vm_mm, vma->vm_start, |
| 1122 | vma->vm_end - vma->vm_start, |
| 1123 | find_grant_ptes, map); |
| 1124 | if (err) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 1125 | pr_warn("find_grant_ptes() failure.\n"); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 1126 | goto out_put_map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1127 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | err = map_grant_pages(map); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 1131 | if (err) |
| 1132 | goto out_put_map; |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 1133 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1134 | if (!use_ptemod) { |
| 1135 | for (i = 0; i < count; i++) { |
| 1136 | err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE, |
| 1137 | map->pages[i]); |
| 1138 | if (err) |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 1139 | goto out_put_map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1140 | } |
David Vrabel | 923b291 | 2014-12-18 14:56:54 +0000 | [diff] [blame] | 1141 | } else { |
| 1142 | #ifdef CONFIG_X86 |
| 1143 | /* |
| 1144 | * If the PTEs were not made special by the grant map |
| 1145 | * hypercall, do so here. |
| 1146 | * |
| 1147 | * This is racy since the mapping is already visible |
| 1148 | * to userspace but userspace should be well-behaved |
| 1149 | * enough to not touch it until the mmap() call |
| 1150 | * returns. |
| 1151 | */ |
| 1152 | if (!xen_feature(XENFEAT_gnttab_map_avail_bits)) { |
| 1153 | apply_to_page_range(vma->vm_mm, vma->vm_start, |
| 1154 | vma->vm_end - vma->vm_start, |
| 1155 | set_grant_ptes_as_special, NULL); |
| 1156 | } |
| 1157 | #endif |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1158 | } |
| 1159 | |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 1160 | return 0; |
| 1161 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1162 | unlock_out: |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 1163 | mutex_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1164 | return err; |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 1165 | |
Dan Carpenter | a93e20a | 2011-03-19 08:45:43 +0300 | [diff] [blame] | 1166 | out_unlock_put: |
David Vrabel | 1401c00 | 2015-01-09 18:06:12 +0000 | [diff] [blame] | 1167 | mutex_unlock(&priv->lock); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 1168 | out_put_map: |
Ross Lagerwall | cf2acf6 | 2018-01-09 12:10:22 +0000 | [diff] [blame] | 1169 | if (use_ptemod) { |
Daniel De Graaf | 84e4075 | 2011-02-09 15:11:59 -0500 | [diff] [blame] | 1170 | map->vma = NULL; |
Ross Lagerwall | cf2acf6 | 2018-01-09 12:10:22 +0000 | [diff] [blame] | 1171 | unmap_grant_pages(map, 0, map->count); |
| 1172 | } |
Daniel De Graaf | 16a1d02 | 2013-01-02 22:57:12 +0000 | [diff] [blame] | 1173 | gntdev_put_map(priv, map); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 1174 | return err; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | static const struct file_operations gntdev_fops = { |
| 1178 | .owner = THIS_MODULE, |
| 1179 | .open = gntdev_open, |
| 1180 | .release = gntdev_release, |
| 1181 | .mmap = gntdev_mmap, |
| 1182 | .unlocked_ioctl = gntdev_ioctl |
| 1183 | }; |
| 1184 | |
| 1185 | static struct miscdevice gntdev_miscdev = { |
| 1186 | .minor = MISC_DYNAMIC_MINOR, |
| 1187 | .name = "xen/gntdev", |
| 1188 | .fops = &gntdev_fops, |
| 1189 | }; |
| 1190 | |
| 1191 | /* ------------------------------------------------------------------ */ |
| 1192 | |
| 1193 | static int __init gntdev_init(void) |
| 1194 | { |
| 1195 | int err; |
| 1196 | |
| 1197 | if (!xen_domain()) |
| 1198 | return -ENODEV; |
| 1199 | |
Konrad Rzeszutek Wilk | 6926f6d | 2014-01-03 10:20:18 -0500 | [diff] [blame] | 1200 | use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 1201 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1202 | err = misc_register(&gntdev_miscdev); |
| 1203 | if (err != 0) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 1204 | pr_err("Could not register gntdev device\n"); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 1205 | return err; |
| 1206 | } |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static void __exit gntdev_exit(void) |
| 1211 | { |
| 1212 | misc_deregister(&gntdev_miscdev); |
| 1213 | } |
| 1214 | |
| 1215 | module_init(gntdev_init); |
| 1216 | module_exit(gntdev_exit); |
| 1217 | |
| 1218 | /* ------------------------------------------------------------------ */ |