virtio_balloon.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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/wait.h>
  30. #include <linux/mm.h>
  31. #include <linux/mount.h>
  32. #include <linux/magic.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 VIRTBALLOON_OOM_NOTIFY_PRIORITY 80
  41. #ifdef CONFIG_BALLOON_COMPACTION
  42. static struct vfsmount *balloon_mnt;
  43. #endif
  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. __virtio32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX];
  69. /* Memory statistics */
  70. struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
  71. /* To register a shrinker to shrink memory upon memory pressure */
  72. struct shrinker shrinker;
  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 void balloon_ack(struct virtqueue *vq)
  86. {
  87. struct virtio_balloon *vb = vq->vdev->priv;
  88. wake_up(&vb->acked);
  89. }
  90. static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
  91. {
  92. struct scatterlist sg;
  93. unsigned int len;
  94. sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
  95. /* We should always be able to add one buffer to an empty queue. */
  96. virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  97. virtqueue_kick(vq);
  98. /* When host has read buffer, this completes via balloon_ack */
  99. wait_event(vb->acked, virtqueue_get_buf(vq, &len));
  100. }
  101. static void set_page_pfns(struct virtio_balloon *vb,
  102. __virtio32 pfns[], struct page *page)
  103. {
  104. unsigned int i;
  105. BUILD_BUG_ON(VIRTIO_BALLOON_PAGES_PER_PAGE > VIRTIO_BALLOON_ARRAY_PFNS_MAX);
  106. /*
  107. * Set balloon pfns pointing at this page.
  108. * Note that the first pfn points at start of the page.
  109. */
  110. for (i = 0; i < VIRTIO_BALLOON_PAGES_PER_PAGE; i++)
  111. pfns[i] = cpu_to_virtio32(vb->vdev,
  112. page_to_balloon_pfn(page) + i);
  113. }
  114. static unsigned fill_balloon(struct virtio_balloon *vb, size_t num)
  115. {
  116. unsigned num_allocated_pages;
  117. unsigned num_pfns;
  118. struct page *page;
  119. LIST_HEAD(pages);
  120. /* We can only do one array worth at a time. */
  121. num = min(num, ARRAY_SIZE(vb->pfns));
  122. for (num_pfns = 0; num_pfns < num;
  123. num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  124. struct page *page = balloon_page_alloc();
  125. if (!page) {
  126. dev_info_ratelimited(&vb->vdev->dev,
  127. "Out of puff! Can't get %u pages\n",
  128. VIRTIO_BALLOON_PAGES_PER_PAGE);
  129. /* Sleep for at least 1/5 of a second before retry. */
  130. msleep(200);
  131. break;
  132. }
  133. balloon_page_push(&pages, page);
  134. }
  135. mutex_lock(&vb->balloon_lock);
  136. vb->num_pfns = 0;
  137. while ((page = balloon_page_pop(&pages))) {
  138. balloon_page_enqueue(&vb->vb_dev_info, page);
  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. vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE;
  145. }
  146. num_allocated_pages = vb->num_pfns;
  147. /* Did we get any? */
  148. if (vb->num_pfns != 0)
  149. tell_host(vb, vb->inflate_vq);
  150. mutex_unlock(&vb->balloon_lock);
  151. return num_allocated_pages;
  152. }
  153. static void release_pages_balloon(struct virtio_balloon *vb,
  154. struct list_head *pages)
  155. {
  156. struct page *page, *next;
  157. list_for_each_entry_safe(page, next, pages, lru) {
  158. if (!virtio_has_feature(vb->vdev,
  159. VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  160. adjust_managed_page_count(page, 1);
  161. list_del(&page->lru);
  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. LIST_HEAD(pages);
  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. list_add(&page->lru, &pages);
  183. vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
  184. }
  185. num_freed_pages = vb->num_pfns;
  186. /*
  187. * Note that if
  188. * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
  189. * is true, we *have* to do it in this order
  190. */
  191. if (vb->num_pfns != 0)
  192. tell_host(vb, vb->deflate_vq);
  193. release_pages_balloon(vb, &pages);
  194. mutex_unlock(&vb->balloon_lock);
  195. return num_freed_pages;
  196. }
  197. static inline void update_stat(struct virtio_balloon *vb, int idx,
  198. u16 tag, u64 val)
  199. {
  200. BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
  201. vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag);
  202. vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val);
  203. }
  204. #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
  205. static unsigned int update_balloon_stats(struct virtio_balloon *vb)
  206. {
  207. unsigned long events[NR_VM_EVENT_ITEMS];
  208. struct sysinfo i;
  209. unsigned int idx = 0;
  210. long available;
  211. unsigned long caches;
  212. all_vm_events(events);
  213. si_meminfo(&i);
  214. available = si_mem_available();
  215. caches = global_node_page_state(NR_FILE_PAGES);
  216. #ifdef CONFIG_VM_EVENT_COUNTERS
  217. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
  218. pages_to_bytes(events[PSWPIN]));
  219. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
  220. pages_to_bytes(events[PSWPOUT]));
  221. update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
  222. update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
  223. #ifdef CONFIG_HUGETLB_PAGE
  224. update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGALLOC,
  225. events[HTLB_BUDDY_PGALLOC]);
  226. update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGFAIL,
  227. events[HTLB_BUDDY_PGALLOC_FAIL]);
  228. #endif
  229. #endif
  230. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
  231. pages_to_bytes(i.freeram));
  232. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
  233. pages_to_bytes(i.totalram));
  234. update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
  235. pages_to_bytes(available));
  236. update_stat(vb, idx++, VIRTIO_BALLOON_S_CACHES,
  237. pages_to_bytes(caches));
  238. return idx;
  239. }
  240. /*
  241. * While most virtqueues communicate guest-initiated requests to the hypervisor,
  242. * the stats queue operates in reverse. The driver initializes the virtqueue
  243. * with a single buffer. From that point forward, all conversations consist of
  244. * a hypervisor request (a call to this function) which directs us to refill
  245. * the virtqueue with a fresh stats buffer. Since stats collection can sleep,
  246. * we delegate the job to a freezable workqueue that will do the actual work via
  247. * stats_handle_request().
  248. */
  249. static void stats_request(struct virtqueue *vq)
  250. {
  251. struct virtio_balloon *vb = vq->vdev->priv;
  252. spin_lock(&vb->stop_update_lock);
  253. if (!vb->stop_update)
  254. queue_work(system_freezable_wq, &vb->update_balloon_stats_work);
  255. spin_unlock(&vb->stop_update_lock);
  256. }
  257. static void stats_handle_request(struct virtio_balloon *vb)
  258. {
  259. struct virtqueue *vq;
  260. struct scatterlist sg;
  261. unsigned int len, num_stats;
  262. num_stats = update_balloon_stats(vb);
  263. vq = vb->stats_vq;
  264. if (!virtqueue_get_buf(vq, &len))
  265. return;
  266. sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
  267. virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  268. virtqueue_kick(vq);
  269. }
  270. static void virtballoon_changed(struct virtio_device *vdev)
  271. {
  272. struct virtio_balloon *vb = vdev->priv;
  273. unsigned long flags;
  274. spin_lock_irqsave(&vb->stop_update_lock, flags);
  275. if (!vb->stop_update)
  276. queue_work(system_freezable_wq, &vb->update_balloon_size_work);
  277. spin_unlock_irqrestore(&vb->stop_update_lock, flags);
  278. }
  279. static inline s64 towards_target(struct virtio_balloon *vb)
  280. {
  281. s64 target;
  282. u32 num_pages;
  283. virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages,
  284. &num_pages);
  285. /* Legacy balloon config space is LE, unlike all other devices. */
  286. if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1))
  287. num_pages = le32_to_cpu((__force __le32)num_pages);
  288. target = num_pages;
  289. return target - vb->num_pages;
  290. }
  291. static void update_balloon_size(struct virtio_balloon *vb)
  292. {
  293. u32 actual = vb->num_pages;
  294. /* Legacy balloon config space is LE, unlike all other devices. */
  295. if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1))
  296. actual = (__force u32)cpu_to_le32(actual);
  297. virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual,
  298. &actual);
  299. }
  300. static void update_balloon_stats_func(struct work_struct *work)
  301. {
  302. struct virtio_balloon *vb;
  303. vb = container_of(work, struct virtio_balloon,
  304. update_balloon_stats_work);
  305. stats_handle_request(vb);
  306. }
  307. static void update_balloon_size_func(struct work_struct *work)
  308. {
  309. struct virtio_balloon *vb;
  310. s64 diff;
  311. vb = container_of(work, struct virtio_balloon,
  312. update_balloon_size_work);
  313. diff = towards_target(vb);
  314. if (diff > 0)
  315. diff -= fill_balloon(vb, diff);
  316. else if (diff < 0)
  317. diff += leak_balloon(vb, -diff);
  318. update_balloon_size(vb);
  319. if (diff)
  320. queue_work(system_freezable_wq, work);
  321. }
  322. static int init_vqs(struct virtio_balloon *vb)
  323. {
  324. struct virtqueue *vqs[3];
  325. vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request };
  326. static const char * const names[] = { "inflate", "deflate", "stats" };
  327. int err, nvqs;
  328. /*
  329. * We expect two virtqueues: inflate and deflate, and
  330. * optionally stat.
  331. */
  332. nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
  333. err = virtio_find_vqs(vb->vdev, nvqs, vqs, callbacks, names, NULL);
  334. if (err)
  335. return err;
  336. vb->inflate_vq = vqs[0];
  337. vb->deflate_vq = vqs[1];
  338. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
  339. struct scatterlist sg;
  340. unsigned int num_stats;
  341. vb->stats_vq = vqs[2];
  342. /*
  343. * Prime this virtqueue with one buffer so the hypervisor can
  344. * use it to signal us later (it can't be broken yet!).
  345. */
  346. num_stats = update_balloon_stats(vb);
  347. sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
  348. err = virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb,
  349. GFP_KERNEL);
  350. if (err) {
  351. dev_warn(&vb->vdev->dev, "%s: add stat_vq failed\n",
  352. __func__);
  353. return err;
  354. }
  355. virtqueue_kick(vb->stats_vq);
  356. }
  357. return 0;
  358. }
  359. #ifdef CONFIG_BALLOON_COMPACTION
  360. /*
  361. * virtballoon_migratepage - perform the balloon page migration on behalf of
  362. * a compation thread. (called under page lock)
  363. * @vb_dev_info: the balloon device
  364. * @newpage: page that will replace the isolated page after migration finishes.
  365. * @page : the isolated (old) page that is about to be migrated to newpage.
  366. * @mode : compaction mode -- not used for balloon page migration.
  367. *
  368. * After a ballooned page gets isolated by compaction procedures, this is the
  369. * function that performs the page migration on behalf of a compaction thread
  370. * The page migration for virtio balloon is done in a simple swap fashion which
  371. * follows these two macro steps:
  372. * 1) insert newpage into vb->pages list and update the host about it;
  373. * 2) update the host about the old page removed from vb->pages list;
  374. *
  375. * This function preforms the balloon page migration task.
  376. * Called through balloon_mapping->a_ops->migratepage
  377. */
  378. static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info,
  379. struct page *newpage, struct page *page, enum migrate_mode mode)
  380. {
  381. struct virtio_balloon *vb = container_of(vb_dev_info,
  382. struct virtio_balloon, vb_dev_info);
  383. unsigned long flags;
  384. /*
  385. * In order to avoid lock contention while migrating pages concurrently
  386. * to leak_balloon() or fill_balloon() we just give up the balloon_lock
  387. * this turn, as it is easier to retry the page migration later.
  388. * This also prevents fill_balloon() getting stuck into a mutex
  389. * recursion in the case it ends up triggering memory compaction
  390. * while it is attempting to inflate the ballon.
  391. */
  392. if (!mutex_trylock(&vb->balloon_lock))
  393. return -EAGAIN;
  394. get_page(newpage); /* balloon reference */
  395. /*
  396. * When we migrate a page to a different zone and adjusted the
  397. * managed page count when inflating, we have to fixup the count of
  398. * both involved zones.
  399. */
  400. if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM) &&
  401. page_zone(page) != page_zone(newpage)) {
  402. adjust_managed_page_count(page, 1);
  403. adjust_managed_page_count(newpage, -1);
  404. }
  405. /* balloon's page migration 1st step -- inflate "newpage" */
  406. spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
  407. balloon_page_insert(vb_dev_info, newpage);
  408. vb_dev_info->isolated_pages--;
  409. __count_vm_event(BALLOON_MIGRATE);
  410. spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
  411. vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
  412. set_page_pfns(vb, vb->pfns, newpage);
  413. tell_host(vb, vb->inflate_vq);
  414. /* balloon's page migration 2nd step -- deflate "page" */
  415. spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
  416. balloon_page_delete(page);
  417. spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
  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 unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,
  441. struct shrink_control *sc)
  442. {
  443. unsigned long pages_to_free, pages_freed = 0;
  444. struct virtio_balloon *vb = container_of(shrinker,
  445. struct virtio_balloon, shrinker);
  446. pages_to_free = sc->nr_to_scan * VIRTIO_BALLOON_PAGES_PER_PAGE;
  447. /*
  448. * One invocation of leak_balloon can deflate at most
  449. * VIRTIO_BALLOON_ARRAY_PFNS_MAX balloon pages, so we call it
  450. * multiple times to deflate pages till reaching pages_to_free.
  451. */
  452. while (vb->num_pages && pages_to_free) {
  453. pages_to_free -= pages_freed;
  454. pages_freed += leak_balloon(vb, pages_to_free);
  455. }
  456. update_balloon_size(vb);
  457. return pages_freed / VIRTIO_BALLOON_PAGES_PER_PAGE;
  458. }
  459. static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker,
  460. struct shrink_control *sc)
  461. {
  462. struct virtio_balloon *vb = container_of(shrinker,
  463. struct virtio_balloon, shrinker);
  464. return vb->num_pages / VIRTIO_BALLOON_PAGES_PER_PAGE;
  465. }
  466. static void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb)
  467. {
  468. unregister_shrinker(&vb->shrinker);
  469. }
  470. static int virtio_balloon_register_shrinker(struct virtio_balloon *vb)
  471. {
  472. vb->shrinker.scan_objects = virtio_balloon_shrinker_scan;
  473. vb->shrinker.count_objects = virtio_balloon_shrinker_count;
  474. vb->shrinker.seeks = DEFAULT_SEEKS;
  475. return register_shrinker(&vb->shrinker);
  476. }
  477. static int virtballoon_probe(struct virtio_device *vdev)
  478. {
  479. struct virtio_balloon *vb;
  480. int err;
  481. if (!vdev->config->get) {
  482. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  483. __func__);
  484. return -EINVAL;
  485. }
  486. vdev->priv = vb = kzalloc(sizeof(*vb), GFP_KERNEL);
  487. if (!vb) {
  488. err = -ENOMEM;
  489. goto out;
  490. }
  491. INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
  492. INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
  493. spin_lock_init(&vb->stop_update_lock);
  494. mutex_init(&vb->balloon_lock);
  495. init_waitqueue_head(&vb->acked);
  496. vb->vdev = vdev;
  497. balloon_devinfo_init(&vb->vb_dev_info);
  498. err = init_vqs(vb);
  499. if (err)
  500. goto out_free_vb;
  501. #ifdef CONFIG_BALLOON_COMPACTION
  502. balloon_mnt = kern_mount(&balloon_fs);
  503. if (IS_ERR(balloon_mnt)) {
  504. err = PTR_ERR(balloon_mnt);
  505. goto out_del_vqs;
  506. }
  507. vb->vb_dev_info.migratepage = virtballoon_migratepage;
  508. vb->vb_dev_info.inode = alloc_anon_inode(balloon_mnt->mnt_sb);
  509. if (IS_ERR(vb->vb_dev_info.inode)) {
  510. err = PTR_ERR(vb->vb_dev_info.inode);
  511. kern_unmount(balloon_mnt);
  512. goto out_del_vqs;
  513. }
  514. vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops;
  515. #endif
  516. /*
  517. * We continue to use VIRTIO_BALLOON_F_DEFLATE_ON_OOM to decide if a
  518. * shrinker needs to be registered to relieve memory pressure.
  519. */
  520. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) {
  521. err = virtio_balloon_register_shrinker(vb);
  522. if (err)
  523. goto out_del_vqs;
  524. }
  525. virtio_device_ready(vdev);
  526. if (towards_target(vb))
  527. virtballoon_changed(vdev);
  528. return 0;
  529. out_del_vqs:
  530. vdev->config->del_vqs(vdev);
  531. out_free_vb:
  532. kfree(vb);
  533. out:
  534. return err;
  535. }
  536. static void remove_common(struct virtio_balloon *vb)
  537. {
  538. /* There might be pages left in the balloon: free them. */
  539. while (vb->num_pages)
  540. leak_balloon(vb, vb->num_pages);
  541. update_balloon_size(vb);
  542. /* Now we reset the device so we can clean up the queues. */
  543. vb->vdev->config->reset(vb->vdev);
  544. vb->vdev->config->del_vqs(vb->vdev);
  545. }
  546. static void virtballoon_remove(struct virtio_device *vdev)
  547. {
  548. struct virtio_balloon *vb = vdev->priv;
  549. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  550. virtio_balloon_unregister_shrinker(vb);
  551. spin_lock_irq(&vb->stop_update_lock);
  552. vb->stop_update = true;
  553. spin_unlock_irq(&vb->stop_update_lock);
  554. cancel_work_sync(&vb->update_balloon_size_work);
  555. cancel_work_sync(&vb->update_balloon_stats_work);
  556. remove_common(vb);
  557. #ifdef CONFIG_BALLOON_COMPACTION
  558. if (vb->vb_dev_info.inode)
  559. iput(vb->vb_dev_info.inode);
  560. kern_unmount(balloon_mnt);
  561. #endif
  562. kfree(vb);
  563. }
  564. #ifdef CONFIG_PM_SLEEP
  565. static int virtballoon_freeze(struct virtio_device *vdev)
  566. {
  567. struct virtio_balloon *vb = vdev->priv;
  568. /*
  569. * The workqueue is already frozen by the PM core before this
  570. * function is called.
  571. */
  572. remove_common(vb);
  573. return 0;
  574. }
  575. static int virtballoon_restore(struct virtio_device *vdev)
  576. {
  577. struct virtio_balloon *vb = vdev->priv;
  578. int ret;
  579. ret = init_vqs(vdev->priv);
  580. if (ret)
  581. return ret;
  582. virtio_device_ready(vdev);
  583. if (towards_target(vb))
  584. virtballoon_changed(vdev);
  585. update_balloon_size(vb);
  586. return 0;
  587. }
  588. #endif
  589. static int virtballoon_validate(struct virtio_device *vdev)
  590. {
  591. __virtio_clear_bit(vdev, VIRTIO_F_IOMMU_PLATFORM);
  592. return 0;
  593. }
  594. static unsigned int features[] = {
  595. VIRTIO_BALLOON_F_MUST_TELL_HOST,
  596. VIRTIO_BALLOON_F_STATS_VQ,
  597. VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
  598. };
  599. static struct virtio_driver virtio_balloon_driver = {
  600. .feature_table = features,
  601. .feature_table_size = ARRAY_SIZE(features),
  602. .driver.name = KBUILD_MODNAME,
  603. .driver.owner = THIS_MODULE,
  604. .id_table = id_table,
  605. .validate = virtballoon_validate,
  606. .probe = virtballoon_probe,
  607. .remove = virtballoon_remove,
  608. .config_changed = virtballoon_changed,
  609. #ifdef CONFIG_PM_SLEEP
  610. .freeze = virtballoon_freeze,
  611. .restore = virtballoon_restore,
  612. #endif
  613. };
  614. module_virtio_driver(virtio_balloon_driver);
  615. MODULE_DEVICE_TABLE(virtio, id_table);
  616. MODULE_DESCRIPTION("Virtio balloon driver");
  617. MODULE_LICENSE("GPL");