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