Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * grant_table.c |
| 3 | * |
| 4 | * Granting foreign access to our memory reservation. |
| 5 | * |
| 6 | * Copyright (c) 2005-2006, Christopher Clark |
| 7 | * Copyright (c) 2004-2005, K A Fraser |
| 8 | * |
| 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: |
| 14 | * |
| 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: |
| 21 | * |
| 22 | * The above copyright notice and this permission notice shall be included in |
| 23 | * all copies or substantial portions of the Software. |
| 24 | * |
| 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 |
| 31 | * IN THE SOFTWARE. |
| 32 | */ |
| 33 | |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 34 | #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt |
| 35 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 36 | #include <linux/module.h> |
| 37 | #include <linux/sched.h> |
| 38 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 40 | #include <linux/vmalloc.h> |
| 41 | #include <linux/uaccess.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 42 | #include <linux/io.h> |
Andres Lagar-Cavilla | c571898 | 2012-09-14 14:26:59 +0000 | [diff] [blame] | 43 | #include <linux/delay.h> |
Stefano Stabellini | f62805f | 2012-04-24 11:55:43 +0100 | [diff] [blame] | 44 | #include <linux/hardirq.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 45 | |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 46 | #include <xen/xen.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 47 | #include <xen/interface/xen.h> |
| 48 | #include <xen/page.h> |
| 49 | #include <xen/grant_table.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 50 | #include <xen/interface/memory.h> |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 51 | #include <xen/hvc-console.h> |
Stefano Stabellini | 3d24bbd | 2013-10-25 10:41:44 +0000 | [diff] [blame] | 52 | #include <xen/swiotlb-xen.h> |
Jeremy Fitzhardinge | ecbf29c | 2008-12-16 12:37:07 -0800 | [diff] [blame] | 53 | #include <asm/xen/hypercall.h> |
Stefano Stabellini | 4d9310e | 2012-08-06 15:27:09 +0100 | [diff] [blame] | 54 | #include <asm/xen/interface.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 55 | |
| 56 | #include <asm/pgtable.h> |
| 57 | #include <asm/sync_bitops.h> |
| 58 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 59 | /* External tools reserve first few grant table entries. */ |
| 60 | #define NR_RESERVED_ENTRIES 8 |
| 61 | #define GNTTAB_LIST_END 0xffffffff |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 62 | |
| 63 | static grant_ref_t **gnttab_list; |
| 64 | static unsigned int nr_grant_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 65 | static int gnttab_free_count; |
| 66 | static grant_ref_t gnttab_free_head; |
| 67 | static DEFINE_SPINLOCK(gnttab_list_lock); |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 68 | struct grant_frames xen_auto_xlat_grant_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 69 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 70 | static union { |
| 71 | struct grant_entry_v1 *v1; |
| 72 | void *addr; |
| 73 | } gnttab_shared; |
| 74 | |
| 75 | /*This is a structure of function pointers for grant table*/ |
| 76 | struct gnttab_ops { |
| 77 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 78 | * Mapping a list of frames for storing grant entries. Frames parameter |
| 79 | * is used to store grant table address when grant table being setup, |
| 80 | * nr_gframes is the number of frames to map grant table. Returning |
| 81 | * GNTST_okay means success and negative value means failure. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 82 | */ |
Ian Campbell | ef32f89 | 2012-10-17 09:39:14 +0100 | [diff] [blame] | 83 | int (*map_frames)(xen_pfn_t *frames, unsigned int nr_gframes); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 84 | /* |
| 85 | * Release a list of frames which are mapped in map_frames for grant |
| 86 | * entry status. |
| 87 | */ |
| 88 | void (*unmap_frames)(void); |
| 89 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 90 | * Introducing a valid entry into the grant table, granting the frame of |
| 91 | * this grant entry to domain for accessing or transfering. Ref |
| 92 | * parameter is reference of this introduced grant entry, domid is id of |
| 93 | * granted domain, frame is the page frame to be granted, and flags is |
| 94 | * status of the grant entry to be updated. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 95 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 96 | void (*update_entry)(grant_ref_t ref, domid_t domid, |
| 97 | unsigned long frame, unsigned flags); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 98 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 99 | * Stop granting a grant entry to domain for accessing. Ref parameter is |
| 100 | * reference of a grant entry whose grant access will be stopped, |
| 101 | * readonly is not in use in this function. If the grant entry is |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 102 | * currently mapped for reading or writing, just return failure(==0) |
| 103 | * directly and don't tear down the grant access. Otherwise, stop grant |
| 104 | * access for this entry and return success(==1). |
| 105 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 106 | int (*end_foreign_access_ref)(grant_ref_t ref, int readonly); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 107 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 108 | * Stop granting a grant entry to domain for transfer. Ref parameter is |
| 109 | * reference of a grant entry whose grant transfer will be stopped. If |
| 110 | * tranfer has not started, just reclaim the grant entry and return |
| 111 | * failure(==0). Otherwise, wait for the transfer to complete and then |
| 112 | * return the frame. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 113 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 114 | unsigned long (*end_foreign_transfer_ref)(grant_ref_t ref); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 115 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 116 | * Query the status of a grant entry. Ref parameter is reference of |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 117 | * queried grant entry, return value is the status of queried entry. |
| 118 | * Detailed status(writing/reading) can be gotten from the return value |
| 119 | * by bit operations. |
| 120 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 121 | int (*query_foreign_access)(grant_ref_t ref); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static struct gnttab_ops *gnttab_interface; |
| 125 | |
| 126 | static int grant_table_version; |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 127 | static int grefs_per_grant_frame; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 128 | |
| 129 | static struct gnttab_free_callback *gnttab_free_callback_list; |
| 130 | |
| 131 | static int gnttab_expand(unsigned int req_entries); |
| 132 | |
| 133 | #define RPP (PAGE_SIZE / sizeof(grant_ref_t)) |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 134 | #define SPP (PAGE_SIZE / sizeof(grant_status_t)) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 135 | |
| 136 | static inline grant_ref_t *__gnttab_entry(grant_ref_t entry) |
| 137 | { |
| 138 | return &gnttab_list[(entry) / RPP][(entry) % RPP]; |
| 139 | } |
| 140 | /* This can be used as an l-value */ |
| 141 | #define gnttab_entry(entry) (*__gnttab_entry(entry)) |
| 142 | |
| 143 | static int get_free_entries(unsigned count) |
| 144 | { |
| 145 | unsigned long flags; |
Konrad Rzeszutek Wilk | 272800d | 2011-07-22 14:00:06 -0400 | [diff] [blame] | 146 | int ref, rc = 0; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 147 | grant_ref_t head; |
| 148 | |
| 149 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 150 | |
| 151 | if ((gnttab_free_count < count) && |
| 152 | ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) { |
| 153 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 154 | return rc; |
| 155 | } |
| 156 | |
| 157 | ref = head = gnttab_free_head; |
| 158 | gnttab_free_count -= count; |
| 159 | while (count-- > 1) |
| 160 | head = gnttab_entry(head); |
| 161 | gnttab_free_head = gnttab_entry(head); |
| 162 | gnttab_entry(head) = GNTTAB_LIST_END; |
| 163 | |
| 164 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 165 | |
| 166 | return ref; |
| 167 | } |
| 168 | |
| 169 | static void do_free_callbacks(void) |
| 170 | { |
| 171 | struct gnttab_free_callback *callback, *next; |
| 172 | |
| 173 | callback = gnttab_free_callback_list; |
| 174 | gnttab_free_callback_list = NULL; |
| 175 | |
| 176 | while (callback != NULL) { |
| 177 | next = callback->next; |
| 178 | if (gnttab_free_count >= callback->count) { |
| 179 | callback->next = NULL; |
| 180 | callback->fn(callback->arg); |
| 181 | } else { |
| 182 | callback->next = gnttab_free_callback_list; |
| 183 | gnttab_free_callback_list = callback; |
| 184 | } |
| 185 | callback = next; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static inline void check_free_callbacks(void) |
| 190 | { |
| 191 | if (unlikely(gnttab_free_callback_list)) |
| 192 | do_free_callbacks(); |
| 193 | } |
| 194 | |
| 195 | static void put_free_entry(grant_ref_t ref) |
| 196 | { |
| 197 | unsigned long flags; |
| 198 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 199 | gnttab_entry(ref) = gnttab_free_head; |
| 200 | gnttab_free_head = ref; |
| 201 | gnttab_free_count++; |
| 202 | check_free_callbacks(); |
| 203 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 204 | } |
| 205 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 206 | /* |
David Vrabel | 438b33c | 2014-07-02 11:25:29 +0100 | [diff] [blame] | 207 | * Following applies to gnttab_update_entry_v1. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 208 | * Introducing a valid entry into the grant table: |
| 209 | * 1. Write ent->domid. |
| 210 | * 2. Write ent->frame: |
| 211 | * GTF_permit_access: Frame to which access is permitted. |
| 212 | * GTF_accept_transfer: Pseudo-phys frame slot being filled by new |
| 213 | * frame, or zero if none. |
| 214 | * 3. Write memory barrier (WMB). |
| 215 | * 4. Write ent->flags, inc. valid type. |
| 216 | */ |
| 217 | static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid, |
| 218 | unsigned long frame, unsigned flags) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 219 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 220 | gnttab_shared.v1[ref].domid = domid; |
| 221 | gnttab_shared.v1[ref].frame = frame; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 222 | wmb(); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 223 | gnttab_shared.v1[ref].flags = flags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Public grant-issuing interface functions |
| 228 | */ |
| 229 | void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid, |
| 230 | unsigned long frame, int readonly) |
| 231 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 232 | gnttab_interface->update_entry(ref, domid, frame, |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 233 | GTF_permit_access | (readonly ? GTF_readonly : 0)); |
| 234 | } |
| 235 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref); |
| 236 | |
| 237 | int gnttab_grant_foreign_access(domid_t domid, unsigned long frame, |
| 238 | int readonly) |
| 239 | { |
| 240 | int ref; |
| 241 | |
| 242 | ref = get_free_entries(1); |
| 243 | if (unlikely(ref < 0)) |
| 244 | return -ENOSPC; |
| 245 | |
| 246 | gnttab_grant_foreign_access_ref(ref, domid, frame, readonly); |
| 247 | |
| 248 | return ref; |
| 249 | } |
| 250 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access); |
| 251 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 252 | static int gnttab_query_foreign_access_v1(grant_ref_t ref) |
| 253 | { |
| 254 | return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing); |
| 255 | } |
| 256 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 257 | int gnttab_query_foreign_access(grant_ref_t ref) |
| 258 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 259 | return gnttab_interface->query_foreign_access(ref); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 260 | } |
| 261 | EXPORT_SYMBOL_GPL(gnttab_query_foreign_access); |
| 262 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 263 | static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 264 | { |
| 265 | u16 flags, nflags; |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 266 | u16 *pflags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 267 | |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 268 | pflags = &gnttab_shared.v1[ref].flags; |
| 269 | nflags = *pflags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 270 | do { |
| 271 | flags = nflags; |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 272 | if (flags & (GTF_reading|GTF_writing)) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 273 | return 0; |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 274 | } while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 275 | |
| 276 | return 1; |
| 277 | } |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 278 | |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 279 | static inline int _gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly) |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 280 | { |
| 281 | return gnttab_interface->end_foreign_access_ref(ref, readonly); |
| 282 | } |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 283 | |
| 284 | int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly) |
| 285 | { |
| 286 | if (_gnttab_end_foreign_access_ref(ref, readonly)) |
| 287 | return 1; |
| 288 | pr_warn("WARNING: g.e. %#x still in use!\n", ref); |
| 289 | return 0; |
| 290 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 291 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref); |
| 292 | |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 293 | struct deferred_entry { |
| 294 | struct list_head list; |
| 295 | grant_ref_t ref; |
| 296 | bool ro; |
| 297 | uint16_t warn_delay; |
| 298 | struct page *page; |
| 299 | }; |
| 300 | static LIST_HEAD(deferred_list); |
| 301 | static void gnttab_handle_deferred(unsigned long); |
| 302 | static DEFINE_TIMER(deferred_timer, gnttab_handle_deferred, 0, 0); |
| 303 | |
| 304 | static void gnttab_handle_deferred(unsigned long unused) |
| 305 | { |
| 306 | unsigned int nr = 10; |
| 307 | struct deferred_entry *first = NULL; |
| 308 | unsigned long flags; |
| 309 | |
| 310 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 311 | while (nr--) { |
| 312 | struct deferred_entry *entry |
| 313 | = list_first_entry(&deferred_list, |
| 314 | struct deferred_entry, list); |
| 315 | |
| 316 | if (entry == first) |
| 317 | break; |
| 318 | list_del(&entry->list); |
| 319 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 320 | if (_gnttab_end_foreign_access_ref(entry->ref, entry->ro)) { |
| 321 | put_free_entry(entry->ref); |
| 322 | if (entry->page) { |
| 323 | pr_debug("freeing g.e. %#x (pfn %#lx)\n", |
| 324 | entry->ref, page_to_pfn(entry->page)); |
| 325 | __free_page(entry->page); |
| 326 | } else |
| 327 | pr_info("freeing g.e. %#x\n", entry->ref); |
| 328 | kfree(entry); |
| 329 | entry = NULL; |
| 330 | } else { |
| 331 | if (!--entry->warn_delay) |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 332 | pr_info("g.e. %#x still pending\n", entry->ref); |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 333 | if (!first) |
| 334 | first = entry; |
| 335 | } |
| 336 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 337 | if (entry) |
| 338 | list_add_tail(&entry->list, &deferred_list); |
| 339 | else if (list_empty(&deferred_list)) |
| 340 | break; |
| 341 | } |
| 342 | if (!list_empty(&deferred_list) && !timer_pending(&deferred_timer)) { |
| 343 | deferred_timer.expires = jiffies + HZ; |
| 344 | add_timer(&deferred_timer); |
| 345 | } |
| 346 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 347 | } |
| 348 | |
| 349 | static void gnttab_add_deferred(grant_ref_t ref, bool readonly, |
| 350 | struct page *page) |
| 351 | { |
| 352 | struct deferred_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
| 353 | const char *what = KERN_WARNING "leaking"; |
| 354 | |
| 355 | if (entry) { |
| 356 | unsigned long flags; |
| 357 | |
| 358 | entry->ref = ref; |
| 359 | entry->ro = readonly; |
| 360 | entry->page = page; |
| 361 | entry->warn_delay = 60; |
| 362 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 363 | list_add_tail(&entry->list, &deferred_list); |
| 364 | if (!timer_pending(&deferred_timer)) { |
| 365 | deferred_timer.expires = jiffies + HZ; |
| 366 | add_timer(&deferred_timer); |
| 367 | } |
| 368 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 369 | what = KERN_DEBUG "deferring"; |
| 370 | } |
| 371 | printk("%s g.e. %#x (pfn %#lx)\n", |
| 372 | what, ref, page ? page_to_pfn(page) : -1); |
| 373 | } |
| 374 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 375 | void gnttab_end_foreign_access(grant_ref_t ref, int readonly, |
| 376 | unsigned long page) |
| 377 | { |
| 378 | if (gnttab_end_foreign_access_ref(ref, readonly)) { |
| 379 | put_free_entry(ref); |
| 380 | if (page != 0) |
| 381 | free_page(page); |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 382 | } else |
| 383 | gnttab_add_deferred(ref, readonly, |
| 384 | page ? virt_to_page(page) : NULL); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 385 | } |
| 386 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_access); |
| 387 | |
| 388 | int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn) |
| 389 | { |
| 390 | int ref; |
| 391 | |
| 392 | ref = get_free_entries(1); |
| 393 | if (unlikely(ref < 0)) |
| 394 | return -ENOSPC; |
| 395 | gnttab_grant_foreign_transfer_ref(ref, domid, pfn); |
| 396 | |
| 397 | return ref; |
| 398 | } |
| 399 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer); |
| 400 | |
| 401 | void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid, |
| 402 | unsigned long pfn) |
| 403 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 404 | gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 405 | } |
| 406 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref); |
| 407 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 408 | static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 409 | { |
| 410 | unsigned long frame; |
| 411 | u16 flags; |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 412 | u16 *pflags; |
| 413 | |
| 414 | pflags = &gnttab_shared.v1[ref].flags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | * If a transfer is not even yet started, try to reclaim the grant |
| 418 | * reference and return failure (== 0). |
| 419 | */ |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 420 | while (!((flags = *pflags) & GTF_transfer_committed)) { |
| 421 | if (sync_cmpxchg(pflags, flags, 0) == flags) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 422 | return 0; |
| 423 | cpu_relax(); |
| 424 | } |
| 425 | |
| 426 | /* If a transfer is in progress then wait until it is completed. */ |
| 427 | while (!(flags & GTF_transfer_completed)) { |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 428 | flags = *pflags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 429 | cpu_relax(); |
| 430 | } |
| 431 | |
| 432 | rmb(); /* Read the frame number /after/ reading completion status. */ |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 433 | frame = gnttab_shared.v1[ref].frame; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 434 | BUG_ON(frame == 0); |
| 435 | |
| 436 | return frame; |
| 437 | } |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 438 | |
| 439 | unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref) |
| 440 | { |
| 441 | return gnttab_interface->end_foreign_transfer_ref(ref); |
| 442 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 443 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref); |
| 444 | |
| 445 | unsigned long gnttab_end_foreign_transfer(grant_ref_t ref) |
| 446 | { |
| 447 | unsigned long frame = gnttab_end_foreign_transfer_ref(ref); |
| 448 | put_free_entry(ref); |
| 449 | return frame; |
| 450 | } |
| 451 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer); |
| 452 | |
| 453 | void gnttab_free_grant_reference(grant_ref_t ref) |
| 454 | { |
| 455 | put_free_entry(ref); |
| 456 | } |
| 457 | EXPORT_SYMBOL_GPL(gnttab_free_grant_reference); |
| 458 | |
| 459 | void gnttab_free_grant_references(grant_ref_t head) |
| 460 | { |
| 461 | grant_ref_t ref; |
| 462 | unsigned long flags; |
| 463 | int count = 1; |
| 464 | if (head == GNTTAB_LIST_END) |
| 465 | return; |
| 466 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 467 | ref = head; |
| 468 | while (gnttab_entry(ref) != GNTTAB_LIST_END) { |
| 469 | ref = gnttab_entry(ref); |
| 470 | count++; |
| 471 | } |
| 472 | gnttab_entry(ref) = gnttab_free_head; |
| 473 | gnttab_free_head = head; |
| 474 | gnttab_free_count += count; |
| 475 | check_free_callbacks(); |
| 476 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 477 | } |
| 478 | EXPORT_SYMBOL_GPL(gnttab_free_grant_references); |
| 479 | |
| 480 | int gnttab_alloc_grant_references(u16 count, grant_ref_t *head) |
| 481 | { |
| 482 | int h = get_free_entries(count); |
| 483 | |
| 484 | if (h < 0) |
| 485 | return -ENOSPC; |
| 486 | |
| 487 | *head = h; |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references); |
| 492 | |
| 493 | int gnttab_empty_grant_references(const grant_ref_t *private_head) |
| 494 | { |
| 495 | return (*private_head == GNTTAB_LIST_END); |
| 496 | } |
| 497 | EXPORT_SYMBOL_GPL(gnttab_empty_grant_references); |
| 498 | |
| 499 | int gnttab_claim_grant_reference(grant_ref_t *private_head) |
| 500 | { |
| 501 | grant_ref_t g = *private_head; |
| 502 | if (unlikely(g == GNTTAB_LIST_END)) |
| 503 | return -ENOSPC; |
| 504 | *private_head = gnttab_entry(g); |
| 505 | return g; |
| 506 | } |
| 507 | EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference); |
| 508 | |
| 509 | void gnttab_release_grant_reference(grant_ref_t *private_head, |
| 510 | grant_ref_t release) |
| 511 | { |
| 512 | gnttab_entry(release) = *private_head; |
| 513 | *private_head = release; |
| 514 | } |
| 515 | EXPORT_SYMBOL_GPL(gnttab_release_grant_reference); |
| 516 | |
| 517 | void gnttab_request_free_callback(struct gnttab_free_callback *callback, |
| 518 | void (*fn)(void *), void *arg, u16 count) |
| 519 | { |
| 520 | unsigned long flags; |
Roger Pau Monne | 5f338d9 | 2013-07-31 17:00:42 +0200 | [diff] [blame] | 521 | struct gnttab_free_callback *cb; |
| 522 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 523 | spin_lock_irqsave(&gnttab_list_lock, flags); |
Roger Pau Monne | 5f338d9 | 2013-07-31 17:00:42 +0200 | [diff] [blame] | 524 | |
| 525 | /* Check if the callback is already on the list */ |
| 526 | cb = gnttab_free_callback_list; |
| 527 | while (cb) { |
| 528 | if (cb == callback) |
| 529 | goto out; |
| 530 | cb = cb->next; |
| 531 | } |
| 532 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 533 | callback->fn = fn; |
| 534 | callback->arg = arg; |
| 535 | callback->count = count; |
| 536 | callback->next = gnttab_free_callback_list; |
| 537 | gnttab_free_callback_list = callback; |
| 538 | check_free_callbacks(); |
| 539 | out: |
| 540 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 541 | } |
| 542 | EXPORT_SYMBOL_GPL(gnttab_request_free_callback); |
| 543 | |
| 544 | void gnttab_cancel_free_callback(struct gnttab_free_callback *callback) |
| 545 | { |
| 546 | struct gnttab_free_callback **pcb; |
| 547 | unsigned long flags; |
| 548 | |
| 549 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 550 | for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) { |
| 551 | if (*pcb == callback) { |
| 552 | *pcb = callback->next; |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 557 | } |
| 558 | EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback); |
| 559 | |
| 560 | static int grow_gnttab_list(unsigned int more_frames) |
| 561 | { |
| 562 | unsigned int new_nr_grant_frames, extra_entries, i; |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 563 | unsigned int nr_glist_frames, new_nr_glist_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 564 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 565 | BUG_ON(grefs_per_grant_frame == 0); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 566 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 567 | new_nr_grant_frames = nr_grant_frames + more_frames; |
| 568 | extra_entries = more_frames * grefs_per_grant_frame; |
| 569 | |
| 570 | nr_glist_frames = (nr_grant_frames * grefs_per_grant_frame + RPP - 1) / RPP; |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 571 | new_nr_glist_frames = |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 572 | (new_nr_grant_frames * grefs_per_grant_frame + RPP - 1) / RPP; |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 573 | for (i = nr_glist_frames; i < new_nr_glist_frames; i++) { |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 574 | gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC); |
| 575 | if (!gnttab_list[i]) |
| 576 | goto grow_nomem; |
| 577 | } |
| 578 | |
| 579 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 580 | for (i = grefs_per_grant_frame * nr_grant_frames; |
| 581 | i < grefs_per_grant_frame * new_nr_grant_frames - 1; i++) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 582 | gnttab_entry(i) = i + 1; |
| 583 | |
| 584 | gnttab_entry(i) = gnttab_free_head; |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 585 | gnttab_free_head = grefs_per_grant_frame * nr_grant_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 586 | gnttab_free_count += extra_entries; |
| 587 | |
| 588 | nr_grant_frames = new_nr_grant_frames; |
| 589 | |
| 590 | check_free_callbacks(); |
| 591 | |
| 592 | return 0; |
| 593 | |
| 594 | grow_nomem: |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 595 | for ( ; i >= nr_glist_frames; i--) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 596 | free_page((unsigned long) gnttab_list[i]); |
| 597 | return -ENOMEM; |
| 598 | } |
| 599 | |
| 600 | static unsigned int __max_nr_grant_frames(void) |
| 601 | { |
| 602 | struct gnttab_query_size query; |
| 603 | int rc; |
| 604 | |
| 605 | query.dom = DOMID_SELF; |
| 606 | |
| 607 | rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1); |
| 608 | if ((rc < 0) || (query.status != GNTST_okay)) |
| 609 | return 4; /* Legacy max supported number of frames */ |
| 610 | |
| 611 | return query.max_nr_frames; |
| 612 | } |
| 613 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 614 | unsigned int gnttab_max_grant_frames(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 615 | { |
| 616 | unsigned int xen_max = __max_nr_grant_frames(); |
Konrad Rzeszutek Wilk | 7f25602 | 2013-12-31 15:55:39 -0500 | [diff] [blame] | 617 | static unsigned int boot_max_nr_grant_frames; |
| 618 | |
| 619 | /* First time, initialize it properly. */ |
| 620 | if (!boot_max_nr_grant_frames) |
| 621 | boot_max_nr_grant_frames = __max_nr_grant_frames(); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 622 | |
| 623 | if (xen_max > boot_max_nr_grant_frames) |
| 624 | return boot_max_nr_grant_frames; |
| 625 | return xen_max; |
| 626 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 627 | EXPORT_SYMBOL_GPL(gnttab_max_grant_frames); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 628 | |
Julien Grall | 47c5420 | 2014-01-30 12:56:34 +0000 | [diff] [blame] | 629 | int gnttab_setup_auto_xlat_frames(phys_addr_t addr) |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 630 | { |
| 631 | xen_pfn_t *pfn; |
| 632 | unsigned int max_nr_gframes = __max_nr_grant_frames(); |
| 633 | unsigned int i; |
| 634 | void *vaddr; |
| 635 | |
| 636 | if (xen_auto_xlat_grant_frames.count) |
| 637 | return -EINVAL; |
| 638 | |
| 639 | vaddr = xen_remap(addr, PAGE_SIZE * max_nr_gframes); |
| 640 | if (vaddr == NULL) { |
Julien Grall | 47c5420 | 2014-01-30 12:56:34 +0000 | [diff] [blame] | 641 | pr_warn("Failed to ioremap gnttab share frames (addr=%pa)!\n", |
| 642 | &addr); |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 643 | return -ENOMEM; |
| 644 | } |
| 645 | pfn = kcalloc(max_nr_gframes, sizeof(pfn[0]), GFP_KERNEL); |
| 646 | if (!pfn) { |
| 647 | xen_unmap(vaddr); |
| 648 | return -ENOMEM; |
| 649 | } |
| 650 | for (i = 0; i < max_nr_gframes; i++) |
| 651 | pfn[i] = PFN_DOWN(addr) + i; |
| 652 | |
| 653 | xen_auto_xlat_grant_frames.vaddr = vaddr; |
| 654 | xen_auto_xlat_grant_frames.pfn = pfn; |
| 655 | xen_auto_xlat_grant_frames.count = max_nr_gframes; |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | EXPORT_SYMBOL_GPL(gnttab_setup_auto_xlat_frames); |
| 660 | |
| 661 | void gnttab_free_auto_xlat_frames(void) |
| 662 | { |
| 663 | if (!xen_auto_xlat_grant_frames.count) |
| 664 | return; |
| 665 | kfree(xen_auto_xlat_grant_frames.pfn); |
| 666 | xen_unmap(xen_auto_xlat_grant_frames.vaddr); |
| 667 | |
| 668 | xen_auto_xlat_grant_frames.pfn = NULL; |
| 669 | xen_auto_xlat_grant_frames.count = 0; |
| 670 | xen_auto_xlat_grant_frames.vaddr = NULL; |
| 671 | } |
| 672 | EXPORT_SYMBOL_GPL(gnttab_free_auto_xlat_frames); |
| 673 | |
Andres Lagar-Cavilla | c571898 | 2012-09-14 14:26:59 +0000 | [diff] [blame] | 674 | /* Handling of paged out grant targets (GNTST_eagain) */ |
| 675 | #define MAX_DELAY 256 |
| 676 | static inline void |
| 677 | gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status, |
| 678 | const char *func) |
| 679 | { |
| 680 | unsigned delay = 1; |
| 681 | |
| 682 | do { |
| 683 | BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1)); |
| 684 | if (*status == GNTST_eagain) |
| 685 | msleep(delay++); |
| 686 | } while ((*status == GNTST_eagain) && (delay < MAX_DELAY)); |
| 687 | |
| 688 | if (delay >= MAX_DELAY) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 689 | pr_err("%s: %s eagain grant\n", func, current->comm); |
Andres Lagar-Cavilla | c571898 | 2012-09-14 14:26:59 +0000 | [diff] [blame] | 690 | *status = GNTST_bad_page; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count) |
| 695 | { |
| 696 | struct gnttab_map_grant_ref *op; |
| 697 | |
| 698 | if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, batch, count)) |
| 699 | BUG(); |
| 700 | for (op = batch; op < batch + count; op++) |
| 701 | if (op->status == GNTST_eagain) |
| 702 | gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, op, |
| 703 | &op->status, __func__); |
| 704 | } |
| 705 | EXPORT_SYMBOL_GPL(gnttab_batch_map); |
| 706 | |
| 707 | void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count) |
| 708 | { |
| 709 | struct gnttab_copy *op; |
| 710 | |
| 711 | if (HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count)) |
| 712 | BUG(); |
| 713 | for (op = batch; op < batch + count; op++) |
| 714 | if (op->status == GNTST_eagain) |
| 715 | gnttab_retry_eagain_gop(GNTTABOP_copy, op, |
| 716 | &op->status, __func__); |
| 717 | } |
| 718 | EXPORT_SYMBOL_GPL(gnttab_batch_copy); |
| 719 | |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 720 | int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops, |
Annie Li | c123799 | 2011-11-22 09:59:56 +0800 | [diff] [blame] | 721 | struct gnttab_map_grant_ref *kmap_ops, |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 722 | struct page **pages, unsigned int count) |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 723 | { |
| 724 | int i, ret; |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 725 | |
| 726 | ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count); |
Jeremy Fitzhardinge | 87f1d40 | 2010-12-13 14:42:30 +0000 | [diff] [blame] | 727 | if (ret) |
| 728 | return ret; |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 729 | |
Andres Lagar-Cavilla | c571898 | 2012-09-14 14:26:59 +0000 | [diff] [blame] | 730 | /* Retry eagain maps */ |
| 731 | for (i = 0; i < count; i++) |
| 732 | if (map_ops[i].status == GNTST_eagain) |
| 733 | gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, map_ops + i, |
| 734 | &map_ops[i].status, __func__); |
| 735 | |
Zoltan Kiss | 1429d46 | 2014-02-27 15:55:30 +0000 | [diff] [blame] | 736 | return set_foreign_p2m_mapping(map_ops, kmap_ops, pages, count); |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 737 | } |
| 738 | EXPORT_SYMBOL_GPL(gnttab_map_refs); |
| 739 | |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 740 | int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops, |
Stefano Stabellini | 2fc136e | 2012-09-12 12:44:30 +0100 | [diff] [blame] | 741 | struct gnttab_map_grant_ref *kmap_ops, |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 742 | struct page **pages, unsigned int count) |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 743 | { |
Zoltan Kiss | 1429d46 | 2014-02-27 15:55:30 +0000 | [diff] [blame] | 744 | int ret; |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 745 | |
| 746 | ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count); |
Jeremy Fitzhardinge | 87f1d40 | 2010-12-13 14:42:30 +0000 | [diff] [blame] | 747 | if (ret) |
| 748 | return ret; |
| 749 | |
Zoltan Kiss | 1429d46 | 2014-02-27 15:55:30 +0000 | [diff] [blame] | 750 | return clear_foreign_p2m_mapping(unmap_ops, kmap_ops, pages, count); |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 751 | } |
| 752 | EXPORT_SYMBOL_GPL(gnttab_unmap_refs); |
| 753 | |
Ian Campbell | ef32f89 | 2012-10-17 09:39:14 +0100 | [diff] [blame] | 754 | static int gnttab_map_frames_v1(xen_pfn_t *frames, unsigned int nr_gframes) |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 755 | { |
| 756 | int rc; |
| 757 | |
| 758 | rc = arch_gnttab_map_shared(frames, nr_gframes, |
| 759 | gnttab_max_grant_frames(), |
| 760 | &gnttab_shared.addr); |
| 761 | BUG_ON(rc); |
| 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static void gnttab_unmap_frames_v1(void) |
| 767 | { |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 768 | arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames); |
| 769 | } |
| 770 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 771 | static int gnttab_map(unsigned int start_idx, unsigned int end_idx) |
| 772 | { |
| 773 | struct gnttab_setup_table setup; |
Ian Campbell | ef32f89 | 2012-10-17 09:39:14 +0100 | [diff] [blame] | 774 | xen_pfn_t *frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 775 | unsigned int nr_gframes = end_idx + 1; |
| 776 | int rc; |
| 777 | |
Konrad Rzeszutek Wilk | 6926f6d | 2014-01-03 10:20:18 -0500 | [diff] [blame] | 778 | if (xen_feature(XENFEAT_auto_translated_physmap)) { |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 779 | struct xen_add_to_physmap xatp; |
| 780 | unsigned int i = end_idx; |
| 781 | rc = 0; |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 782 | BUG_ON(xen_auto_xlat_grant_frames.count < nr_gframes); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 783 | /* |
| 784 | * Loop backwards, so that the first hypercall has the largest |
| 785 | * index, ensuring that the table will grow only once. |
| 786 | */ |
| 787 | do { |
| 788 | xatp.domid = DOMID_SELF; |
| 789 | xatp.idx = i; |
| 790 | xatp.space = XENMAPSPACE_grant_table; |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 791 | xatp.gpfn = xen_auto_xlat_grant_frames.pfn[i]; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 792 | rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp); |
| 793 | if (rc != 0) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 794 | pr_warn("grant table add_to_physmap failed, err=%d\n", |
| 795 | rc); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 796 | break; |
| 797 | } |
| 798 | } while (i-- > start_idx); |
| 799 | |
| 800 | return rc; |
| 801 | } |
| 802 | |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 803 | /* No need for kzalloc as it is initialized in following hypercall |
| 804 | * GNTTABOP_setup_table. |
| 805 | */ |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 806 | frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC); |
| 807 | if (!frames) |
| 808 | return -ENOMEM; |
| 809 | |
| 810 | setup.dom = DOMID_SELF; |
| 811 | setup.nr_frames = nr_gframes; |
Isaku Yamahata | 87e27cf | 2008-04-02 10:53:52 -0700 | [diff] [blame] | 812 | set_xen_guest_handle(setup.frame_list, frames); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 813 | |
| 814 | rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1); |
| 815 | if (rc == -ENOSYS) { |
| 816 | kfree(frames); |
| 817 | return -ENOSYS; |
| 818 | } |
| 819 | |
| 820 | BUG_ON(rc || setup.status); |
| 821 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 822 | rc = gnttab_interface->map_frames(frames, nr_gframes); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 823 | |
| 824 | kfree(frames); |
| 825 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 826 | return rc; |
| 827 | } |
| 828 | |
| 829 | static struct gnttab_ops gnttab_v1_ops = { |
| 830 | .map_frames = gnttab_map_frames_v1, |
| 831 | .unmap_frames = gnttab_unmap_frames_v1, |
| 832 | .update_entry = gnttab_update_entry_v1, |
| 833 | .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1, |
| 834 | .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1, |
| 835 | .query_foreign_access = gnttab_query_foreign_access_v1, |
| 836 | }; |
| 837 | |
| 838 | static void gnttab_request_version(void) |
| 839 | { |
David Vrabel | 438b33c | 2014-07-02 11:25:29 +0100 | [diff] [blame] | 840 | /* Only version 1 is used, which will always be available. */ |
| 841 | grant_table_version = 1; |
| 842 | grefs_per_grant_frame = PAGE_SIZE / sizeof(struct grant_entry_v1); |
| 843 | gnttab_interface = &gnttab_v1_ops; |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 844 | |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 845 | pr_info("Grant tables using version %d layout\n", grant_table_version); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 848 | static int gnttab_setup(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 849 | { |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 850 | unsigned int max_nr_gframes; |
| 851 | |
| 852 | max_nr_gframes = gnttab_max_grant_frames(); |
| 853 | if (max_nr_gframes < nr_grant_frames) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 854 | return -ENOSYS; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 855 | |
Konrad Rzeszutek Wilk | 4568475 | 2013-12-31 16:33:31 -0500 | [diff] [blame] | 856 | if (xen_feature(XENFEAT_auto_translated_physmap) && gnttab_shared.addr == NULL) { |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 857 | gnttab_shared.addr = xen_auto_xlat_grant_frames.vaddr; |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 858 | if (gnttab_shared.addr == NULL) { |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 859 | pr_warn("gnttab share frames (addr=0x%08lx) is not mapped!\n", |
| 860 | (unsigned long)xen_auto_xlat_grant_frames.vaddr); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 861 | return -ENOMEM; |
| 862 | } |
| 863 | } |
Konrad Rzeszutek Wilk | 4568475 | 2013-12-31 16:33:31 -0500 | [diff] [blame] | 864 | return gnttab_map(0, nr_grant_frames - 1); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 867 | int gnttab_resume(void) |
| 868 | { |
| 869 | gnttab_request_version(); |
| 870 | return gnttab_setup(); |
| 871 | } |
| 872 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 873 | int gnttab_suspend(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 874 | { |
David Vrabel | 13cd36a | 2014-06-16 11:12:03 +0100 | [diff] [blame] | 875 | if (!xen_feature(XENFEAT_auto_translated_physmap)) |
| 876 | gnttab_interface->unmap_frames(); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | static int gnttab_expand(unsigned int req_entries) |
| 881 | { |
| 882 | int rc; |
| 883 | unsigned int cur, extra; |
| 884 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 885 | BUG_ON(grefs_per_grant_frame == 0); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 886 | cur = nr_grant_frames; |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 887 | extra = ((req_entries + (grefs_per_grant_frame-1)) / |
| 888 | grefs_per_grant_frame); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 889 | if (cur + extra > gnttab_max_grant_frames()) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 890 | return -ENOSPC; |
| 891 | |
| 892 | rc = gnttab_map(cur, cur + extra - 1); |
| 893 | if (rc == 0) |
| 894 | rc = grow_gnttab_list(extra); |
| 895 | |
| 896 | return rc; |
| 897 | } |
| 898 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 899 | int gnttab_init(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 900 | { |
| 901 | int i; |
David Vrabel | 162e371 | 2014-07-11 16:42:34 +0100 | [diff] [blame] | 902 | unsigned long max_nr_grant_frames; |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 903 | unsigned int max_nr_glist_frames, nr_glist_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 904 | unsigned int nr_init_grefs; |
Julia Lawall | 6b5e7d9 | 2012-04-15 11:27:12 +0200 | [diff] [blame] | 905 | int ret; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 906 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 907 | gnttab_request_version(); |
David Vrabel | 162e371 | 2014-07-11 16:42:34 +0100 | [diff] [blame] | 908 | max_nr_grant_frames = gnttab_max_grant_frames(); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 909 | nr_grant_frames = 1; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 910 | |
| 911 | /* Determine the maximum number of frames required for the |
| 912 | * grant reference free list on the current hypervisor. |
| 913 | */ |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 914 | BUG_ON(grefs_per_grant_frame == 0); |
David Vrabel | 162e371 | 2014-07-11 16:42:34 +0100 | [diff] [blame] | 915 | max_nr_glist_frames = (max_nr_grant_frames * |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 916 | grefs_per_grant_frame / RPP); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 917 | |
| 918 | gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *), |
| 919 | GFP_KERNEL); |
| 920 | if (gnttab_list == NULL) |
| 921 | return -ENOMEM; |
| 922 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 923 | nr_glist_frames = (nr_grant_frames * grefs_per_grant_frame + RPP - 1) / RPP; |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 924 | for (i = 0; i < nr_glist_frames; i++) { |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 925 | gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL); |
Julia Lawall | 6b5e7d9 | 2012-04-15 11:27:12 +0200 | [diff] [blame] | 926 | if (gnttab_list[i] == NULL) { |
| 927 | ret = -ENOMEM; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 928 | goto ini_nomem; |
Julia Lawall | 6b5e7d9 | 2012-04-15 11:27:12 +0200 | [diff] [blame] | 929 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 930 | } |
| 931 | |
David Vrabel | 438b33c | 2014-07-02 11:25:29 +0100 | [diff] [blame] | 932 | ret = arch_gnttab_init(max_nr_grant_frames); |
David Vrabel | 162e371 | 2014-07-11 16:42:34 +0100 | [diff] [blame] | 933 | if (ret < 0) |
| 934 | goto ini_nomem; |
| 935 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 936 | if (gnttab_setup() < 0) { |
Julia Lawall | 6b5e7d9 | 2012-04-15 11:27:12 +0200 | [diff] [blame] | 937 | ret = -ENODEV; |
| 938 | goto ini_nomem; |
| 939 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 940 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 941 | nr_init_grefs = nr_grant_frames * grefs_per_grant_frame; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 942 | |
| 943 | for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++) |
| 944 | gnttab_entry(i) = i + 1; |
| 945 | |
| 946 | gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END; |
| 947 | gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES; |
| 948 | gnttab_free_head = NR_RESERVED_ENTRIES; |
| 949 | |
| 950 | printk("Grant table initialized\n"); |
| 951 | return 0; |
| 952 | |
| 953 | ini_nomem: |
| 954 | for (i--; i >= 0; i--) |
| 955 | free_page((unsigned long)gnttab_list[i]); |
| 956 | kfree(gnttab_list); |
Julia Lawall | 6b5e7d9 | 2012-04-15 11:27:12 +0200 | [diff] [blame] | 957 | return ret; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 958 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 959 | EXPORT_SYMBOL_GPL(gnttab_init); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 960 | |
Greg Kroah-Hartman | 345a525 | 2012-12-21 13:00:00 -0800 | [diff] [blame] | 961 | static int __gnttab_init(void) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 962 | { |
| 963 | /* Delay grant-table initialization in the PV on HVM case */ |
| 964 | if (xen_hvm_domain()) |
| 965 | return 0; |
| 966 | |
| 967 | if (!xen_pv_domain()) |
| 968 | return -ENODEV; |
| 969 | |
| 970 | return gnttab_init(); |
| 971 | } |
Konrad Rzeszutek Wilk | 6926f6d | 2014-01-03 10:20:18 -0500 | [diff] [blame] | 972 | /* Starts after core_initcall so that xen_pvh_gnttab_setup can be called |
| 973 | * beforehand to initialize xen_auto_xlat_grant_frames. */ |
| 974 | core_initcall_sync(__gnttab_init); |