virtio_balloon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Virtio balloon implementation, inspired by Dor Laor and Marcelo
  3. * Tosatti's implementations.
  4. *
  5. * Copyright 2008 Rusty Russell IBM Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <linux/virtio.h>
  22. #include <linux/virtio_balloon.h>
  23. #include <linux/swap.h>
  24. #include <linux/kthread.h>
  25. #include <linux/freezer.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/balloon_compaction.h>
  30. #include <linux/wait.h>
  31. /*
  32. * Balloon device works in 4K page units. So each page is pointed to by
  33. * multiple balloon pages. All memory counters in this driver are in balloon
  34. * page units.
  35. */
  36. #define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT)
  37. #define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256
  38. struct virtio_balloon
  39. {
  40. struct virtio_device *vdev;
  41. struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
  42. /* Where the ballooning thread waits for config to change. */
  43. wait_queue_head_t config_change;
  44. /* The thread servicing the balloon. */
  45. struct task_struct *thread;
  46. /* Waiting for host to ack the pages we released. */
  47. wait_queue_head_t acked;
  48. /* Number of balloon pages we've told the Host we're not using. */
  49. unsigned int num_pages;
  50. /*
  51. * The pages we've told the Host we're not using are enqueued
  52. * at vb_dev_info->pages list.
  53. * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE
  54. * to num_pages above.
  55. */
  56. struct balloon_dev_info vb_dev_info;
  57. /* Synchronize access/update to this struct virtio_balloon elements */
  58. struct mutex balloon_lock;
  59. /* The array of pfns we tell the Host about. */
  60. unsigned int num_pfns;
  61. u32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX];
  62. /* Memory statistics */
  63. int need_stats_update;
  64. struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
  65. };
  66. static struct virtio_device_id id_table[] = {
  67. { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
  68. { 0 },
  69. };
  70. static u32 page_to_balloon_pfn(struct page *page)
  71. {
  72. unsigned long pfn = page_to_pfn(page);
  73. BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT);
  74. /* Convert pfn from Linux page size to balloon page size. */
  75. return pfn * VIRTIO_BALLOON_PAGES_PER_PAGE;
  76. }
  77. static struct page *balloon_pfn_to_page(u32 pfn)
  78. {
  79. BUG_ON(pfn % VIRTIO_BALLOON_PAGES_PER_PAGE);
  80. return pfn_to_page(pfn / VIRTIO_BALLOON_PAGES_PER_PAGE);
  81. }
  82. static void balloon_ack(struct virtqueue *vq)
  83. {
  84. struct virtio_balloon *vb = vq->vdev->priv;
  85. wake_up(&vb->acked);
  86. }
  87. static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
  88. {
  89. struct scatterlist sg;
  90. unsigned int len;
  91. sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
  92. /* We should always be able to add one buffer to an empty queue. */
  93. virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  94. virtqueue_kick(vq);
  95. /* When host has read buffer, this completes via balloon_ack */
  96. wait_event(vb->acked, virtqueue_get_buf(vq, &len));
  97. }
  98. static void set_page_pfns(u32 pfns[], struct page *page)
  99. {
  100. unsigned int i;
  101. /* Set balloon pfns pointing at this page.
  102. * Note that the first pfn points at start of the page. */
  103. for (i = 0; i < VIRTIO_BALLOON_PAGES_PER_PAGE; i++)
  104. pfns[i] = page_to_balloon_pfn(page) + i;
  105. }
  106. static void fill_balloon(struct virtio_balloon *vb, size_t num)
  107. {
  108. struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
  109. /* We can only do one array worth at a time. */
  110. num = min(num, ARRAY_SIZE(vb->pfns));
  111. mutex_lock(&vb->balloon_lock);
  112. for (vb->num_pfns = 0; vb->num_pfns < num;
  113. vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  114. struct page *page = balloon_page_enqueue(vb_dev_info);
  115. if (!page) {
  116. dev_info_ratelimited(&vb->vdev->dev,
  117. "Out of puff! Can't get %u pages\n",
  118. VIRTIO_BALLOON_PAGES_PER_PAGE);
  119. /* Sleep for at least 1/5 of a second before retry. */
  120. msleep(200);
  121. break;
  122. }
  123. set_page_pfns(vb->pfns + vb->num_pfns, page);
  124. vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
  125. adjust_managed_page_count(page, -1);
  126. }
  127. /* Did we get any? */
  128. if (vb->num_pfns != 0)
  129. tell_host(vb, vb->inflate_vq);
  130. mutex_unlock(&vb->balloon_lock);
  131. }
  132. static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
  133. {
  134. unsigned int i;
  135. /* Find pfns pointing at start of each page, get pages and free them. */
  136. for (i = 0; i < num; i += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  137. struct page *page = balloon_pfn_to_page(pfns[i]);
  138. adjust_managed_page_count(page, 1);
  139. put_page(page); /* balloon reference */
  140. }
  141. }
  142. static void leak_balloon(struct virtio_balloon *vb, size_t num)
  143. {
  144. struct page *page;
  145. struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
  146. /* We can only do one array worth at a time. */
  147. num = min(num, ARRAY_SIZE(vb->pfns));
  148. mutex_lock(&vb->balloon_lock);
  149. for (vb->num_pfns = 0; vb->num_pfns < num;
  150. vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  151. page = balloon_page_dequeue(vb_dev_info);
  152. if (!page)
  153. break;
  154. set_page_pfns(vb->pfns + vb->num_pfns, page);
  155. vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
  156. }
  157. /*
  158. * Note that if
  159. * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
  160. * is true, we *have* to do it in this order
  161. */
  162. if (vb->num_pfns != 0)
  163. tell_host(vb, vb->deflate_vq);
  164. mutex_unlock(&vb->balloon_lock);
  165. release_pages_by_pfn(vb->pfns, vb->num_pfns);
  166. }
  167. static inline void update_stat(struct virtio_balloon *vb, int idx,
  168. u16 tag, u64 val)
  169. {
  170. BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
  171. vb->stats[idx].tag = tag;
  172. vb->stats[idx].val = val;
  173. }
  174. #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
  175. static void update_balloon_stats(struct virtio_balloon *vb)
  176. {
  177. unsigned long events[NR_VM_EVENT_ITEMS];
  178. struct sysinfo i;
  179. int idx = 0;
  180. all_vm_events(events);
  181. si_meminfo(&i);
  182. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
  183. pages_to_bytes(events[PSWPIN]));
  184. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
  185. pages_to_bytes(events[PSWPOUT]));
  186. update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
  187. update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
  188. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
  189. pages_to_bytes(i.freeram));
  190. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
  191. pages_to_bytes(i.totalram));
  192. }
  193. /*
  194. * While most virtqueues communicate guest-initiated requests to the hypervisor,
  195. * the stats queue operates in reverse. The driver initializes the virtqueue
  196. * with a single buffer. From that point forward, all conversations consist of
  197. * a hypervisor request (a call to this function) which directs us to refill
  198. * the virtqueue with a fresh stats buffer. Since stats collection can sleep,
  199. * we notify our kthread which does the actual work via stats_handle_request().
  200. */
  201. static void stats_request(struct virtqueue *vq)
  202. {
  203. struct virtio_balloon *vb = vq->vdev->priv;
  204. vb->need_stats_update = 1;
  205. wake_up(&vb->config_change);
  206. }
  207. static void stats_handle_request(struct virtio_balloon *vb)
  208. {
  209. struct virtqueue *vq;
  210. struct scatterlist sg;
  211. unsigned int len;
  212. vb->need_stats_update = 0;
  213. update_balloon_stats(vb);
  214. vq = vb->stats_vq;
  215. if (!virtqueue_get_buf(vq, &len))
  216. return;
  217. sg_init_one(&sg, vb->stats, sizeof(vb->stats));
  218. virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  219. virtqueue_kick(vq);
  220. }
  221. static void virtballoon_changed(struct virtio_device *vdev)
  222. {
  223. struct virtio_balloon *vb = vdev->priv;
  224. wake_up(&vb->config_change);
  225. }
  226. static inline s64 towards_target(struct virtio_balloon *vb)
  227. {
  228. __le32 v;
  229. s64 target;
  230. virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, &v);
  231. target = le32_to_cpu(v);
  232. return target - vb->num_pages;
  233. }
  234. static void update_balloon_size(struct virtio_balloon *vb)
  235. {
  236. __le32 actual = cpu_to_le32(vb->num_pages);
  237. virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual,
  238. &actual);
  239. }
  240. static int balloon(void *_vballoon)
  241. {
  242. struct virtio_balloon *vb = _vballoon;
  243. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  244. set_freezable();
  245. while (!kthread_should_stop()) {
  246. s64 diff;
  247. try_to_freeze();
  248. add_wait_queue(&vb->config_change, &wait);
  249. for (;;) {
  250. if ((diff = towards_target(vb)) != 0 ||
  251. vb->need_stats_update ||
  252. kthread_should_stop() ||
  253. freezing(current))
  254. break;
  255. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  256. }
  257. remove_wait_queue(&vb->config_change, &wait);
  258. if (vb->need_stats_update)
  259. stats_handle_request(vb);
  260. if (diff > 0)
  261. fill_balloon(vb, diff);
  262. else if (diff < 0)
  263. leak_balloon(vb, -diff);
  264. update_balloon_size(vb);
  265. /*
  266. * For large balloon changes, we could spend a lot of time
  267. * and always have work to do. Be nice if preempt disabled.
  268. */
  269. cond_resched();
  270. }
  271. return 0;
  272. }
  273. static int init_vqs(struct virtio_balloon *vb)
  274. {
  275. struct virtqueue *vqs[3];
  276. vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request };
  277. const char *names[] = { "inflate", "deflate", "stats" };
  278. int err, nvqs;
  279. /*
  280. * We expect two virtqueues: inflate and deflate, and
  281. * optionally stat.
  282. */
  283. nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
  284. err = vb->vdev->config->find_vqs(vb->vdev, nvqs, vqs, callbacks, names);
  285. if (err)
  286. return err;
  287. vb->inflate_vq = vqs[0];
  288. vb->deflate_vq = vqs[1];
  289. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
  290. struct scatterlist sg;
  291. vb->stats_vq = vqs[2];
  292. /*
  293. * Prime this virtqueue with one buffer so the hypervisor can
  294. * use it to signal us later (it can't be broken yet!).
  295. */
  296. sg_init_one(&sg, vb->stats, sizeof vb->stats);
  297. if (virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb, GFP_KERNEL)
  298. < 0)
  299. BUG();
  300. virtqueue_kick(vb->stats_vq);
  301. }
  302. return 0;
  303. }
  304. #ifdef CONFIG_BALLOON_COMPACTION
  305. /*
  306. * virtballoon_migratepage - perform the balloon page migration on behalf of
  307. * a compation thread. (called under page lock)
  308. * @vb_dev_info: the balloon device
  309. * @newpage: page that will replace the isolated page after migration finishes.
  310. * @page : the isolated (old) page that is about to be migrated to newpage.
  311. * @mode : compaction mode -- not used for balloon page migration.
  312. *
  313. * After a ballooned page gets isolated by compaction procedures, this is the
  314. * function that performs the page migration on behalf of a compaction thread
  315. * The page migration for virtio balloon is done in a simple swap fashion which
  316. * follows these two macro steps:
  317. * 1) insert newpage into vb->pages list and update the host about it;
  318. * 2) update the host about the old page removed from vb->pages list;
  319. *
  320. * This function preforms the balloon page migration task.
  321. * Called through balloon_mapping->a_ops->migratepage
  322. */
  323. static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info,
  324. struct page *newpage, struct page *page, enum migrate_mode mode)
  325. {
  326. struct virtio_balloon *vb = container_of(vb_dev_info,
  327. struct virtio_balloon, vb_dev_info);
  328. unsigned long flags;
  329. /*
  330. * In order to avoid lock contention while migrating pages concurrently
  331. * to leak_balloon() or fill_balloon() we just give up the balloon_lock
  332. * this turn, as it is easier to retry the page migration later.
  333. * This also prevents fill_balloon() getting stuck into a mutex
  334. * recursion in the case it ends up triggering memory compaction
  335. * while it is attempting to inflate the ballon.
  336. */
  337. if (!mutex_trylock(&vb->balloon_lock))
  338. return -EAGAIN;
  339. get_page(newpage); /* balloon reference */
  340. /* balloon's page migration 1st step -- inflate "newpage" */
  341. spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
  342. balloon_page_insert(vb_dev_info, newpage);
  343. vb_dev_info->isolated_pages--;
  344. __count_vm_event(BALLOON_MIGRATE);
  345. spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
  346. vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
  347. set_page_pfns(vb->pfns, newpage);
  348. tell_host(vb, vb->inflate_vq);
  349. /* balloon's page migration 2nd step -- deflate "page" */
  350. balloon_page_delete(page);
  351. vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
  352. set_page_pfns(vb->pfns, page);
  353. tell_host(vb, vb->deflate_vq);
  354. mutex_unlock(&vb->balloon_lock);
  355. put_page(page); /* balloon reference */
  356. return MIGRATEPAGE_SUCCESS;
  357. }
  358. #endif /* CONFIG_BALLOON_COMPACTION */
  359. static int virtballoon_probe(struct virtio_device *vdev)
  360. {
  361. struct virtio_balloon *vb;
  362. int err;
  363. vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
  364. if (!vb) {
  365. err = -ENOMEM;
  366. goto out;
  367. }
  368. vb->num_pages = 0;
  369. mutex_init(&vb->balloon_lock);
  370. init_waitqueue_head(&vb->config_change);
  371. init_waitqueue_head(&vb->acked);
  372. vb->vdev = vdev;
  373. vb->need_stats_update = 0;
  374. balloon_devinfo_init(&vb->vb_dev_info);
  375. #ifdef CONFIG_BALLOON_COMPACTION
  376. vb->vb_dev_info.migratepage = virtballoon_migratepage;
  377. #endif
  378. err = init_vqs(vb);
  379. if (err)
  380. goto out_free_vb;
  381. virtio_device_ready(vdev);
  382. vb->thread = kthread_run(balloon, vb, "vballoon");
  383. if (IS_ERR(vb->thread)) {
  384. err = PTR_ERR(vb->thread);
  385. goto out_del_vqs;
  386. }
  387. return 0;
  388. out_del_vqs:
  389. vdev->config->del_vqs(vdev);
  390. out_free_vb:
  391. kfree(vb);
  392. out:
  393. return err;
  394. }
  395. static void remove_common(struct virtio_balloon *vb)
  396. {
  397. /* There might be pages left in the balloon: free them. */
  398. while (vb->num_pages)
  399. leak_balloon(vb, vb->num_pages);
  400. update_balloon_size(vb);
  401. /* Now we reset the device so we can clean up the queues. */
  402. vb->vdev->config->reset(vb->vdev);
  403. vb->vdev->config->del_vqs(vb->vdev);
  404. }
  405. static void virtballoon_remove(struct virtio_device *vdev)
  406. {
  407. struct virtio_balloon *vb = vdev->priv;
  408. kthread_stop(vb->thread);
  409. remove_common(vb);
  410. kfree(vb);
  411. }
  412. #ifdef CONFIG_PM_SLEEP
  413. static int virtballoon_freeze(struct virtio_device *vdev)
  414. {
  415. struct virtio_balloon *vb = vdev->priv;
  416. /*
  417. * The kthread is already frozen by the PM core before this
  418. * function is called.
  419. */
  420. remove_common(vb);
  421. return 0;
  422. }
  423. static int virtballoon_restore(struct virtio_device *vdev)
  424. {
  425. struct virtio_balloon *vb = vdev->priv;
  426. int ret;
  427. ret = init_vqs(vdev->priv);
  428. if (ret)
  429. return ret;
  430. virtio_device_ready(vdev);
  431. fill_balloon(vb, towards_target(vb));
  432. update_balloon_size(vb);
  433. return 0;
  434. }
  435. #endif
  436. static unsigned int features[] = {
  437. VIRTIO_BALLOON_F_MUST_TELL_HOST,
  438. VIRTIO_BALLOON_F_STATS_VQ,
  439. };
  440. static struct virtio_driver virtio_balloon_driver = {
  441. .feature_table = features,
  442. .feature_table_size = ARRAY_SIZE(features),
  443. .driver.name = KBUILD_MODNAME,
  444. .driver.owner = THIS_MODULE,
  445. .id_table = id_table,
  446. .probe = virtballoon_probe,
  447. .remove = virtballoon_remove,
  448. .config_changed = virtballoon_changed,
  449. #ifdef CONFIG_PM_SLEEP
  450. .freeze = virtballoon_freeze,
  451. .restore = virtballoon_restore,
  452. #endif
  453. };
  454. module_virtio_driver(virtio_balloon_driver);
  455. MODULE_DEVICE_TABLE(virtio, id_table);
  456. MODULE_DESCRIPTION("Virtio balloon driver");
  457. MODULE_LICENSE("GPL");