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