grant-table.c 26 KB

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