grant-table.c 14 KB

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