virtio_balloon.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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_compaction.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 | __GFP_NOWARN);
  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. unsigned int num_pfns;
  202. struct page *page;
  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. struct page *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. balloon_page_push(&pages, page);
  218. }
  219. mutex_lock(&vb->balloon_lock);
  220. vb->num_pfns = 0;
  221. while ((page = balloon_page_pop(&pages))) {
  222. balloon_page_enqueue(&vb->vb_dev_info, page);
  223. set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
  224. vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
  225. if (!virtio_has_feature(vb->vdev,
  226. VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  227. adjust_managed_page_count(page, -1);
  228. vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE;
  229. }
  230. num_allocated_pages = vb->num_pfns;
  231. /* Did we get any? */
  232. if (vb->num_pfns != 0)
  233. tell_host(vb, vb->inflate_vq);
  234. mutex_unlock(&vb->balloon_lock);
  235. return num_allocated_pages;
  236. }
  237. static void release_pages_balloon(struct virtio_balloon *vb,
  238. struct list_head *pages)
  239. {
  240. struct page *page, *next;
  241. list_for_each_entry_safe(page, next, pages, lru) {
  242. if (!virtio_has_feature(vb->vdev,
  243. VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  244. adjust_managed_page_count(page, 1);
  245. list_del(&page->lru);
  246. put_page(page); /* balloon reference */
  247. }
  248. }
  249. static unsigned int leak_balloon(struct virtio_balloon *vb, size_t num)
  250. {
  251. unsigned int num_freed_pages;
  252. struct page *page;
  253. struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
  254. LIST_HEAD(pages);
  255. /* We can only do one array worth at a time. */
  256. num = min(num, ARRAY_SIZE(vb->pfns));
  257. mutex_lock(&vb->balloon_lock);
  258. /* We can't release more pages than taken */
  259. num = min(num, (size_t)vb->num_pages);
  260. for (vb->num_pfns = 0; vb->num_pfns < num;
  261. vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  262. page = balloon_page_dequeue(vb_dev_info);
  263. if (!page)
  264. break;
  265. set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
  266. list_add(&page->lru, &pages);
  267. vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
  268. }
  269. num_freed_pages = vb->num_pfns;
  270. /*
  271. * Note that if
  272. * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
  273. * is true, we *have* to do it in this order
  274. */
  275. if (vb->num_pfns != 0)
  276. tell_host(vb, vb->deflate_vq);
  277. release_pages_balloon(vb, &pages);
  278. mutex_unlock(&vb->balloon_lock);
  279. return num_freed_pages;
  280. }
  281. static inline void update_stat(struct virtio_balloon *vb, int idx,
  282. u16 tag, u64 val)
  283. {
  284. BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
  285. vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag);
  286. vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val);
  287. }
  288. #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
  289. #ifdef CONFIG_VM_EVENT_COUNTERS
  290. /* Return the number of entries filled by vm events */
  291. static inline unsigned int update_balloon_vm_stats(struct virtio_balloon *vb)
  292. {
  293. unsigned long events[NR_VM_EVENT_ITEMS];
  294. unsigned int idx = 0;
  295. unsigned int zid;
  296. unsigned long stall = 0;
  297. all_vm_events(events);
  298. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
  299. pages_to_bytes(events[PSWPIN]));
  300. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
  301. pages_to_bytes(events[PSWPOUT]));
  302. update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
  303. update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
  304. update_stat(vb, idx++, VIRTIO_BALLOON_S_OOM_KILL, events[OOM_KILL]);
  305. /* sum all the stall events */
  306. for (zid = 0; zid < MAX_NR_ZONES; zid++)
  307. stall += events[ALLOCSTALL_NORMAL - ZONE_NORMAL + zid];
  308. update_stat(vb, idx++, VIRTIO_BALLOON_S_ALLOC_STALL, stall);
  309. update_stat(vb, idx++, VIRTIO_BALLOON_S_ASYNC_SCAN,
  310. pages_to_bytes(events[PGSCAN_KSWAPD]));
  311. update_stat(vb, idx++, VIRTIO_BALLOON_S_DIRECT_SCAN,
  312. pages_to_bytes(events[PGSCAN_DIRECT]));
  313. update_stat(vb, idx++, VIRTIO_BALLOON_S_ASYNC_RECLAIM,
  314. pages_to_bytes(events[PGSTEAL_KSWAPD]));
  315. update_stat(vb, idx++, VIRTIO_BALLOON_S_DIRECT_RECLAIM,
  316. pages_to_bytes(events[PGSTEAL_DIRECT]));
  317. #ifdef CONFIG_HUGETLB_PAGE
  318. update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGALLOC,
  319. events[HTLB_BUDDY_PGALLOC]);
  320. update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGFAIL,
  321. events[HTLB_BUDDY_PGALLOC_FAIL]);
  322. #endif /* CONFIG_HUGETLB_PAGE */
  323. return idx;
  324. }
  325. #else /* CONFIG_VM_EVENT_COUNTERS */
  326. static inline unsigned int update_balloon_vm_stats(struct virtio_balloon *vb)
  327. {
  328. return 0;
  329. }
  330. #endif /* CONFIG_VM_EVENT_COUNTERS */
  331. static unsigned int update_balloon_stats(struct virtio_balloon *vb)
  332. {
  333. struct sysinfo i;
  334. unsigned int idx;
  335. long available;
  336. unsigned long caches;
  337. idx = update_balloon_vm_stats(vb);
  338. si_meminfo(&i);
  339. available = si_mem_available();
  340. caches = global_node_page_state(NR_FILE_PAGES);
  341. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
  342. pages_to_bytes(i.freeram));
  343. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
  344. pages_to_bytes(i.totalram));
  345. update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
  346. pages_to_bytes(available));
  347. update_stat(vb, idx++, VIRTIO_BALLOON_S_CACHES,
  348. pages_to_bytes(caches));
  349. return idx;
  350. }
  351. /*
  352. * While most virtqueues communicate guest-initiated requests to the hypervisor,
  353. * the stats queue operates in reverse. The driver initializes the virtqueue
  354. * with a single buffer. From that point forward, all conversations consist of
  355. * a hypervisor request (a call to this function) which directs us to refill
  356. * the virtqueue with a fresh stats buffer. Since stats collection can sleep,
  357. * we delegate the job to a freezable workqueue that will do the actual work via
  358. * stats_handle_request().
  359. */
  360. static void stats_request(struct virtqueue *vq)
  361. {
  362. struct virtio_balloon *vb = vq->vdev->priv;
  363. spin_lock(&vb->stop_update_lock);
  364. if (!vb->stop_update) {
  365. start_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_STATS);
  366. queue_work(system_freezable_wq, &vb->update_balloon_stats_work);
  367. }
  368. spin_unlock(&vb->stop_update_lock);
  369. }
  370. static void stats_handle_request(struct virtio_balloon *vb)
  371. {
  372. struct virtqueue *vq;
  373. struct scatterlist sg;
  374. unsigned int len, num_stats;
  375. num_stats = update_balloon_stats(vb);
  376. vq = vb->stats_vq;
  377. if (!virtqueue_get_buf(vq, &len))
  378. return;
  379. sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
  380. virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  381. virtqueue_kick(vq);
  382. }
  383. static inline s64 towards_target(struct virtio_balloon *vb)
  384. {
  385. s64 target;
  386. u32 num_pages;
  387. /* Legacy balloon config space is LE, unlike all other devices. */
  388. virtio_cread_le(vb->vdev, struct virtio_balloon_config, num_pages,
  389. &num_pages);
  390. /*
  391. * Aligned up to guest page size to avoid inflating and deflating
  392. * balloon endlessly.
  393. */
  394. target = ALIGN(num_pages, VIRTIO_BALLOON_PAGES_PER_PAGE);
  395. return target - vb->num_pages;
  396. }
  397. /* Gives back @num_to_return blocks of free pages to mm. */
  398. static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
  399. unsigned long num_to_return)
  400. {
  401. struct page *page;
  402. unsigned long num_returned;
  403. spin_lock_irq(&vb->free_page_list_lock);
  404. for (num_returned = 0; num_returned < num_to_return; num_returned++) {
  405. page = balloon_page_pop(&vb->free_page_list);
  406. if (!page)
  407. break;
  408. free_pages((unsigned long)page_address(page),
  409. VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  410. }
  411. vb->num_free_page_blocks -= num_returned;
  412. spin_unlock_irq(&vb->free_page_list_lock);
  413. return num_returned;
  414. }
  415. static void virtio_balloon_queue_free_page_work(struct virtio_balloon *vb)
  416. {
  417. if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  418. return;
  419. /* No need to queue the work if the bit was already set. */
  420. if (test_and_set_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID,
  421. &vb->config_read_bitmap))
  422. return;
  423. queue_work(vb->balloon_wq, &vb->report_free_page_work);
  424. }
  425. static void start_update_balloon_size(struct virtio_balloon *vb)
  426. {
  427. start_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_ADJUST);
  428. queue_work(system_freezable_wq, &vb->update_balloon_size_work);
  429. }
  430. static void virtballoon_changed(struct virtio_device *vdev)
  431. {
  432. struct virtio_balloon *vb = vdev->priv;
  433. unsigned long flags;
  434. spin_lock_irqsave(&vb->stop_update_lock, flags);
  435. if (!vb->stop_update) {
  436. start_update_balloon_size(vb);
  437. virtio_balloon_queue_free_page_work(vb);
  438. }
  439. spin_unlock_irqrestore(&vb->stop_update_lock, flags);
  440. }
  441. static void update_balloon_size(struct virtio_balloon *vb)
  442. {
  443. u32 actual = vb->num_pages;
  444. /* Legacy balloon config space is LE, unlike all other devices. */
  445. virtio_cwrite_le(vb->vdev, struct virtio_balloon_config, actual,
  446. &actual);
  447. }
  448. static void update_balloon_stats_func(struct work_struct *work)
  449. {
  450. struct virtio_balloon *vb;
  451. vb = container_of(work, struct virtio_balloon,
  452. update_balloon_stats_work);
  453. process_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_STATS);
  454. stats_handle_request(vb);
  455. finish_wakeup_event(vb);
  456. }
  457. static void update_balloon_size_func(struct work_struct *work)
  458. {
  459. struct virtio_balloon *vb;
  460. s64 diff;
  461. vb = container_of(work, struct virtio_balloon,
  462. update_balloon_size_work);
  463. process_wakeup_event(vb, VIRTIO_BALLOON_WAKEUP_SIGNAL_ADJUST);
  464. diff = towards_target(vb);
  465. if (diff) {
  466. if (diff > 0)
  467. diff -= fill_balloon(vb, diff);
  468. else
  469. diff += leak_balloon(vb, -diff);
  470. update_balloon_size(vb);
  471. }
  472. if (diff)
  473. queue_work(system_freezable_wq, work);
  474. else
  475. finish_wakeup_event(vb);
  476. }
  477. static int init_vqs(struct virtio_balloon *vb)
  478. {
  479. struct virtqueue_info vqs_info[VIRTIO_BALLOON_VQ_MAX] = {};
  480. struct virtqueue *vqs[VIRTIO_BALLOON_VQ_MAX];
  481. int err;
  482. /*
  483. * Inflateq and deflateq are used unconditionally. The names[]
  484. * will be NULL if the related feature is not enabled, which will
  485. * cause no allocation for the corresponding virtqueue in find_vqs.
  486. */
  487. vqs_info[VIRTIO_BALLOON_VQ_INFLATE].callback = balloon_ack;
  488. vqs_info[VIRTIO_BALLOON_VQ_INFLATE].name = "inflate";
  489. vqs_info[VIRTIO_BALLOON_VQ_DEFLATE].callback = balloon_ack;
  490. vqs_info[VIRTIO_BALLOON_VQ_DEFLATE].name = "deflate";
  491. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
  492. vqs_info[VIRTIO_BALLOON_VQ_STATS].name = "stats";
  493. vqs_info[VIRTIO_BALLOON_VQ_STATS].callback = stats_request;
  494. }
  495. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  496. vqs_info[VIRTIO_BALLOON_VQ_FREE_PAGE].name = "free_page_vq";
  497. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
  498. vqs_info[VIRTIO_BALLOON_VQ_REPORTING].name = "reporting_vq";
  499. vqs_info[VIRTIO_BALLOON_VQ_REPORTING].callback = balloon_ack;
  500. }
  501. err = virtio_find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX, vqs,
  502. vqs_info, NULL);
  503. if (err)
  504. return err;
  505. vb->inflate_vq = vqs[VIRTIO_BALLOON_VQ_INFLATE];
  506. vb->deflate_vq = vqs[VIRTIO_BALLOON_VQ_DEFLATE];
  507. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
  508. struct scatterlist sg;
  509. unsigned int num_stats;
  510. vb->stats_vq = vqs[VIRTIO_BALLOON_VQ_STATS];
  511. /*
  512. * Prime this virtqueue with one buffer so the hypervisor can
  513. * use it to signal us later (it can't be broken yet!).
  514. */
  515. num_stats = update_balloon_stats(vb);
  516. sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
  517. err = virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb,
  518. GFP_KERNEL);
  519. if (err) {
  520. dev_warn(&vb->vdev->dev, "%s: add stat_vq failed\n",
  521. __func__);
  522. return err;
  523. }
  524. virtqueue_kick(vb->stats_vq);
  525. }
  526. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  527. vb->free_page_vq = vqs[VIRTIO_BALLOON_VQ_FREE_PAGE];
  528. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
  529. vb->reporting_vq = vqs[VIRTIO_BALLOON_VQ_REPORTING];
  530. return 0;
  531. }
  532. static u32 virtio_balloon_cmd_id_received(struct virtio_balloon *vb)
  533. {
  534. if (test_and_clear_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID,
  535. &vb->config_read_bitmap)) {
  536. /* Legacy balloon config space is LE, unlike all other devices. */
  537. virtio_cread_le(vb->vdev, struct virtio_balloon_config,
  538. free_page_hint_cmd_id,
  539. &vb->cmd_id_received_cache);
  540. }
  541. return vb->cmd_id_received_cache;
  542. }
  543. static int send_cmd_id_start(struct virtio_balloon *vb)
  544. {
  545. struct scatterlist sg;
  546. struct virtqueue *vq = vb->free_page_vq;
  547. int err, unused;
  548. /* Detach all the used buffers from the vq */
  549. while (virtqueue_get_buf(vq, &unused))
  550. ;
  551. vb->cmd_id_active = cpu_to_virtio32(vb->vdev,
  552. virtio_balloon_cmd_id_received(vb));
  553. sg_init_one(&sg, &vb->cmd_id_active, sizeof(vb->cmd_id_active));
  554. err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_active, GFP_KERNEL);
  555. if (!err)
  556. virtqueue_kick(vq);
  557. return err;
  558. }
  559. static int send_cmd_id_stop(struct virtio_balloon *vb)
  560. {
  561. struct scatterlist sg;
  562. struct virtqueue *vq = vb->free_page_vq;
  563. int err, unused;
  564. /* Detach all the used buffers from the vq */
  565. while (virtqueue_get_buf(vq, &unused))
  566. ;
  567. sg_init_one(&sg, &vb->cmd_id_stop, sizeof(vb->cmd_id_stop));
  568. err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_stop, GFP_KERNEL);
  569. if (!err)
  570. virtqueue_kick(vq);
  571. return err;
  572. }
  573. static int get_free_page_and_send(struct virtio_balloon *vb)
  574. {
  575. struct virtqueue *vq = vb->free_page_vq;
  576. struct page *page;
  577. struct scatterlist sg;
  578. int err, unused;
  579. void *p;
  580. /* Detach all the used buffers from the vq */
  581. while (virtqueue_get_buf(vq, &unused))
  582. ;
  583. page = alloc_pages(VIRTIO_BALLOON_FREE_PAGE_ALLOC_FLAG,
  584. VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  585. /*
  586. * When the allocation returns NULL, it indicates that we have got all
  587. * the possible free pages, so return -EINTR to stop.
  588. */
  589. if (!page)
  590. return -EINTR;
  591. p = page_address(page);
  592. sg_init_one(&sg, p, VIRTIO_BALLOON_HINT_BLOCK_BYTES);
  593. /* There is always 1 entry reserved for the cmd id to use. */
  594. if (vq->num_free > 1) {
  595. err = virtqueue_add_inbuf(vq, &sg, 1, p, GFP_KERNEL);
  596. if (unlikely(err)) {
  597. free_pages((unsigned long)p,
  598. VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  599. return err;
  600. }
  601. virtqueue_kick(vq);
  602. spin_lock_irq(&vb->free_page_list_lock);
  603. balloon_page_push(&vb->free_page_list, page);
  604. vb->num_free_page_blocks++;
  605. spin_unlock_irq(&vb->free_page_list_lock);
  606. } else {
  607. /*
  608. * The vq has no available entry to add this page block, so
  609. * just free it.
  610. */
  611. free_pages((unsigned long)p, VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  612. }
  613. return 0;
  614. }
  615. static int send_free_pages(struct virtio_balloon *vb)
  616. {
  617. int err;
  618. u32 cmd_id_active;
  619. while (1) {
  620. /*
  621. * If a stop id or a new cmd id was just received from host,
  622. * stop the reporting.
  623. */
  624. cmd_id_active = virtio32_to_cpu(vb->vdev, vb->cmd_id_active);
  625. if (unlikely(cmd_id_active !=
  626. virtio_balloon_cmd_id_received(vb)))
  627. break;
  628. /*
  629. * The free page blocks are allocated and sent to host one by
  630. * one.
  631. */
  632. err = get_free_page_and_send(vb);
  633. if (err == -EINTR)
  634. break;
  635. else if (unlikely(err))
  636. return err;
  637. }
  638. return 0;
  639. }
  640. static void virtio_balloon_report_free_page(struct virtio_balloon *vb)
  641. {
  642. int err;
  643. struct device *dev = &vb->vdev->dev;
  644. /* Start by sending the received cmd id to host with an outbuf. */
  645. err = send_cmd_id_start(vb);
  646. if (unlikely(err))
  647. dev_err(dev, "Failed to send a start id, err = %d\n", err);
  648. err = send_free_pages(vb);
  649. if (unlikely(err))
  650. dev_err(dev, "Failed to send a free page, err = %d\n", err);
  651. /* End by sending a stop id to host with an outbuf. */
  652. err = send_cmd_id_stop(vb);
  653. if (unlikely(err))
  654. dev_err(dev, "Failed to send a stop id, err = %d\n", err);
  655. }
  656. static void report_free_page_func(struct work_struct *work)
  657. {
  658. struct virtio_balloon *vb = container_of(work, struct virtio_balloon,
  659. report_free_page_work);
  660. u32 cmd_id_received;
  661. cmd_id_received = virtio_balloon_cmd_id_received(vb);
  662. if (cmd_id_received == VIRTIO_BALLOON_CMD_ID_DONE) {
  663. /* Pass ULONG_MAX to give back all the free pages */
  664. return_free_pages_to_mm(vb, ULONG_MAX);
  665. } else if (cmd_id_received != VIRTIO_BALLOON_CMD_ID_STOP &&
  666. cmd_id_received !=
  667. virtio32_to_cpu(vb->vdev, vb->cmd_id_active)) {
  668. virtio_balloon_report_free_page(vb);
  669. }
  670. }
  671. #ifdef CONFIG_BALLOON_COMPACTION
  672. /*
  673. * virtballoon_migratepage - perform the balloon page migration on behalf of
  674. * a compaction thread. (called under page lock)
  675. * @vb_dev_info: the balloon device
  676. * @newpage: page that will replace the isolated page after migration finishes.
  677. * @page : the isolated (old) page that is about to be migrated to newpage.
  678. * @mode : compaction mode -- not used for balloon page migration.
  679. *
  680. * After a ballooned page gets isolated by compaction procedures, this is the
  681. * function that performs the page migration on behalf of a compaction thread
  682. * The page migration for virtio balloon is done in a simple swap fashion which
  683. * follows these two macro steps:
  684. * 1) insert newpage into vb->pages list and update the host about it;
  685. * 2) update the host about the old page removed from vb->pages list;
  686. *
  687. * This function preforms the balloon page migration task.
  688. * Called through movable_operations->migrate_page
  689. */
  690. static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info,
  691. struct page *newpage, struct page *page, enum migrate_mode mode)
  692. {
  693. struct virtio_balloon *vb = container_of(vb_dev_info,
  694. struct virtio_balloon, vb_dev_info);
  695. unsigned long flags;
  696. /*
  697. * In order to avoid lock contention while migrating pages concurrently
  698. * to leak_balloon() or fill_balloon() we just give up the balloon_lock
  699. * this turn, as it is easier to retry the page migration later.
  700. * This also prevents fill_balloon() getting stuck into a mutex
  701. * recursion in the case it ends up triggering memory compaction
  702. * while it is attempting to inflate the ballon.
  703. */
  704. if (!mutex_trylock(&vb->balloon_lock))
  705. return -EAGAIN;
  706. get_page(newpage); /* balloon reference */
  707. /*
  708. * When we migrate a page to a different zone and adjusted the
  709. * managed page count when inflating, we have to fixup the count of
  710. * both involved zones.
  711. */
  712. if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM) &&
  713. page_zone(page) != page_zone(newpage)) {
  714. adjust_managed_page_count(page, 1);
  715. adjust_managed_page_count(newpage, -1);
  716. }
  717. /* balloon's page migration 1st step -- inflate "newpage" */
  718. spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
  719. balloon_page_insert(vb_dev_info, newpage);
  720. vb_dev_info->isolated_pages--;
  721. __count_vm_event(BALLOON_MIGRATE);
  722. spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
  723. vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
  724. set_page_pfns(vb, vb->pfns, newpage);
  725. tell_host(vb, vb->inflate_vq);
  726. /* balloon's page migration 2nd step -- deflate "page" */
  727. spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
  728. balloon_page_delete(page);
  729. spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
  730. vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
  731. set_page_pfns(vb, vb->pfns, page);
  732. tell_host(vb, vb->deflate_vq);
  733. mutex_unlock(&vb->balloon_lock);
  734. put_page(page); /* balloon reference */
  735. return MIGRATEPAGE_SUCCESS;
  736. }
  737. #endif /* CONFIG_BALLOON_COMPACTION */
  738. static unsigned long shrink_free_pages(struct virtio_balloon *vb,
  739. unsigned long pages_to_free)
  740. {
  741. unsigned long blocks_to_free, blocks_freed;
  742. pages_to_free = round_up(pages_to_free,
  743. VIRTIO_BALLOON_HINT_BLOCK_PAGES);
  744. blocks_to_free = pages_to_free / VIRTIO_BALLOON_HINT_BLOCK_PAGES;
  745. blocks_freed = return_free_pages_to_mm(vb, blocks_to_free);
  746. return blocks_freed * VIRTIO_BALLOON_HINT_BLOCK_PAGES;
  747. }
  748. static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,
  749. struct shrink_control *sc)
  750. {
  751. struct virtio_balloon *vb = shrinker->private_data;
  752. return shrink_free_pages(vb, sc->nr_to_scan);
  753. }
  754. static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker,
  755. struct shrink_control *sc)
  756. {
  757. struct virtio_balloon *vb = shrinker->private_data;
  758. return vb->num_free_page_blocks * VIRTIO_BALLOON_HINT_BLOCK_PAGES;
  759. }
  760. static int virtio_balloon_oom_notify(struct notifier_block *nb,
  761. unsigned long dummy, void *parm)
  762. {
  763. struct virtio_balloon *vb = container_of(nb,
  764. struct virtio_balloon, oom_nb);
  765. unsigned long *freed = parm;
  766. *freed += leak_balloon(vb, VIRTIO_BALLOON_OOM_NR_PAGES) /
  767. VIRTIO_BALLOON_PAGES_PER_PAGE;
  768. update_balloon_size(vb);
  769. return NOTIFY_OK;
  770. }
  771. static void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb)
  772. {
  773. shrinker_free(vb->shrinker);
  774. }
  775. static int virtio_balloon_register_shrinker(struct virtio_balloon *vb)
  776. {
  777. vb->shrinker = shrinker_alloc(0, "virtio-balloon");
  778. if (!vb->shrinker)
  779. return -ENOMEM;
  780. vb->shrinker->scan_objects = virtio_balloon_shrinker_scan;
  781. vb->shrinker->count_objects = virtio_balloon_shrinker_count;
  782. vb->shrinker->private_data = vb;
  783. shrinker_register(vb->shrinker);
  784. return 0;
  785. }
  786. static int virtballoon_probe(struct virtio_device *vdev)
  787. {
  788. struct virtio_balloon *vb;
  789. int err;
  790. if (!vdev->config->get) {
  791. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  792. __func__);
  793. return -EINVAL;
  794. }
  795. vdev->priv = vb = kzalloc(sizeof(*vb), GFP_KERNEL);
  796. if (!vb) {
  797. err = -ENOMEM;
  798. goto out;
  799. }
  800. INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
  801. INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
  802. spin_lock_init(&vb->stop_update_lock);
  803. mutex_init(&vb->balloon_lock);
  804. init_waitqueue_head(&vb->acked);
  805. vb->vdev = vdev;
  806. balloon_devinfo_init(&vb->vb_dev_info);
  807. err = init_vqs(vb);
  808. if (err)
  809. goto out_free_vb;
  810. #ifdef CONFIG_BALLOON_COMPACTION
  811. vb->vb_dev_info.migratepage = virtballoon_migratepage;
  812. #endif
  813. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
  814. /*
  815. * There is always one entry reserved for cmd id, so the ring
  816. * size needs to be at least two to report free page hints.
  817. */
  818. if (virtqueue_get_vring_size(vb->free_page_vq) < 2) {
  819. err = -ENOSPC;
  820. goto out_del_vqs;
  821. }
  822. vb->balloon_wq = alloc_workqueue("balloon-wq",
  823. WQ_FREEZABLE | WQ_CPU_INTENSIVE, 0);
  824. if (!vb->balloon_wq) {
  825. err = -ENOMEM;
  826. goto out_del_vqs;
  827. }
  828. INIT_WORK(&vb->report_free_page_work, report_free_page_func);
  829. vb->cmd_id_received_cache = VIRTIO_BALLOON_CMD_ID_STOP;
  830. vb->cmd_id_active = cpu_to_virtio32(vb->vdev,
  831. VIRTIO_BALLOON_CMD_ID_STOP);
  832. vb->cmd_id_stop = cpu_to_virtio32(vb->vdev,
  833. VIRTIO_BALLOON_CMD_ID_STOP);
  834. spin_lock_init(&vb->free_page_list_lock);
  835. INIT_LIST_HEAD(&vb->free_page_list);
  836. /*
  837. * We're allowed to reuse any free pages, even if they are
  838. * still to be processed by the host.
  839. */
  840. err = virtio_balloon_register_shrinker(vb);
  841. if (err)
  842. goto out_del_balloon_wq;
  843. }
  844. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) {
  845. vb->oom_nb.notifier_call = virtio_balloon_oom_notify;
  846. vb->oom_nb.priority = VIRTIO_BALLOON_OOM_NOTIFY_PRIORITY;
  847. err = register_oom_notifier(&vb->oom_nb);
  848. if (err < 0)
  849. goto out_unregister_shrinker;
  850. }
  851. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) {
  852. /* Start with poison val of 0 representing general init */
  853. __u32 poison_val = 0;
  854. /*
  855. * Let the hypervisor know that we are expecting a
  856. * specific value to be written back in balloon pages.
  857. *
  858. * If the PAGE_POISON value was larger than a byte we would
  859. * need to byte swap poison_val here to guarantee it is
  860. * little-endian. However for now it is a single byte so we
  861. * can pass it as-is.
  862. */
  863. if (!want_init_on_free())
  864. memset(&poison_val, PAGE_POISON, sizeof(poison_val));
  865. virtio_cwrite_le(vb->vdev, struct virtio_balloon_config,
  866. poison_val, &poison_val);
  867. }
  868. vb->pr_dev_info.report = virtballoon_free_page_report;
  869. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
  870. unsigned int capacity;
  871. capacity = virtqueue_get_vring_size(vb->reporting_vq);
  872. if (capacity < PAGE_REPORTING_CAPACITY) {
  873. err = -ENOSPC;
  874. goto out_unregister_oom;
  875. }
  876. /*
  877. * The default page reporting order is @pageblock_order, which
  878. * corresponds to 512MB in size on ARM64 when 64KB base page
  879. * size is used. The page reporting won't be triggered if the
  880. * freeing page can't come up with a free area like that huge.
  881. * So we specify the page reporting order to 5, corresponding
  882. * to 2MB. It helps to avoid THP splitting if 4KB base page
  883. * size is used by host.
  884. *
  885. * Ideally, the page reporting order is selected based on the
  886. * host's base page size. However, it needs more work to report
  887. * that value. The hard-coded order would be fine currently.
  888. */
  889. #if defined(CONFIG_ARM64) && defined(CONFIG_ARM64_64K_PAGES)
  890. vb->pr_dev_info.order = 5;
  891. #endif
  892. err = page_reporting_register(&vb->pr_dev_info);
  893. if (err)
  894. goto out_unregister_oom;
  895. }
  896. spin_lock_init(&vb->wakeup_lock);
  897. /*
  898. * The virtio balloon itself can't wake up the device, but it is
  899. * responsible for processing wakeup events passed up from the transport
  900. * layer. Wakeup sources don't support nesting/chaining calls, so we use
  901. * our own wakeup source to ensure wakeup events are properly handled
  902. * without trampling on the transport layer's wakeup source.
  903. */
  904. device_set_wakeup_capable(&vb->vdev->dev, true);
  905. virtio_device_ready(vdev);
  906. if (towards_target(vb))
  907. virtballoon_changed(vdev);
  908. return 0;
  909. out_unregister_oom:
  910. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  911. unregister_oom_notifier(&vb->oom_nb);
  912. out_unregister_shrinker:
  913. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  914. virtio_balloon_unregister_shrinker(vb);
  915. out_del_balloon_wq:
  916. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  917. destroy_workqueue(vb->balloon_wq);
  918. out_del_vqs:
  919. vdev->config->del_vqs(vdev);
  920. out_free_vb:
  921. kfree(vb);
  922. out:
  923. return err;
  924. }
  925. static void remove_common(struct virtio_balloon *vb)
  926. {
  927. /* There might be pages left in the balloon: free them. */
  928. while (vb->num_pages)
  929. leak_balloon(vb, vb->num_pages);
  930. update_balloon_size(vb);
  931. /* There might be free pages that are being reported: release them. */
  932. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  933. return_free_pages_to_mm(vb, ULONG_MAX);
  934. /* Now we reset the device so we can clean up the queues. */
  935. virtio_reset_device(vb->vdev);
  936. vb->vdev->config->del_vqs(vb->vdev);
  937. }
  938. static void virtballoon_remove(struct virtio_device *vdev)
  939. {
  940. struct virtio_balloon *vb = vdev->priv;
  941. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
  942. page_reporting_unregister(&vb->pr_dev_info);
  943. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  944. unregister_oom_notifier(&vb->oom_nb);
  945. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  946. virtio_balloon_unregister_shrinker(vb);
  947. spin_lock_irq(&vb->stop_update_lock);
  948. vb->stop_update = true;
  949. spin_unlock_irq(&vb->stop_update_lock);
  950. cancel_work_sync(&vb->update_balloon_size_work);
  951. cancel_work_sync(&vb->update_balloon_stats_work);
  952. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
  953. cancel_work_sync(&vb->report_free_page_work);
  954. destroy_workqueue(vb->balloon_wq);
  955. }
  956. remove_common(vb);
  957. kfree(vb);
  958. }
  959. #ifdef CONFIG_PM_SLEEP
  960. static int virtballoon_freeze(struct virtio_device *vdev)
  961. {
  962. struct virtio_balloon *vb = vdev->priv;
  963. /*
  964. * The workqueue is already frozen by the PM core before this
  965. * function is called.
  966. */
  967. remove_common(vb);
  968. return 0;
  969. }
  970. static int virtballoon_restore(struct virtio_device *vdev)
  971. {
  972. struct virtio_balloon *vb = vdev->priv;
  973. int ret;
  974. ret = init_vqs(vdev->priv);
  975. if (ret)
  976. return ret;
  977. virtio_device_ready(vdev);
  978. if (towards_target(vb))
  979. virtballoon_changed(vdev);
  980. update_balloon_size(vb);
  981. return 0;
  982. }
  983. #endif
  984. static int virtballoon_validate(struct virtio_device *vdev)
  985. {
  986. /*
  987. * Inform the hypervisor that our pages are poisoned or
  988. * initialized. If we cannot do that then we should disable
  989. * page reporting as it could potentially change the contents
  990. * of our free pages.
  991. */
  992. if (!want_init_on_free() && !page_poisoning_enabled_static())
  993. __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_PAGE_POISON);
  994. else if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON))
  995. __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_REPORTING);
  996. __virtio_clear_bit(vdev, VIRTIO_F_ACCESS_PLATFORM);
  997. return 0;
  998. }
  999. static unsigned int features[] = {
  1000. VIRTIO_BALLOON_F_MUST_TELL_HOST,
  1001. VIRTIO_BALLOON_F_STATS_VQ,
  1002. VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
  1003. VIRTIO_BALLOON_F_FREE_PAGE_HINT,
  1004. VIRTIO_BALLOON_F_PAGE_POISON,
  1005. VIRTIO_BALLOON_F_REPORTING,
  1006. };
  1007. static struct virtio_driver virtio_balloon_driver = {
  1008. .feature_table = features,
  1009. .feature_table_size = ARRAY_SIZE(features),
  1010. .driver.name = KBUILD_MODNAME,
  1011. .id_table = id_table,
  1012. .validate = virtballoon_validate,
  1013. .probe = virtballoon_probe,
  1014. .remove = virtballoon_remove,
  1015. .config_changed = virtballoon_changed,
  1016. #ifdef CONFIG_PM_SLEEP
  1017. .freeze = virtballoon_freeze,
  1018. .restore = virtballoon_restore,
  1019. #endif
  1020. };
  1021. module_virtio_driver(virtio_balloon_driver);
  1022. MODULE_DEVICE_TABLE(virtio, id_table);
  1023. MODULE_DESCRIPTION("Virtio balloon driver");
  1024. MODULE_LICENSE("GPL");