grant-table.c 29 KB

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