virtio_balloon.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio balloon implementation, inspired by Dor Laor and Marcelo
  4. * Tosatti's implementations.
  5. *
  6. * Copyright 2008 Rusty Russell IBM Corporation
  7. */
  8. #include <linux/virtio.h>
  9. #include <linux/virtio_balloon.h>
  10. #include <linux/swap.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/delay.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/balloon.h>
  16. #include <linux/oom.h>
  17. #include <linux/wait.h>
  18. #include <linux/mm.h>
  19. #include <linux/page_reporting.h>
  20. /*
  21. * Balloon device works in 4K page units. So each page is pointed to by
  22. * multiple balloon pages. All memory counters in this driver are in balloon
  23. * page units.
  24. */
  25. #define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned int)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT)
  26. #define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256
  27. /* Maximum number of (4k) pages to deflate on OOM notifications. */
  28. #define VIRTIO_BALLOON_OOM_NR_PAGES 256
  29. #define VIRTIO_BALLOON_OOM_NOTIFY_PRIORITY 80
  30. #define VIRTIO_BALLOON_FREE_PAGE_ALLOC_FLAG (__GFP_NORETRY | __GFP_NOWARN | \
  31. __GFP_NOMEMALLOC)
  32. /* The order of free page blocks to report to host */
  33. #define VIRTIO_BALLOON_HINT_BLOCK_ORDER MAX_PAGE_ORDER
  34. /* The size of a free page block in bytes */
  35. #define VIRTIO_BALLOON_HINT_BLOCK_BYTES \
  36. (1 << (VIRTIO_BALLOON_HINT_BLOCK_ORDER + PAGE_SHIFT))
  37. #define VIRTIO_BALLOON_HINT_BLOCK_PAGES (1 << VIRTIO_BALLOON_HINT_BLOCK_ORDER)
  38. enum virtio_balloon_vq {
  39. VIRTIO_BALLOON_VQ_INFLATE,
  40. VIRTIO_BALLOON_VQ_DEFLATE,
  41. VIRTIO_BALLOON_VQ_STATS,
  42. VIRTIO_BALLOON_VQ_FREE_PAGE,
  43. VIRTIO_BALLOON_VQ_REPORTING,
  44. VIRTIO_BALLOON_VQ_MAX
  45. };
  46. enum virtio_balloon_config_read {
  47. VIRTIO_BALLOON_CONFIG_READ_CMD_ID = 0,
  48. };
  49. struct virtio_balloon {
  50. struct virtio_device *vdev;
  51. struct virtqueue *inflate_vq, *deflate_vq, *stats_vq, *free_page_vq;
  52. /* Balloon's own wq for cpu-intensive work items */
  53. struct workqueue_struct *balloon_wq;
  54. /* The free page reporting work item submitted to the balloon wq */
  55. struct work_struct report_free_page_work;
  56. /* The balloon servicing is delegated to a freezable workqueue. */
  57. struct work_struct update_balloon_stats_work;
  58. struct work_struct update_balloon_size_work;
  59. /* Prevent updating balloon when it is being canceled. */
  60. spinlock_t stop_update_lock;
  61. bool stop_update;
  62. /* Bitmap to indicate if reading the related config fields are needed */
  63. unsigned long config_read_bitmap;
  64. /* The list of allocated free pages, waiting to be given back to mm */
  65. struct list_head free_page_list;
  66. spinlock_t free_page_list_lock;
  67. /* The number of free page blocks on the above list */
  68. unsigned long num_free_page_blocks;
  69. /*
  70. * The cmd id received from host.
  71. * Read it via virtio_balloon_cmd_id_received to get the latest value
  72. * sent from host.
  73. */
  74. u32 cmd_id_received_cache;
  75. /* The cmd id that is actively in use */
  76. __virtio32 cmd_id_active;
  77. /* Buffer to store the stop sign */
  78. __virtio32 cmd_id_stop;
  79. /* Waiting for host to ack the pages we released. */
  80. wait_queue_head_t acked;
  81. /* Number of balloon pages we've told the Host we're not using. */
  82. unsigned int num_pages;
  83. /*
  84. * The pages we've told the Host we're not using are enqueued
  85. * at vb_dev_info->pages list.
  86. * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE
  87. * to num_pages above.
  88. */
  89. struct balloon_dev_info vb_dev_info;
  90. /* Synchronize access/update to this struct virtio_balloon elements */
  91. struct mutex balloon_lock;
  92. /* The array of pfns we tell the Host about. */
  93. unsigned int num_pfns;
  94. __virtio32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX];
  95. /* Memory statistics */
  96. struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
  97. /* Shrinker to return free pages - VIRTIO_BALLOON_F_FREE_PAGE_HINT */
  98. struct shrinker *shrinker;
  99. /* OOM notifier to deflate on OOM - VIRTIO_BALLOON_F_DEFLATE_ON_OOM */
  100. struct notifier_block oom_nb;
  101. /* Free page reporting device */
  102. struct virtqueue *reporting_vq;
  103. struct page_reporting_dev_info pr_dev_info;
  104. /* State for keeping the wakeup_source active while adjusting the balloon */
  105. spinlock_t wakeup_lock;
  106. bool processing_wakeup_event;
  107. u32 wakeup_signal_mask;
  108. };
  109. #define VIRTIO_BALLOON_WAKEUP_SIGNAL_ADJUST (1 << 0)
  110. #define VIRTIO_BALLOON_WAKEUP_SIGNAL_STATS (1 << 1)
  111. static const struct virtio_device_id id_table[] = {
  112. { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
  113. { 0 },
  114. };
  115. static u32 page_to_balloon_pfn(struct page *page)
  116. {
  117. unsigned long pfn = page_to_pfn(page);
  118. BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT);
  119. /* Convert pfn from Linux page size to balloon page size. */
  120. return pfn * VIRTIO_BALLOON_PAGES_PER_PAGE;
  121. }
  122. static void start_wakeup_event(struct virtio_balloon *vb, u32 mask)
  123. {
  124. unsigned long flags;
  125. spin_lock_irqsave(&vb->wakeup_lock, flags);
  126. vb->wakeup_signal_mask |= mask;
  127. if (!vb->processing_wakeup_event) {
  128. vb->processing_wakeup_event = true;
  129. pm_stay_awake(&vb->vdev->dev);
  130. }
  131. spin_unlock_irqrestore(&vb->wakeup_lock, flags);
  132. }
  133. static void process_wakeup_event(struct virtio_balloon *vb, u32 mask)
  134. {
  135. spin_lock_irq(&vb->wakeup_lock);
  136. vb->wakeup_signal_mask &= ~mask;
  137. spin_unlock_irq(&vb->wakeup_lock);
  138. }
  139. static void finish_wakeup_event(struct virtio_balloon *vb)
  140. {
  141. spin_lock_irq(&vb->wakeup_lock);
  142. if (!vb->wakeup_signal_mask && vb->processing_wakeup_event) {
  143. vb->processing_wakeup_event = false;
  144. pm_relax(&vb->vdev->dev);
  145. }
  146. spin_unlock_irq(&vb->wakeup_lock);
  147. }
  148. static void balloon_ack(struct virtqueue *vq)
  149. {
  150. struct virtio_balloon *vb = vq->vdev->priv;
  151. wake_up(&vb->acked);
  152. }
  153. static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
  154. {
  155. struct scatterlist sg;
  156. unsigned int len;
  157. sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
  158. /* We should always be able to add one buffer to an empty queue. */
  159. virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  160. virtqueue_kick(vq);
  161. /* When host has read buffer, this completes via balloon_ack */
  162. wait_event(vb->acked, virtqueue_get_buf(vq, &len));
  163. }
  164. static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_info,
  165. struct scatterlist *sg, unsigned int nents)
  166. {
  167. struct virtio_balloon *vb =
  168. container_of(pr_dev_info, struct virtio_balloon, pr_dev_info);
  169. struct virtqueue *vq = vb->reporting_vq;
  170. unsigned int unused, err;
  171. /* We should always be able to add these buffers to an empty queue. */
  172. err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT);
  173. /*
  174. * In the extremely unlikely case that something has occurred and we
  175. * are able to trigger an error we will simply display a warning
  176. * and exit without actually processing the pages.
  177. */
  178. if (WARN_ON_ONCE(err))
  179. return err;
  180. virtqueue_kick(vq);
  181. /* When host has read buffer, this completes via balloon_ack */
  182. wait_event(vb->acked, virtqueue_get_buf(vq, &unused));
  183. return 0;
  184. }
  185. static void set_page_pfns(struct virtio_balloon *vb,
  186. __virtio32 pfns[], struct page *page)
  187. {
  188. unsigned int i;
  189. BUILD_BUG_ON(VIRTIO_BALLOON_PAGES_PER_PAGE > VIRTIO_BALLOON_ARRAY_PFNS_MAX);
  190. /*
  191. * Set balloon pfns pointing at this page.
  192. * Note that the first pfn points at start of the page.
  193. */
  194. for (i = 0; i < VIRTIO_BALLOON_PAGES_PER_PAGE; i++)
  195. pfns[i] = cpu_to_virtio32(vb->vdev,
  196. page_to_balloon_pfn(page) + i);
  197. }
  198. static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num)
  199. {
  200. unsigned int num_allocated_pages;
  201. struct page *page, *next;
  202. unsigned int num_pfns;
  203. LIST_HEAD(pages);
  204. /* We can only do one array worth at a time. */
  205. num = min(num, ARRAY_SIZE(vb->pfns));
  206. for (num_pfns = 0; num_pfns < num;
  207. num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  208. page = balloon_page_alloc();
  209. if (!page) {
  210. dev_info_ratelimited(&vb->vdev->dev,
  211. "Out of puff! Can't get %u pages\n",
  212. VIRTIO_BALLOON_PAGES_PER_PAGE);
  213. /* Sleep for at least 1/5 of a second before retry. */
  214. msleep(200);
  215. break;
  216. }
  217. list_add(&page->lru, &pages);
  218. }
  219. mutex_lock(&vb->balloon_lock);
  220. vb->num_pfns = 0;
  221. list_for_each_entry_safe(page, next, &pages, lru) {
  222. list_del(&page->lru);
  223. balloon_page_enqueue(&vb->vb_dev_info, page);
  224. set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
  225. vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
  226. vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE;
  227. }
  228. num_allocated_pages = vb->num_pfns;
  229. /* Did we get any? */
  230. if (vb->num_pfns != 0)
  231. tell_host(vb, vb->inflate_vq);
  232. mutex_unlock(&vb->balloon_lock);
  233. return num_allocated_pages;
  234. }
  235. static void release_pages_balloon(struct virtio_balloon *vb,
  236. struct list_head *pages)
  237. {
  238. struct page *page, *next;
  239. list_for_each_entry_safe(page, next, pages, lru) {
  240. list_del(&page->lru);
  241. put_page(page); /* balloon reference */
  242. }
  243. }
  244. static unsigned int leak_balloon(struct virtio_balloon *vb, size_t num)
  245. {
  246. unsigned int num_freed_pages;
  247. struct page *page;
  248. struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
  249. LIST_HEAD(pages);
  250. /* We can only do one array worth at a time. */
  251. num = min(num, ARRAY_SIZE(vb->pfns));
  252. mutex_lock(&vb->balloon_lock);
  253. /* We can't release more pages than taken */
  254. num = min(num, (size_t)vb->num_pages);
  255. for (vb->num_pfns = 0; vb->num_pfns < num;
  256. vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  257. page = balloon_page_dequeue(vb_dev_info);
  258. if (!page)
  259. break;
  260. set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
  261. list_add(&page->lru, &pages);
  262. vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
  263. }
  264. num_freed_pages = vb->num_pfns;
  265. /*
  266. * Note that if
  267. * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
  268. * is true, we *have* to do it in this order
  269. */
  270. if (vb->num_pfns != 0)
  271. tell_host(vb, vb->deflate_vq);
  272. release_pages_balloon(vb, &pages);
  273. mutex_unlock(&vb->balloon_lock);
  274. return num_freed_pages;
  275. }
  276. static inline void update_stat(struct virtio_balloon *vb, int idx,
  277. u16 tag, u64 val)
  278. {
  279. BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
  280. vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag);
  281. vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val);
  282. }
  283. #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
  284. #ifdef CONFIG_VM_EVENT_COUNTERS
  285. /* Return the number of entries filled by vm events */
  286. static inline unsigned int update_balloon_vm_stats(struct virtio_balloon *vb)
  287. {
  288. unsigned long events[NR_VM_EVENT_ITEMS];
  289. unsigned int idx = 0;
  290. unsigned int zid;
  291. unsigned long stall = 0;
  292. all_vm_events(events);
  293. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
  294. pages_to_bytes(events[PSWPIN]));
  295. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
  296. pages_to_bytes(events[PSWPOUT]));
  297. update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
  298. update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
  299. update_stat(vb, idx++, VIRTIO_BALLOON_S_OOM_KILL, events[OOM_KILL]);
  300. /* sum all the stall events */
  301. for (zid = 0; zid < MAX_NR_ZONES; zid++)
  302. stall += events[ALLOCSTALL_NORMAL - ZONE_NORMAL + zid];
  303. update_stat(vb, idx++, VIRTIO_BALLOON_S_ALLOC_STALL, stall);
  304. update_stat(vb, idx++, VIRTIO_BALLOON_S_ASYNC_SCAN,
  305. pages_to_bytes(events[PGSCAN_KSWAPD]));
  306. update_stat(vb, idx++, VIRTIO_BALLOON_S_DIRECT_SCAN,
  307. pages_to_bytes(events[PGSCAN_DIRECT]));
  308. update_stat(vb, idx++, VIRTIO_BALLOON_S_ASYNC_RECLAIM,
  309. pages_to_bytes(events[PGSTEAL_KSWAPD]));
  310. update_stat(vb, idx++, VIRTIO_BALLOON_S_DIRECT_RECLAIM,
  311. pages_to_bytes(events[PGSTEAL_DIRECT]));
  312. #ifdef CONFIG_HUGETLB_PAGE
  313. update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGALLOC,
  314. events[HTLB_BUDDY_PGALLOC]);
  315. update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGFAIL,
  316. events[HTLB_BUDDY_PGALLOC_FAIL]);
  317. #endif /* CONFIG_HUGETLB_PAGE */
  318. return idx;
  319. }
  320. #else /* CONFIG_VM_EVENT_COUNTERS */
  321. static inline unsigned int update_balloon_vm_stats(struct virtio_balloon *vb)
  322. {
  323. return 0;
  324. }
  325. #endif /* CONFIG_VM_EVENT_COUNTERS */
  326. static unsigned int update_balloon_stats(struct virtio_balloon *vb)
  327. {
  328. struct sysinfo i;
  329. unsigned int idx;
  330. long available;
  331. unsigned long caches;
  332. idx = update_balloon_vm_stats(vb);
  333. si_meminfo(&i);
  334. available = si_mem_available();
  335. caches = global_node_page_state(NR_FILE_PAGES);
  336. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
  337. pages_to_bytes(i.freeram));
  338. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
  339. pages_to_bytes(i.totalram));
  340. update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
  341. pages_to_bytes(available));
  342. update_stat(vb, idx++, VIRTIO_BALLOON_S_CACHES,
  343. pages_to_bytes(caches));
  344. return idx;
  345. }
  346. /*
  347. * While most virtqueues communicate guest-initiated requests to the hypervisor,
  348. * the stats queue operates in reverse. The driver initializes the virtqueue
  349. * with a single buffer. From that point forward, all conversations consist of
  350. * a hypervisor request (a call to this function) which directs us to refill
  351. * the virtqueue with a fresh stats buffer. Since stats collection can sleep,
  352. * we delegate the job to a freezable workqueue that will do the actual work via
  353. * stats_handle_request().
  354. */
  355. static void stats_request(struct virtqueue *vq)
  356. {
  357. struct virtio_balloon *vb = vq->vdev->priv;
  358. spin_lock(&vb->stop_update_lock);
  359. if (!vb->stop_update) {
  360. start_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_STATS);
  361. queue_work(system_freezable_wq, &vb->update_balloon_stats_work);
  362. }
  363. spin_unlock(&vb->stop_update_lock);
  364. }
  365. static void stats_handle_request(struct virtio_balloon *vb)
  366. {
  367. struct virtqueue *vq;
  368. struct scatterlist sg;
  369. unsigned int len, num_stats;
  370. num_stats = update_balloon_stats(vb);
  371. vq = vb->stats_vq;
  372. if (!virtqueue_get_buf(vq, &len))
  373. return;
  374. sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
  375. virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  376. virtqueue_kick(vq);
  377. }
  378. static inline s64 towards_target(struct virtio_balloon *vb)
  379. {
  380. s64 target;
  381. u32 num_pages;
  382. /* Legacy balloon config space is LE, unlike all other devices. */
  383. virtio_cread_le(vb->vdev, struct virtio_balloon_config, num_pages,
  384. &num_pages);
  385. /*
  386. * Aligned up to guest page size to avoid inflating and deflating
  387. * balloon endlessly.
  388. */
  389. target = ALIGN(num_pages, VIRTIO_BALLOON_PAGES_PER_PAGE);
  390. return target - vb->num_pages;
  391. }
  392. /* Gives back @num_to_return blocks of free pages to mm. */
  393. static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
  394. unsigned long num_to_return)
  395. {
  396. unsigned long num_returned = 0;
  397. struct page *page, *next;
  398. if (unlikely(!num_to_return))
  399. return 0;
  400. spin_lock_irq(&vb->free_page_list_lock);
  401. list_for_each_entry_safe(page, next, &vb->free_page_list, lru) {
  402. list_del(&page->lru);
  403. __free_pages(page, VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  404. if (++num_returned == num_to_return)
  405. break;
  406. }
  407. vb->num_free_page_blocks -= num_returned;
  408. spin_unlock_irq(&vb->free_page_list_lock);
  409. return num_returned;
  410. }
  411. static void virtio_balloon_queue_free_page_work(struct virtio_balloon *vb)
  412. {
  413. if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  414. return;
  415. /* No need to queue the work if the bit was already set. */
  416. if (test_and_set_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID,
  417. &vb->config_read_bitmap))
  418. return;
  419. queue_work(vb->balloon_wq, &vb->report_free_page_work);
  420. }
  421. static void start_update_balloon_size(struct virtio_balloon *vb)
  422. {
  423. start_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_ADJUST);
  424. queue_work(system_freezable_wq, &vb->update_balloon_size_work);
  425. }
  426. static void virtballoon_changed(struct virtio_device *vdev)
  427. {
  428. struct virtio_balloon *vb = vdev->priv;
  429. unsigned long flags;
  430. spin_lock_irqsave(&vb->stop_update_lock, flags);
  431. if (!vb->stop_update) {
  432. start_update_balloon_size(vb);
  433. virtio_balloon_queue_free_page_work(vb);
  434. }
  435. spin_unlock_irqrestore(&vb->stop_update_lock, flags);
  436. }
  437. static void update_balloon_size(struct virtio_balloon *vb)
  438. {
  439. u32 actual = vb->num_pages;
  440. /* Legacy balloon config space is LE, unlike all other devices. */
  441. virtio_cwrite_le(vb->vdev, struct virtio_balloon_config, actual,
  442. &actual);
  443. }
  444. static void update_balloon_stats_func(struct work_struct *work)
  445. {
  446. struct virtio_balloon *vb;
  447. vb = container_of(work, struct virtio_balloon,
  448. update_balloon_stats_work);
  449. process_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_STATS);
  450. stats_handle_request(vb);
  451. finish_wakeup_event(vb);
  452. }
  453. static void update_balloon_size_func(struct work_struct *work)
  454. {
  455. struct virtio_balloon *vb;
  456. s64 diff;
  457. vb = container_of(work, struct virtio_balloon,
  458. update_balloon_size_work);
  459. process_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_ADJUST);
  460. diff = towards_target(vb);
  461. if (diff) {
  462. if (diff > 0)
  463. diff -= fill_balloon(vb, diff);
  464. else
  465. diff += leak_balloon(vb, -diff);
  466. update_balloon_size(vb);
  467. }
  468. if (diff)
  469. queue_work(system_freezable_wq, work);
  470. else
  471. finish_wakeup_event(vb);
  472. }
  473. static int init_vqs(struct virtio_balloon *vb)
  474. {
  475. struct virtqueue_info vqs_info[VIRTIO_BALLOON_VQ_MAX] = {};
  476. struct virtqueue *vqs[VIRTIO_BALLOON_VQ_MAX];
  477. int err;
  478. /*
  479. * Inflateq and deflateq are used unconditionally. The names[]
  480. * will be NULL if the related feature is not enabled, which will
  481. * cause no allocation for the corresponding virtqueue in find_vqs.
  482. */
  483. vqs_info[VIRTIO_BALLOON_VQ_INFLATE].callback = balloon_ack;
  484. vqs_info[VIRTIO_BALLOON_VQ_INFLATE].name = "inflate";
  485. vqs_info[VIRTIO_BALLOON_VQ_DEFLATE].callback = balloon_ack;
  486. vqs_info[VIRTIO_BALLOON_VQ_DEFLATE].name = "deflate";
  487. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
  488. vqs_info[VIRTIO_BALLOON_VQ_STATS].name = "stats";
  489. vqs_info[VIRTIO_BALLOON_VQ_STATS].callback = stats_request;
  490. }
  491. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  492. vqs_info[VIRTIO_BALLOON_VQ_FREE_PAGE].name = "free_page_vq";
  493. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
  494. vqs_info[VIRTIO_BALLOON_VQ_REPORTING].name = "reporting_vq";
  495. vqs_info[VIRTIO_BALLOON_VQ_REPORTING].callback = balloon_ack;
  496. }
  497. err = virtio_find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX, vqs,
  498. vqs_info, NULL);
  499. if (err)
  500. return err;
  501. vb->inflate_vq = vqs[VIRTIO_BALLOON_VQ_INFLATE];
  502. vb->deflate_vq = vqs[VIRTIO_BALLOON_VQ_DEFLATE];
  503. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
  504. struct scatterlist sg;
  505. unsigned int num_stats;
  506. vb->stats_vq = vqs[VIRTIO_BALLOON_VQ_STATS];
  507. /*
  508. * Prime this virtqueue with one buffer so the hypervisor can
  509. * use it to signal us later (it can't be broken yet!).
  510. */
  511. num_stats = update_balloon_stats(vb);
  512. sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
  513. err = virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb,
  514. GFP_KERNEL);
  515. if (err) {
  516. dev_warn(&vb->vdev->dev, "%s: add stat_vq failed\n",
  517. __func__);
  518. return err;
  519. }
  520. virtqueue_kick(vb->stats_vq);
  521. }
  522. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  523. vb->free_page_vq = vqs[VIRTIO_BALLOON_VQ_FREE_PAGE];
  524. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
  525. vb->reporting_vq = vqs[VIRTIO_BALLOON_VQ_REPORTING];
  526. return 0;
  527. }
  528. static u32 virtio_balloon_cmd_id_received(struct virtio_balloon *vb)
  529. {
  530. if (test_and_clear_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID,
  531. &vb->config_read_bitmap)) {
  532. /* Legacy balloon config space is LE, unlike all other devices. */
  533. virtio_cread_le(vb->vdev, struct virtio_balloon_config,
  534. free_page_hint_cmd_id,
  535. &vb->cmd_id_received_cache);
  536. }
  537. return vb->cmd_id_received_cache;
  538. }
  539. static int send_cmd_id_start(struct virtio_balloon *vb)
  540. {
  541. struct scatterlist sg;
  542. struct virtqueue *vq = vb->free_page_vq;
  543. int err, unused;
  544. /* Detach all the used buffers from the vq */
  545. while (virtqueue_get_buf(vq, &unused))
  546. ;
  547. vb->cmd_id_active = cpu_to_virtio32(vb->vdev,
  548. virtio_balloon_cmd_id_received(vb));
  549. sg_init_one(&sg, &vb->cmd_id_active, sizeof(vb->cmd_id_active));
  550. err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_active, GFP_KERNEL);
  551. if (!err)
  552. virtqueue_kick(vq);
  553. return err;
  554. }
  555. static int send_cmd_id_stop(struct virtio_balloon *vb)
  556. {
  557. struct scatterlist sg;
  558. struct virtqueue *vq = vb->free_page_vq;
  559. int err, unused;
  560. /* Detach all the used buffers from the vq */
  561. while (virtqueue_get_buf(vq, &unused))
  562. ;
  563. sg_init_one(&sg, &vb->cmd_id_stop, sizeof(vb->cmd_id_stop));
  564. err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_stop, GFP_KERNEL);
  565. if (!err)
  566. virtqueue_kick(vq);
  567. return err;
  568. }
  569. static int get_free_page_and_send(struct virtio_balloon *vb)
  570. {
  571. struct virtqueue *vq = vb->free_page_vq;
  572. struct page *page;
  573. struct scatterlist sg;
  574. int err, unused;
  575. void *p;
  576. /* Detach all the used buffers from the vq */
  577. while (virtqueue_get_buf(vq, &unused))
  578. ;
  579. page = alloc_pages(VIRTIO_BALLOON_FREE_PAGE_ALLOC_FLAG,
  580. VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  581. /*
  582. * When the allocation returns NULL, it indicates that we have got all
  583. * the possible free pages, so return -EINTR to stop.
  584. */
  585. if (!page)
  586. return -EINTR;
  587. p = page_address(page);
  588. sg_init_one(&sg, p, VIRTIO_BALLOON_HINT_BLOCK_BYTES);
  589. /* There is always 1 entry reserved for the cmd id to use. */
  590. if (vq->num_free > 1) {
  591. err = virtqueue_add_inbuf(vq, &sg, 1, p, GFP_KERNEL);
  592. if (unlikely(err)) {
  593. __free_pages(page, VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  594. return err;
  595. }
  596. virtqueue_kick(vq);
  597. spin_lock_irq(&vb->free_page_list_lock);
  598. list_add(&page->lru, &vb->free_page_list);
  599. vb->num_free_page_blocks++;
  600. spin_unlock_irq(&vb->free_page_list_lock);
  601. } else {
  602. /*
  603. * The vq has no available entry to add this page block, so
  604. * just free it.
  605. */
  606. __free_pages(page, VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  607. }
  608. return 0;
  609. }
  610. static int send_free_pages(struct virtio_balloon *vb)
  611. {
  612. int err;
  613. u32 cmd_id_active;
  614. while (1) {
  615. /*
  616. * If a stop id or a new cmd id was just received from host,
  617. * stop the reporting.
  618. */
  619. cmd_id_active = virtio32_to_cpu(vb->vdev, vb->cmd_id_active);
  620. if (unlikely(cmd_id_active !=
  621. virtio_balloon_cmd_id_received(vb)))
  622. break;
  623. /*
  624. * The free page blocks are allocated and sent to host one by
  625. * one.
  626. */
  627. err = get_free_page_and_send(vb);
  628. if (err == -EINTR)
  629. break;
  630. else if (unlikely(err))
  631. return err;
  632. }
  633. return 0;
  634. }
  635. static void virtio_balloon_report_free_page(struct virtio_balloon *vb)
  636. {
  637. int err;
  638. struct device *dev = &vb->vdev->dev;
  639. /* Start by sending the received cmd id to host with an outbuf. */
  640. err = send_cmd_id_start(vb);
  641. if (unlikely(err))
  642. dev_err(dev, "Failed to send a start id, err = %d\n", err);
  643. err = send_free_pages(vb);
  644. if (unlikely(err))
  645. dev_err(dev, "Failed to send a free page, err = %d\n", err);
  646. /* End by sending a stop id to host with an outbuf. */
  647. err = send_cmd_id_stop(vb);
  648. if (unlikely(err))
  649. dev_err(dev, "Failed to send a stop id, err = %d\n", err);
  650. }
  651. static void report_free_page_func(struct work_struct *work)
  652. {
  653. struct virtio_balloon *vb = container_of(work, struct virtio_balloon,
  654. report_free_page_work);
  655. u32 cmd_id_received;
  656. cmd_id_received = virtio_balloon_cmd_id_received(vb);
  657. if (cmd_id_received == VIRTIO_BALLOON_CMD_ID_DONE) {
  658. /* Pass ULONG_MAX to give back all the free pages */
  659. return_free_pages_to_mm(vb, ULONG_MAX);
  660. } else if (cmd_id_received != VIRTIO_BALLOON_CMD_ID_STOP &&
  661. cmd_id_received !=
  662. virtio32_to_cpu(vb->vdev, vb->cmd_id_active)) {
  663. virtio_balloon_report_free_page(vb);
  664. }
  665. }
  666. #ifdef CONFIG_BALLOON_MIGRATION
  667. /*
  668. * virtballoon_migratepage - perform the balloon page migration on behalf of
  669. * a compaction thread. (called under page lock)
  670. * @vb_dev_info: the balloon device
  671. * @newpage: page that will replace the isolated page after migration finishes.
  672. * @page : the isolated (old) page that is about to be migrated to newpage.
  673. * @mode : compaction mode -- not used for balloon page migration.
  674. *
  675. * After a ballooned page gets isolated by compaction procedures, this is the
  676. * function that performs the page migration on behalf of a compaction thread
  677. * The page migration for virtio balloon is done in a simple swap fashion which
  678. * follows these two macro steps:
  679. * 1) insert newpage into vb->pages list and update the host about it;
  680. * 2) update the host about the old page removed from vb->pages list;
  681. *
  682. * This function preforms the balloon page migration task.
  683. * Called through movable_operations->migrate_page
  684. */
  685. static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info,
  686. struct page *newpage, struct page *page, enum migrate_mode mode)
  687. {
  688. struct virtio_balloon *vb = container_of(vb_dev_info,
  689. struct virtio_balloon, vb_dev_info);
  690. /*
  691. * In order to avoid lock contention while migrating pages concurrently
  692. * to leak_balloon() or fill_balloon() we just give up the balloon_lock
  693. * this turn, as it is easier to retry the page migration later.
  694. * This also prevents fill_balloon() getting stuck into a mutex
  695. * recursion in the case it ends up triggering memory compaction
  696. * while it is attempting to inflate the ballon.
  697. */
  698. if (!mutex_trylock(&vb->balloon_lock))
  699. return -EAGAIN;
  700. /* balloon's page migration 1st step -- inflate "newpage" */
  701. vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
  702. set_page_pfns(vb, vb->pfns, newpage);
  703. tell_host(vb, vb->inflate_vq);
  704. /* balloon's page migration 2nd step -- deflate "page" */
  705. vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
  706. set_page_pfns(vb, vb->pfns, page);
  707. tell_host(vb, vb->deflate_vq);
  708. mutex_unlock(&vb->balloon_lock);
  709. return 0;
  710. }
  711. #endif /* CONFIG_BALLOON_MIGRATION */
  712. static unsigned long shrink_free_pages(struct virtio_balloon *vb,
  713. unsigned long pages_to_free)
  714. {
  715. unsigned long blocks_to_free, blocks_freed;
  716. pages_to_free = round_up(pages_to_free,
  717. VIRTIO_BALLOON_HINT_BLOCK_PAGES);
  718. blocks_to_free = pages_to_free / VIRTIO_BALLOON_HINT_BLOCK_PAGES;
  719. blocks_freed = return_free_pages_to_mm(vb, blocks_to_free);
  720. return blocks_freed * VIRTIO_BALLOON_HINT_BLOCK_PAGES;
  721. }
  722. static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,
  723. struct shrink_control *sc)
  724. {
  725. struct virtio_balloon *vb = shrinker->private_data;
  726. return shrink_free_pages(vb, sc->nr_to_scan);
  727. }
  728. static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker,
  729. struct shrink_control *sc)
  730. {
  731. struct virtio_balloon *vb = shrinker->private_data;
  732. return vb->num_free_page_blocks * VIRTIO_BALLOON_HINT_BLOCK_PAGES;
  733. }
  734. static int virtio_balloon_oom_notify(struct notifier_block *nb,
  735. unsigned long dummy, void *parm)
  736. {
  737. struct virtio_balloon *vb = container_of(nb,
  738. struct virtio_balloon, oom_nb);
  739. unsigned long *freed = parm;
  740. *freed += leak_balloon(vb, VIRTIO_BALLOON_OOM_NR_PAGES) /
  741. VIRTIO_BALLOON_PAGES_PER_PAGE;
  742. update_balloon_size(vb);
  743. return NOTIFY_OK;
  744. }
  745. static void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb)
  746. {
  747. shrinker_free(vb->shrinker);
  748. }
  749. static int virtio_balloon_register_shrinker(struct virtio_balloon *vb)
  750. {
  751. vb->shrinker = shrinker_alloc(0, "virtio-balloon");
  752. if (!vb->shrinker)
  753. return -ENOMEM;
  754. vb->shrinker->scan_objects = virtio_balloon_shrinker_scan;
  755. vb->shrinker->count_objects = virtio_balloon_shrinker_count;
  756. vb->shrinker->private_data = vb;
  757. shrinker_register(vb->shrinker);
  758. return 0;
  759. }
  760. static int virtballoon_probe(struct virtio_device *vdev)
  761. {
  762. struct virtio_balloon *vb;
  763. int err;
  764. if (!vdev->config->get) {
  765. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  766. __func__);
  767. return -EINVAL;
  768. }
  769. vdev->priv = vb = kzalloc_obj(*vb);
  770. if (!vb) {
  771. err = -ENOMEM;
  772. goto out;
  773. }
  774. INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
  775. INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
  776. spin_lock_init(&vb->stop_update_lock);
  777. mutex_init(&vb->balloon_lock);
  778. init_waitqueue_head(&vb->acked);
  779. vb->vdev = vdev;
  780. balloon_devinfo_init(&vb->vb_dev_info);
  781. err = init_vqs(vb);
  782. if (err)
  783. goto out_free_vb;
  784. if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  785. vb->vb_dev_info.adjust_managed_page_count = true;
  786. #ifdef CONFIG_BALLOON_MIGRATION
  787. vb->vb_dev_info.migratepage = virtballoon_migratepage;
  788. #endif
  789. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
  790. /*
  791. * There is always one entry reserved for cmd id, so the ring
  792. * size needs to be at least two to report free page hints.
  793. */
  794. if (virtqueue_get_vring_size(vb->free_page_vq) < 2) {
  795. err = -ENOSPC;
  796. goto out_del_vqs;
  797. }
  798. vb->balloon_wq = alloc_workqueue("balloon-wq",
  799. WQ_FREEZABLE | WQ_CPU_INTENSIVE | WQ_PERCPU,
  800. 0);
  801. if (!vb->balloon_wq) {
  802. err = -ENOMEM;
  803. goto out_del_vqs;
  804. }
  805. INIT_WORK(&vb->report_free_page_work, report_free_page_func);
  806. vb->cmd_id_received_cache = VIRTIO_BALLOON_CMD_ID_STOP;
  807. vb->cmd_id_active = cpu_to_virtio32(vb->vdev,
  808. VIRTIO_BALLOON_CMD_ID_STOP);
  809. vb->cmd_id_stop = cpu_to_virtio32(vb->vdev,
  810. VIRTIO_BALLOON_CMD_ID_STOP);
  811. spin_lock_init(&vb->free_page_list_lock);
  812. INIT_LIST_HEAD(&vb->free_page_list);
  813. /*
  814. * We're allowed to reuse any free pages, even if they are
  815. * still to be processed by the host.
  816. */
  817. err = virtio_balloon_register_shrinker(vb);
  818. if (err)
  819. goto out_del_balloon_wq;
  820. }
  821. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) {
  822. vb->oom_nb.notifier_call = virtio_balloon_oom_notify;
  823. vb->oom_nb.priority = VIRTIO_BALLOON_OOM_NOTIFY_PRIORITY;
  824. err = register_oom_notifier(&vb->oom_nb);
  825. if (err < 0)
  826. goto out_unregister_shrinker;
  827. }
  828. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) {
  829. /* Start with poison val of 0 representing general init */
  830. __u32 poison_val = 0;
  831. /*
  832. * Let the hypervisor know that we are expecting a
  833. * specific value to be written back in balloon pages.
  834. *
  835. * If the PAGE_POISON value was larger than a byte we would
  836. * need to byte swap poison_val here to guarantee it is
  837. * little-endian. However for now it is a single byte so we
  838. * can pass it as-is.
  839. */
  840. if (!want_init_on_free())
  841. memset(&poison_val, PAGE_POISON, sizeof(poison_val));
  842. virtio_cwrite_le(vb->vdev, struct virtio_balloon_config,
  843. poison_val, &poison_val);
  844. }
  845. vb->pr_dev_info.report = virtballoon_free_page_report;
  846. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
  847. unsigned int capacity;
  848. capacity = virtqueue_get_vring_size(vb->reporting_vq);
  849. if (capacity < PAGE_REPORTING_CAPACITY) {
  850. err = -ENOSPC;
  851. goto out_unregister_oom;
  852. }
  853. /*
  854. * The default page reporting order is @pageblock_order, which
  855. * corresponds to 512MB in size on ARM64 when 64KB base page
  856. * size is used. The page reporting won't be triggered if the
  857. * freeing page can't come up with a free area like that huge.
  858. * So we specify the page reporting order to 5, corresponding
  859. * to 2MB. It helps to avoid THP splitting if 4KB base page
  860. * size is used by host.
  861. *
  862. * Ideally, the page reporting order is selected based on the
  863. * host's base page size. However, it needs more work to report
  864. * that value. The hard-coded order would be fine currently.
  865. */
  866. #if defined(CONFIG_ARM64) && defined(CONFIG_ARM64_64K_PAGES)
  867. vb->pr_dev_info.order = 5;
  868. #endif
  869. err = page_reporting_register(&vb->pr_dev_info);
  870. if (err)
  871. goto out_unregister_oom;
  872. }
  873. spin_lock_init(&vb->wakeup_lock);
  874. /*
  875. * The virtio balloon itself can't wake up the device, but it is
  876. * responsible for processing wakeup events passed up from the transport
  877. * layer. Wakeup sources don't support nesting/chaining calls, so we use
  878. * our own wakeup source to ensure wakeup events are properly handled
  879. * without trampling on the transport layer's wakeup source.
  880. */
  881. device_set_wakeup_capable(&vb->vdev->dev, true);
  882. virtio_device_ready(vdev);
  883. if (towards_target(vb))
  884. virtballoon_changed(vdev);
  885. return 0;
  886. out_unregister_oom:
  887. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  888. unregister_oom_notifier(&vb->oom_nb);
  889. out_unregister_shrinker:
  890. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  891. virtio_balloon_unregister_shrinker(vb);
  892. out_del_balloon_wq:
  893. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  894. destroy_workqueue(vb->balloon_wq);
  895. out_del_vqs:
  896. vdev->config->del_vqs(vdev);
  897. out_free_vb:
  898. kfree(vb);
  899. out:
  900. return err;
  901. }
  902. static void remove_common(struct virtio_balloon *vb)
  903. {
  904. /* There might be pages left in the balloon: free them. */
  905. while (vb->num_pages)
  906. leak_balloon(vb, vb->num_pages);
  907. update_balloon_size(vb);
  908. /* There might be free pages that are being reported: release them. */
  909. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  910. return_free_pages_to_mm(vb, ULONG_MAX);
  911. /* Now we reset the device so we can clean up the queues. */
  912. virtio_reset_device(vb->vdev);
  913. vb->vdev->config->del_vqs(vb->vdev);
  914. }
  915. static void virtballoon_remove(struct virtio_device *vdev)
  916. {
  917. struct virtio_balloon *vb = vdev->priv;
  918. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
  919. page_reporting_unregister(&vb->pr_dev_info);
  920. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  921. unregister_oom_notifier(&vb->oom_nb);
  922. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  923. virtio_balloon_unregister_shrinker(vb);
  924. spin_lock_irq(&vb->stop_update_lock);
  925. vb->stop_update = true;
  926. spin_unlock_irq(&vb->stop_update_lock);
  927. cancel_work_sync(&vb->update_balloon_size_work);
  928. cancel_work_sync(&vb->update_balloon_stats_work);
  929. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
  930. cancel_work_sync(&vb->report_free_page_work);
  931. destroy_workqueue(vb->balloon_wq);
  932. }
  933. remove_common(vb);
  934. kfree(vb);
  935. }
  936. #ifdef CONFIG_PM_SLEEP
  937. static int virtballoon_freeze(struct virtio_device *vdev)
  938. {
  939. struct virtio_balloon *vb = vdev->priv;
  940. /*
  941. * The workqueue is already frozen by the PM core before this
  942. * function is called.
  943. */
  944. remove_common(vb);
  945. return 0;
  946. }
  947. static int virtballoon_restore(struct virtio_device *vdev)
  948. {
  949. struct virtio_balloon *vb = vdev->priv;
  950. int ret;
  951. ret = init_vqs(vdev->priv);
  952. if (ret)
  953. return ret;
  954. virtio_device_ready(vdev);
  955. if (towards_target(vb))
  956. virtballoon_changed(vdev);
  957. update_balloon_size(vb);
  958. return 0;
  959. }
  960. #endif
  961. static int virtballoon_validate(struct virtio_device *vdev)
  962. {
  963. /*
  964. * Inform the hypervisor that our pages are poisoned or
  965. * initialized. If we cannot do that then we should disable
  966. * page reporting as it could potentially change the contents
  967. * of our free pages.
  968. */
  969. if (!want_init_on_free() && !page_poisoning_enabled_static())
  970. __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_PAGE_POISON);
  971. else if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON))
  972. __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_REPORTING);
  973. __virtio_clear_bit(vdev, VIRTIO_F_ACCESS_PLATFORM);
  974. return 0;
  975. }
  976. static unsigned int features[] = {
  977. VIRTIO_BALLOON_F_MUST_TELL_HOST,
  978. VIRTIO_BALLOON_F_STATS_VQ,
  979. VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
  980. VIRTIO_BALLOON_F_FREE_PAGE_HINT,
  981. VIRTIO_BALLOON_F_PAGE_POISON,
  982. VIRTIO_BALLOON_F_REPORTING,
  983. };
  984. static struct virtio_driver virtio_balloon_driver = {
  985. .feature_table = features,
  986. .feature_table_size = ARRAY_SIZE(features),
  987. .driver.name = KBUILD_MODNAME,
  988. .id_table = id_table,
  989. .validate = virtballoon_validate,
  990. .probe = virtballoon_probe,
  991. .remove = virtballoon_remove,
  992. .config_changed = virtballoon_changed,
  993. #ifdef CONFIG_PM_SLEEP
  994. .freeze = virtballoon_freeze,
  995. .restore = virtballoon_restore,
  996. #endif
  997. };
  998. module_virtio_driver(virtio_balloon_driver);
  999. MODULE_DEVICE_TABLE(virtio, id_table);
  1000. MODULE_DESCRIPTION("Virtio balloon driver");
  1001. MODULE_LICENSE("GPL");