virtio_balloon.c 20 KB

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