virtio_balloon.c 19 KB

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