1 /******************************************************************************
4 * Granting foreign access to our memory reservation.
6 * Copyright (c) 2005-2006, Christopher Clark
7 * Copyright (c) 2004-2005, K A Fraser
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
36 #include <linux/memblock.h>
37 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/vmalloc.h>
41 #include <linux/uaccess.h>
43 #include <linux/delay.h>
44 #include <linux/hardirq.h>
45 #include <linux/workqueue.h>
46 #include <linux/ratelimit.h>
47 #include <linux/moduleparam.h>
48 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
49 #include <linux/dma-mapping.h>
53 #include <xen/interface/xen.h>
55 #include <xen/grant_table.h>
56 #include <xen/interface/memory.h>
57 #include <xen/hvc-console.h>
58 #include <xen/swiotlb-xen.h>
59 #include <xen/balloon.h>
61 #include <asm/xen/cpuid.h>
63 #include <xen/mem-reservation.h>
64 #include <asm/xen/hypercall.h>
65 #include <asm/xen/interface.h>
67 #include <asm/sync_bitops.h>
69 /* External tools reserve first few grant table entries. */
70 #define NR_RESERVED_ENTRIES 8
71 #define GNTTAB_LIST_END 0xffffffff
73 static grant_ref_t **gnttab_list;
74 static unsigned int nr_grant_frames;
75 static int gnttab_free_count;
76 static grant_ref_t gnttab_free_head;
77 static DEFINE_SPINLOCK(gnttab_list_lock);
78 struct grant_frames xen_auto_xlat_grant_frames;
79 static unsigned int xen_gnttab_version;
80 module_param_named(version, xen_gnttab_version, uint, 0);
83 struct grant_entry_v1 *v1;
84 union grant_entry_v2 *v2;
88 /*This is a structure of function pointers for grant table*/
91 * Version of the grant interface.
95 * Grant refs per grant frame.
97 unsigned int grefs_per_grant_frame;
99 * Mapping a list of frames for storing grant entries. Frames parameter
100 * is used to store grant table address when grant table being setup,
101 * nr_gframes is the number of frames to map grant table. Returning
102 * GNTST_okay means success and negative value means failure.
104 int (*map_frames)(xen_pfn_t *frames, unsigned int nr_gframes);
106 * Release a list of frames which are mapped in map_frames for grant
109 void (*unmap_frames)(void);
111 * Introducing a valid entry into the grant table, granting the frame of
112 * this grant entry to domain for accessing or transfering. Ref
113 * parameter is reference of this introduced grant entry, domid is id of
114 * granted domain, frame is the page frame to be granted, and flags is
115 * status of the grant entry to be updated.
117 void (*update_entry)(grant_ref_t ref, domid_t domid,
118 unsigned long frame, unsigned flags);
120 * Stop granting a grant entry to domain for accessing. Ref parameter is
121 * reference of a grant entry whose grant access will be stopped,
122 * readonly is not in use in this function. If the grant entry is
123 * currently mapped for reading or writing, just return failure(==0)
124 * directly and don't tear down the grant access. Otherwise, stop grant
125 * access for this entry and return success(==1).
127 int (*end_foreign_access_ref)(grant_ref_t ref, int readonly);
129 * Stop granting a grant entry to domain for transfer. Ref parameter is
130 * reference of a grant entry whose grant transfer will be stopped. If
131 * tranfer has not started, just reclaim the grant entry and return
132 * failure(==0). Otherwise, wait for the transfer to complete and then
135 unsigned long (*end_foreign_transfer_ref)(grant_ref_t ref);
138 struct unmap_refs_callback_data {
139 struct completion completion;
143 static const struct gnttab_ops *gnttab_interface;
145 /* This reflects status of grant entries, so act as a global value. */
146 static grant_status_t *grstatus;
148 static struct gnttab_free_callback *gnttab_free_callback_list;
150 static int gnttab_expand(unsigned int req_entries);
152 #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
153 #define SPP (PAGE_SIZE / sizeof(grant_status_t))
155 static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
157 return &gnttab_list[(entry) / RPP][(entry) % RPP];
159 /* This can be used as an l-value */
160 #define gnttab_entry(entry) (*__gnttab_entry(entry))
162 static int get_free_entries(unsigned count)
168 spin_lock_irqsave(&gnttab_list_lock, flags);
170 if ((gnttab_free_count < count) &&
171 ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
172 spin_unlock_irqrestore(&gnttab_list_lock, flags);
176 ref = head = gnttab_free_head;
177 gnttab_free_count -= count;
179 head = gnttab_entry(head);
180 gnttab_free_head = gnttab_entry(head);
181 gnttab_entry(head) = GNTTAB_LIST_END;
183 spin_unlock_irqrestore(&gnttab_list_lock, flags);
188 static void do_free_callbacks(void)
190 struct gnttab_free_callback *callback, *next;
192 callback = gnttab_free_callback_list;
193 gnttab_free_callback_list = NULL;
195 while (callback != NULL) {
196 next = callback->next;
197 if (gnttab_free_count >= callback->count) {
198 callback->next = NULL;
199 callback->fn(callback->arg);
201 callback->next = gnttab_free_callback_list;
202 gnttab_free_callback_list = callback;
208 static inline void check_free_callbacks(void)
210 if (unlikely(gnttab_free_callback_list))
214 static void put_free_entry(grant_ref_t ref)
217 spin_lock_irqsave(&gnttab_list_lock, flags);
218 gnttab_entry(ref) = gnttab_free_head;
219 gnttab_free_head = ref;
221 check_free_callbacks();
222 spin_unlock_irqrestore(&gnttab_list_lock, flags);
226 * Following applies to gnttab_update_entry_v1 and gnttab_update_entry_v2.
227 * Introducing a valid entry into the grant table:
228 * 1. Write ent->domid.
229 * 2. Write ent->frame:
230 * GTF_permit_access: Frame to which access is permitted.
231 * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
232 * frame, or zero if none.
233 * 3. Write memory barrier (WMB).
234 * 4. Write ent->flags, inc. valid type.
236 static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid,
237 unsigned long frame, unsigned flags)
239 gnttab_shared.v1[ref].domid = domid;
240 gnttab_shared.v1[ref].frame = frame;
242 gnttab_shared.v1[ref].flags = flags;
245 static void gnttab_update_entry_v2(grant_ref_t ref, domid_t domid,
246 unsigned long frame, unsigned int flags)
248 gnttab_shared.v2[ref].hdr.domid = domid;
249 gnttab_shared.v2[ref].full_page.frame = frame;
250 wmb(); /* Hypervisor concurrent accesses. */
251 gnttab_shared.v2[ref].hdr.flags = GTF_permit_access | flags;
255 * Public grant-issuing interface functions
257 void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
258 unsigned long frame, int readonly)
260 gnttab_interface->update_entry(ref, domid, frame,
261 GTF_permit_access | (readonly ? GTF_readonly : 0));
263 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
265 int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
270 ref = get_free_entries(1);
271 if (unlikely(ref < 0))
274 gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
278 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
280 static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly)
285 pflags = &gnttab_shared.v1[ref].flags;
289 if (flags & (GTF_reading|GTF_writing))
291 } while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags);
296 static int gnttab_end_foreign_access_ref_v2(grant_ref_t ref, int readonly)
298 gnttab_shared.v2[ref].hdr.flags = 0;
299 mb(); /* Concurrent access by hypervisor. */
300 if (grstatus[ref] & (GTF_reading|GTF_writing)) {
304 * The read of grstatus needs to have acquire semantics.
305 * On x86, reads already have that, and we just need to
306 * protect against compiler reorderings.
307 * On other architectures we may need a full barrier.
319 static inline int _gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
321 return gnttab_interface->end_foreign_access_ref(ref, readonly);
324 int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
326 if (_gnttab_end_foreign_access_ref(ref, readonly))
328 pr_warn("WARNING: g.e. %#x still in use!\n", ref);
331 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
333 struct deferred_entry {
334 struct list_head list;
340 static LIST_HEAD(deferred_list);
341 static void gnttab_handle_deferred(struct timer_list *);
342 static DEFINE_TIMER(deferred_timer, gnttab_handle_deferred);
344 static void gnttab_handle_deferred(struct timer_list *unused)
346 unsigned int nr = 10;
347 struct deferred_entry *first = NULL;
350 spin_lock_irqsave(&gnttab_list_lock, flags);
352 struct deferred_entry *entry
353 = list_first_entry(&deferred_list,
354 struct deferred_entry, list);
358 list_del(&entry->list);
359 spin_unlock_irqrestore(&gnttab_list_lock, flags);
360 if (_gnttab_end_foreign_access_ref(entry->ref, entry->ro)) {
361 put_free_entry(entry->ref);
363 pr_debug("freeing g.e. %#x (pfn %#lx)\n",
364 entry->ref, page_to_pfn(entry->page));
365 put_page(entry->page);
367 pr_info("freeing g.e. %#x\n", entry->ref);
371 if (!--entry->warn_delay)
372 pr_info("g.e. %#x still pending\n", entry->ref);
376 spin_lock_irqsave(&gnttab_list_lock, flags);
378 list_add_tail(&entry->list, &deferred_list);
379 else if (list_empty(&deferred_list))
382 if (!list_empty(&deferred_list) && !timer_pending(&deferred_timer)) {
383 deferred_timer.expires = jiffies + HZ;
384 add_timer(&deferred_timer);
386 spin_unlock_irqrestore(&gnttab_list_lock, flags);
389 static void gnttab_add_deferred(grant_ref_t ref, bool readonly,
392 struct deferred_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
393 const char *what = KERN_WARNING "leaking";
399 entry->ro = readonly;
401 entry->warn_delay = 60;
402 spin_lock_irqsave(&gnttab_list_lock, flags);
403 list_add_tail(&entry->list, &deferred_list);
404 if (!timer_pending(&deferred_timer)) {
405 deferred_timer.expires = jiffies + HZ;
406 add_timer(&deferred_timer);
408 spin_unlock_irqrestore(&gnttab_list_lock, flags);
409 what = KERN_DEBUG "deferring";
411 printk("%s g.e. %#x (pfn %#lx)\n",
412 what, ref, page ? page_to_pfn(page) : -1);
415 int gnttab_try_end_foreign_access(grant_ref_t ref)
417 int ret = _gnttab_end_foreign_access_ref(ref, 0);
424 EXPORT_SYMBOL_GPL(gnttab_try_end_foreign_access);
426 void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
429 if (gnttab_try_end_foreign_access(ref)) {
431 put_page(virt_to_page(page));
433 gnttab_add_deferred(ref, readonly,
434 page ? virt_to_page(page) : NULL);
436 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
438 int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
442 ref = get_free_entries(1);
443 if (unlikely(ref < 0))
445 gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
449 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
451 void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
454 gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer);
456 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
458 static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref)
464 pflags = &gnttab_shared.v1[ref].flags;
467 * If a transfer is not even yet started, try to reclaim the grant
468 * reference and return failure (== 0).
470 while (!((flags = *pflags) & GTF_transfer_committed)) {
471 if (sync_cmpxchg(pflags, flags, 0) == flags)
476 /* If a transfer is in progress then wait until it is completed. */
477 while (!(flags & GTF_transfer_completed)) {
482 rmb(); /* Read the frame number /after/ reading completion status. */
483 frame = gnttab_shared.v1[ref].frame;
489 static unsigned long gnttab_end_foreign_transfer_ref_v2(grant_ref_t ref)
495 pflags = &gnttab_shared.v2[ref].hdr.flags;
498 * If a transfer is not even yet started, try to reclaim the grant
499 * reference and return failure (== 0).
501 while (!((flags = *pflags) & GTF_transfer_committed)) {
502 if (sync_cmpxchg(pflags, flags, 0) == flags)
507 /* If a transfer is in progress then wait until it is completed. */
508 while (!(flags & GTF_transfer_completed)) {
513 rmb(); /* Read the frame number /after/ reading completion status. */
514 frame = gnttab_shared.v2[ref].full_page.frame;
520 unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
522 return gnttab_interface->end_foreign_transfer_ref(ref);
524 EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
526 unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
528 unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
532 EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
534 void gnttab_free_grant_reference(grant_ref_t ref)
538 EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
540 void gnttab_free_grant_references(grant_ref_t head)
545 if (head == GNTTAB_LIST_END)
547 spin_lock_irqsave(&gnttab_list_lock, flags);
549 while (gnttab_entry(ref) != GNTTAB_LIST_END) {
550 ref = gnttab_entry(ref);
553 gnttab_entry(ref) = gnttab_free_head;
554 gnttab_free_head = head;
555 gnttab_free_count += count;
556 check_free_callbacks();
557 spin_unlock_irqrestore(&gnttab_list_lock, flags);
559 EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
561 int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
563 int h = get_free_entries(count);
572 EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
574 int gnttab_empty_grant_references(const grant_ref_t *private_head)
576 return (*private_head == GNTTAB_LIST_END);
578 EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
580 int gnttab_claim_grant_reference(grant_ref_t *private_head)
582 grant_ref_t g = *private_head;
583 if (unlikely(g == GNTTAB_LIST_END))
585 *private_head = gnttab_entry(g);
588 EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
590 void gnttab_release_grant_reference(grant_ref_t *private_head,
593 gnttab_entry(release) = *private_head;
594 *private_head = release;
596 EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
598 void gnttab_request_free_callback(struct gnttab_free_callback *callback,
599 void (*fn)(void *), void *arg, u16 count)
602 struct gnttab_free_callback *cb;
604 spin_lock_irqsave(&gnttab_list_lock, flags);
606 /* Check if the callback is already on the list */
607 cb = gnttab_free_callback_list;
616 callback->count = count;
617 callback->next = gnttab_free_callback_list;
618 gnttab_free_callback_list = callback;
619 check_free_callbacks();
621 spin_unlock_irqrestore(&gnttab_list_lock, flags);
623 EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
625 void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
627 struct gnttab_free_callback **pcb;
630 spin_lock_irqsave(&gnttab_list_lock, flags);
631 for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
632 if (*pcb == callback) {
633 *pcb = callback->next;
637 spin_unlock_irqrestore(&gnttab_list_lock, flags);
639 EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
641 static unsigned int gnttab_frames(unsigned int frames, unsigned int align)
643 return (frames * gnttab_interface->grefs_per_grant_frame + align - 1) /
647 static int grow_gnttab_list(unsigned int more_frames)
649 unsigned int new_nr_grant_frames, extra_entries, i;
650 unsigned int nr_glist_frames, new_nr_glist_frames;
651 unsigned int grefs_per_frame;
653 grefs_per_frame = gnttab_interface->grefs_per_grant_frame;
655 new_nr_grant_frames = nr_grant_frames + more_frames;
656 extra_entries = more_frames * grefs_per_frame;
658 nr_glist_frames = gnttab_frames(nr_grant_frames, RPP);
659 new_nr_glist_frames = gnttab_frames(new_nr_grant_frames, RPP);
660 for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
661 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
667 for (i = grefs_per_frame * nr_grant_frames;
668 i < grefs_per_frame * new_nr_grant_frames - 1; i++)
669 gnttab_entry(i) = i + 1;
671 gnttab_entry(i) = gnttab_free_head;
672 gnttab_free_head = grefs_per_frame * nr_grant_frames;
673 gnttab_free_count += extra_entries;
675 nr_grant_frames = new_nr_grant_frames;
677 check_free_callbacks();
682 while (i-- > nr_glist_frames)
683 free_page((unsigned long) gnttab_list[i]);
687 static unsigned int __max_nr_grant_frames(void)
689 struct gnttab_query_size query;
692 query.dom = DOMID_SELF;
694 rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
695 if ((rc < 0) || (query.status != GNTST_okay))
696 return 4; /* Legacy max supported number of frames */
698 return query.max_nr_frames;
701 unsigned int gnttab_max_grant_frames(void)
703 unsigned int xen_max = __max_nr_grant_frames();
704 static unsigned int boot_max_nr_grant_frames;
706 /* First time, initialize it properly. */
707 if (!boot_max_nr_grant_frames)
708 boot_max_nr_grant_frames = __max_nr_grant_frames();
710 if (xen_max > boot_max_nr_grant_frames)
711 return boot_max_nr_grant_frames;
714 EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
716 int gnttab_setup_auto_xlat_frames(phys_addr_t addr)
719 unsigned int max_nr_gframes = __max_nr_grant_frames();
723 if (xen_auto_xlat_grant_frames.count)
726 vaddr = xen_remap(addr, XEN_PAGE_SIZE * max_nr_gframes);
728 pr_warn("Failed to ioremap gnttab share frames (addr=%pa)!\n",
732 pfn = kcalloc(max_nr_gframes, sizeof(pfn[0]), GFP_KERNEL);
737 for (i = 0; i < max_nr_gframes; i++)
738 pfn[i] = XEN_PFN_DOWN(addr) + i;
740 xen_auto_xlat_grant_frames.vaddr = vaddr;
741 xen_auto_xlat_grant_frames.pfn = pfn;
742 xen_auto_xlat_grant_frames.count = max_nr_gframes;
746 EXPORT_SYMBOL_GPL(gnttab_setup_auto_xlat_frames);
748 void gnttab_free_auto_xlat_frames(void)
750 if (!xen_auto_xlat_grant_frames.count)
752 kfree(xen_auto_xlat_grant_frames.pfn);
753 xen_unmap(xen_auto_xlat_grant_frames.vaddr);
755 xen_auto_xlat_grant_frames.pfn = NULL;
756 xen_auto_xlat_grant_frames.count = 0;
757 xen_auto_xlat_grant_frames.vaddr = NULL;
759 EXPORT_SYMBOL_GPL(gnttab_free_auto_xlat_frames);
761 int gnttab_pages_set_private(int nr_pages, struct page **pages)
765 for (i = 0; i < nr_pages; i++) {
766 #if BITS_PER_LONG < 64
767 struct xen_page_foreign *foreign;
769 foreign = kzalloc(sizeof(*foreign), GFP_KERNEL);
773 set_page_private(pages[i], (unsigned long)foreign);
775 SetPagePrivate(pages[i]);
780 EXPORT_SYMBOL_GPL(gnttab_pages_set_private);
783 * gnttab_alloc_pages - alloc pages suitable for grant mapping into
784 * @nr_pages: number of pages to alloc
785 * @pages: returns the pages
787 int gnttab_alloc_pages(int nr_pages, struct page **pages)
791 ret = xen_alloc_unpopulated_pages(nr_pages, pages);
795 ret = gnttab_pages_set_private(nr_pages, pages);
797 gnttab_free_pages(nr_pages, pages);
801 EXPORT_SYMBOL_GPL(gnttab_alloc_pages);
803 #ifdef CONFIG_XEN_UNPOPULATED_ALLOC
804 static inline void cache_init(struct gnttab_page_cache *cache)
809 static inline bool cache_empty(struct gnttab_page_cache *cache)
811 return !cache->pages;
814 static inline struct page *cache_deq(struct gnttab_page_cache *cache)
819 cache->pages = page->zone_device_data;
824 static inline void cache_enq(struct gnttab_page_cache *cache, struct page *page)
826 page->zone_device_data = cache->pages;
830 static inline void cache_init(struct gnttab_page_cache *cache)
832 INIT_LIST_HEAD(&cache->pages);
835 static inline bool cache_empty(struct gnttab_page_cache *cache)
837 return list_empty(&cache->pages);
840 static inline struct page *cache_deq(struct gnttab_page_cache *cache)
844 page = list_first_entry(&cache->pages, struct page, lru);
845 list_del(&page->lru);
850 static inline void cache_enq(struct gnttab_page_cache *cache, struct page *page)
852 list_add(&page->lru, &cache->pages);
856 void gnttab_page_cache_init(struct gnttab_page_cache *cache)
858 spin_lock_init(&cache->lock);
860 cache->num_pages = 0;
862 EXPORT_SYMBOL_GPL(gnttab_page_cache_init);
864 int gnttab_page_cache_get(struct gnttab_page_cache *cache, struct page **page)
868 spin_lock_irqsave(&cache->lock, flags);
870 if (cache_empty(cache)) {
871 spin_unlock_irqrestore(&cache->lock, flags);
872 return gnttab_alloc_pages(1, page);
875 page[0] = cache_deq(cache);
878 spin_unlock_irqrestore(&cache->lock, flags);
882 EXPORT_SYMBOL_GPL(gnttab_page_cache_get);
884 void gnttab_page_cache_put(struct gnttab_page_cache *cache, struct page **page,
890 spin_lock_irqsave(&cache->lock, flags);
892 for (i = 0; i < num; i++)
893 cache_enq(cache, page[i]);
894 cache->num_pages += num;
896 spin_unlock_irqrestore(&cache->lock, flags);
898 EXPORT_SYMBOL_GPL(gnttab_page_cache_put);
900 void gnttab_page_cache_shrink(struct gnttab_page_cache *cache, unsigned int num)
902 struct page *page[10];
906 spin_lock_irqsave(&cache->lock, flags);
908 while (cache->num_pages > num) {
909 page[i] = cache_deq(cache);
911 if (++i == ARRAY_SIZE(page)) {
912 spin_unlock_irqrestore(&cache->lock, flags);
913 gnttab_free_pages(i, page);
915 spin_lock_irqsave(&cache->lock, flags);
919 spin_unlock_irqrestore(&cache->lock, flags);
922 gnttab_free_pages(i, page);
924 EXPORT_SYMBOL_GPL(gnttab_page_cache_shrink);
926 void gnttab_pages_clear_private(int nr_pages, struct page **pages)
930 for (i = 0; i < nr_pages; i++) {
931 if (PagePrivate(pages[i])) {
932 #if BITS_PER_LONG < 64
933 kfree((void *)page_private(pages[i]));
935 ClearPagePrivate(pages[i]);
939 EXPORT_SYMBOL_GPL(gnttab_pages_clear_private);
942 * gnttab_free_pages - free pages allocated by gnttab_alloc_pages()
943 * @nr_pages; number of pages to free
946 void gnttab_free_pages(int nr_pages, struct page **pages)
948 gnttab_pages_clear_private(nr_pages, pages);
949 xen_free_unpopulated_pages(nr_pages, pages);
951 EXPORT_SYMBOL_GPL(gnttab_free_pages);
953 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
955 * gnttab_dma_alloc_pages - alloc DMAable pages suitable for grant mapping into
956 * @args: arguments to the function
958 int gnttab_dma_alloc_pages(struct gnttab_dma_alloc_args *args)
960 unsigned long pfn, start_pfn;
964 size = args->nr_pages << PAGE_SHIFT;
966 args->vaddr = dma_alloc_coherent(args->dev, size,
968 GFP_KERNEL | __GFP_NOWARN);
970 args->vaddr = dma_alloc_wc(args->dev, size,
972 GFP_KERNEL | __GFP_NOWARN);
974 pr_debug("Failed to allocate DMA buffer of size %zu\n", size);
978 start_pfn = __phys_to_pfn(args->dev_bus_addr);
979 for (pfn = start_pfn, i = 0; pfn < start_pfn + args->nr_pages;
981 struct page *page = pfn_to_page(pfn);
983 args->pages[i] = page;
984 args->frames[i] = xen_page_to_gfn(page);
985 xenmem_reservation_scrub_page(page);
988 xenmem_reservation_va_mapping_reset(args->nr_pages, args->pages);
990 ret = xenmem_reservation_decrease(args->nr_pages, args->frames);
991 if (ret != args->nr_pages) {
992 pr_debug("Failed to decrease reservation for DMA buffer\n");
997 ret = gnttab_pages_set_private(args->nr_pages, args->pages);
1004 gnttab_dma_free_pages(args);
1007 EXPORT_SYMBOL_GPL(gnttab_dma_alloc_pages);
1010 * gnttab_dma_free_pages - free DMAable pages
1011 * @args: arguments to the function
1013 int gnttab_dma_free_pages(struct gnttab_dma_alloc_args *args)
1018 gnttab_pages_clear_private(args->nr_pages, args->pages);
1020 for (i = 0; i < args->nr_pages; i++)
1021 args->frames[i] = page_to_xen_pfn(args->pages[i]);
1023 ret = xenmem_reservation_increase(args->nr_pages, args->frames);
1024 if (ret != args->nr_pages) {
1025 pr_debug("Failed to increase reservation for DMA buffer\n");
1031 xenmem_reservation_va_mapping_update(args->nr_pages, args->pages,
1034 size = args->nr_pages << PAGE_SHIFT;
1036 dma_free_coherent(args->dev, size,
1037 args->vaddr, args->dev_bus_addr);
1039 dma_free_wc(args->dev, size,
1040 args->vaddr, args->dev_bus_addr);
1043 EXPORT_SYMBOL_GPL(gnttab_dma_free_pages);
1046 /* Handling of paged out grant targets (GNTST_eagain) */
1047 #define MAX_DELAY 256
1049 gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
1055 BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
1056 if (*status == GNTST_eagain)
1058 } while ((*status == GNTST_eagain) && (delay < MAX_DELAY));
1060 if (delay >= MAX_DELAY) {
1061 pr_err("%s: %s eagain grant\n", func, current->comm);
1062 *status = GNTST_bad_page;
1066 void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count)
1068 struct gnttab_map_grant_ref *op;
1070 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, batch, count))
1072 for (op = batch; op < batch + count; op++)
1073 if (op->status == GNTST_eagain)
1074 gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, op,
1075 &op->status, __func__);
1077 EXPORT_SYMBOL_GPL(gnttab_batch_map);
1079 void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count)
1081 struct gnttab_copy *op;
1083 if (HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count))
1085 for (op = batch; op < batch + count; op++)
1086 if (op->status == GNTST_eagain)
1087 gnttab_retry_eagain_gop(GNTTABOP_copy, op,
1088 &op->status, __func__);
1090 EXPORT_SYMBOL_GPL(gnttab_batch_copy);
1092 void gnttab_foreach_grant_in_range(struct page *page,
1093 unsigned int offset,
1098 unsigned int goffset;
1100 unsigned long xen_pfn;
1102 len = min_t(unsigned int, PAGE_SIZE - offset, len);
1103 goffset = xen_offset_in_page(offset);
1105 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(offset);
1108 glen = min_t(unsigned int, XEN_PAGE_SIZE - goffset, len);
1109 fn(pfn_to_gfn(xen_pfn), goffset, glen, data);
1116 EXPORT_SYMBOL_GPL(gnttab_foreach_grant_in_range);
1118 void gnttab_foreach_grant(struct page **pages,
1119 unsigned int nr_grefs,
1123 unsigned int goffset = 0;
1124 unsigned long xen_pfn = 0;
1127 for (i = 0; i < nr_grefs; i++) {
1128 if ((i % XEN_PFN_PER_PAGE) == 0) {
1129 xen_pfn = page_to_xen_pfn(pages[i / XEN_PFN_PER_PAGE]);
1133 fn(pfn_to_gfn(xen_pfn), goffset, XEN_PAGE_SIZE, data);
1135 goffset += XEN_PAGE_SIZE;
1140 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
1141 struct gnttab_map_grant_ref *kmap_ops,
1142 struct page **pages, unsigned int count)
1146 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
1150 for (i = 0; i < count; i++) {
1151 switch (map_ops[i].status) {
1154 struct xen_page_foreign *foreign;
1156 SetPageForeign(pages[i]);
1157 foreign = xen_page_foreign(pages[i]);
1158 foreign->domid = map_ops[i].dom;
1159 foreign->gref = map_ops[i].ref;
1163 case GNTST_no_device_space:
1164 pr_warn_ratelimited("maptrack limit reached, can't map all guest pages\n");
1168 /* Retry eagain maps */
1169 gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref,
1171 &map_ops[i].status, __func__);
1172 /* Test status in next loop iteration. */
1181 return set_foreign_p2m_mapping(map_ops, kmap_ops, pages, count);
1183 EXPORT_SYMBOL_GPL(gnttab_map_refs);
1185 int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
1186 struct gnttab_unmap_grant_ref *kunmap_ops,
1187 struct page **pages, unsigned int count)
1192 ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
1196 for (i = 0; i < count; i++)
1197 ClearPageForeign(pages[i]);
1199 return clear_foreign_p2m_mapping(unmap_ops, kunmap_ops, pages, count);
1201 EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
1203 #define GNTTAB_UNMAP_REFS_DELAY 5
1205 static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item);
1207 static void gnttab_unmap_work(struct work_struct *work)
1209 struct gntab_unmap_queue_data
1210 *unmap_data = container_of(work,
1211 struct gntab_unmap_queue_data,
1213 if (unmap_data->age != UINT_MAX)
1215 __gnttab_unmap_refs_async(unmap_data);
1218 static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
1223 for (pc = 0; pc < item->count; pc++) {
1224 if (page_count(item->pages[pc]) > 1) {
1225 unsigned long delay = GNTTAB_UNMAP_REFS_DELAY * (item->age + 1);
1226 schedule_delayed_work(&item->gnttab_work,
1227 msecs_to_jiffies(delay));
1232 ret = gnttab_unmap_refs(item->unmap_ops, item->kunmap_ops,
1233 item->pages, item->count);
1234 item->done(ret, item);
1237 void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
1239 INIT_DELAYED_WORK(&item->gnttab_work, gnttab_unmap_work);
1242 __gnttab_unmap_refs_async(item);
1244 EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async);
1246 static void unmap_refs_callback(int result,
1247 struct gntab_unmap_queue_data *data)
1249 struct unmap_refs_callback_data *d = data->data;
1252 complete(&d->completion);
1255 int gnttab_unmap_refs_sync(struct gntab_unmap_queue_data *item)
1257 struct unmap_refs_callback_data data;
1259 init_completion(&data.completion);
1261 item->done = &unmap_refs_callback;
1262 gnttab_unmap_refs_async(item);
1263 wait_for_completion(&data.completion);
1267 EXPORT_SYMBOL_GPL(gnttab_unmap_refs_sync);
1269 static unsigned int nr_status_frames(unsigned int nr_grant_frames)
1271 return gnttab_frames(nr_grant_frames, SPP);
1274 static int gnttab_map_frames_v1(xen_pfn_t *frames, unsigned int nr_gframes)
1278 rc = arch_gnttab_map_shared(frames, nr_gframes,
1279 gnttab_max_grant_frames(),
1280 &gnttab_shared.addr);
1286 static void gnttab_unmap_frames_v1(void)
1288 arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
1291 static int gnttab_map_frames_v2(xen_pfn_t *frames, unsigned int nr_gframes)
1294 unsigned int nr_sframes;
1295 struct gnttab_get_status_frames getframes;
1298 nr_sframes = nr_status_frames(nr_gframes);
1300 /* No need for kzalloc as it is initialized in following hypercall
1301 * GNTTABOP_get_status_frames.
1303 sframes = kmalloc_array(nr_sframes, sizeof(uint64_t), GFP_ATOMIC);
1307 getframes.dom = DOMID_SELF;
1308 getframes.nr_frames = nr_sframes;
1309 set_xen_guest_handle(getframes.frame_list, sframes);
1311 rc = HYPERVISOR_grant_table_op(GNTTABOP_get_status_frames,
1313 if (rc == -ENOSYS) {
1318 BUG_ON(rc || getframes.status);
1320 rc = arch_gnttab_map_status(sframes, nr_sframes,
1321 nr_status_frames(gnttab_max_grant_frames()),
1326 rc = arch_gnttab_map_shared(frames, nr_gframes,
1327 gnttab_max_grant_frames(),
1328 &gnttab_shared.addr);
1334 static void gnttab_unmap_frames_v2(void)
1336 arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
1337 arch_gnttab_unmap(grstatus, nr_status_frames(nr_grant_frames));
1340 static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
1342 struct gnttab_setup_table setup;
1344 unsigned int nr_gframes = end_idx + 1;
1347 if (xen_feature(XENFEAT_auto_translated_physmap)) {
1348 struct xen_add_to_physmap xatp;
1349 unsigned int i = end_idx;
1351 BUG_ON(xen_auto_xlat_grant_frames.count < nr_gframes);
1353 * Loop backwards, so that the first hypercall has the largest
1354 * index, ensuring that the table will grow only once.
1357 xatp.domid = DOMID_SELF;
1359 xatp.space = XENMAPSPACE_grant_table;
1360 xatp.gpfn = xen_auto_xlat_grant_frames.pfn[i];
1361 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
1363 pr_warn("grant table add_to_physmap failed, err=%d\n",
1367 } while (i-- > start_idx);
1372 /* No need for kzalloc as it is initialized in following hypercall
1373 * GNTTABOP_setup_table.
1375 frames = kmalloc_array(nr_gframes, sizeof(unsigned long), GFP_ATOMIC);
1379 setup.dom = DOMID_SELF;
1380 setup.nr_frames = nr_gframes;
1381 set_xen_guest_handle(setup.frame_list, frames);
1383 rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
1384 if (rc == -ENOSYS) {
1389 BUG_ON(rc || setup.status);
1391 rc = gnttab_interface->map_frames(frames, nr_gframes);
1398 static const struct gnttab_ops gnttab_v1_ops = {
1400 .grefs_per_grant_frame = XEN_PAGE_SIZE /
1401 sizeof(struct grant_entry_v1),
1402 .map_frames = gnttab_map_frames_v1,
1403 .unmap_frames = gnttab_unmap_frames_v1,
1404 .update_entry = gnttab_update_entry_v1,
1405 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1,
1406 .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1,
1409 static const struct gnttab_ops gnttab_v2_ops = {
1411 .grefs_per_grant_frame = XEN_PAGE_SIZE /
1412 sizeof(union grant_entry_v2),
1413 .map_frames = gnttab_map_frames_v2,
1414 .unmap_frames = gnttab_unmap_frames_v2,
1415 .update_entry = gnttab_update_entry_v2,
1416 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v2,
1417 .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v2,
1420 static bool gnttab_need_v2(void)
1423 uint32_t base, width;
1425 if (xen_pv_domain()) {
1426 base = xen_cpuid_base();
1427 if (cpuid_eax(base) < 5)
1428 return false; /* Information not available, use V1. */
1429 width = cpuid_ebx(base + 5) &
1430 XEN_CPUID_MACHINE_ADDRESS_WIDTH_MASK;
1431 return width > 32 + PAGE_SHIFT;
1434 return !!(max_possible_pfn >> 32);
1437 static void gnttab_request_version(void)
1440 struct gnttab_set_version gsv;
1442 if (gnttab_need_v2())
1447 /* Boot parameter overrides automatic selection. */
1448 if (xen_gnttab_version >= 1 && xen_gnttab_version <= 2)
1449 gsv.version = xen_gnttab_version;
1451 rc = HYPERVISOR_grant_table_op(GNTTABOP_set_version, &gsv, 1);
1452 if (rc == 0 && gsv.version == 2)
1453 gnttab_interface = &gnttab_v2_ops;
1455 gnttab_interface = &gnttab_v1_ops;
1456 pr_info("Grant tables using version %d layout\n",
1457 gnttab_interface->version);
1460 static int gnttab_setup(void)
1462 unsigned int max_nr_gframes;
1464 max_nr_gframes = gnttab_max_grant_frames();
1465 if (max_nr_gframes < nr_grant_frames)
1468 if (xen_feature(XENFEAT_auto_translated_physmap) && gnttab_shared.addr == NULL) {
1469 gnttab_shared.addr = xen_auto_xlat_grant_frames.vaddr;
1470 if (gnttab_shared.addr == NULL) {
1471 pr_warn("gnttab share frames is not mapped!\n");
1475 return gnttab_map(0, nr_grant_frames - 1);
1478 int gnttab_resume(void)
1480 gnttab_request_version();
1481 return gnttab_setup();
1484 int gnttab_suspend(void)
1486 if (!xen_feature(XENFEAT_auto_translated_physmap))
1487 gnttab_interface->unmap_frames();
1491 static int gnttab_expand(unsigned int req_entries)
1494 unsigned int cur, extra;
1496 cur = nr_grant_frames;
1497 extra = ((req_entries + gnttab_interface->grefs_per_grant_frame - 1) /
1498 gnttab_interface->grefs_per_grant_frame);
1499 if (cur + extra > gnttab_max_grant_frames()) {
1500 pr_warn_ratelimited("xen/grant-table: max_grant_frames reached"
1501 " cur=%u extra=%u limit=%u"
1502 " gnttab_free_count=%u req_entries=%u\n",
1503 cur, extra, gnttab_max_grant_frames(),
1504 gnttab_free_count, req_entries);
1508 rc = gnttab_map(cur, cur + extra - 1);
1510 rc = grow_gnttab_list(extra);
1515 int gnttab_init(void)
1518 unsigned long max_nr_grant_frames;
1519 unsigned int max_nr_glist_frames, nr_glist_frames;
1520 unsigned int nr_init_grefs;
1523 gnttab_request_version();
1524 max_nr_grant_frames = gnttab_max_grant_frames();
1525 nr_grant_frames = 1;
1527 /* Determine the maximum number of frames required for the
1528 * grant reference free list on the current hypervisor.
1530 max_nr_glist_frames = (max_nr_grant_frames *
1531 gnttab_interface->grefs_per_grant_frame / RPP);
1533 gnttab_list = kmalloc_array(max_nr_glist_frames,
1534 sizeof(grant_ref_t *),
1536 if (gnttab_list == NULL)
1539 nr_glist_frames = gnttab_frames(nr_grant_frames, RPP);
1540 for (i = 0; i < nr_glist_frames; i++) {
1541 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
1542 if (gnttab_list[i] == NULL) {
1548 ret = arch_gnttab_init(max_nr_grant_frames,
1549 nr_status_frames(max_nr_grant_frames));
1553 if (gnttab_setup() < 0) {
1558 nr_init_grefs = nr_grant_frames *
1559 gnttab_interface->grefs_per_grant_frame;
1561 for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
1562 gnttab_entry(i) = i + 1;
1564 gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
1565 gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
1566 gnttab_free_head = NR_RESERVED_ENTRIES;
1568 printk("Grant table initialized\n");
1572 for (i--; i >= 0; i--)
1573 free_page((unsigned long)gnttab_list[i]);
1577 EXPORT_SYMBOL_GPL(gnttab_init);
1579 static int __gnttab_init(void)
1584 /* Delay grant-table initialization in the PV on HVM case */
1585 if (xen_hvm_domain() && !xen_pvh_domain())
1588 return gnttab_init();
1590 /* Starts after core_initcall so that xen_pvh_gnttab_setup can be called
1591 * beforehand to initialize xen_auto_xlat_grant_frames. */
1592 core_initcall_sync(__gnttab_init);