virtio_balloon.c 33 KB

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