virtio_balloon.c 18 KB

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