grant-table.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. #include <linux/module.h>
  34. #include <linux/sched.h>
  35. #include <linux/mm.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/uaccess.h>
  38. #include <xen/interface/xen.h>
  39. #include <xen/page.h>
  40. #include <xen/grant_table.h>
  41. #include <asm/xen/hypercall.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/sync_bitops.h>
  44. /* External tools reserve first few grant table entries. */
  45. #define NR_RESERVED_ENTRIES 8
  46. #define GNTTAB_LIST_END 0xffffffff
  47. #define GREFS_PER_GRANT_FRAME (PAGE_SIZE / sizeof(struct grant_entry))
  48. static grant_ref_t **gnttab_list;
  49. static unsigned int nr_grant_frames;
  50. static unsigned int boot_max_nr_grant_frames;
  51. static int gnttab_free_count;
  52. static grant_ref_t gnttab_free_head;
  53. static DEFINE_SPINLOCK(gnttab_list_lock);
  54. static struct grant_entry *shared;
  55. static struct gnttab_free_callback *gnttab_free_callback_list;
  56. static int gnttab_expand(unsigned int req_entries);
  57. #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
  58. static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
  59. {
  60. return &gnttab_list[(entry) / RPP][(entry) % RPP];
  61. }
  62. /* This can be used as an l-value */
  63. #define gnttab_entry(entry) (*__gnttab_entry(entry))
  64. static int get_free_entries(unsigned count)
  65. {
  66. unsigned long flags;
  67. int ref, rc;
  68. grant_ref_t head;
  69. spin_lock_irqsave(&gnttab_list_lock, flags);
  70. if ((gnttab_free_count < count) &&
  71. ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
  72. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  73. return rc;
  74. }
  75. ref = head = gnttab_free_head;
  76. gnttab_free_count -= count;
  77. while (count-- > 1)
  78. head = gnttab_entry(head);
  79. gnttab_free_head = gnttab_entry(head);
  80. gnttab_entry(head) = GNTTAB_LIST_END;
  81. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  82. return ref;
  83. }
  84. static void do_free_callbacks(void)
  85. {
  86. struct gnttab_free_callback *callback, *next;
  87. callback = gnttab_free_callback_list;
  88. gnttab_free_callback_list = NULL;
  89. while (callback != NULL) {
  90. next = callback->next;
  91. if (gnttab_free_count >= callback->count) {
  92. callback->next = NULL;
  93. callback->fn(callback->arg);
  94. } else {
  95. callback->next = gnttab_free_callback_list;
  96. gnttab_free_callback_list = callback;
  97. }
  98. callback = next;
  99. }
  100. }
  101. static inline void check_free_callbacks(void)
  102. {
  103. if (unlikely(gnttab_free_callback_list))
  104. do_free_callbacks();
  105. }
  106. static void put_free_entry(grant_ref_t ref)
  107. {
  108. unsigned long flags;
  109. spin_lock_irqsave(&gnttab_list_lock, flags);
  110. gnttab_entry(ref) = gnttab_free_head;
  111. gnttab_free_head = ref;
  112. gnttab_free_count++;
  113. check_free_callbacks();
  114. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  115. }
  116. static void update_grant_entry(grant_ref_t ref, domid_t domid,
  117. unsigned long frame, unsigned flags)
  118. {
  119. /*
  120. * Introducing a valid entry into the grant table:
  121. * 1. Write ent->domid.
  122. * 2. Write ent->frame:
  123. * GTF_permit_access: Frame to which access is permitted.
  124. * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
  125. * frame, or zero if none.
  126. * 3. Write memory barrier (WMB).
  127. * 4. Write ent->flags, inc. valid type.
  128. */
  129. shared[ref].frame = frame;
  130. shared[ref].domid = domid;
  131. wmb();
  132. shared[ref].flags = flags;
  133. }
  134. /*
  135. * Public grant-issuing interface functions
  136. */
  137. void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
  138. unsigned long frame, int readonly)
  139. {
  140. update_grant_entry(ref, domid, frame,
  141. GTF_permit_access | (readonly ? GTF_readonly : 0));
  142. }
  143. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
  144. int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
  145. int readonly)
  146. {
  147. int ref;
  148. ref = get_free_entries(1);
  149. if (unlikely(ref < 0))
  150. return -ENOSPC;
  151. gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
  152. return ref;
  153. }
  154. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
  155. int gnttab_query_foreign_access(grant_ref_t ref)
  156. {
  157. u16 nflags;
  158. nflags = shared[ref].flags;
  159. return (nflags & (GTF_reading|GTF_writing));
  160. }
  161. EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
  162. int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
  163. {
  164. u16 flags, nflags;
  165. nflags = shared[ref].flags;
  166. do {
  167. flags = nflags;
  168. if (flags & (GTF_reading|GTF_writing)) {
  169. printk(KERN_ALERT "WARNING: g.e. still in use!\n");
  170. return 0;
  171. }
  172. } while ((nflags = sync_cmpxchg(&shared[ref].flags, flags, 0)) != flags);
  173. return 1;
  174. }
  175. EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
  176. void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
  177. unsigned long page)
  178. {
  179. if (gnttab_end_foreign_access_ref(ref, readonly)) {
  180. put_free_entry(ref);
  181. if (page != 0)
  182. free_page(page);
  183. } else {
  184. /* XXX This needs to be fixed so that the ref and page are
  185. placed on a list to be freed up later. */
  186. printk(KERN_WARNING
  187. "WARNING: leaking g.e. and page still in use!\n");
  188. }
  189. }
  190. EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
  191. int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
  192. {
  193. int ref;
  194. ref = get_free_entries(1);
  195. if (unlikely(ref < 0))
  196. return -ENOSPC;
  197. gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
  198. return ref;
  199. }
  200. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
  201. void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
  202. unsigned long pfn)
  203. {
  204. update_grant_entry(ref, domid, pfn, GTF_accept_transfer);
  205. }
  206. EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
  207. unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
  208. {
  209. unsigned long frame;
  210. u16 flags;
  211. /*
  212. * If a transfer is not even yet started, try to reclaim the grant
  213. * reference and return failure (== 0).
  214. */
  215. while (!((flags = shared[ref].flags) & GTF_transfer_committed)) {
  216. if (sync_cmpxchg(&shared[ref].flags, flags, 0) == flags)
  217. return 0;
  218. cpu_relax();
  219. }
  220. /* If a transfer is in progress then wait until it is completed. */
  221. while (!(flags & GTF_transfer_completed)) {
  222. flags = shared[ref].flags;
  223. cpu_relax();
  224. }
  225. rmb(); /* Read the frame number /after/ reading completion status. */
  226. frame = shared[ref].frame;
  227. BUG_ON(frame == 0);
  228. return frame;
  229. }
  230. EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
  231. unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
  232. {
  233. unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
  234. put_free_entry(ref);
  235. return frame;
  236. }
  237. EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
  238. void gnttab_free_grant_reference(grant_ref_t ref)
  239. {
  240. put_free_entry(ref);
  241. }
  242. EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
  243. void gnttab_free_grant_references(grant_ref_t head)
  244. {
  245. grant_ref_t ref;
  246. unsigned long flags;
  247. int count = 1;
  248. if (head == GNTTAB_LIST_END)
  249. return;
  250. spin_lock_irqsave(&gnttab_list_lock, flags);
  251. ref = head;
  252. while (gnttab_entry(ref) != GNTTAB_LIST_END) {
  253. ref = gnttab_entry(ref);
  254. count++;
  255. }
  256. gnttab_entry(ref) = gnttab_free_head;
  257. gnttab_free_head = head;
  258. gnttab_free_count += count;
  259. check_free_callbacks();
  260. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  261. }
  262. EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
  263. int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
  264. {
  265. int h = get_free_entries(count);
  266. if (h < 0)
  267. return -ENOSPC;
  268. *head = h;
  269. return 0;
  270. }
  271. EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
  272. int gnttab_empty_grant_references(const grant_ref_t *private_head)
  273. {
  274. return (*private_head == GNTTAB_LIST_END);
  275. }
  276. EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
  277. int gnttab_claim_grant_reference(grant_ref_t *private_head)
  278. {
  279. grant_ref_t g = *private_head;
  280. if (unlikely(g == GNTTAB_LIST_END))
  281. return -ENOSPC;
  282. *private_head = gnttab_entry(g);
  283. return g;
  284. }
  285. EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
  286. void gnttab_release_grant_reference(grant_ref_t *private_head,
  287. grant_ref_t release)
  288. {
  289. gnttab_entry(release) = *private_head;
  290. *private_head = release;
  291. }
  292. EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
  293. void gnttab_request_free_callback(struct gnttab_free_callback *callback,
  294. void (*fn)(void *), void *arg, u16 count)
  295. {
  296. unsigned long flags;
  297. spin_lock_irqsave(&gnttab_list_lock, flags);
  298. if (callback->next)
  299. goto out;
  300. callback->fn = fn;
  301. callback->arg = arg;
  302. callback->count = count;
  303. callback->next = gnttab_free_callback_list;
  304. gnttab_free_callback_list = callback;
  305. check_free_callbacks();
  306. out:
  307. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  308. }
  309. EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
  310. void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
  311. {
  312. struct gnttab_free_callback **pcb;
  313. unsigned long flags;
  314. spin_lock_irqsave(&gnttab_list_lock, flags);
  315. for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
  316. if (*pcb == callback) {
  317. *pcb = callback->next;
  318. break;
  319. }
  320. }
  321. spin_unlock_irqrestore(&gnttab_list_lock, flags);
  322. }
  323. EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
  324. static int grow_gnttab_list(unsigned int more_frames)
  325. {
  326. unsigned int new_nr_grant_frames, extra_entries, i;
  327. unsigned int nr_glist_frames, new_nr_glist_frames;
  328. new_nr_grant_frames = nr_grant_frames + more_frames;
  329. extra_entries = more_frames * GREFS_PER_GRANT_FRAME;
  330. nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
  331. new_nr_glist_frames =
  332. (new_nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
  333. for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
  334. gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
  335. if (!gnttab_list[i])
  336. goto grow_nomem;
  337. }
  338. for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames;
  339. i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
  340. gnttab_entry(i) = i + 1;
  341. gnttab_entry(i) = gnttab_free_head;
  342. gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames;
  343. gnttab_free_count += extra_entries;
  344. nr_grant_frames = new_nr_grant_frames;
  345. check_free_callbacks();
  346. return 0;
  347. grow_nomem:
  348. for ( ; i >= nr_glist_frames; i--)
  349. free_page((unsigned long) gnttab_list[i]);
  350. return -ENOMEM;
  351. }
  352. static unsigned int __max_nr_grant_frames(void)
  353. {
  354. struct gnttab_query_size query;
  355. int rc;
  356. query.dom = DOMID_SELF;
  357. rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
  358. if ((rc < 0) || (query.status != GNTST_okay))
  359. return 4; /* Legacy max supported number of frames */
  360. return query.max_nr_frames;
  361. }
  362. static inline unsigned int max_nr_grant_frames(void)
  363. {
  364. unsigned int xen_max = __max_nr_grant_frames();
  365. if (xen_max > boot_max_nr_grant_frames)
  366. return boot_max_nr_grant_frames;
  367. return xen_max;
  368. }
  369. static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
  370. {
  371. struct gnttab_setup_table setup;
  372. unsigned long *frames;
  373. unsigned int nr_gframes = end_idx + 1;
  374. int rc;
  375. frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
  376. if (!frames)
  377. return -ENOMEM;
  378. setup.dom = DOMID_SELF;
  379. setup.nr_frames = nr_gframes;
  380. set_xen_guest_handle(setup.frame_list, frames);
  381. rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
  382. if (rc == -ENOSYS) {
  383. kfree(frames);
  384. return -ENOSYS;
  385. }
  386. BUG_ON(rc || setup.status);
  387. rc = arch_gnttab_map_shared(frames, nr_gframes, max_nr_grant_frames(),
  388. &shared);
  389. BUG_ON(rc);
  390. kfree(frames);
  391. return 0;
  392. }
  393. int gnttab_resume(void)
  394. {
  395. if (max_nr_grant_frames() < nr_grant_frames)
  396. return -ENOSYS;
  397. return gnttab_map(0, nr_grant_frames - 1);
  398. }
  399. int gnttab_suspend(void)
  400. {
  401. arch_gnttab_unmap_shared(shared, nr_grant_frames);
  402. return 0;
  403. }
  404. static int gnttab_expand(unsigned int req_entries)
  405. {
  406. int rc;
  407. unsigned int cur, extra;
  408. cur = nr_grant_frames;
  409. extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) /
  410. GREFS_PER_GRANT_FRAME);
  411. if (cur + extra > max_nr_grant_frames())
  412. return -ENOSPC;
  413. rc = gnttab_map(cur, cur + extra - 1);
  414. if (rc == 0)
  415. rc = grow_gnttab_list(extra);
  416. return rc;
  417. }
  418. static int __devinit gnttab_init(void)
  419. {
  420. int i;
  421. unsigned int max_nr_glist_frames, nr_glist_frames;
  422. unsigned int nr_init_grefs;
  423. if (!xen_domain())
  424. return -ENODEV;
  425. nr_grant_frames = 1;
  426. boot_max_nr_grant_frames = __max_nr_grant_frames();
  427. /* Determine the maximum number of frames required for the
  428. * grant reference free list on the current hypervisor.
  429. */
  430. max_nr_glist_frames = (boot_max_nr_grant_frames *
  431. GREFS_PER_GRANT_FRAME / RPP);
  432. gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *),
  433. GFP_KERNEL);
  434. if (gnttab_list == NULL)
  435. return -ENOMEM;
  436. nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
  437. for (i = 0; i < nr_glist_frames; i++) {
  438. gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
  439. if (gnttab_list[i] == NULL)
  440. goto ini_nomem;
  441. }
  442. if (gnttab_resume() < 0)
  443. return -ENODEV;
  444. nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME;
  445. for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
  446. gnttab_entry(i) = i + 1;
  447. gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
  448. gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
  449. gnttab_free_head = NR_RESERVED_ENTRIES;
  450. printk("Grant table initialized\n");
  451. return 0;
  452. ini_nomem:
  453. for (i--; i >= 0; i--)
  454. free_page((unsigned long)gnttab_list[i]);
  455. kfree(gnttab_list);
  456. return -ENOMEM;
  457. }
  458. core_initcall(gnttab_init);