- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / gntdev / gntdev.c
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  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <linux/atomic.h>
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/fs.h>
24 #include <linux/miscdevice.h>
25 #include <linux/mm.h>
26 #include <linux/slab.h>
27 #include <asm/uaccess.h>
28 #include <asm/io.h>
29 #include <xen/gnttab.h>
30 #include <asm/hypervisor.h>
31 #include <xen/balloon.h>
32 #include <xen/evtchn.h>
33 #include <xen/public/gntdev.h>
34
35
36 #define DRIVER_AUTHOR "Derek G. Murray <Derek.Murray@cl.cam.ac.uk>"
37 #define DRIVER_DESC   "User-space granted page access driver"
38
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR(DRIVER_AUTHOR);
41 MODULE_DESCRIPTION(DRIVER_DESC);
42
43 #define GNTDEV_NAME "gntdev"
44 MODULE_ALIAS("devname:xen/" GNTDEV_NAME);
45
46 #define MAX_GRANTS_LIMIT   1024
47 #define DEFAULT_MAX_GRANTS 128
48
49 /* A slot can be in one of three states:
50  *
51  * 0. GNTDEV_SLOT_INVALID:
52  *    This slot is not associated with a grant reference, and is therefore free
53  *    to be overwritten by a new grant reference.
54  *
55  * 1. GNTDEV_SLOT_NOT_YET_MAPPED:
56  *    This slot is associated with a grant reference (via the 
57  *    IOCTL_GNTDEV_MAP_GRANT_REF ioctl), but it has not yet been mmap()-ed.
58  *
59  * 2. GNTDEV_SLOT_MAPPED:
60  *    This slot is associated with a grant reference, and has been mmap()-ed.
61  */
62 typedef enum gntdev_slot_state {
63         GNTDEV_SLOT_INVALID = 0,
64         GNTDEV_SLOT_NOT_YET_MAPPED,
65         GNTDEV_SLOT_MAPPED
66 } gntdev_slot_state_t;
67
68 #define GNTDEV_INVALID_HANDLE    -1
69 #define GNTDEV_FREE_LIST_INVALID -1
70 /* Each opened instance of gntdev is associated with a list of grants,
71  * represented by an array of elements of the following type,
72  * gntdev_grant_info_t.
73  */
74 typedef struct gntdev_grant_info {
75         gntdev_slot_state_t state;
76         union {
77                 uint32_t free_list_index;
78                 struct {
79                         domid_t domid;
80                         grant_ref_t ref;
81                         grant_handle_t kernel_handle;
82                         grant_handle_t user_handle;
83                         uint64_t dev_bus_addr;
84                 } valid;
85         } u;
86 } gntdev_grant_info_t;
87
88 /* Private data structure, which is stored in the file pointer for files
89  * associated with this device.
90  */
91 typedef struct gntdev_file_private_data {
92   
93         /* Array of grant information. */
94         gntdev_grant_info_t *grants;
95         uint32_t grants_size;
96
97         /* Read/write semaphore used to protect the grants array. */
98         struct rw_semaphore grants_sem;
99
100         /* An array of indices of free slots in the grants array.
101          * N.B. An entry in this list may temporarily have the value
102          * GNTDEV_FREE_LIST_INVALID if the corresponding slot has been removed
103          * from the list by the contiguous allocator, but the list has not yet
104          * been compressed. However, this is not visible across invocations of
105          * the device.
106          */
107         int32_t *free_list;
108         
109         /* The number of free slots in the grants array. */
110         uint32_t free_list_size;
111
112         /* Read/write semaphore used to protect the free list. */
113         struct rw_semaphore free_list_sem;
114         
115         /* Index of the next slot after the most recent contiguous allocation, 
116          * for use in a next-fit allocator.
117          */
118         uint32_t next_fit_index;
119
120         /* Used to map grants into the kernel, before mapping them into user
121          * space.
122          */
123         struct page **foreign_pages;
124
125 } gntdev_file_private_data_t;
126
127 /* Module lifecycle operations. */
128 static int __init gntdev_init(void);
129 static void __exit gntdev_exit(void);
130
131 module_init(gntdev_init);
132 module_exit(gntdev_exit);
133
134 /* File operations. */
135 static int gntdev_open(struct inode *inode, struct file *flip);
136 static int gntdev_release(struct inode *inode, struct file *flip);
137 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma);
138 static long gntdev_ioctl(struct file *flip,
139                          unsigned int cmd, unsigned long arg);
140
141 static const struct file_operations gntdev_fops = {
142         .owner = THIS_MODULE,
143         .open = gntdev_open,
144         .llseek = no_llseek,
145         .release = gntdev_release,
146         .mmap = gntdev_mmap,
147         .unlocked_ioctl = gntdev_ioctl
148 };
149
150 /* VM operations. */
151 static void gntdev_vma_close(struct vm_area_struct *vma);
152 static pte_t gntdev_clear_pte(struct vm_area_struct *vma, unsigned long addr,
153                               pte_t *ptep, int is_fullmm);
154
155 static struct vm_operations_struct gntdev_vmops = {
156         .close = gntdev_vma_close,
157         .zap_pte = gntdev_clear_pte
158 };
159
160 /* Memory mapping functions
161  * ------------------------
162  *
163  * Every granted page is mapped into both kernel and user space, and the two
164  * following functions return the respective virtual addresses of these pages.
165  *
166  * When shadow paging is disabled, the granted page is mapped directly into
167  * user space; when it is enabled, it is mapped into the kernel and remapped
168  * into user space using vm_insert_page() (see gntdev_mmap(), below).
169  */
170
171 /* Returns the virtual address (in user space) of the @page_index'th page
172  * in the given VM area.
173  */
174 static inline unsigned long get_user_vaddr (struct vm_area_struct *vma,
175                                             int page_index)
176 {
177         return (unsigned long) vma->vm_start + (page_index << PAGE_SHIFT);
178 }
179
180 /* Returns the virtual address (in kernel space) of the @slot_index'th page
181  * mapped by the gntdev instance that owns the given private data struct.
182  */
183 static inline unsigned long get_kernel_vaddr (gntdev_file_private_data_t *priv,
184                                               int slot_index)
185 {
186         unsigned long pfn;
187         void *kaddr;
188         pfn = page_to_pfn(priv->foreign_pages[slot_index]);
189         kaddr = pfn_to_kaddr(pfn);
190         return (unsigned long) kaddr;
191 }
192
193 /* Helper functions. */
194
195 /* Adds information about a grant reference to the list of grants in the file's
196  * private data structure. Returns non-zero on failure. On success, sets the
197  * value of *offset to the offset that should be mmap()-ed in order to map the
198  * grant reference.
199  */
200 static int add_grant_reference(gntdev_file_private_data_t *private_data,
201                                struct ioctl_gntdev_grant_ref *op,
202                                uint64_t *offset)
203 {
204         uint32_t slot_index;
205
206         slot_index = private_data->free_list[--private_data->free_list_size];
207         private_data->free_list[private_data->free_list_size]
208                 = GNTDEV_FREE_LIST_INVALID;
209
210         /* Copy the grant information into file's private data. */
211         private_data->grants[slot_index].state = GNTDEV_SLOT_NOT_YET_MAPPED;
212         private_data->grants[slot_index].u.valid.domid = op->domid;
213         private_data->grants[slot_index].u.valid.ref = op->ref;
214
215         /* The offset is calculated as the index of the chosen entry in the
216          * file's private data's array of grant information. This is then
217          * shifted to give an offset into the virtual "file address space".
218          */
219         *offset = slot_index << PAGE_SHIFT;
220
221         return 0;
222 }
223
224 /* Adds the @count grant references to the contiguous range in the slot array
225  * beginning at @first_slot. It is assumed that @first_slot was returned by a
226  * previous invocation of find_contiguous_free_range(), during the same
227  * invocation of the driver.
228  */
229 static int add_grant_references(gntdev_file_private_data_t *private_data,
230                                 uint32_t count,
231                                 struct ioctl_gntdev_grant_ref *ops,
232                                 uint32_t first_slot)
233 {
234         uint32_t i;
235         
236         for (i = 0; i < count; ++i) {
237
238                 /* First, mark the slot's entry in the free list as invalid. */
239                 uint32_t free_list_index =
240                         private_data->grants[first_slot+i].u.free_list_index;
241                 private_data->free_list[free_list_index] = 
242                         GNTDEV_FREE_LIST_INVALID;
243
244                 /* Now, update the slot. */
245                 private_data->grants[first_slot+i].state = 
246                         GNTDEV_SLOT_NOT_YET_MAPPED;
247                 private_data->grants[first_slot+i].u.valid.domid =
248                         ops[i].domid;
249                 private_data->grants[first_slot+i].u.valid.ref = ops[i].ref;
250         }
251
252         return 0;       
253 }
254
255 /* Scans through the free list for @flip, removing entries that are marked as
256  * GNTDEV_SLOT_INVALID. This will reduce the recorded size of the free list to
257  * the number of valid entries.
258  */
259 static void compress_free_list(gntdev_file_private_data_t *private_data)
260 {
261         uint32_t i, j = 0, old_size;
262         
263         old_size = private_data->free_list_size;
264         for (i = 0; i < old_size; ++i) {
265                 if (private_data->free_list[i] != GNTDEV_FREE_LIST_INVALID) {
266                         if (i > j) {
267                                 int32_t slot_index;
268
269                                 slot_index = private_data->free_list[i];
270                                 private_data->free_list[j] = slot_index;
271                                 private_data->grants[slot_index].u
272                                         .free_list_index = j;
273                                 private_data->free_list[i] 
274                                         = GNTDEV_FREE_LIST_INVALID;
275                         }
276                         ++j;
277                 } else {
278                         --private_data->free_list_size;
279                 }
280         }
281 }
282
283 /* Searches the grant array in the private data of @flip for a range of
284  * @num_slots contiguous slots in the GNTDEV_SLOT_INVALID state.
285  *
286  * Returns the index of the first slot if a range is found, otherwise -ENOMEM.
287  */
288 static int find_contiguous_free_range(gntdev_file_private_data_t *private_data,
289                                       uint32_t num_slots) 
290 {
291         uint32_t i, start_index = private_data->next_fit_index;
292         uint32_t range_start = 0, range_length;
293
294         /* First search from the start_index to the end of the array. */
295         range_length = 0;
296         for (i = start_index; i < private_data->grants_size; ++i) {
297                 if (private_data->grants[i].state == GNTDEV_SLOT_INVALID) {
298                         if (range_length == 0) {
299                                 range_start = i;
300                         }
301                         ++range_length;
302                         if (range_length == num_slots) {
303                                 return range_start;
304                         }
305                 }
306         }
307         
308         /* Now search from the start of the array to the start_index. */
309         range_length = 0;
310         for (i = 0; i < start_index; ++i) {
311                 if (private_data->grants[i].state == GNTDEV_SLOT_INVALID) {
312                         if (range_length == 0) {
313                                 range_start = i;
314                         }
315                         ++range_length;
316                         if (range_length == num_slots) {
317                                 return range_start;
318                         }
319                 }
320         }
321         
322         return -ENOMEM;
323 }
324
325 static int init_private_data(gntdev_file_private_data_t *priv,
326                              uint32_t max_grants)
327 {
328         int i;
329
330         /* Allocate space for the kernel-mapping of granted pages. */
331         priv->foreign_pages = 
332                 alloc_empty_pages_and_pagevec(max_grants);
333         if (!priv->foreign_pages)
334                 goto nomem_out;
335
336         /* Allocate the grant list and free-list. */
337         priv->grants = kmalloc(max_grants * sizeof(gntdev_grant_info_t),
338                                GFP_KERNEL);
339         if (!priv->grants)
340                 goto nomem_out2;
341         priv->free_list = kmalloc(max_grants * sizeof(int32_t), GFP_KERNEL);
342         if (!priv->free_list)
343                 goto nomem_out3;
344
345         /* Initialise the free-list, which contains all slots at first. */
346         for (i = 0; i < max_grants; ++i) {
347                 priv->free_list[max_grants - i - 1] = i;
348                 priv->grants[i].state = GNTDEV_SLOT_INVALID;
349                 priv->grants[i].u.free_list_index = max_grants - i - 1;
350         }
351         priv->grants_size = max_grants;
352         priv->free_list_size = max_grants;
353         priv->next_fit_index = 0;
354
355         return 0;
356
357 nomem_out3:
358         kfree(priv->grants);
359 nomem_out2:
360         free_empty_pages_and_pagevec(priv->foreign_pages, max_grants);
361 nomem_out:
362         return -ENOMEM;
363
364 }
365
366 /* Interface functions. */
367
368 static struct miscdevice gntdev_miscdev = {
369         .minor        = MISC_DYNAMIC_MINOR,
370         .name         = GNTDEV_NAME,
371         .nodename     = "xen/" GNTDEV_NAME,
372         .fops         = &gntdev_fops,
373 };
374
375 /* Initialises the driver. Called when the module is loaded. */
376 static int __init gntdev_init(void)
377 {
378         int err;
379
380         if (!is_running_on_xen()) {
381                 pr_err("You must be running Xen to use gntdev\n");
382                 return -ENODEV;
383         }
384
385         err = misc_register(&gntdev_miscdev);
386         if (err)
387         {
388                 pr_err("Could not register gntdev device\n");
389                 return err;
390         }
391
392         return 0;
393 }
394
395 /* Cleans up and unregisters the driver. Called when the driver is unloaded.
396  */
397 static void __exit gntdev_exit(void)
398 {
399         misc_deregister(&gntdev_miscdev);
400 }
401
402 /* Called when the device is opened. */
403 static int gntdev_open(struct inode *inode, struct file *flip)
404 {
405         gntdev_file_private_data_t *private_data;
406
407         nonseekable_open(inode, flip);
408
409         /* Allocate space for the per-instance private data. */
410         private_data = kmalloc(sizeof(*private_data), GFP_KERNEL);
411         if (!private_data)
412                 goto nomem_out;
413
414         /* These will be lazily initialised by init_private_data. */
415         private_data->grants = NULL;
416         private_data->free_list = NULL;
417         private_data->foreign_pages = NULL;
418
419         init_rwsem(&private_data->grants_sem);
420         init_rwsem(&private_data->free_list_sem);
421
422         flip->private_data = private_data;
423
424         return 0;
425
426 nomem_out:
427         return -ENOMEM;
428 }
429
430 /* Called when the device is closed.
431  */
432 static int gntdev_release(struct inode *inode, struct file *flip)
433 {
434         if (flip->private_data) {
435                 gntdev_file_private_data_t *private_data = 
436                         (gntdev_file_private_data_t *) flip->private_data;
437                 if (private_data->foreign_pages)
438                         free_empty_pages_and_pagevec
439                                 (private_data->foreign_pages,
440                                  private_data->grants_size);
441                 if (private_data->grants) 
442                         kfree(private_data->grants);
443                 if (private_data->free_list)
444                         kfree(private_data->free_list);
445                 kfree(private_data);
446         }
447         return 0;
448 }
449
450 /* Called when an attempt is made to mmap() the device. The private data from
451  * @flip contains the list of grant references that can be mapped. The vm_pgoff
452  * field of @vma contains the index into that list that refers to the grant
453  * reference that will be mapped. Only mappings that are a multiple of
454  * PAGE_SIZE are handled.
455  */
456 static int gntdev_mmap (struct file *flip, struct vm_area_struct *vma) 
457 {
458         struct gnttab_map_grant_ref op;
459         unsigned long slot_index = vma->vm_pgoff;
460         unsigned long kernel_vaddr, user_vaddr;
461         uint32_t size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
462         uint64_t ptep;
463         int ret, exit_ret;
464         int flags;
465         int i;
466         struct page *page;
467         gntdev_file_private_data_t *private_data = flip->private_data;
468
469         if (unlikely(!private_data)) {
470                 pr_err("file's private data is NULL\n");
471                 return -EINVAL;
472         }
473
474         /* Test to make sure that the grants array has been initialised. */
475         down_read(&private_data->grants_sem);
476         if (unlikely(!private_data->grants)) {
477                 up_read(&private_data->grants_sem);
478                 pr_err("attempted to mmap before ioctl\n");
479                 return -EINVAL;
480         }
481         up_read(&private_data->grants_sem);
482
483         if (unlikely((size <= 0) || 
484                      (size + slot_index) > private_data->grants_size)) {
485                 pr_err("Invalid number of pages or offset"
486                        "(num_pages = %d, first_slot = %ld)\n",
487                        size, slot_index);
488                 return -ENXIO;
489         }
490
491         if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED)) {
492                 pr_err("writable mappings must be shared\n");
493                 return -EINVAL;
494         }
495
496         /* Slots must be in the NOT_YET_MAPPED state. */
497         down_write(&private_data->grants_sem);
498         for (i = 0; i < size; ++i) {
499                 if (private_data->grants[slot_index + i].state != 
500                     GNTDEV_SLOT_NOT_YET_MAPPED) {
501                         pr_err("Slot (index = %ld) is in the wrong "
502                                "state (%d)\n", slot_index + i,
503                                private_data->grants[slot_index + i].state);
504                         up_write(&private_data->grants_sem);
505                         return -EINVAL;
506                 }
507         }
508
509         /* Install the hook for unmapping. */
510         vma->vm_ops = &gntdev_vmops;
511     
512         /* The VM area contains pages from another VM. */
513         vma->vm_flags |= VM_FOREIGN;
514         vma->vm_private_data = kzalloc(size * sizeof(struct page *),
515                                        GFP_KERNEL);
516         if (vma->vm_private_data == NULL) {
517                 pr_err("couldn't allocate mapping structure for VM area\n");
518                 return -ENOMEM;
519         }
520
521         /* This flag prevents Bad PTE errors when the memory is unmapped. */
522         vma->vm_flags |= VM_RESERVED;
523
524         /* This flag prevents this VM area being copied on a fork(). A better
525          * behaviour might be to explicitly carry out the appropriate mappings
526          * on fork(), but I don't know if there's a hook for this.
527          */
528         vma->vm_flags |= VM_DONTCOPY;
529
530 #ifdef CONFIG_X86
531         /* This flag ensures that the page tables are not unpinned before the
532          * VM area is unmapped. Therefore Xen still recognises the PTE as
533          * belonging to an L1 pagetable, and the grant unmap operation will
534          * succeed, even if the process does not exit cleanly.
535          */
536         vma->vm_mm->context.has_foreign_mappings = 1;
537 #endif
538
539         exit_ret = -ENOMEM;
540         for (i = 0; i < size; ++i) {
541
542                 flags = GNTMAP_host_map;
543                 if (!(vma->vm_flags & VM_WRITE))
544                         flags |= GNTMAP_readonly;
545
546                 kernel_vaddr = get_kernel_vaddr(private_data, slot_index + i);
547                 user_vaddr = get_user_vaddr(vma, i);
548                 page = private_data->foreign_pages[slot_index + i];
549
550                 gnttab_set_map_op(&op, kernel_vaddr, flags,   
551                                   private_data->grants[slot_index+i]
552                                   .u.valid.ref, 
553                                   private_data->grants[slot_index+i]
554                                   .u.valid.domid);
555
556                 /* Carry out the mapping of the grant reference. */
557                 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, 
558                                                 &op, 1);
559                 BUG_ON(ret);
560                 if (op.status != GNTST_okay) {
561                         if (op.status != GNTST_eagain)
562                                 pr_err("Error mapping the grant reference "
563                                        "into the kernel (%d). domid = %d; ref = %d\n",
564                                        op.status,
565                                        private_data->grants[slot_index+i]
566                                        .u.valid.domid,
567                                        private_data->grants[slot_index+i]
568                                        .u.valid.ref);
569                         else
570                                 /* Propagate eagain instead of trying to fix it up */
571                                 exit_ret = -EAGAIN;
572                         goto undo_map_out;
573                 }
574
575                 /* Store a reference to the page that will be mapped into user
576                  * space.
577                  */
578                 ((struct page **) vma->vm_private_data)[i] = page;
579
580                 /* Mark mapped page as reserved. */
581                 SetPageReserved(page);
582
583                 /* Record the grant handle, for use in the unmap operation. */
584                 private_data->grants[slot_index+i].u.valid.kernel_handle = 
585                         op.handle;
586                 private_data->grants[slot_index+i].u.valid.dev_bus_addr = 
587                         op.dev_bus_addr;
588                 
589                 private_data->grants[slot_index+i].state = GNTDEV_SLOT_MAPPED;
590                 private_data->grants[slot_index+i].u.valid.user_handle =
591                         GNTDEV_INVALID_HANDLE;
592
593                 /* Now perform the mapping to user space. */
594                 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
595
596                         /* NOT USING SHADOW PAGE TABLES. */
597                         /* In this case, we map the grant(s) straight into user
598                          * space.
599                          */
600
601                         /* Get the machine address of the PTE for the user 
602                          *  page.
603                          */
604                         if ((ret = create_lookup_pte_addr(vma->vm_mm, 
605                                                           vma->vm_start 
606                                                           + (i << PAGE_SHIFT), 
607                                                           &ptep)))
608                         {
609                                 pr_err("Error obtaining PTE pointer (%d)\n",
610                                        ret);
611                                 goto undo_map_out;
612                         }
613                         
614                         /* Configure the map operation. */
615                 
616                         /* The reference is to be used by host CPUs. */
617                         flags = GNTMAP_host_map;
618                         
619                         /* Specifies a user space mapping. */
620                         flags |= GNTMAP_application_map;
621                         
622                         /* The map request contains the machine address of the
623                          * PTE to update.
624                          */
625                         flags |= GNTMAP_contains_pte;
626                         
627                         if (!(vma->vm_flags & VM_WRITE))
628                                 flags |= GNTMAP_readonly;
629
630                         gnttab_set_map_op(&op, ptep, flags, 
631                                           private_data->grants[slot_index+i]
632                                           .u.valid.ref, 
633                                           private_data->grants[slot_index+i]
634                                           .u.valid.domid);
635
636                         /* Carry out the mapping of the grant reference. */
637                         ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref,
638                                                         &op, 1);
639                         BUG_ON(ret);
640                         if (op.status != GNTST_okay) {
641                                 pr_err("Error mapping the grant "
642                                        "reference into user space (%d). domid "
643                                        "= %d; ref = %d\n", op.status,
644                                        private_data->grants[slot_index+i].u
645                                        .valid.domid,
646                                        private_data->grants[slot_index+i].u
647                                        .valid.ref);
648                                 /* This should never happen after we've mapped into
649                                 * the kernel space. */
650                                 BUG_ON(op.status == GNTST_eagain);
651                                 goto undo_map_out;
652                         }
653                         
654                         /* Record the grant handle, for use in the unmap 
655                          * operation. 
656                          */
657                         private_data->grants[slot_index+i].u.
658                                 valid.user_handle = op.handle;
659
660                         /* Update p2m structure with the new mapping. */
661                         set_phys_to_machine(__pa(kernel_vaddr) >> PAGE_SHIFT,
662                                             FOREIGN_FRAME(private_data->
663                                                           grants[slot_index+i]
664                                                           .u.valid.dev_bus_addr
665                                                           >> PAGE_SHIFT));
666                 } else {
667                         /* USING SHADOW PAGE TABLES. */
668                         /* In this case, we simply insert the page into the VM
669                          * area. */
670                         ret = vm_insert_page(vma, user_vaddr, page);
671                 }
672
673         }
674         exit_ret = 0;
675
676         up_write(&private_data->grants_sem);
677         return exit_ret;
678
679 undo_map_out:
680         /* If we have a mapping failure, the unmapping will be taken care of
681          * by do_mmap_pgoff(), which will eventually call gntdev_clear_pte().
682          * All we need to do here is free the vma_private_data.
683          */
684         kfree(vma->vm_private_data);
685
686         /* THIS IS VERY UNPLEASANT: do_mmap_pgoff() will set the vma->vm_file
687          * to NULL on failure. However, we need this in gntdev_clear_pte() to
688          * unmap the grants. Therefore, we smuggle a reference to the file's
689          * private data in the VM area's private data pointer.
690          */
691         vma->vm_private_data = private_data;
692         
693         up_write(&private_data->grants_sem);
694
695         return exit_ret;
696 }
697
698 static pte_t gntdev_clear_pte(struct vm_area_struct *vma, unsigned long addr,
699                               pte_t *ptep, int is_fullmm)
700 {
701         int slot_index, ret;
702         pte_t copy;
703         struct gnttab_unmap_grant_ref op;
704         gntdev_file_private_data_t *private_data;
705
706         /* THIS IS VERY UNPLEASANT: do_mmap_pgoff() will set the vma->vm_file
707          * to NULL on failure. However, we need this in gntdev_clear_pte() to
708          * unmap the grants. Therefore, we smuggle a reference to the file's
709          * private data in the VM area's private data pointer.
710          */
711         if (vma->vm_file) {
712                 private_data = (gntdev_file_private_data_t *)
713                         vma->vm_file->private_data;
714         } else if (vma->vm_private_data) {
715                 private_data = (gntdev_file_private_data_t *)
716                         vma->vm_private_data;
717         } else {
718                 private_data = NULL; /* gcc warning */
719                 BUG();
720         }
721
722         /* Calculate the grant relating to this PTE. */
723         slot_index = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
724
725         /* Only unmap grants if the slot has been mapped. This could be being
726          * called from a failing mmap().
727          */
728         if (private_data->grants[slot_index].state == GNTDEV_SLOT_MAPPED) {
729
730                 /* First, we clear the user space mapping, if it has been made.
731                  */
732                 if (private_data->grants[slot_index].u.valid.user_handle !=
733                     GNTDEV_INVALID_HANDLE && 
734                     !xen_feature(XENFEAT_auto_translated_physmap)) {
735                         /* NOT USING SHADOW PAGE TABLES. */
736
737                         /* Copy the existing value of the PTE for returning. */
738                         copy = *ptep;
739
740                         gnttab_set_unmap_op(&op, ptep_to_machine(ptep), 
741                                             GNTMAP_contains_pte,
742                                             private_data->grants[slot_index]
743                                             .u.valid.user_handle);
744                         ret = HYPERVISOR_grant_table_op(
745                                 GNTTABOP_unmap_grant_ref, &op, 1);
746                         BUG_ON(ret);
747                         if (op.status != GNTST_okay)
748                                 pr_warning("User unmap grant status = %d\n",
749                                            op.status);
750                 } else {
751                         /* USING SHADOW PAGE TABLES. */
752                         copy = xen_ptep_get_and_clear_full(vma, addr, ptep, is_fullmm);
753                 }
754
755                 /* Finally, we unmap the grant from kernel space. */
756                 gnttab_set_unmap_op(&op, 
757                                     get_kernel_vaddr(private_data, slot_index),
758                                     GNTMAP_host_map, 
759                                     private_data->grants[slot_index].u.valid
760                                     .kernel_handle);
761                 ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, 
762                                                 &op, 1);
763                 BUG_ON(ret);
764                 if (op.status != GNTST_okay)
765                         pr_warning("Kernel unmap grant status = %d\n",
766                                    op.status);
767
768
769                 /* Return slot to the not-yet-mapped state, so that it may be
770                  * mapped again, or removed by a subsequent ioctl.
771                  */
772                 private_data->grants[slot_index].state = 
773                         GNTDEV_SLOT_NOT_YET_MAPPED;
774
775                 /* Invalidate the physical to machine mapping for this page. */
776                 set_phys_to_machine(
777                         page_to_pfn(private_data->foreign_pages[slot_index]),
778                         INVALID_P2M_ENTRY);
779
780         } else {
781                 copy = xen_ptep_get_and_clear_full(vma, addr, ptep, is_fullmm);
782         }
783
784         return copy;
785 }
786
787 /* "Destructor" for a VM area.
788  */
789 static void gntdev_vma_close(struct vm_area_struct *vma) {
790         if (vma->vm_private_data) {
791                 kfree(vma->vm_private_data);
792         }
793 }
794
795 /* Called when an ioctl is made on the device.
796  */
797 static long gntdev_ioctl(struct file *flip,
798                          unsigned int cmd, unsigned long arg)
799 {
800         int rc = 0;
801         gntdev_file_private_data_t *private_data = 
802                 (gntdev_file_private_data_t *) flip->private_data;
803
804         /* On the first invocation, we will lazily initialise the grant array
805          * and free-list.
806          */
807         if (unlikely(!private_data->grants) 
808             && likely(cmd != IOCTL_GNTDEV_SET_MAX_GRANTS)) {
809                 down_write(&private_data->grants_sem);
810                 
811                 if (unlikely(private_data->grants)) {
812                         up_write(&private_data->grants_sem);
813                         goto private_data_initialised;
814                 }
815                 
816                 /* Just use the default. Setting to a non-default is handled
817                  * in the ioctl switch.
818                  */
819                 rc = init_private_data(private_data, DEFAULT_MAX_GRANTS);
820                 
821                 up_write(&private_data->grants_sem);
822
823                 if (rc) {
824                         pr_err("Initialising gntdev private data failed\n");
825                         return rc;
826                 }
827         }
828             
829 private_data_initialised:
830         switch (cmd) {
831         case IOCTL_GNTDEV_MAP_GRANT_REF:
832         {
833                 struct ioctl_gntdev_map_grant_ref op;
834                 struct ioctl_gntdev_grant_ref *refs = NULL;
835
836                 if (copy_from_user(&op, (void __user *)arg, sizeof(op)))
837                         return -EFAULT;
838                 if (unlikely(op.count <= 0))
839                         return -EINVAL;
840
841                 if (op.count > 1 && op.count <= private_data->grants_size) {
842                         struct ioctl_gntdev_grant_ref *u;
843
844                         refs = kmalloc(op.count * sizeof(*refs), GFP_KERNEL);
845                         if (!refs)
846                                 return -ENOMEM;
847                         u = ((struct ioctl_gntdev_map_grant_ref *)arg)->refs;
848                         if (copy_from_user(refs, (void __user *)u,
849                                            sizeof(*refs) * op.count)) {
850                                 kfree(refs);
851                                 return -EFAULT;
852                         }
853                 }
854
855                 down_write(&private_data->grants_sem);
856                 down_write(&private_data->free_list_sem);
857
858                 if (unlikely(op.count > private_data->free_list_size)) {
859                         rc = -ENOMEM;
860                         goto map_out;
861                 }
862
863                 if (op.count == 1) {
864                         if ((rc = add_grant_reference(private_data, op.refs,
865                                                       &op.index)) < 0) {
866                                 pr_err("Adding grant reference failed (%d)\n",
867                                        rc);
868                                 goto map_out;
869                         }
870                 } else {
871                         if ((rc = find_contiguous_free_range(private_data,
872                                                              op.count)) < 0) {
873                                 pr_err("Finding contiguous range failed"
874                                        " (%d)\n", rc);
875                                 goto map_out;
876                         }
877                         op.index = rc << PAGE_SHIFT;
878                         if ((rc = add_grant_references(private_data, op.count,
879                                                        refs, rc))) {
880                                 pr_err("Adding grant references failed (%d)\n",
881                                        rc);
882                                 goto map_out;
883                         }
884                         compress_free_list(private_data);
885                 }
886
887         map_out:
888                 up_write(&private_data->free_list_sem);
889                 up_write(&private_data->grants_sem);
890
891                 kfree(refs);
892
893                 if (!rc && copy_to_user((void __user *)arg, &op, sizeof(op)))
894                         rc = -EFAULT;
895                 return rc;
896         }
897         case IOCTL_GNTDEV_UNMAP_GRANT_REF:
898         {
899                 struct ioctl_gntdev_unmap_grant_ref op;
900                 uint32_t i, start_index;
901
902                 if (copy_from_user(&op, (void __user *)arg, sizeof(op)))
903                         return -EFAULT;
904
905                 start_index = op.index >> PAGE_SHIFT;
906                 if (start_index + op.count > private_data->grants_size)
907                         return -EINVAL;
908
909                 down_write(&private_data->grants_sem);
910
911                 /* First, check that all pages are in the NOT_YET_MAPPED
912                  * state.
913                  */
914                 for (i = 0; i < op.count; ++i) {
915                         if (unlikely
916                             (private_data->grants[start_index + i].state
917                              != GNTDEV_SLOT_NOT_YET_MAPPED)) {
918                                 if (private_data->grants[start_index + i].state
919                                     == GNTDEV_SLOT_INVALID) {
920                                         pr_err("Tried to remove an invalid "
921                                                "grant at offset 0x%x.",
922                                                (start_index + i) 
923                                                << PAGE_SHIFT);
924                                         rc = -EINVAL;
925                                 } else {
926                                         pr_err("Tried to remove a grant which "
927                                                "is currently mmap()-ed at "
928                                                "offset 0x%x.",
929                                                (start_index + i) 
930                                                << PAGE_SHIFT);
931                                         rc = -EBUSY;
932                                 }
933                                 goto unmap_out;
934                         }
935                 }
936
937                 down_write(&private_data->free_list_sem);
938
939                 /* Unmap pages and add them to the free list.
940                  */
941                 for (i = 0; i < op.count; ++i) {
942                         private_data->grants[start_index+i].state = 
943                                 GNTDEV_SLOT_INVALID;
944                         private_data->grants[start_index+i].u.free_list_index =
945                                 private_data->free_list_size;
946                         private_data->free_list[private_data->free_list_size] =
947                                 start_index + i;
948                         ++private_data->free_list_size;
949                 }
950
951                 up_write(&private_data->free_list_sem);
952         unmap_out:
953                 up_write(&private_data->grants_sem);
954                 return rc;
955         }
956         case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
957         {
958                 struct ioctl_gntdev_get_offset_for_vaddr op;
959                 struct vm_area_struct *vma;
960                 unsigned long vaddr;
961
962                 if (copy_from_user(&op, (void __user *)arg, sizeof(op)))
963                         return -EFAULT;
964
965                 vaddr = (unsigned long)op.vaddr;
966
967                 down_read(&current->mm->mmap_sem);              
968                 vma = find_vma(current->mm, vaddr);
969                 if (!vma || vma->vm_ops != &gntdev_vmops) {
970                         rc = -EFAULT;
971                         goto get_offset_out;
972                 }
973                 if (vma->vm_start != vaddr) {
974                         pr_err("The vaddr specified in an "
975                                "IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR must be at "
976                                "the start of the VM area. vma->vm_start = "
977                                "%#lx; vaddr = %#lx\n",
978                                vma->vm_start, vaddr);
979                         rc = -EFAULT;
980                         goto get_offset_out;
981                 }
982                 op.offset = vma->vm_pgoff << PAGE_SHIFT;
983                 op.count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
984         get_offset_out:
985                 up_read(&current->mm->mmap_sem);
986                 if (!rc && copy_to_user((void __user *)arg, &op, sizeof(op)))
987                         rc = -EFAULT;
988                 return rc;
989         }
990         case IOCTL_GNTDEV_SET_MAX_GRANTS:
991         {
992                 struct ioctl_gntdev_set_max_grants op;
993
994                 if (copy_from_user(&op, (void __user *)arg, sizeof(op)))
995                         return -EFAULT;
996                 if (op.count > MAX_GRANTS_LIMIT)
997                         return -EINVAL;
998
999                 down_write(&private_data->grants_sem);
1000                 if (unlikely(private_data->grants))
1001                         rc = -EBUSY;
1002                 else
1003                         rc = init_private_data(private_data, op.count);
1004                 up_write(&private_data->grants_sem);
1005                 return rc;
1006         }
1007         default:
1008                 return -ENOIOCTLCMD;
1009         }
1010
1011         return 0;
1012 }