Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * gntalloc.c |
| 3 | * |
| 4 | * Device for creating grant references (in user-space) that may be shared |
| 5 | * with other domains. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * This driver exists to allow userspace programs in Linux to allocate kernel |
| 19 | * memory that will later be shared with another domain. Without this device, |
| 20 | * Linux userspace programs cannot create grant references. |
| 21 | * |
| 22 | * How this stuff works: |
| 23 | * X -> granting a page to Y |
| 24 | * Y -> mapping the grant from X |
| 25 | * |
| 26 | * 1. X uses the gntalloc device to allocate a page of kernel memory, P. |
| 27 | * 2. X creates an entry in the grant table that says domid(Y) can access P. |
| 28 | * This is done without a hypercall unless the grant table needs expansion. |
| 29 | * 3. X gives the grant reference identifier, GREF, to Y. |
| 30 | * 4. Y maps the page, either directly into kernel memory for use in a backend |
| 31 | * driver, or via a the gntdev device to map into the address space of an |
| 32 | * application running in Y. This is the first point at which Xen does any |
| 33 | * tracking of the page. |
| 34 | * 5. A program in X mmap()s a segment of the gntalloc device that corresponds |
| 35 | * to the shared page, and can now communicate with Y over the shared page. |
| 36 | * |
| 37 | * |
| 38 | * NOTE TO USERSPACE LIBRARIES: |
| 39 | * The grant allocation and mmap()ing are, naturally, two separate operations. |
| 40 | * You set up the sharing by calling the create ioctl() and then the mmap(). |
| 41 | * Teardown requires munmap() and either close() or ioctl(). |
| 42 | * |
| 43 | * WARNING: Since Xen does not allow a guest to forcibly end the use of a grant |
| 44 | * reference, this device can be used to consume kernel memory by leaving grant |
| 45 | * references mapped by another domain when an application exits. Therefore, |
| 46 | * there is a global limit on the number of pages that can be allocated. When |
| 47 | * all references to the page are unmapped, it will be freed during the next |
| 48 | * grant operation. |
| 49 | */ |
| 50 | |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 51 | #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt |
| 52 | |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 53 | #include <linux/atomic.h> |
| 54 | #include <linux/module.h> |
| 55 | #include <linux/miscdevice.h> |
| 56 | #include <linux/kernel.h> |
| 57 | #include <linux/init.h> |
| 58 | #include <linux/slab.h> |
| 59 | #include <linux/fs.h> |
| 60 | #include <linux/device.h> |
| 61 | #include <linux/mm.h> |
| 62 | #include <linux/uaccess.h> |
| 63 | #include <linux/types.h> |
| 64 | #include <linux/list.h> |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 65 | #include <linux/highmem.h> |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 66 | |
| 67 | #include <xen/xen.h> |
| 68 | #include <xen/page.h> |
| 69 | #include <xen/grant_table.h> |
| 70 | #include <xen/gntalloc.h> |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 71 | #include <xen/events.h> |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 72 | |
| 73 | static int limit = 1024; |
| 74 | module_param(limit, int, 0644); |
| 75 | MODULE_PARM_DESC(limit, "Maximum number of grants that may be allocated by " |
| 76 | "the gntalloc device"); |
| 77 | |
| 78 | static LIST_HEAD(gref_list); |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 79 | static DEFINE_MUTEX(gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 80 | static int gref_size; |
| 81 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 82 | struct notify_info { |
| 83 | uint16_t pgoff:12; /* Bits 0-11: Offset of the byte to clear */ |
| 84 | uint16_t flags:2; /* Bits 12-13: Unmap notification flags */ |
| 85 | int event; /* Port (event channel) to notify */ |
| 86 | }; |
| 87 | |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 88 | /* Metadata on a grant reference. */ |
| 89 | struct gntalloc_gref { |
| 90 | struct list_head next_gref; /* list entry gref_list */ |
| 91 | struct list_head next_file; /* list entry file->list, if open */ |
| 92 | struct page *page; /* The shared page */ |
| 93 | uint64_t file_index; /* File offset for mmap() */ |
| 94 | unsigned int users; /* Use count - when zero, waiting on Xen */ |
| 95 | grant_ref_t gref_id; /* The grant reference number */ |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 96 | struct notify_info notify; /* Unmap notification */ |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | struct gntalloc_file_private_data { |
| 100 | struct list_head list; |
| 101 | uint64_t index; |
| 102 | }; |
| 103 | |
Daniel De Graaf | 243082e | 2011-11-28 11:49:11 -0500 | [diff] [blame] | 104 | struct gntalloc_vma_private_data { |
| 105 | struct gntalloc_gref *gref; |
| 106 | int users; |
| 107 | int count; |
| 108 | }; |
| 109 | |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 110 | static void __del_gref(struct gntalloc_gref *gref); |
| 111 | |
| 112 | static void do_cleanup(void) |
| 113 | { |
| 114 | struct gntalloc_gref *gref, *n; |
| 115 | list_for_each_entry_safe(gref, n, &gref_list, next_gref) { |
| 116 | if (!gref->users) |
| 117 | __del_gref(gref); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static int add_grefs(struct ioctl_gntalloc_alloc_gref *op, |
| 122 | uint32_t *gref_ids, struct gntalloc_file_private_data *priv) |
| 123 | { |
| 124 | int i, rc, readonly; |
| 125 | LIST_HEAD(queue_gref); |
| 126 | LIST_HEAD(queue_file); |
David Vrabel | 5903c6b | 2014-09-02 15:21:30 +0100 | [diff] [blame] | 127 | struct gntalloc_gref *gref, *next; |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 128 | |
| 129 | readonly = !(op->flags & GNTALLOC_FLAG_WRITABLE); |
| 130 | rc = -ENOMEM; |
| 131 | for (i = 0; i < op->count; i++) { |
| 132 | gref = kzalloc(sizeof(*gref), GFP_KERNEL); |
| 133 | if (!gref) |
| 134 | goto undo; |
| 135 | list_add_tail(&gref->next_gref, &queue_gref); |
| 136 | list_add_tail(&gref->next_file, &queue_file); |
| 137 | gref->users = 1; |
| 138 | gref->file_index = op->index + i * PAGE_SIZE; |
| 139 | gref->page = alloc_page(GFP_KERNEL|__GFP_ZERO); |
| 140 | if (!gref->page) |
| 141 | goto undo; |
| 142 | |
| 143 | /* Grant foreign access to the page. */ |
David Vrabel | e9de2e5 | 2014-09-02 15:21:29 +0100 | [diff] [blame] | 144 | rc = gnttab_grant_foreign_access(op->domid, |
Julien Grall | 0df4f26 | 2015-08-07 17:34:37 +0100 | [diff] [blame] | 145 | xen_page_to_gfn(gref->page), |
| 146 | readonly); |
David Vrabel | e9de2e5 | 2014-09-02 15:21:29 +0100 | [diff] [blame] | 147 | if (rc < 0) |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 148 | goto undo; |
David Vrabel | e9de2e5 | 2014-09-02 15:21:29 +0100 | [diff] [blame] | 149 | gref_ids[i] = gref->gref_id = rc; |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* Add to gref lists. */ |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 153 | mutex_lock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 154 | list_splice_tail(&queue_gref, &gref_list); |
| 155 | list_splice_tail(&queue_file, &priv->list); |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 156 | mutex_unlock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 157 | |
| 158 | return 0; |
| 159 | |
| 160 | undo: |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 161 | mutex_lock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 162 | gref_size -= (op->count - i); |
| 163 | |
David Vrabel | 5903c6b | 2014-09-02 15:21:30 +0100 | [diff] [blame] | 164 | list_for_each_entry_safe(gref, next, &queue_file, next_file) { |
| 165 | list_del(&gref->next_file); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 166 | __del_gref(gref); |
| 167 | } |
| 168 | |
| 169 | /* It's possible for the target domain to map the just-allocated grant |
| 170 | * references by blindly guessing their IDs; if this is done, then |
| 171 | * __del_gref will leave them in the queue_gref list. They need to be |
| 172 | * added to the global list so that we can free them when they are no |
| 173 | * longer referenced. |
| 174 | */ |
| 175 | if (unlikely(!list_empty(&queue_gref))) |
| 176 | list_splice_tail(&queue_gref, &gref_list); |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 177 | mutex_unlock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 178 | return rc; |
| 179 | } |
| 180 | |
| 181 | static void __del_gref(struct gntalloc_gref *gref) |
| 182 | { |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 183 | if (gref->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { |
| 184 | uint8_t *tmp = kmap(gref->page); |
| 185 | tmp[gref->notify.pgoff] = 0; |
| 186 | kunmap(gref->page); |
| 187 | } |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 188 | if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 189 | notify_remote_via_evtchn(gref->notify.event); |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 190 | evtchn_put(gref->notify.event); |
| 191 | } |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 192 | |
| 193 | gref->notify.flags = 0; |
| 194 | |
David Vrabel | e9de2e5 | 2014-09-02 15:21:29 +0100 | [diff] [blame] | 195 | if (gref->gref_id) { |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 196 | if (gnttab_query_foreign_access(gref->gref_id)) |
| 197 | return; |
| 198 | |
| 199 | if (!gnttab_end_foreign_access_ref(gref->gref_id, 0)) |
| 200 | return; |
Daniel De Graaf | 0105d2b | 2011-11-28 11:49:10 -0500 | [diff] [blame] | 201 | |
| 202 | gnttab_free_grant_reference(gref->gref_id); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | gref_size--; |
| 206 | list_del(&gref->next_gref); |
| 207 | |
| 208 | if (gref->page) |
| 209 | __free_page(gref->page); |
| 210 | |
| 211 | kfree(gref); |
| 212 | } |
| 213 | |
| 214 | /* finds contiguous grant references in a file, returns the first */ |
| 215 | static struct gntalloc_gref *find_grefs(struct gntalloc_file_private_data *priv, |
| 216 | uint64_t index, uint32_t count) |
| 217 | { |
| 218 | struct gntalloc_gref *rv = NULL, *gref; |
| 219 | list_for_each_entry(gref, &priv->list, next_file) { |
| 220 | if (gref->file_index == index && !rv) |
| 221 | rv = gref; |
| 222 | if (rv) { |
| 223 | if (gref->file_index != index) |
| 224 | return NULL; |
| 225 | index += PAGE_SIZE; |
| 226 | count--; |
| 227 | if (count == 0) |
| 228 | return rv; |
| 229 | } |
| 230 | } |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * ------------------------------------- |
| 236 | * File operations. |
| 237 | * ------------------------------------- |
| 238 | */ |
| 239 | static int gntalloc_open(struct inode *inode, struct file *filp) |
| 240 | { |
| 241 | struct gntalloc_file_private_data *priv; |
| 242 | |
| 243 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 244 | if (!priv) |
| 245 | goto out_nomem; |
| 246 | INIT_LIST_HEAD(&priv->list); |
| 247 | |
| 248 | filp->private_data = priv; |
| 249 | |
| 250 | pr_debug("%s: priv %p\n", __func__, priv); |
| 251 | |
| 252 | return 0; |
| 253 | |
| 254 | out_nomem: |
| 255 | return -ENOMEM; |
| 256 | } |
| 257 | |
| 258 | static int gntalloc_release(struct inode *inode, struct file *filp) |
| 259 | { |
| 260 | struct gntalloc_file_private_data *priv = filp->private_data; |
| 261 | struct gntalloc_gref *gref; |
| 262 | |
| 263 | pr_debug("%s: priv %p\n", __func__, priv); |
| 264 | |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 265 | mutex_lock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 266 | while (!list_empty(&priv->list)) { |
| 267 | gref = list_entry(priv->list.next, |
| 268 | struct gntalloc_gref, next_file); |
| 269 | list_del(&gref->next_file); |
| 270 | gref->users--; |
| 271 | if (gref->users == 0) |
| 272 | __del_gref(gref); |
| 273 | } |
| 274 | kfree(priv); |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 275 | mutex_unlock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static long gntalloc_ioctl_alloc(struct gntalloc_file_private_data *priv, |
| 281 | struct ioctl_gntalloc_alloc_gref __user *arg) |
| 282 | { |
| 283 | int rc = 0; |
| 284 | struct ioctl_gntalloc_alloc_gref op; |
| 285 | uint32_t *gref_ids; |
| 286 | |
| 287 | pr_debug("%s: priv %p\n", __func__, priv); |
| 288 | |
| 289 | if (copy_from_user(&op, arg, sizeof(op))) { |
| 290 | rc = -EFAULT; |
| 291 | goto out; |
| 292 | } |
| 293 | |
Dan Carpenter | 21643e6 | 2011-11-04 21:24:08 +0300 | [diff] [blame] | 294 | gref_ids = kcalloc(op.count, sizeof(gref_ids[0]), GFP_TEMPORARY); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 295 | if (!gref_ids) { |
| 296 | rc = -ENOMEM; |
| 297 | goto out; |
| 298 | } |
| 299 | |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 300 | mutex_lock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 301 | /* Clean up pages that were at zero (local) users but were still mapped |
| 302 | * by remote domains. Since those pages count towards the limit that we |
| 303 | * are about to enforce, removing them here is a good idea. |
| 304 | */ |
| 305 | do_cleanup(); |
| 306 | if (gref_size + op.count > limit) { |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 307 | mutex_unlock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 308 | rc = -ENOSPC; |
| 309 | goto out_free; |
| 310 | } |
| 311 | gref_size += op.count; |
| 312 | op.index = priv->index; |
| 313 | priv->index += op.count * PAGE_SIZE; |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 314 | mutex_unlock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 315 | |
| 316 | rc = add_grefs(&op, gref_ids, priv); |
| 317 | if (rc < 0) |
| 318 | goto out_free; |
| 319 | |
| 320 | /* Once we finish add_grefs, it is unsafe to touch the new reference, |
| 321 | * since it is possible for a concurrent ioctl to remove it (by guessing |
| 322 | * its index). If the userspace application doesn't provide valid memory |
| 323 | * to write the IDs to, then it will need to close the file in order to |
| 324 | * release - which it will do by segfaulting when it tries to access the |
| 325 | * IDs to close them. |
| 326 | */ |
| 327 | if (copy_to_user(arg, &op, sizeof(op))) { |
| 328 | rc = -EFAULT; |
| 329 | goto out_free; |
| 330 | } |
| 331 | if (copy_to_user(arg->gref_ids, gref_ids, |
| 332 | sizeof(gref_ids[0]) * op.count)) { |
| 333 | rc = -EFAULT; |
| 334 | goto out_free; |
| 335 | } |
| 336 | |
| 337 | out_free: |
| 338 | kfree(gref_ids); |
| 339 | out: |
| 340 | return rc; |
| 341 | } |
| 342 | |
| 343 | static long gntalloc_ioctl_dealloc(struct gntalloc_file_private_data *priv, |
| 344 | void __user *arg) |
| 345 | { |
| 346 | int i, rc = 0; |
| 347 | struct ioctl_gntalloc_dealloc_gref op; |
| 348 | struct gntalloc_gref *gref, *n; |
| 349 | |
| 350 | pr_debug("%s: priv %p\n", __func__, priv); |
| 351 | |
| 352 | if (copy_from_user(&op, arg, sizeof(op))) { |
| 353 | rc = -EFAULT; |
| 354 | goto dealloc_grant_out; |
| 355 | } |
| 356 | |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 357 | mutex_lock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 358 | gref = find_grefs(priv, op.index, op.count); |
| 359 | if (gref) { |
| 360 | /* Remove from the file list only, and decrease reference count. |
| 361 | * The later call to do_cleanup() will remove from gref_list and |
| 362 | * free the memory if the pages aren't mapped anywhere. |
| 363 | */ |
| 364 | for (i = 0; i < op.count; i++) { |
| 365 | n = list_entry(gref->next_file.next, |
| 366 | struct gntalloc_gref, next_file); |
| 367 | list_del(&gref->next_file); |
| 368 | gref->users--; |
| 369 | gref = n; |
| 370 | } |
| 371 | } else { |
| 372 | rc = -EINVAL; |
| 373 | } |
| 374 | |
| 375 | do_cleanup(); |
| 376 | |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 377 | mutex_unlock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 378 | dealloc_grant_out: |
| 379 | return rc; |
| 380 | } |
| 381 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 382 | static long gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data *priv, |
| 383 | void __user *arg) |
| 384 | { |
| 385 | struct ioctl_gntalloc_unmap_notify op; |
| 386 | struct gntalloc_gref *gref; |
| 387 | uint64_t index; |
| 388 | int pgoff; |
| 389 | int rc; |
| 390 | |
| 391 | if (copy_from_user(&op, arg, sizeof(op))) |
| 392 | return -EFAULT; |
| 393 | |
| 394 | index = op.index & ~(PAGE_SIZE - 1); |
| 395 | pgoff = op.index & (PAGE_SIZE - 1); |
| 396 | |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 397 | mutex_lock(&gref_mutex); |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 398 | |
| 399 | gref = find_grefs(priv, index, 1); |
| 400 | if (!gref) { |
| 401 | rc = -ENOENT; |
| 402 | goto unlock_out; |
| 403 | } |
| 404 | |
| 405 | if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) { |
| 406 | rc = -EINVAL; |
| 407 | goto unlock_out; |
| 408 | } |
| 409 | |
Daniel De Graaf | 0cc678f | 2011-10-27 17:58:49 -0400 | [diff] [blame] | 410 | /* We need to grab a reference to the event channel we are going to use |
| 411 | * to send the notify before releasing the reference we may already have |
| 412 | * (if someone has called this ioctl twice). This is required so that |
| 413 | * it is possible to change the clear_byte part of the notification |
| 414 | * without disturbing the event channel part, which may now be the last |
| 415 | * reference to that event channel. |
| 416 | */ |
| 417 | if (op.action & UNMAP_NOTIFY_SEND_EVENT) { |
| 418 | if (evtchn_get(op.event_channel_port)) { |
| 419 | rc = -EINVAL; |
| 420 | goto unlock_out; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT) |
| 425 | evtchn_put(gref->notify.event); |
| 426 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 427 | gref->notify.flags = op.action; |
| 428 | gref->notify.pgoff = pgoff; |
| 429 | gref->notify.event = op.event_channel_port; |
| 430 | rc = 0; |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 431 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 432 | unlock_out: |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 433 | mutex_unlock(&gref_mutex); |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 434 | return rc; |
| 435 | } |
| 436 | |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 437 | static long gntalloc_ioctl(struct file *filp, unsigned int cmd, |
| 438 | unsigned long arg) |
| 439 | { |
| 440 | struct gntalloc_file_private_data *priv = filp->private_data; |
| 441 | |
| 442 | switch (cmd) { |
| 443 | case IOCTL_GNTALLOC_ALLOC_GREF: |
| 444 | return gntalloc_ioctl_alloc(priv, (void __user *)arg); |
| 445 | |
| 446 | case IOCTL_GNTALLOC_DEALLOC_GREF: |
| 447 | return gntalloc_ioctl_dealloc(priv, (void __user *)arg); |
| 448 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 449 | case IOCTL_GNTALLOC_SET_UNMAP_NOTIFY: |
| 450 | return gntalloc_ioctl_unmap_notify(priv, (void __user *)arg); |
| 451 | |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 452 | default: |
| 453 | return -ENOIOCTLCMD; |
| 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 459 | static void gntalloc_vma_open(struct vm_area_struct *vma) |
| 460 | { |
Daniel De Graaf | 243082e | 2011-11-28 11:49:11 -0500 | [diff] [blame] | 461 | struct gntalloc_vma_private_data *priv = vma->vm_private_data; |
| 462 | |
| 463 | if (!priv) |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 464 | return; |
| 465 | |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 466 | mutex_lock(&gref_mutex); |
Daniel De Graaf | 243082e | 2011-11-28 11:49:11 -0500 | [diff] [blame] | 467 | priv->users++; |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 468 | mutex_unlock(&gref_mutex); |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 469 | } |
| 470 | |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 471 | static void gntalloc_vma_close(struct vm_area_struct *vma) |
| 472 | { |
Daniel De Graaf | 243082e | 2011-11-28 11:49:11 -0500 | [diff] [blame] | 473 | struct gntalloc_vma_private_data *priv = vma->vm_private_data; |
| 474 | struct gntalloc_gref *gref, *next; |
| 475 | int i; |
| 476 | |
| 477 | if (!priv) |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 478 | return; |
| 479 | |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 480 | mutex_lock(&gref_mutex); |
Daniel De Graaf | 243082e | 2011-11-28 11:49:11 -0500 | [diff] [blame] | 481 | priv->users--; |
| 482 | if (priv->users == 0) { |
| 483 | gref = priv->gref; |
| 484 | for (i = 0; i < priv->count; i++) { |
| 485 | gref->users--; |
| 486 | next = list_entry(gref->next_gref.next, |
| 487 | struct gntalloc_gref, next_gref); |
| 488 | if (gref->users == 0) |
| 489 | __del_gref(gref); |
| 490 | gref = next; |
| 491 | } |
| 492 | kfree(priv); |
| 493 | } |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 494 | mutex_unlock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 495 | } |
| 496 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 497 | static const struct vm_operations_struct gntalloc_vmops = { |
Daniel De Graaf | d79647a | 2011-03-07 15:18:57 -0500 | [diff] [blame] | 498 | .open = gntalloc_vma_open, |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 499 | .close = gntalloc_vma_close, |
| 500 | }; |
| 501 | |
| 502 | static int gntalloc_mmap(struct file *filp, struct vm_area_struct *vma) |
| 503 | { |
| 504 | struct gntalloc_file_private_data *priv = filp->private_data; |
Daniel De Graaf | 243082e | 2011-11-28 11:49:11 -0500 | [diff] [blame] | 505 | struct gntalloc_vma_private_data *vm_priv; |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 506 | struct gntalloc_gref *gref; |
| 507 | int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; |
| 508 | int rv, i; |
| 509 | |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 510 | if (!(vma->vm_flags & VM_SHARED)) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 511 | pr_err("%s: Mapping must be shared\n", __func__); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 512 | return -EINVAL; |
| 513 | } |
| 514 | |
Daniel De Graaf | 243082e | 2011-11-28 11:49:11 -0500 | [diff] [blame] | 515 | vm_priv = kmalloc(sizeof(*vm_priv), GFP_KERNEL); |
| 516 | if (!vm_priv) |
| 517 | return -ENOMEM; |
| 518 | |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 519 | mutex_lock(&gref_mutex); |
Daniel De Graaf | 243082e | 2011-11-28 11:49:11 -0500 | [diff] [blame] | 520 | |
| 521 | pr_debug("%s: priv %p,%p, page %lu+%d\n", __func__, |
| 522 | priv, vm_priv, vma->vm_pgoff, count); |
| 523 | |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 524 | gref = find_grefs(priv, vma->vm_pgoff << PAGE_SHIFT, count); |
| 525 | if (gref == NULL) { |
| 526 | rv = -ENOENT; |
| 527 | pr_debug("%s: Could not find grant reference", |
| 528 | __func__); |
Julia Lawall | 2e16341 | 2011-12-23 18:39:29 +0100 | [diff] [blame] | 529 | kfree(vm_priv); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 530 | goto out_unlock; |
| 531 | } |
| 532 | |
Daniel De Graaf | 243082e | 2011-11-28 11:49:11 -0500 | [diff] [blame] | 533 | vm_priv->gref = gref; |
| 534 | vm_priv->users = 1; |
| 535 | vm_priv->count = count; |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 536 | |
Daniel De Graaf | 243082e | 2011-11-28 11:49:11 -0500 | [diff] [blame] | 537 | vma->vm_private_data = vm_priv; |
| 538 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 539 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 540 | |
| 541 | vma->vm_ops = &gntalloc_vmops; |
| 542 | |
| 543 | for (i = 0; i < count; i++) { |
| 544 | gref->users++; |
| 545 | rv = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE, |
| 546 | gref->page); |
| 547 | if (rv) |
| 548 | goto out_unlock; |
| 549 | |
| 550 | gref = list_entry(gref->next_file.next, |
| 551 | struct gntalloc_gref, next_file); |
| 552 | } |
| 553 | rv = 0; |
| 554 | |
| 555 | out_unlock: |
Daniel De Graaf | 8ca19a8 | 2011-10-27 17:58:48 -0400 | [diff] [blame] | 556 | mutex_unlock(&gref_mutex); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 557 | return rv; |
| 558 | } |
| 559 | |
| 560 | static const struct file_operations gntalloc_fops = { |
| 561 | .owner = THIS_MODULE, |
| 562 | .open = gntalloc_open, |
| 563 | .release = gntalloc_release, |
| 564 | .unlocked_ioctl = gntalloc_ioctl, |
| 565 | .mmap = gntalloc_mmap |
| 566 | }; |
| 567 | |
| 568 | /* |
| 569 | * ------------------------------------- |
| 570 | * Module creation/destruction. |
| 571 | * ------------------------------------- |
| 572 | */ |
| 573 | static struct miscdevice gntalloc_miscdev = { |
| 574 | .minor = MISC_DYNAMIC_MINOR, |
| 575 | .name = "xen/gntalloc", |
| 576 | .fops = &gntalloc_fops, |
| 577 | }; |
| 578 | |
| 579 | static int __init gntalloc_init(void) |
| 580 | { |
| 581 | int err; |
| 582 | |
| 583 | if (!xen_domain()) |
| 584 | return -ENODEV; |
| 585 | |
| 586 | err = misc_register(&gntalloc_miscdev); |
| 587 | if (err != 0) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 588 | pr_err("Could not register misc gntalloc device\n"); |
Daniel De Graaf | dd31405 | 2011-02-07 17:23:05 -0500 | [diff] [blame] | 589 | return err; |
| 590 | } |
| 591 | |
| 592 | pr_debug("Created grant allocation device at %d,%d\n", |
| 593 | MISC_MAJOR, gntalloc_miscdev.minor); |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static void __exit gntalloc_exit(void) |
| 599 | { |
| 600 | misc_deregister(&gntalloc_miscdev); |
| 601 | } |
| 602 | |
| 603 | module_init(gntalloc_init); |
| 604 | module_exit(gntalloc_exit); |
| 605 | |
| 606 | MODULE_LICENSE("GPL"); |
| 607 | MODULE_AUTHOR("Carter Weatherly <carter.weatherly@jhuapl.edu>, " |
| 608 | "Daniel De Graaf <dgdegra@tycho.nsa.gov>"); |
| 609 | MODULE_DESCRIPTION("User-space grant reference allocator driver"); |