virtio_balloon.c 15 KB

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