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