virtio_balloon.c 18 KB

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