grant-table.c 15 KB

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