grant-table.c 30 KB

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