virtio_balloon.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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/mount.h>
  20. #include <linux/magic.h>
  21. #include <linux/pseudo_fs.h>
  22. #include <linux/page_reporting.h>
  23. /*
  24. * Balloon device works in 4K page units. So each page is pointed to by
  25. * multiple balloon pages. All memory counters in this driver are in balloon
  26. * page units.
  27. */
  28. #define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT)
  29. #define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256
  30. /* Maximum number of (4k) pages to deflate on OOM notifications. */
  31. #define VIRTIO_BALLOON_OOM_NR_PAGES 256
  32. #define VIRTIO_BALLOON_OOM_NOTIFY_PRIORITY 80
  33. #define VIRTIO_BALLOON_FREE_PAGE_ALLOC_FLAG (__GFP_NORETRY | __GFP_NOWARN | \
  34. __GFP_NOMEMALLOC)
  35. /* The order of free page blocks to report to host */
  36. #define VIRTIO_BALLOON_HINT_BLOCK_ORDER (MAX_ORDER - 1)
  37. /* The size of a free page block in bytes */
  38. #define VIRTIO_BALLOON_HINT_BLOCK_BYTES \
  39. (1 << (VIRTIO_BALLOON_HINT_BLOCK_ORDER + PAGE_SHIFT))
  40. #define VIRTIO_BALLOON_HINT_BLOCK_PAGES (1 << VIRTIO_BALLOON_HINT_BLOCK_ORDER)
  41. #ifdef CONFIG_BALLOON_COMPACTION
  42. static struct vfsmount *balloon_mnt;
  43. #endif
  44. enum virtio_balloon_vq {
  45. VIRTIO_BALLOON_VQ_INFLATE,
  46. VIRTIO_BALLOON_VQ_DEFLATE,
  47. VIRTIO_BALLOON_VQ_STATS,
  48. VIRTIO_BALLOON_VQ_FREE_PAGE,
  49. VIRTIO_BALLOON_VQ_REPORTING,
  50. VIRTIO_BALLOON_VQ_MAX
  51. };
  52. enum virtio_balloon_config_read {
  53. VIRTIO_BALLOON_CONFIG_READ_CMD_ID = 0,
  54. };
  55. struct virtio_balloon {
  56. struct virtio_device *vdev;
  57. struct virtqueue *inflate_vq, *deflate_vq, *stats_vq, *free_page_vq;
  58. /* Balloon's own wq for cpu-intensive work items */
  59. struct workqueue_struct *balloon_wq;
  60. /* The free page reporting work item submitted to the balloon wq */
  61. struct work_struct report_free_page_work;
  62. /* The balloon servicing is delegated to a freezable workqueue. */
  63. struct work_struct update_balloon_stats_work;
  64. struct work_struct update_balloon_size_work;
  65. /* Prevent updating balloon when it is being canceled. */
  66. spinlock_t stop_update_lock;
  67. bool stop_update;
  68. /* Bitmap to indicate if reading the related config fields are needed */
  69. unsigned long config_read_bitmap;
  70. /* The list of allocated free pages, waiting to be given back to mm */
  71. struct list_head free_page_list;
  72. spinlock_t free_page_list_lock;
  73. /* The number of free page blocks on the above list */
  74. unsigned long num_free_page_blocks;
  75. /*
  76. * The cmd id received from host.
  77. * Read it via virtio_balloon_cmd_id_received to get the latest value
  78. * sent from host.
  79. */
  80. u32 cmd_id_received_cache;
  81. /* The cmd id that is actively in use */
  82. __virtio32 cmd_id_active;
  83. /* Buffer to store the stop sign */
  84. __virtio32 cmd_id_stop;
  85. /* Waiting for host to ack the pages we released. */
  86. wait_queue_head_t acked;
  87. /* Number of balloon pages we've told the Host we're not using. */
  88. unsigned int num_pages;
  89. /*
  90. * The pages we've told the Host we're not using are enqueued
  91. * at vb_dev_info->pages list.
  92. * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE
  93. * to num_pages above.
  94. */
  95. struct balloon_dev_info vb_dev_info;
  96. /* Synchronize access/update to this struct virtio_balloon elements */
  97. struct mutex balloon_lock;
  98. /* The array of pfns we tell the Host about. */
  99. unsigned int num_pfns;
  100. __virtio32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX];
  101. /* Memory statistics */
  102. struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
  103. /* Shrinker to return free pages - VIRTIO_BALLOON_F_FREE_PAGE_HINT */
  104. struct shrinker shrinker;
  105. /* OOM notifier to deflate on OOM - VIRTIO_BALLOON_F_DEFLATE_ON_OOM */
  106. struct notifier_block oom_nb;
  107. /* Free page reporting device */
  108. struct virtqueue *reporting_vq;
  109. struct page_reporting_dev_info pr_dev_info;
  110. };
  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 balloon_ack(struct virtqueue *vq)
  123. {
  124. struct virtio_balloon *vb = vq->vdev->priv;
  125. wake_up(&vb->acked);
  126. }
  127. static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
  128. {
  129. struct scatterlist sg;
  130. unsigned int len;
  131. sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
  132. /* We should always be able to add one buffer to an empty queue. */
  133. virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  134. virtqueue_kick(vq);
  135. /* When host has read buffer, this completes via balloon_ack */
  136. wait_event(vb->acked, virtqueue_get_buf(vq, &len));
  137. }
  138. static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_info,
  139. struct scatterlist *sg, unsigned int nents)
  140. {
  141. struct virtio_balloon *vb =
  142. container_of(pr_dev_info, struct virtio_balloon, pr_dev_info);
  143. struct virtqueue *vq = vb->reporting_vq;
  144. unsigned int unused, err;
  145. /* We should always be able to add these buffers to an empty queue. */
  146. err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT | __GFP_NOWARN);
  147. /*
  148. * In the extremely unlikely case that something has occurred and we
  149. * are able to trigger an error we will simply display a warning
  150. * and exit without actually processing the pages.
  151. */
  152. if (WARN_ON_ONCE(err))
  153. return err;
  154. virtqueue_kick(vq);
  155. /* When host has read buffer, this completes via balloon_ack */
  156. wait_event(vb->acked, virtqueue_get_buf(vq, &unused));
  157. return 0;
  158. }
  159. static void set_page_pfns(struct virtio_balloon *vb,
  160. __virtio32 pfns[], struct page *page)
  161. {
  162. unsigned int i;
  163. BUILD_BUG_ON(VIRTIO_BALLOON_PAGES_PER_PAGE > VIRTIO_BALLOON_ARRAY_PFNS_MAX);
  164. /*
  165. * Set balloon pfns pointing at this page.
  166. * Note that the first pfn points at start of the page.
  167. */
  168. for (i = 0; i < VIRTIO_BALLOON_PAGES_PER_PAGE; i++)
  169. pfns[i] = cpu_to_virtio32(vb->vdev,
  170. page_to_balloon_pfn(page) + i);
  171. }
  172. static unsigned fill_balloon(struct virtio_balloon *vb, size_t num)
  173. {
  174. unsigned num_allocated_pages;
  175. unsigned num_pfns;
  176. struct page *page;
  177. LIST_HEAD(pages);
  178. /* We can only do one array worth at a time. */
  179. num = min(num, ARRAY_SIZE(vb->pfns));
  180. for (num_pfns = 0; num_pfns < num;
  181. num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  182. struct page *page = balloon_page_alloc();
  183. if (!page) {
  184. dev_info_ratelimited(&vb->vdev->dev,
  185. "Out of puff! Can't get %u pages\n",
  186. VIRTIO_BALLOON_PAGES_PER_PAGE);
  187. /* Sleep for at least 1/5 of a second before retry. */
  188. msleep(200);
  189. break;
  190. }
  191. balloon_page_push(&pages, page);
  192. }
  193. mutex_lock(&vb->balloon_lock);
  194. vb->num_pfns = 0;
  195. while ((page = balloon_page_pop(&pages))) {
  196. balloon_page_enqueue(&vb->vb_dev_info, page);
  197. set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
  198. vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
  199. if (!virtio_has_feature(vb->vdev,
  200. VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  201. adjust_managed_page_count(page, -1);
  202. vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE;
  203. }
  204. num_allocated_pages = vb->num_pfns;
  205. /* Did we get any? */
  206. if (vb->num_pfns != 0)
  207. tell_host(vb, vb->inflate_vq);
  208. mutex_unlock(&vb->balloon_lock);
  209. return num_allocated_pages;
  210. }
  211. static void release_pages_balloon(struct virtio_balloon *vb,
  212. struct list_head *pages)
  213. {
  214. struct page *page, *next;
  215. list_for_each_entry_safe(page, next, pages, lru) {
  216. if (!virtio_has_feature(vb->vdev,
  217. VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  218. adjust_managed_page_count(page, 1);
  219. list_del(&page->lru);
  220. put_page(page); /* balloon reference */
  221. }
  222. }
  223. static unsigned leak_balloon(struct virtio_balloon *vb, size_t num)
  224. {
  225. unsigned num_freed_pages;
  226. struct page *page;
  227. struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
  228. LIST_HEAD(pages);
  229. /* We can only do one array worth at a time. */
  230. num = min(num, ARRAY_SIZE(vb->pfns));
  231. mutex_lock(&vb->balloon_lock);
  232. /* We can't release more pages than taken */
  233. num = min(num, (size_t)vb->num_pages);
  234. for (vb->num_pfns = 0; vb->num_pfns < num;
  235. vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
  236. page = balloon_page_dequeue(vb_dev_info);
  237. if (!page)
  238. break;
  239. set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
  240. list_add(&page->lru, &pages);
  241. vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
  242. }
  243. num_freed_pages = vb->num_pfns;
  244. /*
  245. * Note that if
  246. * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
  247. * is true, we *have* to do it in this order
  248. */
  249. if (vb->num_pfns != 0)
  250. tell_host(vb, vb->deflate_vq);
  251. release_pages_balloon(vb, &pages);
  252. mutex_unlock(&vb->balloon_lock);
  253. return num_freed_pages;
  254. }
  255. static inline void update_stat(struct virtio_balloon *vb, int idx,
  256. u16 tag, u64 val)
  257. {
  258. BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
  259. vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag);
  260. vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val);
  261. }
  262. #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
  263. static unsigned int update_balloon_stats(struct virtio_balloon *vb)
  264. {
  265. unsigned long events[NR_VM_EVENT_ITEMS];
  266. struct sysinfo i;
  267. unsigned int idx = 0;
  268. long available;
  269. unsigned long caches;
  270. all_vm_events(events);
  271. si_meminfo(&i);
  272. available = si_mem_available();
  273. caches = global_node_page_state(NR_FILE_PAGES);
  274. #ifdef CONFIG_VM_EVENT_COUNTERS
  275. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
  276. pages_to_bytes(events[PSWPIN]));
  277. update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
  278. pages_to_bytes(events[PSWPOUT]));
  279. update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
  280. update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
  281. #ifdef CONFIG_HUGETLB_PAGE
  282. update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGALLOC,
  283. events[HTLB_BUDDY_PGALLOC]);
  284. update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGFAIL,
  285. events[HTLB_BUDDY_PGALLOC_FAIL]);
  286. #endif
  287. #endif
  288. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
  289. pages_to_bytes(i.freeram));
  290. update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
  291. pages_to_bytes(i.totalram));
  292. update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
  293. pages_to_bytes(available));
  294. update_stat(vb, idx++, VIRTIO_BALLOON_S_CACHES,
  295. pages_to_bytes(caches));
  296. return idx;
  297. }
  298. /*
  299. * While most virtqueues communicate guest-initiated requests to the hypervisor,
  300. * the stats queue operates in reverse. The driver initializes the virtqueue
  301. * with a single buffer. From that point forward, all conversations consist of
  302. * a hypervisor request (a call to this function) which directs us to refill
  303. * the virtqueue with a fresh stats buffer. Since stats collection can sleep,
  304. * we delegate the job to a freezable workqueue that will do the actual work via
  305. * stats_handle_request().
  306. */
  307. static void stats_request(struct virtqueue *vq)
  308. {
  309. struct virtio_balloon *vb = vq->vdev->priv;
  310. spin_lock(&vb->stop_update_lock);
  311. if (!vb->stop_update)
  312. queue_work(system_freezable_wq, &vb->update_balloon_stats_work);
  313. spin_unlock(&vb->stop_update_lock);
  314. }
  315. static void stats_handle_request(struct virtio_balloon *vb)
  316. {
  317. struct virtqueue *vq;
  318. struct scatterlist sg;
  319. unsigned int len, num_stats;
  320. num_stats = update_balloon_stats(vb);
  321. vq = vb->stats_vq;
  322. if (!virtqueue_get_buf(vq, &len))
  323. return;
  324. sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
  325. virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
  326. virtqueue_kick(vq);
  327. }
  328. static inline s64 towards_target(struct virtio_balloon *vb)
  329. {
  330. s64 target;
  331. u32 num_pages;
  332. /* Legacy balloon config space is LE, unlike all other devices. */
  333. virtio_cread_le(vb->vdev, struct virtio_balloon_config, num_pages,
  334. &num_pages);
  335. target = num_pages;
  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 = vb->vdev->config->find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX,
  442. vqs, callbacks, names, NULL, 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. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
  469. vb->reporting_vq = vqs[VIRTIO_BALLOON_VQ_REPORTING];
  470. return 0;
  471. }
  472. static u32 virtio_balloon_cmd_id_received(struct virtio_balloon *vb)
  473. {
  474. if (test_and_clear_bit(VIRTIO_BALLOON_CONFIG_READ_CMD_ID,
  475. &vb->config_read_bitmap)) {
  476. /* Legacy balloon config space is LE, unlike all other devices. */
  477. virtio_cread_le(vb->vdev, struct virtio_balloon_config,
  478. free_page_hint_cmd_id,
  479. &vb->cmd_id_received_cache);
  480. }
  481. return vb->cmd_id_received_cache;
  482. }
  483. static int send_cmd_id_start(struct virtio_balloon *vb)
  484. {
  485. struct scatterlist sg;
  486. struct virtqueue *vq = vb->free_page_vq;
  487. int err, unused;
  488. /* Detach all the used buffers from the vq */
  489. while (virtqueue_get_buf(vq, &unused))
  490. ;
  491. vb->cmd_id_active = cpu_to_virtio32(vb->vdev,
  492. virtio_balloon_cmd_id_received(vb));
  493. sg_init_one(&sg, &vb->cmd_id_active, sizeof(vb->cmd_id_active));
  494. err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_active, GFP_KERNEL);
  495. if (!err)
  496. virtqueue_kick(vq);
  497. return err;
  498. }
  499. static int send_cmd_id_stop(struct virtio_balloon *vb)
  500. {
  501. struct scatterlist sg;
  502. struct virtqueue *vq = vb->free_page_vq;
  503. int err, unused;
  504. /* Detach all the used buffers from the vq */
  505. while (virtqueue_get_buf(vq, &unused))
  506. ;
  507. sg_init_one(&sg, &vb->cmd_id_stop, sizeof(vb->cmd_id_stop));
  508. err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_stop, GFP_KERNEL);
  509. if (!err)
  510. virtqueue_kick(vq);
  511. return err;
  512. }
  513. static int get_free_page_and_send(struct virtio_balloon *vb)
  514. {
  515. struct virtqueue *vq = vb->free_page_vq;
  516. struct page *page;
  517. struct scatterlist sg;
  518. int err, unused;
  519. void *p;
  520. /* Detach all the used buffers from the vq */
  521. while (virtqueue_get_buf(vq, &unused))
  522. ;
  523. page = alloc_pages(VIRTIO_BALLOON_FREE_PAGE_ALLOC_FLAG,
  524. VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  525. /*
  526. * When the allocation returns NULL, it indicates that we have got all
  527. * the possible free pages, so return -EINTR to stop.
  528. */
  529. if (!page)
  530. return -EINTR;
  531. p = page_address(page);
  532. sg_init_one(&sg, p, VIRTIO_BALLOON_HINT_BLOCK_BYTES);
  533. /* There is always 1 entry reserved for the cmd id to use. */
  534. if (vq->num_free > 1) {
  535. err = virtqueue_add_inbuf(vq, &sg, 1, p, GFP_KERNEL);
  536. if (unlikely(err)) {
  537. free_pages((unsigned long)p,
  538. VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  539. return err;
  540. }
  541. virtqueue_kick(vq);
  542. spin_lock_irq(&vb->free_page_list_lock);
  543. balloon_page_push(&vb->free_page_list, page);
  544. vb->num_free_page_blocks++;
  545. spin_unlock_irq(&vb->free_page_list_lock);
  546. } else {
  547. /*
  548. * The vq has no available entry to add this page block, so
  549. * just free it.
  550. */
  551. free_pages((unsigned long)p, VIRTIO_BALLOON_HINT_BLOCK_ORDER);
  552. }
  553. return 0;
  554. }
  555. static int send_free_pages(struct virtio_balloon *vb)
  556. {
  557. int err;
  558. u32 cmd_id_active;
  559. while (1) {
  560. /*
  561. * If a stop id or a new cmd id was just received from host,
  562. * stop the reporting.
  563. */
  564. cmd_id_active = virtio32_to_cpu(vb->vdev, vb->cmd_id_active);
  565. if (unlikely(cmd_id_active !=
  566. virtio_balloon_cmd_id_received(vb)))
  567. break;
  568. /*
  569. * The free page blocks are allocated and sent to host one by
  570. * one.
  571. */
  572. err = get_free_page_and_send(vb);
  573. if (err == -EINTR)
  574. break;
  575. else if (unlikely(err))
  576. return err;
  577. }
  578. return 0;
  579. }
  580. static void virtio_balloon_report_free_page(struct virtio_balloon *vb)
  581. {
  582. int err;
  583. struct device *dev = &vb->vdev->dev;
  584. /* Start by sending the received cmd id to host with an outbuf. */
  585. err = send_cmd_id_start(vb);
  586. if (unlikely(err))
  587. dev_err(dev, "Failed to send a start id, err = %d\n", err);
  588. err = send_free_pages(vb);
  589. if (unlikely(err))
  590. dev_err(dev, "Failed to send a free page, err = %d\n", err);
  591. /* End by sending a stop id to host with an outbuf. */
  592. err = send_cmd_id_stop(vb);
  593. if (unlikely(err))
  594. dev_err(dev, "Failed to send a stop id, err = %d\n", err);
  595. }
  596. static void report_free_page_func(struct work_struct *work)
  597. {
  598. struct virtio_balloon *vb = container_of(work, struct virtio_balloon,
  599. report_free_page_work);
  600. u32 cmd_id_received;
  601. cmd_id_received = virtio_balloon_cmd_id_received(vb);
  602. if (cmd_id_received == VIRTIO_BALLOON_CMD_ID_DONE) {
  603. /* Pass ULONG_MAX to give back all the free pages */
  604. return_free_pages_to_mm(vb, ULONG_MAX);
  605. } else if (cmd_id_received != VIRTIO_BALLOON_CMD_ID_STOP &&
  606. cmd_id_received !=
  607. virtio32_to_cpu(vb->vdev, vb->cmd_id_active)) {
  608. virtio_balloon_report_free_page(vb);
  609. }
  610. }
  611. #ifdef CONFIG_BALLOON_COMPACTION
  612. /*
  613. * virtballoon_migratepage - perform the balloon page migration on behalf of
  614. * a compation thread. (called under page lock)
  615. * @vb_dev_info: the balloon device
  616. * @newpage: page that will replace the isolated page after migration finishes.
  617. * @page : the isolated (old) page that is about to be migrated to newpage.
  618. * @mode : compaction mode -- not used for balloon page migration.
  619. *
  620. * After a ballooned page gets isolated by compaction procedures, this is the
  621. * function that performs the page migration on behalf of a compaction thread
  622. * The page migration for virtio balloon is done in a simple swap fashion which
  623. * follows these two macro steps:
  624. * 1) insert newpage into vb->pages list and update the host about it;
  625. * 2) update the host about the old page removed from vb->pages list;
  626. *
  627. * This function preforms the balloon page migration task.
  628. * Called through balloon_mapping->a_ops->migratepage
  629. */
  630. static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info,
  631. struct page *newpage, struct page *page, enum migrate_mode mode)
  632. {
  633. struct virtio_balloon *vb = container_of(vb_dev_info,
  634. struct virtio_balloon, vb_dev_info);
  635. unsigned long flags;
  636. /*
  637. * In order to avoid lock contention while migrating pages concurrently
  638. * to leak_balloon() or fill_balloon() we just give up the balloon_lock
  639. * this turn, as it is easier to retry the page migration later.
  640. * This also prevents fill_balloon() getting stuck into a mutex
  641. * recursion in the case it ends up triggering memory compaction
  642. * while it is attempting to inflate the ballon.
  643. */
  644. if (!mutex_trylock(&vb->balloon_lock))
  645. return -EAGAIN;
  646. get_page(newpage); /* balloon reference */
  647. /*
  648. * When we migrate a page to a different zone and adjusted the
  649. * managed page count when inflating, we have to fixup the count of
  650. * both involved zones.
  651. */
  652. if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM) &&
  653. page_zone(page) != page_zone(newpage)) {
  654. adjust_managed_page_count(page, 1);
  655. adjust_managed_page_count(newpage, -1);
  656. }
  657. /* balloon's page migration 1st step -- inflate "newpage" */
  658. spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
  659. balloon_page_insert(vb_dev_info, newpage);
  660. vb_dev_info->isolated_pages--;
  661. __count_vm_event(BALLOON_MIGRATE);
  662. spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
  663. vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
  664. set_page_pfns(vb, vb->pfns, newpage);
  665. tell_host(vb, vb->inflate_vq);
  666. /* balloon's page migration 2nd step -- deflate "page" */
  667. spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
  668. balloon_page_delete(page);
  669. spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
  670. vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
  671. set_page_pfns(vb, vb->pfns, page);
  672. tell_host(vb, vb->deflate_vq);
  673. mutex_unlock(&vb->balloon_lock);
  674. put_page(page); /* balloon reference */
  675. return MIGRATEPAGE_SUCCESS;
  676. }
  677. static int balloon_init_fs_context(struct fs_context *fc)
  678. {
  679. return init_pseudo(fc, BALLOON_KVM_MAGIC) ? 0 : -ENOMEM;
  680. }
  681. static struct file_system_type balloon_fs = {
  682. .name = "balloon-kvm",
  683. .init_fs_context = balloon_init_fs_context,
  684. .kill_sb = kill_anon_super,
  685. };
  686. #endif /* CONFIG_BALLOON_COMPACTION */
  687. static unsigned long shrink_free_pages(struct virtio_balloon *vb,
  688. unsigned long pages_to_free)
  689. {
  690. unsigned long blocks_to_free, blocks_freed;
  691. pages_to_free = round_up(pages_to_free,
  692. VIRTIO_BALLOON_HINT_BLOCK_PAGES);
  693. blocks_to_free = pages_to_free / VIRTIO_BALLOON_HINT_BLOCK_PAGES;
  694. blocks_freed = return_free_pages_to_mm(vb, blocks_to_free);
  695. return blocks_freed * VIRTIO_BALLOON_HINT_BLOCK_PAGES;
  696. }
  697. static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,
  698. struct shrink_control *sc)
  699. {
  700. struct virtio_balloon *vb = container_of(shrinker,
  701. struct virtio_balloon, shrinker);
  702. return shrink_free_pages(vb, sc->nr_to_scan);
  703. }
  704. static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker,
  705. struct shrink_control *sc)
  706. {
  707. struct virtio_balloon *vb = container_of(shrinker,
  708. struct virtio_balloon, shrinker);
  709. return vb->num_free_page_blocks * VIRTIO_BALLOON_HINT_BLOCK_PAGES;
  710. }
  711. static int virtio_balloon_oom_notify(struct notifier_block *nb,
  712. unsigned long dummy, void *parm)
  713. {
  714. struct virtio_balloon *vb = container_of(nb,
  715. struct virtio_balloon, oom_nb);
  716. unsigned long *freed = parm;
  717. *freed += leak_balloon(vb, VIRTIO_BALLOON_OOM_NR_PAGES) /
  718. VIRTIO_BALLOON_PAGES_PER_PAGE;
  719. update_balloon_size(vb);
  720. return NOTIFY_OK;
  721. }
  722. static void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb)
  723. {
  724. unregister_shrinker(&vb->shrinker);
  725. }
  726. static int virtio_balloon_register_shrinker(struct virtio_balloon *vb)
  727. {
  728. vb->shrinker.scan_objects = virtio_balloon_shrinker_scan;
  729. vb->shrinker.count_objects = virtio_balloon_shrinker_count;
  730. vb->shrinker.seeks = DEFAULT_SEEKS;
  731. return register_shrinker(&vb->shrinker);
  732. }
  733. static int virtballoon_probe(struct virtio_device *vdev)
  734. {
  735. struct virtio_balloon *vb;
  736. int err;
  737. if (!vdev->config->get) {
  738. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  739. __func__);
  740. return -EINVAL;
  741. }
  742. vdev->priv = vb = kzalloc(sizeof(*vb), GFP_KERNEL);
  743. if (!vb) {
  744. err = -ENOMEM;
  745. goto out;
  746. }
  747. INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
  748. INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
  749. spin_lock_init(&vb->stop_update_lock);
  750. mutex_init(&vb->balloon_lock);
  751. init_waitqueue_head(&vb->acked);
  752. vb->vdev = vdev;
  753. balloon_devinfo_init(&vb->vb_dev_info);
  754. err = init_vqs(vb);
  755. if (err)
  756. goto out_free_vb;
  757. #ifdef CONFIG_BALLOON_COMPACTION
  758. balloon_mnt = kern_mount(&balloon_fs);
  759. if (IS_ERR(balloon_mnt)) {
  760. err = PTR_ERR(balloon_mnt);
  761. goto out_del_vqs;
  762. }
  763. vb->vb_dev_info.migratepage = virtballoon_migratepage;
  764. vb->vb_dev_info.inode = alloc_anon_inode(balloon_mnt->mnt_sb);
  765. if (IS_ERR(vb->vb_dev_info.inode)) {
  766. err = PTR_ERR(vb->vb_dev_info.inode);
  767. goto out_kern_unmount;
  768. }
  769. vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops;
  770. #endif
  771. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
  772. /*
  773. * There is always one entry reserved for cmd id, so the ring
  774. * size needs to be at least two to report free page hints.
  775. */
  776. if (virtqueue_get_vring_size(vb->free_page_vq) < 2) {
  777. err = -ENOSPC;
  778. goto out_iput;
  779. }
  780. vb->balloon_wq = alloc_workqueue("balloon-wq",
  781. WQ_FREEZABLE | WQ_CPU_INTENSIVE, 0);
  782. if (!vb->balloon_wq) {
  783. err = -ENOMEM;
  784. goto out_iput;
  785. }
  786. INIT_WORK(&vb->report_free_page_work, report_free_page_func);
  787. vb->cmd_id_received_cache = VIRTIO_BALLOON_CMD_ID_STOP;
  788. vb->cmd_id_active = cpu_to_virtio32(vb->vdev,
  789. VIRTIO_BALLOON_CMD_ID_STOP);
  790. vb->cmd_id_stop = cpu_to_virtio32(vb->vdev,
  791. VIRTIO_BALLOON_CMD_ID_STOP);
  792. spin_lock_init(&vb->free_page_list_lock);
  793. INIT_LIST_HEAD(&vb->free_page_list);
  794. /*
  795. * We're allowed to reuse any free pages, even if they are
  796. * still to be processed by the host.
  797. */
  798. err = virtio_balloon_register_shrinker(vb);
  799. if (err)
  800. goto out_del_balloon_wq;
  801. }
  802. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) {
  803. vb->oom_nb.notifier_call = virtio_balloon_oom_notify;
  804. vb->oom_nb.priority = VIRTIO_BALLOON_OOM_NOTIFY_PRIORITY;
  805. err = register_oom_notifier(&vb->oom_nb);
  806. if (err < 0)
  807. goto out_unregister_shrinker;
  808. }
  809. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) {
  810. /* Start with poison val of 0 representing general init */
  811. __u32 poison_val = 0;
  812. /*
  813. * Let the hypervisor know that we are expecting a
  814. * specific value to be written back in balloon pages.
  815. *
  816. * If the PAGE_POISON value was larger than a byte we would
  817. * need to byte swap poison_val here to guarantee it is
  818. * little-endian. However for now it is a single byte so we
  819. * can pass it as-is.
  820. */
  821. if (!want_init_on_free())
  822. memset(&poison_val, PAGE_POISON, sizeof(poison_val));
  823. virtio_cwrite_le(vb->vdev, struct virtio_balloon_config,
  824. poison_val, &poison_val);
  825. }
  826. vb->pr_dev_info.report = virtballoon_free_page_report;
  827. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
  828. unsigned int capacity;
  829. capacity = virtqueue_get_vring_size(vb->reporting_vq);
  830. if (capacity < PAGE_REPORTING_CAPACITY) {
  831. err = -ENOSPC;
  832. goto out_unregister_oom;
  833. }
  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_iput:
  852. #ifdef CONFIG_BALLOON_COMPACTION
  853. iput(vb->vb_dev_info.inode);
  854. out_kern_unmount:
  855. kern_unmount(balloon_mnt);
  856. out_del_vqs:
  857. #endif
  858. vdev->config->del_vqs(vdev);
  859. out_free_vb:
  860. kfree(vb);
  861. out:
  862. return err;
  863. }
  864. static void remove_common(struct virtio_balloon *vb)
  865. {
  866. /* There might be pages left in the balloon: free them. */
  867. while (vb->num_pages)
  868. leak_balloon(vb, vb->num_pages);
  869. update_balloon_size(vb);
  870. /* There might be free pages that are being reported: release them. */
  871. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  872. return_free_pages_to_mm(vb, ULONG_MAX);
  873. /* Now we reset the device so we can clean up the queues. */
  874. vb->vdev->config->reset(vb->vdev);
  875. vb->vdev->config->del_vqs(vb->vdev);
  876. }
  877. static void virtballoon_remove(struct virtio_device *vdev)
  878. {
  879. struct virtio_balloon *vb = vdev->priv;
  880. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
  881. page_reporting_unregister(&vb->pr_dev_info);
  882. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
  883. unregister_oom_notifier(&vb->oom_nb);
  884. if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
  885. virtio_balloon_unregister_shrinker(vb);
  886. spin_lock_irq(&vb->stop_update_lock);
  887. vb->stop_update = true;
  888. spin_unlock_irq(&vb->stop_update_lock);
  889. cancel_work_sync(&vb->update_balloon_size_work);
  890. cancel_work_sync(&vb->update_balloon_stats_work);
  891. if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
  892. cancel_work_sync(&vb->report_free_page_work);
  893. destroy_workqueue(vb->balloon_wq);
  894. }
  895. remove_common(vb);
  896. #ifdef CONFIG_BALLOON_COMPACTION
  897. if (vb->vb_dev_info.inode)
  898. iput(vb->vb_dev_info.inode);
  899. kern_unmount(balloon_mnt);
  900. #endif
  901. kfree(vb);
  902. }
  903. #ifdef CONFIG_PM_SLEEP
  904. static int virtballoon_freeze(struct virtio_device *vdev)
  905. {
  906. struct virtio_balloon *vb = vdev->priv;
  907. /*
  908. * The workqueue is already frozen by the PM core before this
  909. * function is called.
  910. */
  911. remove_common(vb);
  912. return 0;
  913. }
  914. static int virtballoon_restore(struct virtio_device *vdev)
  915. {
  916. struct virtio_balloon *vb = vdev->priv;
  917. int ret;
  918. ret = init_vqs(vdev->priv);
  919. if (ret)
  920. return ret;
  921. virtio_device_ready(vdev);
  922. if (towards_target(vb))
  923. virtballoon_changed(vdev);
  924. update_balloon_size(vb);
  925. return 0;
  926. }
  927. #endif
  928. static int virtballoon_validate(struct virtio_device *vdev)
  929. {
  930. /*
  931. * Inform the hypervisor that our pages are poisoned or
  932. * initialized. If we cannot do that then we should disable
  933. * page reporting as it could potentially change the contents
  934. * of our free pages.
  935. */
  936. if (!want_init_on_free() && !page_poisoning_enabled_static())
  937. __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_PAGE_POISON);
  938. else if (!virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON))
  939. __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_REPORTING);
  940. __virtio_clear_bit(vdev, VIRTIO_F_ACCESS_PLATFORM);
  941. return 0;
  942. }
  943. static unsigned int features[] = {
  944. VIRTIO_BALLOON_F_MUST_TELL_HOST,
  945. VIRTIO_BALLOON_F_STATS_VQ,
  946. VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
  947. VIRTIO_BALLOON_F_FREE_PAGE_HINT,
  948. VIRTIO_BALLOON_F_PAGE_POISON,
  949. VIRTIO_BALLOON_F_REPORTING,
  950. };
  951. static struct virtio_driver virtio_balloon_driver = {
  952. .feature_table = features,
  953. .feature_table_size = ARRAY_SIZE(features),
  954. .driver.name = KBUILD_MODNAME,
  955. .driver.owner = THIS_MODULE,
  956. .id_table = id_table,
  957. .validate = virtballoon_validate,
  958. .probe = virtballoon_probe,
  959. .remove = virtballoon_remove,
  960. .config_changed = virtballoon_changed,
  961. #ifdef CONFIG_PM_SLEEP
  962. .freeze = virtballoon_freeze,
  963. .restore = virtballoon_restore,
  964. #endif
  965. };
  966. module_virtio_driver(virtio_balloon_driver);
  967. MODULE_DEVICE_TABLE(virtio, id_table);
  968. MODULE_DESCRIPTION("Virtio balloon driver");
  969. MODULE_LICENSE("GPL");