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/sched.h> |
| 37 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/slab.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 39 | #include <linux/vmalloc.h> |
| 40 | #include <linux/uaccess.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 41 | #include <linux/io.h> |
Andres Lagar-Cavilla | c571898 | 2012-09-14 14:26:59 +0000 | [diff] [blame] | 42 | #include <linux/delay.h> |
Stefano Stabellini | f62805f | 2012-04-24 11:55:43 +0100 | [diff] [blame] | 43 | #include <linux/hardirq.h> |
Jennifer Herbert | 3f9f1c6 | 2014-12-09 18:28:37 +0000 | [diff] [blame] | 44 | #include <linux/workqueue.h> |
Wengang Wang | 29d11cfd | 2017-07-18 09:40:35 +0200 | [diff] [blame] | 45 | #include <linux/ratelimit.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 46 | |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 47 | #include <xen/xen.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 48 | #include <xen/interface/xen.h> |
| 49 | #include <xen/page.h> |
| 50 | #include <xen/grant_table.h> |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 51 | #include <xen/interface/memory.h> |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 52 | #include <xen/hvc-console.h> |
Stefano Stabellini | 3d24bbd | 2013-10-25 10:41:44 +0000 | [diff] [blame] | 53 | #include <xen/swiotlb-xen.h> |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 54 | #include <xen/balloon.h> |
Jeremy Fitzhardinge | ecbf29c | 2008-12-16 12:37:07 -0800 | [diff] [blame] | 55 | #include <asm/xen/hypercall.h> |
Stefano Stabellini | 4d9310e | 2012-08-06 15:27:09 +0100 | [diff] [blame] | 56 | #include <asm/xen/interface.h> |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 57 | |
| 58 | #include <asm/pgtable.h> |
| 59 | #include <asm/sync_bitops.h> |
| 60 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 61 | /* External tools reserve first few grant table entries. */ |
| 62 | #define NR_RESERVED_ENTRIES 8 |
| 63 | #define GNTTAB_LIST_END 0xffffffff |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 64 | |
| 65 | static grant_ref_t **gnttab_list; |
| 66 | static unsigned int nr_grant_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 67 | static int gnttab_free_count; |
| 68 | static grant_ref_t gnttab_free_head; |
| 69 | static DEFINE_SPINLOCK(gnttab_list_lock); |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 70 | struct grant_frames xen_auto_xlat_grant_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 71 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 72 | static union { |
| 73 | struct grant_entry_v1 *v1; |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 74 | union grant_entry_v2 *v2; |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 75 | void *addr; |
| 76 | } gnttab_shared; |
| 77 | |
| 78 | /*This is a structure of function pointers for grant table*/ |
| 79 | struct gnttab_ops { |
| 80 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 81 | * Mapping a list of frames for storing grant entries. Frames parameter |
| 82 | * is used to store grant table address when grant table being setup, |
| 83 | * nr_gframes is the number of frames to map grant table. Returning |
| 84 | * GNTST_okay means success and negative value means failure. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 85 | */ |
Ian Campbell | ef32f89 | 2012-10-17 09:39:14 +0100 | [diff] [blame] | 86 | int (*map_frames)(xen_pfn_t *frames, unsigned int nr_gframes); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 87 | /* |
| 88 | * Release a list of frames which are mapped in map_frames for grant |
| 89 | * entry status. |
| 90 | */ |
| 91 | void (*unmap_frames)(void); |
| 92 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 93 | * Introducing a valid entry into the grant table, granting the frame of |
| 94 | * this grant entry to domain for accessing or transfering. Ref |
| 95 | * parameter is reference of this introduced grant entry, domid is id of |
| 96 | * granted domain, frame is the page frame to be granted, and flags is |
| 97 | * status of the grant entry to be updated. |
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 | void (*update_entry)(grant_ref_t ref, domid_t domid, |
| 100 | unsigned long frame, unsigned flags); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 101 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 102 | * Stop granting a grant entry to domain for accessing. Ref parameter is |
| 103 | * reference of a grant entry whose grant access will be stopped, |
| 104 | * 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] | 105 | * currently mapped for reading or writing, just return failure(==0) |
| 106 | * directly and don't tear down the grant access. Otherwise, stop grant |
| 107 | * access for this entry and return success(==1). |
| 108 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 109 | int (*end_foreign_access_ref)(grant_ref_t ref, int readonly); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 110 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 111 | * Stop granting a grant entry to domain for transfer. Ref parameter is |
| 112 | * reference of a grant entry whose grant transfer will be stopped. If |
| 113 | * tranfer has not started, just reclaim the grant entry and return |
| 114 | * failure(==0). Otherwise, wait for the transfer to complete and then |
| 115 | * return the frame. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 116 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 117 | unsigned long (*end_foreign_transfer_ref)(grant_ref_t ref); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 118 | /* |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 119 | * Query the status of a grant entry. Ref parameter is reference of |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 120 | * queried grant entry, return value is the status of queried entry. |
| 121 | * Detailed status(writing/reading) can be gotten from the return value |
| 122 | * by bit operations. |
| 123 | */ |
Annie Li | 9dbc71d | 2011-12-12 18:13:57 +0800 | [diff] [blame] | 124 | int (*query_foreign_access)(grant_ref_t ref); |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 125 | /* |
| 126 | * Grant a domain to access a range of bytes within the page referred by |
| 127 | * an available grant entry. Ref parameter is reference of a grant entry |
| 128 | * which will be sub-page accessed, domid is id of grantee domain, frame |
| 129 | * is frame address of subpage grant, flags is grant type and flag |
| 130 | * information, page_off is offset of the range of bytes, and length is |
| 131 | * length of bytes to be accessed. |
| 132 | */ |
| 133 | void (*update_subpage_entry)(grant_ref_t ref, domid_t domid, |
| 134 | unsigned long frame, int flags, |
| 135 | unsigned page_off, unsigned length); |
| 136 | /* |
| 137 | * Redirect an available grant entry on domain A to another grant |
| 138 | * reference of domain B, then allow domain C to use grant reference |
| 139 | * of domain B transitively. Ref parameter is an available grant entry |
| 140 | * reference on domain A, domid is id of domain C which accesses grant |
| 141 | * entry transitively, flags is grant type and flag information, |
| 142 | * trans_domid is id of domain B whose grant entry is finally accessed |
| 143 | * transitively, trans_gref is grant entry transitive reference of |
| 144 | * domain B. |
| 145 | */ |
| 146 | void (*update_trans_entry)(grant_ref_t ref, domid_t domid, int flags, |
| 147 | domid_t trans_domid, grant_ref_t trans_gref); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 148 | }; |
| 149 | |
Bob Liu | b44166c | 2015-04-03 14:42:59 +0800 | [diff] [blame] | 150 | struct unmap_refs_callback_data { |
| 151 | struct completion completion; |
| 152 | int result; |
| 153 | }; |
| 154 | |
Julia Lawall | 86fc213 | 2015-11-28 15:28:40 +0100 | [diff] [blame] | 155 | static const struct gnttab_ops *gnttab_interface; |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 156 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 157 | /* This reflects status of grant entries, so act as a global value. */ |
| 158 | static grant_status_t *grstatus; |
| 159 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 160 | static int grant_table_version; |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 161 | static int grefs_per_grant_frame; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 162 | |
| 163 | static struct gnttab_free_callback *gnttab_free_callback_list; |
| 164 | |
| 165 | static int gnttab_expand(unsigned int req_entries); |
| 166 | |
| 167 | #define RPP (PAGE_SIZE / sizeof(grant_ref_t)) |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 168 | #define SPP (PAGE_SIZE / sizeof(grant_status_t)) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 169 | |
| 170 | static inline grant_ref_t *__gnttab_entry(grant_ref_t entry) |
| 171 | { |
| 172 | return &gnttab_list[(entry) / RPP][(entry) % RPP]; |
| 173 | } |
| 174 | /* This can be used as an l-value */ |
| 175 | #define gnttab_entry(entry) (*__gnttab_entry(entry)) |
| 176 | |
| 177 | static int get_free_entries(unsigned count) |
| 178 | { |
| 179 | unsigned long flags; |
Konrad Rzeszutek Wilk | 272800d | 2011-07-22 14:00:06 -0400 | [diff] [blame] | 180 | int ref, rc = 0; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 181 | grant_ref_t head; |
| 182 | |
| 183 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 184 | |
| 185 | if ((gnttab_free_count < count) && |
| 186 | ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) { |
| 187 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 188 | return rc; |
| 189 | } |
| 190 | |
| 191 | ref = head = gnttab_free_head; |
| 192 | gnttab_free_count -= count; |
| 193 | while (count-- > 1) |
| 194 | head = gnttab_entry(head); |
| 195 | gnttab_free_head = gnttab_entry(head); |
| 196 | gnttab_entry(head) = GNTTAB_LIST_END; |
| 197 | |
| 198 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 199 | |
| 200 | return ref; |
| 201 | } |
| 202 | |
| 203 | static void do_free_callbacks(void) |
| 204 | { |
| 205 | struct gnttab_free_callback *callback, *next; |
| 206 | |
| 207 | callback = gnttab_free_callback_list; |
| 208 | gnttab_free_callback_list = NULL; |
| 209 | |
| 210 | while (callback != NULL) { |
| 211 | next = callback->next; |
| 212 | if (gnttab_free_count >= callback->count) { |
| 213 | callback->next = NULL; |
| 214 | callback->fn(callback->arg); |
| 215 | } else { |
| 216 | callback->next = gnttab_free_callback_list; |
| 217 | gnttab_free_callback_list = callback; |
| 218 | } |
| 219 | callback = next; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static inline void check_free_callbacks(void) |
| 224 | { |
| 225 | if (unlikely(gnttab_free_callback_list)) |
| 226 | do_free_callbacks(); |
| 227 | } |
| 228 | |
| 229 | static void put_free_entry(grant_ref_t ref) |
| 230 | { |
| 231 | unsigned long flags; |
| 232 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 233 | gnttab_entry(ref) = gnttab_free_head; |
| 234 | gnttab_free_head = ref; |
| 235 | gnttab_free_count++; |
| 236 | check_free_callbacks(); |
| 237 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 238 | } |
| 239 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 240 | /* |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 241 | * Following applies to gnttab_update_entry_v1 and gnttab_update_entry_v2. |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 242 | * Introducing a valid entry into the grant table: |
| 243 | * 1. Write ent->domid. |
| 244 | * 2. Write ent->frame: |
| 245 | * GTF_permit_access: Frame to which access is permitted. |
| 246 | * GTF_accept_transfer: Pseudo-phys frame slot being filled by new |
| 247 | * frame, or zero if none. |
| 248 | * 3. Write memory barrier (WMB). |
| 249 | * 4. Write ent->flags, inc. valid type. |
| 250 | */ |
| 251 | static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid, |
| 252 | unsigned long frame, unsigned flags) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 253 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 254 | gnttab_shared.v1[ref].domid = domid; |
| 255 | gnttab_shared.v1[ref].frame = frame; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 256 | wmb(); |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 257 | gnttab_shared.v1[ref].flags = flags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 260 | static void gnttab_update_entry_v2(grant_ref_t ref, domid_t domid, |
| 261 | unsigned long frame, unsigned int flags) |
| 262 | { |
| 263 | gnttab_shared.v2[ref].hdr.domid = domid; |
| 264 | gnttab_shared.v2[ref].full_page.frame = frame; |
| 265 | wmb(); /* Hypervisor concurrent accesses. */ |
| 266 | gnttab_shared.v2[ref].hdr.flags = GTF_permit_access | flags; |
| 267 | } |
| 268 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 269 | /* |
| 270 | * Public grant-issuing interface functions |
| 271 | */ |
| 272 | void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid, |
| 273 | unsigned long frame, int readonly) |
| 274 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 275 | gnttab_interface->update_entry(ref, domid, frame, |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 276 | GTF_permit_access | (readonly ? GTF_readonly : 0)); |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref); |
| 279 | |
| 280 | int gnttab_grant_foreign_access(domid_t domid, unsigned long frame, |
| 281 | int readonly) |
| 282 | { |
| 283 | int ref; |
| 284 | |
| 285 | ref = get_free_entries(1); |
| 286 | if (unlikely(ref < 0)) |
| 287 | return -ENOSPC; |
| 288 | |
| 289 | gnttab_grant_foreign_access_ref(ref, domid, frame, readonly); |
| 290 | |
| 291 | return ref; |
| 292 | } |
| 293 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access); |
| 294 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 295 | static void gnttab_update_subpage_entry_v2(grant_ref_t ref, domid_t domid, |
| 296 | unsigned long frame, int flags, |
| 297 | unsigned page_off, unsigned length) |
| 298 | { |
| 299 | gnttab_shared.v2[ref].sub_page.frame = frame; |
| 300 | gnttab_shared.v2[ref].sub_page.page_off = page_off; |
| 301 | gnttab_shared.v2[ref].sub_page.length = length; |
| 302 | gnttab_shared.v2[ref].hdr.domid = domid; |
| 303 | wmb(); |
| 304 | gnttab_shared.v2[ref].hdr.flags = |
| 305 | GTF_permit_access | GTF_sub_page | flags; |
| 306 | } |
| 307 | |
| 308 | int gnttab_grant_foreign_access_subpage_ref(grant_ref_t ref, domid_t domid, |
| 309 | unsigned long frame, int flags, |
| 310 | unsigned page_off, |
| 311 | unsigned length) |
| 312 | { |
| 313 | if (flags & (GTF_accept_transfer | GTF_reading | |
| 314 | GTF_writing | GTF_transitive)) |
| 315 | return -EPERM; |
| 316 | |
| 317 | if (gnttab_interface->update_subpage_entry == NULL) |
| 318 | return -ENOSYS; |
| 319 | |
| 320 | gnttab_interface->update_subpage_entry(ref, domid, frame, flags, |
| 321 | page_off, length); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_subpage_ref); |
| 326 | |
| 327 | int gnttab_grant_foreign_access_subpage(domid_t domid, unsigned long frame, |
| 328 | int flags, unsigned page_off, |
| 329 | unsigned length) |
| 330 | { |
| 331 | int ref, rc; |
| 332 | |
| 333 | ref = get_free_entries(1); |
| 334 | if (unlikely(ref < 0)) |
| 335 | return -ENOSPC; |
| 336 | |
| 337 | rc = gnttab_grant_foreign_access_subpage_ref(ref, domid, frame, flags, |
| 338 | page_off, length); |
| 339 | if (rc < 0) { |
| 340 | put_free_entry(ref); |
| 341 | return rc; |
| 342 | } |
| 343 | |
| 344 | return ref; |
| 345 | } |
| 346 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_subpage); |
| 347 | |
| 348 | bool gnttab_subpage_grants_available(void) |
| 349 | { |
| 350 | return gnttab_interface->update_subpage_entry != NULL; |
| 351 | } |
| 352 | EXPORT_SYMBOL_GPL(gnttab_subpage_grants_available); |
| 353 | |
| 354 | static void gnttab_update_trans_entry_v2(grant_ref_t ref, domid_t domid, |
| 355 | int flags, domid_t trans_domid, |
| 356 | grant_ref_t trans_gref) |
| 357 | { |
| 358 | gnttab_shared.v2[ref].transitive.trans_domid = trans_domid; |
| 359 | gnttab_shared.v2[ref].transitive.gref = trans_gref; |
| 360 | gnttab_shared.v2[ref].hdr.domid = domid; |
| 361 | wmb(); |
| 362 | gnttab_shared.v2[ref].hdr.flags = |
| 363 | GTF_permit_access | GTF_transitive | flags; |
| 364 | } |
| 365 | |
| 366 | int gnttab_grant_foreign_access_trans_ref(grant_ref_t ref, domid_t domid, |
| 367 | int flags, domid_t trans_domid, |
| 368 | grant_ref_t trans_gref) |
| 369 | { |
| 370 | if (flags & (GTF_accept_transfer | GTF_reading | |
| 371 | GTF_writing | GTF_sub_page)) |
| 372 | return -EPERM; |
| 373 | |
| 374 | if (gnttab_interface->update_trans_entry == NULL) |
| 375 | return -ENOSYS; |
| 376 | |
| 377 | gnttab_interface->update_trans_entry(ref, domid, flags, trans_domid, |
| 378 | trans_gref); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_trans_ref); |
| 383 | |
| 384 | int gnttab_grant_foreign_access_trans(domid_t domid, int flags, |
| 385 | domid_t trans_domid, |
| 386 | grant_ref_t trans_gref) |
| 387 | { |
| 388 | int ref, rc; |
| 389 | |
| 390 | ref = get_free_entries(1); |
| 391 | if (unlikely(ref < 0)) |
| 392 | return -ENOSPC; |
| 393 | |
| 394 | rc = gnttab_grant_foreign_access_trans_ref(ref, domid, flags, |
| 395 | trans_domid, trans_gref); |
| 396 | if (rc < 0) { |
| 397 | put_free_entry(ref); |
| 398 | return rc; |
| 399 | } |
| 400 | |
| 401 | return ref; |
| 402 | } |
| 403 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_trans); |
| 404 | |
| 405 | bool gnttab_trans_grants_available(void) |
| 406 | { |
| 407 | return gnttab_interface->update_trans_entry != NULL; |
| 408 | } |
| 409 | EXPORT_SYMBOL_GPL(gnttab_trans_grants_available); |
| 410 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 411 | static int gnttab_query_foreign_access_v1(grant_ref_t ref) |
| 412 | { |
| 413 | return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing); |
| 414 | } |
| 415 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 416 | static int gnttab_query_foreign_access_v2(grant_ref_t ref) |
| 417 | { |
| 418 | return grstatus[ref] & (GTF_reading|GTF_writing); |
| 419 | } |
| 420 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 421 | int gnttab_query_foreign_access(grant_ref_t ref) |
| 422 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 423 | return gnttab_interface->query_foreign_access(ref); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 424 | } |
| 425 | EXPORT_SYMBOL_GPL(gnttab_query_foreign_access); |
| 426 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 427 | 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] | 428 | { |
| 429 | u16 flags, nflags; |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 430 | u16 *pflags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 431 | |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 432 | pflags = &gnttab_shared.v1[ref].flags; |
| 433 | nflags = *pflags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 434 | do { |
| 435 | flags = nflags; |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 436 | if (flags & (GTF_reading|GTF_writing)) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 437 | return 0; |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 438 | } while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 439 | |
| 440 | return 1; |
| 441 | } |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 442 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 443 | static int gnttab_end_foreign_access_ref_v2(grant_ref_t ref, int readonly) |
| 444 | { |
| 445 | gnttab_shared.v2[ref].hdr.flags = 0; |
| 446 | mb(); /* Concurrent access by hypervisor. */ |
| 447 | if (grstatus[ref] & (GTF_reading|GTF_writing)) { |
| 448 | return 0; |
| 449 | } else { |
| 450 | /* |
| 451 | * The read of grstatus needs to have acquire semantics. |
| 452 | * On x86, reads already have that, and we just need to |
| 453 | * protect against compiler reorderings. |
| 454 | * On other architectures we may need a full barrier. |
| 455 | */ |
| 456 | #ifdef CONFIG_X86 |
| 457 | barrier(); |
| 458 | #else |
| 459 | mb(); |
| 460 | #endif |
| 461 | } |
| 462 | |
| 463 | return 1; |
| 464 | } |
| 465 | |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 466 | 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] | 467 | { |
| 468 | return gnttab_interface->end_foreign_access_ref(ref, readonly); |
| 469 | } |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 470 | |
| 471 | int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly) |
| 472 | { |
| 473 | if (_gnttab_end_foreign_access_ref(ref, readonly)) |
| 474 | return 1; |
| 475 | pr_warn("WARNING: g.e. %#x still in use!\n", ref); |
| 476 | return 0; |
| 477 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 478 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref); |
| 479 | |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 480 | struct deferred_entry { |
| 481 | struct list_head list; |
| 482 | grant_ref_t ref; |
| 483 | bool ro; |
| 484 | uint16_t warn_delay; |
| 485 | struct page *page; |
| 486 | }; |
| 487 | static LIST_HEAD(deferred_list); |
| 488 | static void gnttab_handle_deferred(unsigned long); |
| 489 | static DEFINE_TIMER(deferred_timer, gnttab_handle_deferred, 0, 0); |
| 490 | |
| 491 | static void gnttab_handle_deferred(unsigned long unused) |
| 492 | { |
| 493 | unsigned int nr = 10; |
| 494 | struct deferred_entry *first = NULL; |
| 495 | unsigned long flags; |
| 496 | |
| 497 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 498 | while (nr--) { |
| 499 | struct deferred_entry *entry |
| 500 | = list_first_entry(&deferred_list, |
| 501 | struct deferred_entry, list); |
| 502 | |
| 503 | if (entry == first) |
| 504 | break; |
| 505 | list_del(&entry->list); |
| 506 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 507 | if (_gnttab_end_foreign_access_ref(entry->ref, entry->ro)) { |
| 508 | put_free_entry(entry->ref); |
| 509 | if (entry->page) { |
| 510 | pr_debug("freeing g.e. %#x (pfn %#lx)\n", |
| 511 | entry->ref, page_to_pfn(entry->page)); |
| 512 | __free_page(entry->page); |
| 513 | } else |
| 514 | pr_info("freeing g.e. %#x\n", entry->ref); |
| 515 | kfree(entry); |
| 516 | entry = NULL; |
| 517 | } else { |
| 518 | if (!--entry->warn_delay) |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 519 | pr_info("g.e. %#x still pending\n", entry->ref); |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 520 | if (!first) |
| 521 | first = entry; |
| 522 | } |
| 523 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 524 | if (entry) |
| 525 | list_add_tail(&entry->list, &deferred_list); |
| 526 | else if (list_empty(&deferred_list)) |
| 527 | break; |
| 528 | } |
| 529 | if (!list_empty(&deferred_list) && !timer_pending(&deferred_timer)) { |
| 530 | deferred_timer.expires = jiffies + HZ; |
| 531 | add_timer(&deferred_timer); |
| 532 | } |
| 533 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 534 | } |
| 535 | |
| 536 | static void gnttab_add_deferred(grant_ref_t ref, bool readonly, |
| 537 | struct page *page) |
| 538 | { |
| 539 | struct deferred_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
| 540 | const char *what = KERN_WARNING "leaking"; |
| 541 | |
| 542 | if (entry) { |
| 543 | unsigned long flags; |
| 544 | |
| 545 | entry->ref = ref; |
| 546 | entry->ro = readonly; |
| 547 | entry->page = page; |
| 548 | entry->warn_delay = 60; |
| 549 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 550 | list_add_tail(&entry->list, &deferred_list); |
| 551 | if (!timer_pending(&deferred_timer)) { |
| 552 | deferred_timer.expires = jiffies + HZ; |
| 553 | add_timer(&deferred_timer); |
| 554 | } |
| 555 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 556 | what = KERN_DEBUG "deferring"; |
| 557 | } |
| 558 | printk("%s g.e. %#x (pfn %#lx)\n", |
| 559 | what, ref, page ? page_to_pfn(page) : -1); |
| 560 | } |
| 561 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 562 | void gnttab_end_foreign_access(grant_ref_t ref, int readonly, |
| 563 | unsigned long page) |
| 564 | { |
| 565 | if (gnttab_end_foreign_access_ref(ref, readonly)) { |
| 566 | put_free_entry(ref); |
| 567 | if (page != 0) |
| 568 | free_page(page); |
Jan Beulich | 569ca5b | 2012-04-05 16:10:07 +0100 | [diff] [blame] | 569 | } else |
| 570 | gnttab_add_deferred(ref, readonly, |
| 571 | page ? virt_to_page(page) : NULL); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 572 | } |
| 573 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_access); |
| 574 | |
| 575 | int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn) |
| 576 | { |
| 577 | int ref; |
| 578 | |
| 579 | ref = get_free_entries(1); |
| 580 | if (unlikely(ref < 0)) |
| 581 | return -ENOSPC; |
| 582 | gnttab_grant_foreign_transfer_ref(ref, domid, pfn); |
| 583 | |
| 584 | return ref; |
| 585 | } |
| 586 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer); |
| 587 | |
| 588 | void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid, |
| 589 | unsigned long pfn) |
| 590 | { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 591 | gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 592 | } |
| 593 | EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref); |
| 594 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 595 | 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] | 596 | { |
| 597 | unsigned long frame; |
| 598 | u16 flags; |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 599 | u16 *pflags; |
| 600 | |
| 601 | pflags = &gnttab_shared.v1[ref].flags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 602 | |
| 603 | /* |
| 604 | * If a transfer is not even yet started, try to reclaim the grant |
| 605 | * reference and return failure (== 0). |
| 606 | */ |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 607 | while (!((flags = *pflags) & GTF_transfer_committed)) { |
| 608 | if (sync_cmpxchg(pflags, flags, 0) == flags) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 609 | return 0; |
| 610 | cpu_relax(); |
| 611 | } |
| 612 | |
| 613 | /* If a transfer is in progress then wait until it is completed. */ |
| 614 | while (!(flags & GTF_transfer_completed)) { |
Annie Li | b1e495b | 2011-11-22 09:58:47 +0800 | [diff] [blame] | 615 | flags = *pflags; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 616 | cpu_relax(); |
| 617 | } |
| 618 | |
| 619 | rmb(); /* Read the frame number /after/ reading completion status. */ |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 620 | frame = gnttab_shared.v1[ref].frame; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 621 | BUG_ON(frame == 0); |
| 622 | |
| 623 | return frame; |
| 624 | } |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 625 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 626 | static unsigned long gnttab_end_foreign_transfer_ref_v2(grant_ref_t ref) |
| 627 | { |
| 628 | unsigned long frame; |
| 629 | u16 flags; |
| 630 | u16 *pflags; |
| 631 | |
| 632 | pflags = &gnttab_shared.v2[ref].hdr.flags; |
| 633 | |
| 634 | /* |
| 635 | * If a transfer is not even yet started, try to reclaim the grant |
| 636 | * reference and return failure (== 0). |
| 637 | */ |
| 638 | while (!((flags = *pflags) & GTF_transfer_committed)) { |
| 639 | if (sync_cmpxchg(pflags, flags, 0) == flags) |
| 640 | return 0; |
| 641 | cpu_relax(); |
| 642 | } |
| 643 | |
| 644 | /* If a transfer is in progress then wait until it is completed. */ |
| 645 | while (!(flags & GTF_transfer_completed)) { |
| 646 | flags = *pflags; |
| 647 | cpu_relax(); |
| 648 | } |
| 649 | |
| 650 | rmb(); /* Read the frame number /after/ reading completion status. */ |
| 651 | frame = gnttab_shared.v2[ref].full_page.frame; |
| 652 | BUG_ON(frame == 0); |
| 653 | |
| 654 | return frame; |
| 655 | } |
| 656 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 657 | unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref) |
| 658 | { |
| 659 | return gnttab_interface->end_foreign_transfer_ref(ref); |
| 660 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 661 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref); |
| 662 | |
| 663 | unsigned long gnttab_end_foreign_transfer(grant_ref_t ref) |
| 664 | { |
| 665 | unsigned long frame = gnttab_end_foreign_transfer_ref(ref); |
| 666 | put_free_entry(ref); |
| 667 | return frame; |
| 668 | } |
| 669 | EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer); |
| 670 | |
| 671 | void gnttab_free_grant_reference(grant_ref_t ref) |
| 672 | { |
| 673 | put_free_entry(ref); |
| 674 | } |
| 675 | EXPORT_SYMBOL_GPL(gnttab_free_grant_reference); |
| 676 | |
| 677 | void gnttab_free_grant_references(grant_ref_t head) |
| 678 | { |
| 679 | grant_ref_t ref; |
| 680 | unsigned long flags; |
| 681 | int count = 1; |
| 682 | if (head == GNTTAB_LIST_END) |
| 683 | return; |
| 684 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 685 | ref = head; |
| 686 | while (gnttab_entry(ref) != GNTTAB_LIST_END) { |
| 687 | ref = gnttab_entry(ref); |
| 688 | count++; |
| 689 | } |
| 690 | gnttab_entry(ref) = gnttab_free_head; |
| 691 | gnttab_free_head = head; |
| 692 | gnttab_free_count += count; |
| 693 | check_free_callbacks(); |
| 694 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 695 | } |
| 696 | EXPORT_SYMBOL_GPL(gnttab_free_grant_references); |
| 697 | |
| 698 | int gnttab_alloc_grant_references(u16 count, grant_ref_t *head) |
| 699 | { |
| 700 | int h = get_free_entries(count); |
| 701 | |
| 702 | if (h < 0) |
| 703 | return -ENOSPC; |
| 704 | |
| 705 | *head = h; |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references); |
| 710 | |
| 711 | int gnttab_empty_grant_references(const grant_ref_t *private_head) |
| 712 | { |
| 713 | return (*private_head == GNTTAB_LIST_END); |
| 714 | } |
| 715 | EXPORT_SYMBOL_GPL(gnttab_empty_grant_references); |
| 716 | |
| 717 | int gnttab_claim_grant_reference(grant_ref_t *private_head) |
| 718 | { |
| 719 | grant_ref_t g = *private_head; |
| 720 | if (unlikely(g == GNTTAB_LIST_END)) |
| 721 | return -ENOSPC; |
| 722 | *private_head = gnttab_entry(g); |
| 723 | return g; |
| 724 | } |
| 725 | EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference); |
| 726 | |
| 727 | void gnttab_release_grant_reference(grant_ref_t *private_head, |
| 728 | grant_ref_t release) |
| 729 | { |
| 730 | gnttab_entry(release) = *private_head; |
| 731 | *private_head = release; |
| 732 | } |
| 733 | EXPORT_SYMBOL_GPL(gnttab_release_grant_reference); |
| 734 | |
| 735 | void gnttab_request_free_callback(struct gnttab_free_callback *callback, |
| 736 | void (*fn)(void *), void *arg, u16 count) |
| 737 | { |
| 738 | unsigned long flags; |
Roger Pau Monne | 5f338d9 | 2013-07-31 17:00:42 +0200 | [diff] [blame] | 739 | struct gnttab_free_callback *cb; |
| 740 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 741 | spin_lock_irqsave(&gnttab_list_lock, flags); |
Roger Pau Monne | 5f338d9 | 2013-07-31 17:00:42 +0200 | [diff] [blame] | 742 | |
| 743 | /* Check if the callback is already on the list */ |
| 744 | cb = gnttab_free_callback_list; |
| 745 | while (cb) { |
| 746 | if (cb == callback) |
| 747 | goto out; |
| 748 | cb = cb->next; |
| 749 | } |
| 750 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 751 | callback->fn = fn; |
| 752 | callback->arg = arg; |
| 753 | callback->count = count; |
| 754 | callback->next = gnttab_free_callback_list; |
| 755 | gnttab_free_callback_list = callback; |
| 756 | check_free_callbacks(); |
| 757 | out: |
| 758 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 759 | } |
| 760 | EXPORT_SYMBOL_GPL(gnttab_request_free_callback); |
| 761 | |
| 762 | void gnttab_cancel_free_callback(struct gnttab_free_callback *callback) |
| 763 | { |
| 764 | struct gnttab_free_callback **pcb; |
| 765 | unsigned long flags; |
| 766 | |
| 767 | spin_lock_irqsave(&gnttab_list_lock, flags); |
| 768 | for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) { |
| 769 | if (*pcb == callback) { |
| 770 | *pcb = callback->next; |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | spin_unlock_irqrestore(&gnttab_list_lock, flags); |
| 775 | } |
| 776 | EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback); |
| 777 | |
| 778 | static int grow_gnttab_list(unsigned int more_frames) |
| 779 | { |
| 780 | unsigned int new_nr_grant_frames, extra_entries, i; |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 781 | unsigned int nr_glist_frames, new_nr_glist_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 782 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 783 | BUG_ON(grefs_per_grant_frame == 0); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 784 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 785 | new_nr_grant_frames = nr_grant_frames + more_frames; |
| 786 | extra_entries = more_frames * grefs_per_grant_frame; |
| 787 | |
| 788 | 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] | 789 | new_nr_glist_frames = |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 790 | (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] | 791 | for (i = nr_glist_frames; i < new_nr_glist_frames; i++) { |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 792 | gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC); |
| 793 | if (!gnttab_list[i]) |
| 794 | goto grow_nomem; |
| 795 | } |
| 796 | |
| 797 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 798 | for (i = grefs_per_grant_frame * nr_grant_frames; |
| 799 | i < grefs_per_grant_frame * new_nr_grant_frames - 1; i++) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 800 | gnttab_entry(i) = i + 1; |
| 801 | |
| 802 | gnttab_entry(i) = gnttab_free_head; |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 803 | gnttab_free_head = grefs_per_grant_frame * nr_grant_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 804 | gnttab_free_count += extra_entries; |
| 805 | |
| 806 | nr_grant_frames = new_nr_grant_frames; |
| 807 | |
| 808 | check_free_callbacks(); |
| 809 | |
| 810 | return 0; |
| 811 | |
| 812 | grow_nomem: |
Chen Gang | 46e3626 | 2014-08-26 23:38:44 +0800 | [diff] [blame] | 813 | while (i-- > nr_glist_frames) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 814 | free_page((unsigned long) gnttab_list[i]); |
| 815 | return -ENOMEM; |
| 816 | } |
| 817 | |
| 818 | static unsigned int __max_nr_grant_frames(void) |
| 819 | { |
| 820 | struct gnttab_query_size query; |
| 821 | int rc; |
| 822 | |
| 823 | query.dom = DOMID_SELF; |
| 824 | |
| 825 | rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1); |
| 826 | if ((rc < 0) || (query.status != GNTST_okay)) |
| 827 | return 4; /* Legacy max supported number of frames */ |
| 828 | |
| 829 | return query.max_nr_frames; |
| 830 | } |
| 831 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 832 | unsigned int gnttab_max_grant_frames(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 833 | { |
| 834 | unsigned int xen_max = __max_nr_grant_frames(); |
Konrad Rzeszutek Wilk | 7f25602 | 2013-12-31 15:55:39 -0500 | [diff] [blame] | 835 | static unsigned int boot_max_nr_grant_frames; |
| 836 | |
| 837 | /* First time, initialize it properly. */ |
| 838 | if (!boot_max_nr_grant_frames) |
| 839 | boot_max_nr_grant_frames = __max_nr_grant_frames(); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 840 | |
| 841 | if (xen_max > boot_max_nr_grant_frames) |
| 842 | return boot_max_nr_grant_frames; |
| 843 | return xen_max; |
| 844 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 845 | EXPORT_SYMBOL_GPL(gnttab_max_grant_frames); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 846 | |
Julien Grall | 47c5420 | 2014-01-30 12:56:34 +0000 | [diff] [blame] | 847 | int gnttab_setup_auto_xlat_frames(phys_addr_t addr) |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 848 | { |
| 849 | xen_pfn_t *pfn; |
| 850 | unsigned int max_nr_gframes = __max_nr_grant_frames(); |
| 851 | unsigned int i; |
| 852 | void *vaddr; |
| 853 | |
| 854 | if (xen_auto_xlat_grant_frames.count) |
| 855 | return -EINVAL; |
| 856 | |
Julien Grall | 5ed5451 | 2015-05-05 16:37:49 +0100 | [diff] [blame] | 857 | vaddr = xen_remap(addr, XEN_PAGE_SIZE * max_nr_gframes); |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 858 | if (vaddr == NULL) { |
Julien Grall | 47c5420 | 2014-01-30 12:56:34 +0000 | [diff] [blame] | 859 | pr_warn("Failed to ioremap gnttab share frames (addr=%pa)!\n", |
| 860 | &addr); |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 861 | return -ENOMEM; |
| 862 | } |
| 863 | pfn = kcalloc(max_nr_gframes, sizeof(pfn[0]), GFP_KERNEL); |
| 864 | if (!pfn) { |
| 865 | xen_unmap(vaddr); |
| 866 | return -ENOMEM; |
| 867 | } |
| 868 | for (i = 0; i < max_nr_gframes; i++) |
Julien Grall | 5ed5451 | 2015-05-05 16:37:49 +0100 | [diff] [blame] | 869 | pfn[i] = XEN_PFN_DOWN(addr) + i; |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 870 | |
| 871 | xen_auto_xlat_grant_frames.vaddr = vaddr; |
| 872 | xen_auto_xlat_grant_frames.pfn = pfn; |
| 873 | xen_auto_xlat_grant_frames.count = max_nr_gframes; |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | EXPORT_SYMBOL_GPL(gnttab_setup_auto_xlat_frames); |
| 878 | |
| 879 | void gnttab_free_auto_xlat_frames(void) |
| 880 | { |
| 881 | if (!xen_auto_xlat_grant_frames.count) |
| 882 | return; |
| 883 | kfree(xen_auto_xlat_grant_frames.pfn); |
| 884 | xen_unmap(xen_auto_xlat_grant_frames.vaddr); |
| 885 | |
| 886 | xen_auto_xlat_grant_frames.pfn = NULL; |
| 887 | xen_auto_xlat_grant_frames.count = 0; |
| 888 | xen_auto_xlat_grant_frames.vaddr = NULL; |
| 889 | } |
| 890 | EXPORT_SYMBOL_GPL(gnttab_free_auto_xlat_frames); |
| 891 | |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 892 | /** |
| 893 | * gnttab_alloc_pages - alloc pages suitable for grant mapping into |
| 894 | * @nr_pages: number of pages to alloc |
| 895 | * @pages: returns the pages |
| 896 | */ |
| 897 | int gnttab_alloc_pages(int nr_pages, struct page **pages) |
| 898 | { |
Jennifer Herbert | 8da7633 | 2014-12-24 14:17:06 +0000 | [diff] [blame] | 899 | int i; |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 900 | int ret; |
| 901 | |
David Vrabel | 81b286e | 2015-06-25 13:12:46 +0100 | [diff] [blame] | 902 | ret = alloc_xenballooned_pages(nr_pages, pages); |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 903 | if (ret < 0) |
| 904 | return ret; |
| 905 | |
Jennifer Herbert | 8da7633 | 2014-12-24 14:17:06 +0000 | [diff] [blame] | 906 | for (i = 0; i < nr_pages; i++) { |
| 907 | #if BITS_PER_LONG < 64 |
| 908 | struct xen_page_foreign *foreign; |
| 909 | |
| 910 | foreign = kzalloc(sizeof(*foreign), GFP_KERNEL); |
| 911 | if (!foreign) { |
| 912 | gnttab_free_pages(nr_pages, pages); |
| 913 | return -ENOMEM; |
| 914 | } |
| 915 | set_page_private(pages[i], (unsigned long)foreign); |
| 916 | #endif |
| 917 | SetPagePrivate(pages[i]); |
| 918 | } |
| 919 | |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 920 | return 0; |
| 921 | } |
| 922 | EXPORT_SYMBOL(gnttab_alloc_pages); |
| 923 | |
| 924 | /** |
| 925 | * gnttab_free_pages - free pages allocated by gnttab_alloc_pages() |
| 926 | * @nr_pages; number of pages to free |
| 927 | * @pages: the pages |
| 928 | */ |
| 929 | void gnttab_free_pages(int nr_pages, struct page **pages) |
| 930 | { |
Jennifer Herbert | 8da7633 | 2014-12-24 14:17:06 +0000 | [diff] [blame] | 931 | int i; |
| 932 | |
| 933 | for (i = 0; i < nr_pages; i++) { |
| 934 | if (PagePrivate(pages[i])) { |
| 935 | #if BITS_PER_LONG < 64 |
| 936 | kfree((void *)page_private(pages[i])); |
| 937 | #endif |
| 938 | ClearPagePrivate(pages[i]); |
| 939 | } |
| 940 | } |
David Vrabel | ff4b156 | 2015-01-08 18:06:01 +0000 | [diff] [blame] | 941 | free_xenballooned_pages(nr_pages, pages); |
| 942 | } |
| 943 | EXPORT_SYMBOL(gnttab_free_pages); |
| 944 | |
Andres Lagar-Cavilla | c571898 | 2012-09-14 14:26:59 +0000 | [diff] [blame] | 945 | /* Handling of paged out grant targets (GNTST_eagain) */ |
| 946 | #define MAX_DELAY 256 |
| 947 | static inline void |
| 948 | gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status, |
| 949 | const char *func) |
| 950 | { |
| 951 | unsigned delay = 1; |
| 952 | |
| 953 | do { |
| 954 | BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1)); |
| 955 | if (*status == GNTST_eagain) |
| 956 | msleep(delay++); |
| 957 | } while ((*status == GNTST_eagain) && (delay < MAX_DELAY)); |
| 958 | |
| 959 | if (delay >= MAX_DELAY) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 960 | pr_err("%s: %s eagain grant\n", func, current->comm); |
Andres Lagar-Cavilla | c571898 | 2012-09-14 14:26:59 +0000 | [diff] [blame] | 961 | *status = GNTST_bad_page; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count) |
| 966 | { |
| 967 | struct gnttab_map_grant_ref *op; |
| 968 | |
| 969 | if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, batch, count)) |
| 970 | BUG(); |
| 971 | for (op = batch; op < batch + count; op++) |
| 972 | if (op->status == GNTST_eagain) |
| 973 | gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, op, |
| 974 | &op->status, __func__); |
| 975 | } |
| 976 | EXPORT_SYMBOL_GPL(gnttab_batch_map); |
| 977 | |
| 978 | void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count) |
| 979 | { |
| 980 | struct gnttab_copy *op; |
| 981 | |
| 982 | if (HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count)) |
| 983 | BUG(); |
| 984 | for (op = batch; op < batch + count; op++) |
| 985 | if (op->status == GNTST_eagain) |
| 986 | gnttab_retry_eagain_gop(GNTTABOP_copy, op, |
| 987 | &op->status, __func__); |
| 988 | } |
| 989 | EXPORT_SYMBOL_GPL(gnttab_batch_copy); |
| 990 | |
Julien Grall | 008c320 | 2015-06-19 17:49:03 +0100 | [diff] [blame] | 991 | void gnttab_foreach_grant_in_range(struct page *page, |
| 992 | unsigned int offset, |
| 993 | unsigned int len, |
| 994 | xen_grant_fn_t fn, |
| 995 | void *data) |
| 996 | { |
| 997 | unsigned int goffset; |
| 998 | unsigned int glen; |
| 999 | unsigned long xen_pfn; |
| 1000 | |
| 1001 | len = min_t(unsigned int, PAGE_SIZE - offset, len); |
| 1002 | goffset = xen_offset_in_page(offset); |
| 1003 | |
| 1004 | xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(offset); |
| 1005 | |
| 1006 | while (len) { |
| 1007 | glen = min_t(unsigned int, XEN_PAGE_SIZE - goffset, len); |
| 1008 | fn(pfn_to_gfn(xen_pfn), goffset, glen, data); |
| 1009 | |
| 1010 | goffset = 0; |
| 1011 | xen_pfn++; |
| 1012 | len -= glen; |
| 1013 | } |
| 1014 | } |
| 1015 | EXPORT_SYMBOL_GPL(gnttab_foreach_grant_in_range); |
| 1016 | |
Julien Grall | f73314b | 2015-10-13 17:50:12 +0100 | [diff] [blame] | 1017 | void gnttab_foreach_grant(struct page **pages, |
| 1018 | unsigned int nr_grefs, |
| 1019 | xen_grant_fn_t fn, |
| 1020 | void *data) |
| 1021 | { |
| 1022 | unsigned int goffset = 0; |
| 1023 | unsigned long xen_pfn = 0; |
| 1024 | unsigned int i; |
| 1025 | |
| 1026 | for (i = 0; i < nr_grefs; i++) { |
| 1027 | if ((i % XEN_PFN_PER_PAGE) == 0) { |
| 1028 | xen_pfn = page_to_xen_pfn(pages[i / XEN_PFN_PER_PAGE]); |
| 1029 | goffset = 0; |
| 1030 | } |
| 1031 | |
| 1032 | fn(pfn_to_gfn(xen_pfn), goffset, XEN_PAGE_SIZE, data); |
| 1033 | |
| 1034 | goffset += XEN_PAGE_SIZE; |
| 1035 | xen_pfn++; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 1039 | int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops, |
Annie Li | c123799 | 2011-11-22 09:59:56 +0800 | [diff] [blame] | 1040 | struct gnttab_map_grant_ref *kmap_ops, |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 1041 | struct page **pages, unsigned int count) |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 1042 | { |
| 1043 | int i, ret; |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 1044 | |
| 1045 | ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count); |
Jeremy Fitzhardinge | 87f1d40 | 2010-12-13 14:42:30 +0000 | [diff] [blame] | 1046 | if (ret) |
| 1047 | return ret; |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 1048 | |
Jennifer Herbert | 8da7633 | 2014-12-24 14:17:06 +0000 | [diff] [blame] | 1049 | for (i = 0; i < count; i++) { |
| 1050 | /* Retry eagain maps */ |
Andres Lagar-Cavilla | c571898 | 2012-09-14 14:26:59 +0000 | [diff] [blame] | 1051 | if (map_ops[i].status == GNTST_eagain) |
| 1052 | gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, map_ops + i, |
| 1053 | &map_ops[i].status, __func__); |
| 1054 | |
Jennifer Herbert | 8da7633 | 2014-12-24 14:17:06 +0000 | [diff] [blame] | 1055 | if (map_ops[i].status == GNTST_okay) { |
| 1056 | struct xen_page_foreign *foreign; |
| 1057 | |
| 1058 | SetPageForeign(pages[i]); |
| 1059 | foreign = xen_page_foreign(pages[i]); |
| 1060 | foreign->domid = map_ops[i].dom; |
| 1061 | foreign->gref = map_ops[i].ref; |
| 1062 | } |
| 1063 | } |
| 1064 | |
Zoltan Kiss | 1429d46 | 2014-02-27 15:55:30 +0000 | [diff] [blame] | 1065 | return set_foreign_p2m_mapping(map_ops, kmap_ops, pages, count); |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 1066 | } |
| 1067 | EXPORT_SYMBOL_GPL(gnttab_map_refs); |
| 1068 | |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 1069 | int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops, |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 1070 | struct gnttab_unmap_grant_ref *kunmap_ops, |
Konrad Rzeszutek Wilk | e85fc98 | 2014-02-03 06:43:59 -0500 | [diff] [blame] | 1071 | struct page **pages, unsigned int count) |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 1072 | { |
Jennifer Herbert | 8da7633 | 2014-12-24 14:17:06 +0000 | [diff] [blame] | 1073 | unsigned int i; |
Zoltan Kiss | 1429d46 | 2014-02-27 15:55:30 +0000 | [diff] [blame] | 1074 | int ret; |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 1075 | |
| 1076 | ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count); |
Jeremy Fitzhardinge | 87f1d40 | 2010-12-13 14:42:30 +0000 | [diff] [blame] | 1077 | if (ret) |
| 1078 | return ret; |
| 1079 | |
Jennifer Herbert | 8da7633 | 2014-12-24 14:17:06 +0000 | [diff] [blame] | 1080 | for (i = 0; i < count; i++) |
| 1081 | ClearPageForeign(pages[i]); |
| 1082 | |
David Vrabel | 853d028 | 2015-01-05 14:13:41 +0000 | [diff] [blame] | 1083 | return clear_foreign_p2m_mapping(unmap_ops, kunmap_ops, pages, count); |
Stefano Stabellini | 289b777 | 2010-12-10 14:54:44 +0000 | [diff] [blame] | 1084 | } |
| 1085 | EXPORT_SYMBOL_GPL(gnttab_unmap_refs); |
| 1086 | |
Jennifer Herbert | 3f9f1c6 | 2014-12-09 18:28:37 +0000 | [diff] [blame] | 1087 | #define GNTTAB_UNMAP_REFS_DELAY 5 |
| 1088 | |
| 1089 | static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item); |
| 1090 | |
| 1091 | static void gnttab_unmap_work(struct work_struct *work) |
| 1092 | { |
| 1093 | struct gntab_unmap_queue_data |
| 1094 | *unmap_data = container_of(work, |
| 1095 | struct gntab_unmap_queue_data, |
| 1096 | gnttab_work.work); |
| 1097 | if (unmap_data->age != UINT_MAX) |
| 1098 | unmap_data->age++; |
| 1099 | __gnttab_unmap_refs_async(unmap_data); |
| 1100 | } |
| 1101 | |
| 1102 | static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item) |
| 1103 | { |
| 1104 | int ret; |
| 1105 | int pc; |
| 1106 | |
| 1107 | for (pc = 0; pc < item->count; pc++) { |
| 1108 | if (page_count(item->pages[pc]) > 1) { |
| 1109 | unsigned long delay = GNTTAB_UNMAP_REFS_DELAY * (item->age + 1); |
| 1110 | schedule_delayed_work(&item->gnttab_work, |
| 1111 | msecs_to_jiffies(delay)); |
| 1112 | return; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | ret = gnttab_unmap_refs(item->unmap_ops, item->kunmap_ops, |
| 1117 | item->pages, item->count); |
| 1118 | item->done(ret, item); |
| 1119 | } |
| 1120 | |
| 1121 | void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item) |
| 1122 | { |
| 1123 | INIT_DELAYED_WORK(&item->gnttab_work, gnttab_unmap_work); |
| 1124 | item->age = 0; |
| 1125 | |
| 1126 | __gnttab_unmap_refs_async(item); |
| 1127 | } |
| 1128 | EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async); |
| 1129 | |
Bob Liu | b44166c | 2015-04-03 14:42:59 +0800 | [diff] [blame] | 1130 | static void unmap_refs_callback(int result, |
| 1131 | struct gntab_unmap_queue_data *data) |
| 1132 | { |
| 1133 | struct unmap_refs_callback_data *d = data->data; |
| 1134 | |
| 1135 | d->result = result; |
| 1136 | complete(&d->completion); |
| 1137 | } |
| 1138 | |
| 1139 | int gnttab_unmap_refs_sync(struct gntab_unmap_queue_data *item) |
| 1140 | { |
| 1141 | struct unmap_refs_callback_data data; |
| 1142 | |
| 1143 | init_completion(&data.completion); |
| 1144 | item->data = &data; |
| 1145 | item->done = &unmap_refs_callback; |
| 1146 | gnttab_unmap_refs_async(item); |
| 1147 | wait_for_completion(&data.completion); |
| 1148 | |
| 1149 | return data.result; |
| 1150 | } |
| 1151 | EXPORT_SYMBOL_GPL(gnttab_unmap_refs_sync); |
| 1152 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 1153 | static unsigned int nr_status_frames(unsigned int nr_grant_frames) |
| 1154 | { |
| 1155 | BUG_ON(grefs_per_grant_frame == 0); |
| 1156 | return (nr_grant_frames * grefs_per_grant_frame + SPP - 1) / SPP; |
| 1157 | } |
| 1158 | |
Ian Campbell | ef32f89 | 2012-10-17 09:39:14 +0100 | [diff] [blame] | 1159 | 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] | 1160 | { |
| 1161 | int rc; |
| 1162 | |
| 1163 | rc = arch_gnttab_map_shared(frames, nr_gframes, |
| 1164 | gnttab_max_grant_frames(), |
| 1165 | &gnttab_shared.addr); |
| 1166 | BUG_ON(rc); |
| 1167 | |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | static void gnttab_unmap_frames_v1(void) |
| 1172 | { |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 1173 | arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames); |
| 1174 | } |
| 1175 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 1176 | static int gnttab_map_frames_v2(xen_pfn_t *frames, unsigned int nr_gframes) |
| 1177 | { |
| 1178 | uint64_t *sframes; |
| 1179 | unsigned int nr_sframes; |
| 1180 | struct gnttab_get_status_frames getframes; |
| 1181 | int rc; |
| 1182 | |
| 1183 | nr_sframes = nr_status_frames(nr_gframes); |
| 1184 | |
| 1185 | /* No need for kzalloc as it is initialized in following hypercall |
| 1186 | * GNTTABOP_get_status_frames. |
| 1187 | */ |
| 1188 | sframes = kmalloc_array(nr_sframes, sizeof(uint64_t), GFP_ATOMIC); |
| 1189 | if (!sframes) |
| 1190 | return -ENOMEM; |
| 1191 | |
| 1192 | getframes.dom = DOMID_SELF; |
| 1193 | getframes.nr_frames = nr_sframes; |
| 1194 | set_xen_guest_handle(getframes.frame_list, sframes); |
| 1195 | |
| 1196 | rc = HYPERVISOR_grant_table_op(GNTTABOP_get_status_frames, |
| 1197 | &getframes, 1); |
| 1198 | if (rc == -ENOSYS) { |
| 1199 | kfree(sframes); |
| 1200 | return -ENOSYS; |
| 1201 | } |
| 1202 | |
| 1203 | BUG_ON(rc || getframes.status); |
| 1204 | |
| 1205 | rc = arch_gnttab_map_status(sframes, nr_sframes, |
| 1206 | nr_status_frames(gnttab_max_grant_frames()), |
| 1207 | &grstatus); |
| 1208 | BUG_ON(rc); |
| 1209 | kfree(sframes); |
| 1210 | |
| 1211 | rc = arch_gnttab_map_shared(frames, nr_gframes, |
| 1212 | gnttab_max_grant_frames(), |
| 1213 | &gnttab_shared.addr); |
| 1214 | BUG_ON(rc); |
| 1215 | |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | static void gnttab_unmap_frames_v2(void) |
| 1220 | { |
| 1221 | arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames); |
| 1222 | arch_gnttab_unmap(grstatus, nr_status_frames(nr_grant_frames)); |
| 1223 | } |
| 1224 | |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1225 | static int gnttab_map(unsigned int start_idx, unsigned int end_idx) |
| 1226 | { |
| 1227 | struct gnttab_setup_table setup; |
Ian Campbell | ef32f89 | 2012-10-17 09:39:14 +0100 | [diff] [blame] | 1228 | xen_pfn_t *frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1229 | unsigned int nr_gframes = end_idx + 1; |
| 1230 | int rc; |
| 1231 | |
Konrad Rzeszutek Wilk | 6926f6d | 2014-01-03 10:20:18 -0500 | [diff] [blame] | 1232 | if (xen_feature(XENFEAT_auto_translated_physmap)) { |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1233 | struct xen_add_to_physmap xatp; |
| 1234 | unsigned int i = end_idx; |
| 1235 | rc = 0; |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 1236 | BUG_ON(xen_auto_xlat_grant_frames.count < nr_gframes); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1237 | /* |
| 1238 | * Loop backwards, so that the first hypercall has the largest |
| 1239 | * index, ensuring that the table will grow only once. |
| 1240 | */ |
| 1241 | do { |
| 1242 | xatp.domid = DOMID_SELF; |
| 1243 | xatp.idx = i; |
| 1244 | xatp.space = XENMAPSPACE_grant_table; |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 1245 | xatp.gpfn = xen_auto_xlat_grant_frames.pfn[i]; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1246 | rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp); |
| 1247 | if (rc != 0) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 1248 | pr_warn("grant table add_to_physmap failed, err=%d\n", |
| 1249 | rc); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1250 | break; |
| 1251 | } |
| 1252 | } while (i-- > start_idx); |
| 1253 | |
| 1254 | return rc; |
| 1255 | } |
| 1256 | |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 1257 | /* No need for kzalloc as it is initialized in following hypercall |
| 1258 | * GNTTABOP_setup_table. |
| 1259 | */ |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1260 | frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC); |
| 1261 | if (!frames) |
| 1262 | return -ENOMEM; |
| 1263 | |
| 1264 | setup.dom = DOMID_SELF; |
| 1265 | setup.nr_frames = nr_gframes; |
Isaku Yamahata | 87e27cf | 2008-04-02 10:53:52 -0700 | [diff] [blame] | 1266 | set_xen_guest_handle(setup.frame_list, frames); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1267 | |
| 1268 | rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1); |
| 1269 | if (rc == -ENOSYS) { |
| 1270 | kfree(frames); |
| 1271 | return -ENOSYS; |
| 1272 | } |
| 1273 | |
| 1274 | BUG_ON(rc || setup.status); |
| 1275 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 1276 | rc = gnttab_interface->map_frames(frames, nr_gframes); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1277 | |
| 1278 | kfree(frames); |
| 1279 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 1280 | return rc; |
| 1281 | } |
| 1282 | |
Julia Lawall | 86fc213 | 2015-11-28 15:28:40 +0100 | [diff] [blame] | 1283 | static const struct gnttab_ops gnttab_v1_ops = { |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 1284 | .map_frames = gnttab_map_frames_v1, |
| 1285 | .unmap_frames = gnttab_unmap_frames_v1, |
| 1286 | .update_entry = gnttab_update_entry_v1, |
| 1287 | .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1, |
| 1288 | .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1, |
| 1289 | .query_foreign_access = gnttab_query_foreign_access_v1, |
| 1290 | }; |
| 1291 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 1292 | static const struct gnttab_ops gnttab_v2_ops = { |
| 1293 | .map_frames = gnttab_map_frames_v2, |
| 1294 | .unmap_frames = gnttab_unmap_frames_v2, |
| 1295 | .update_entry = gnttab_update_entry_v2, |
| 1296 | .end_foreign_access_ref = gnttab_end_foreign_access_ref_v2, |
| 1297 | .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v2, |
| 1298 | .query_foreign_access = gnttab_query_foreign_access_v2, |
| 1299 | .update_subpage_entry = gnttab_update_subpage_entry_v2, |
| 1300 | .update_trans_entry = gnttab_update_trans_entry_v2, |
| 1301 | }; |
| 1302 | |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 1303 | static void gnttab_request_version(void) |
| 1304 | { |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 1305 | int rc; |
| 1306 | struct gnttab_set_version gsv; |
Annie Li | 85ff6ac | 2011-11-22 09:59:21 +0800 | [diff] [blame] | 1307 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 1308 | gsv.version = 1; |
| 1309 | |
| 1310 | rc = HYPERVISOR_grant_table_op(GNTTABOP_set_version, &gsv, 1); |
| 1311 | if (rc == 0 && gsv.version == 2) { |
| 1312 | grant_table_version = 2; |
| 1313 | grefs_per_grant_frame = XEN_PAGE_SIZE / |
| 1314 | sizeof(union grant_entry_v2); |
| 1315 | gnttab_interface = &gnttab_v2_ops; |
| 1316 | } else if (grant_table_version == 2) { |
| 1317 | /* |
| 1318 | * If we've already used version 2 features, |
| 1319 | * but then suddenly discover that they're not |
| 1320 | * available (e.g. migrating to an older |
| 1321 | * version of Xen), almost unbounded badness |
| 1322 | * can happen. |
| 1323 | */ |
| 1324 | panic("we need grant tables version 2, but only version 1 is available"); |
| 1325 | } else { |
| 1326 | grant_table_version = 1; |
| 1327 | grefs_per_grant_frame = XEN_PAGE_SIZE / |
| 1328 | sizeof(struct grant_entry_v1); |
| 1329 | gnttab_interface = &gnttab_v1_ops; |
| 1330 | } |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 1331 | pr_info("Grant tables using version %d layout\n", grant_table_version); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 1334 | static int gnttab_setup(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1335 | { |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1336 | unsigned int max_nr_gframes; |
| 1337 | |
| 1338 | max_nr_gframes = gnttab_max_grant_frames(); |
| 1339 | if (max_nr_gframes < nr_grant_frames) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1340 | return -ENOSYS; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1341 | |
Konrad Rzeszutek Wilk | 4568475 | 2013-12-31 16:33:31 -0500 | [diff] [blame] | 1342 | if (xen_feature(XENFEAT_auto_translated_physmap) && gnttab_shared.addr == NULL) { |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 1343 | gnttab_shared.addr = xen_auto_xlat_grant_frames.vaddr; |
Annie Li | 0f9f5a9 | 2011-11-22 09:58:06 +0800 | [diff] [blame] | 1344 | if (gnttab_shared.addr == NULL) { |
Konrad Rzeszutek Wilk | efaf30a | 2014-01-06 10:40:36 -0500 | [diff] [blame] | 1345 | pr_warn("gnttab share frames (addr=0x%08lx) is not mapped!\n", |
| 1346 | (unsigned long)xen_auto_xlat_grant_frames.vaddr); |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1347 | return -ENOMEM; |
| 1348 | } |
| 1349 | } |
Konrad Rzeszutek Wilk | 4568475 | 2013-12-31 16:33:31 -0500 | [diff] [blame] | 1350 | return gnttab_map(0, nr_grant_frames - 1); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 1353 | int gnttab_resume(void) |
| 1354 | { |
| 1355 | gnttab_request_version(); |
| 1356 | return gnttab_setup(); |
| 1357 | } |
| 1358 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 1359 | int gnttab_suspend(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1360 | { |
David Vrabel | 13cd36a | 2014-06-16 11:12:03 +0100 | [diff] [blame] | 1361 | if (!xen_feature(XENFEAT_auto_translated_physmap)) |
| 1362 | gnttab_interface->unmap_frames(); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | static int gnttab_expand(unsigned int req_entries) |
| 1367 | { |
| 1368 | int rc; |
| 1369 | unsigned int cur, extra; |
| 1370 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 1371 | BUG_ON(grefs_per_grant_frame == 0); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1372 | cur = nr_grant_frames; |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 1373 | extra = ((req_entries + (grefs_per_grant_frame-1)) / |
| 1374 | grefs_per_grant_frame); |
Wengang Wang | 29d11cfd | 2017-07-18 09:40:35 +0200 | [diff] [blame] | 1375 | if (cur + extra > gnttab_max_grant_frames()) { |
| 1376 | pr_warn_ratelimited("xen/grant-table: max_grant_frames reached" |
| 1377 | " cur=%u extra=%u limit=%u" |
| 1378 | " gnttab_free_count=%u req_entries=%u\n", |
| 1379 | cur, extra, gnttab_max_grant_frames(), |
| 1380 | gnttab_free_count, req_entries); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1381 | return -ENOSPC; |
Wengang Wang | 29d11cfd | 2017-07-18 09:40:35 +0200 | [diff] [blame] | 1382 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1383 | |
| 1384 | rc = gnttab_map(cur, cur + extra - 1); |
| 1385 | if (rc == 0) |
| 1386 | rc = grow_gnttab_list(extra); |
| 1387 | |
| 1388 | return rc; |
| 1389 | } |
| 1390 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1391 | int gnttab_init(void) |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1392 | { |
| 1393 | int i; |
David Vrabel | 162e371 | 2014-07-11 16:42:34 +0100 | [diff] [blame] | 1394 | unsigned long max_nr_grant_frames; |
Michael Abd-El-Malek | bbc60c1 | 2008-04-04 02:33:48 -0700 | [diff] [blame] | 1395 | unsigned int max_nr_glist_frames, nr_glist_frames; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1396 | unsigned int nr_init_grefs; |
Julia Lawall | 6b5e7d9 | 2012-04-15 11:27:12 +0200 | [diff] [blame] | 1397 | int ret; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1398 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 1399 | gnttab_request_version(); |
David Vrabel | 162e371 | 2014-07-11 16:42:34 +0100 | [diff] [blame] | 1400 | max_nr_grant_frames = gnttab_max_grant_frames(); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1401 | nr_grant_frames = 1; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1402 | |
| 1403 | /* Determine the maximum number of frames required for the |
| 1404 | * grant reference free list on the current hypervisor. |
| 1405 | */ |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 1406 | BUG_ON(grefs_per_grant_frame == 0); |
David Vrabel | 162e371 | 2014-07-11 16:42:34 +0100 | [diff] [blame] | 1407 | max_nr_glist_frames = (max_nr_grant_frames * |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 1408 | grefs_per_grant_frame / RPP); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1409 | |
| 1410 | gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *), |
| 1411 | GFP_KERNEL); |
| 1412 | if (gnttab_list == NULL) |
| 1413 | return -ENOMEM; |
| 1414 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 1415 | 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] | 1416 | for (i = 0; i < nr_glist_frames; i++) { |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1417 | gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL); |
Julia Lawall | 6b5e7d9 | 2012-04-15 11:27:12 +0200 | [diff] [blame] | 1418 | if (gnttab_list[i] == NULL) { |
| 1419 | ret = -ENOMEM; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1420 | goto ini_nomem; |
Julia Lawall | 6b5e7d9 | 2012-04-15 11:27:12 +0200 | [diff] [blame] | 1421 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Juergen Gross | b988b8f | 2017-11-02 10:19:17 +0100 | [diff] [blame^] | 1424 | ret = arch_gnttab_init(max_nr_grant_frames, |
| 1425 | nr_status_frames(max_nr_grant_frames)); |
David Vrabel | 162e371 | 2014-07-11 16:42:34 +0100 | [diff] [blame] | 1426 | if (ret < 0) |
| 1427 | goto ini_nomem; |
| 1428 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 1429 | if (gnttab_setup() < 0) { |
Julia Lawall | 6b5e7d9 | 2012-04-15 11:27:12 +0200 | [diff] [blame] | 1430 | ret = -ENODEV; |
| 1431 | goto ini_nomem; |
| 1432 | } |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1433 | |
Matt Wilson | d0b4d64 | 2013-01-15 13:21:27 +0000 | [diff] [blame] | 1434 | nr_init_grefs = nr_grant_frames * grefs_per_grant_frame; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1435 | |
| 1436 | for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++) |
| 1437 | gnttab_entry(i) = i + 1; |
| 1438 | |
| 1439 | gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END; |
| 1440 | gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES; |
| 1441 | gnttab_free_head = NR_RESERVED_ENTRIES; |
| 1442 | |
| 1443 | printk("Grant table initialized\n"); |
| 1444 | return 0; |
| 1445 | |
| 1446 | ini_nomem: |
| 1447 | for (i--; i >= 0; i--) |
| 1448 | free_page((unsigned long)gnttab_list[i]); |
| 1449 | kfree(gnttab_list); |
Julia Lawall | 6b5e7d9 | 2012-04-15 11:27:12 +0200 | [diff] [blame] | 1450 | return ret; |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1451 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1452 | EXPORT_SYMBOL_GPL(gnttab_init); |
Jeremy Fitzhardinge | ad9a861 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1453 | |
Greg Kroah-Hartman | 345a525 | 2012-12-21 13:00:00 -0800 | [diff] [blame] | 1454 | static int __gnttab_init(void) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1455 | { |
Boris Ostrovsky | 8613d78 | 2017-02-06 10:56:15 -0500 | [diff] [blame] | 1456 | if (!xen_domain()) |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1457 | return -ENODEV; |
| 1458 | |
Boris Ostrovsky | 8613d78 | 2017-02-06 10:56:15 -0500 | [diff] [blame] | 1459 | /* Delay grant-table initialization in the PV on HVM case */ |
| 1460 | if (xen_hvm_domain() && !xen_pvh_domain()) |
| 1461 | return 0; |
| 1462 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1463 | return gnttab_init(); |
| 1464 | } |
Konrad Rzeszutek Wilk | 6926f6d | 2014-01-03 10:20:18 -0500 | [diff] [blame] | 1465 | /* Starts after core_initcall so that xen_pvh_gnttab_setup can be called |
| 1466 | * beforehand to initialize xen_auto_xlat_grant_frames. */ |
| 1467 | core_initcall_sync(__gnttab_init); |