1 /*
2  * Virtio balloon implementation, inspired by Dor Laor and Marcelo
3  * Tosatti's implementations.
4  *
5  *  Copyright 2008 Rusty Russell IBM Corporation
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include <linux/virtio.h>
23 #include <linux/virtio_balloon.h>
24 #include <linux/swap.h>
25 #include <linux/workqueue.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/balloon_compaction.h>
30 #include <linux/wait.h>
31 #include <linux/mm.h>
32 #include <linux/mount.h>
33 #include <linux/magic.h>
34 
35 /*
36  * Balloon device works in 4K page units.  So each page is pointed to by
37  * multiple balloon pages.  All memory counters in this driver are in balloon
38  * page units.
39  */
40 #define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT)
41 #define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256
42 #define VIRTBALLOON_OOM_NOTIFY_PRIORITY 80
43 
44 #ifdef CONFIG_BALLOON_COMPACTION
45 static struct vfsmount *balloon_mnt;
46 #endif
47 
48 struct virtio_balloon {
49 	struct virtio_device *vdev;
50 	struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
51 
52 	/* The balloon servicing is delegated to a freezable workqueue. */
53 	struct work_struct update_balloon_stats_work;
54 	struct work_struct update_balloon_size_work;
55 
56 	/* Prevent updating balloon when it is being canceled. */
57 	spinlock_t stop_update_lock;
58 	bool stop_update;
59 
60 	/* Waiting for host to ack the pages we released. */
61 	wait_queue_head_t acked;
62 
63 	/* Number of balloon pages we've told the Host we're not using. */
64 	unsigned int num_pages;
65 	/*
66 	 * The pages we've told the Host we're not using are enqueued
67 	 * at vb_dev_info->pages list.
68 	 * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE
69 	 * to num_pages above.
70 	 */
71 	struct balloon_dev_info vb_dev_info;
72 
73 	/* Synchronize access/update to this struct virtio_balloon elements */
74 	struct mutex balloon_lock;
75 
76 	/* The array of pfns we tell the Host about. */
77 	unsigned int num_pfns;
78 	__virtio32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX];
79 
80 	/* Memory statistics */
81 	struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
82 
83 	/* To register a shrinker to shrink memory upon memory pressure */
84 	struct shrinker shrinker;
85 };
86 
87 static struct virtio_device_id id_table[] = {
88 	{ VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
89 	{ 0 },
90 };
91 
92 static u32 page_to_balloon_pfn(struct page *page)
93 {
94 	unsigned long pfn = page_to_pfn(page);
95 
96 	BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT);
97 	/* Convert pfn from Linux page size to balloon page size. */
98 	return pfn * VIRTIO_BALLOON_PAGES_PER_PAGE;
99 }
100 
101 static void balloon_ack(struct virtqueue *vq)
102 {
103 	struct virtio_balloon *vb = vq->vdev->priv;
104 
105 	wake_up(&vb->acked);
106 }
107 
108 static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
109 {
110 	struct scatterlist sg;
111 	unsigned int len;
112 
113 	sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
114 
115 	/* We should always be able to add one buffer to an empty queue. */
116 	virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
117 	virtqueue_kick(vq);
118 
119 	/* When host has read buffer, this completes via balloon_ack */
120 	wait_event(vb->acked, virtqueue_get_buf(vq, &len));
121 
122 }
123 
124 static void set_page_pfns(struct virtio_balloon *vb,
125 			  __virtio32 pfns[], struct page *page)
126 {
127 	unsigned int i;
128 
129 	/*
130 	 * Set balloon pfns pointing at this page.
131 	 * Note that the first pfn points at start of the page.
132 	 */
133 	for (i = 0; i < VIRTIO_BALLOON_PAGES_PER_PAGE; i++)
134 		pfns[i] = cpu_to_virtio32(vb->vdev,
135 					  page_to_balloon_pfn(page) + i);
136 }
137 
138 static unsigned fill_balloon(struct virtio_balloon *vb, size_t num)
139 {
140 	unsigned num_allocated_pages;
141 	unsigned num_pfns;
142 	struct page *page;
143 	LIST_HEAD(pages);
144 
145 	/* We can only do one array worth at a time. */
146 	num = min(num, ARRAY_SIZE(vb->pfns));
147 
148 	for (num_pfns = 0; num_pfns < num;
149 	     num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
150 		struct page *page = balloon_page_alloc();
151 
152 		if (!page) {
153 			dev_info_ratelimited(&vb->vdev->dev,
154 					     "Out of puff! Can't get %u pages\n",
155 					     VIRTIO_BALLOON_PAGES_PER_PAGE);
156 			/* Sleep for at least 1/5 of a second before retry. */
157 			msleep(200);
158 			break;
159 		}
160 
161 		balloon_page_push(&pages, page);
162 	}
163 
164 	mutex_lock(&vb->balloon_lock);
165 
166 	vb->num_pfns = 0;
167 
168 	while ((page = balloon_page_pop(&pages))) {
169 		balloon_page_enqueue(&vb->vb_dev_info, page);
170 
171 		set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
172 		vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
173 		if (!virtio_has_feature(vb->vdev,
174 					VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
175 			adjust_managed_page_count(page, -1);
176 		vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE;
177 	}
178 
179 	num_allocated_pages = vb->num_pfns;
180 	/* Did we get any? */
181 	if (vb->num_pfns != 0)
182 		tell_host(vb, vb->inflate_vq);
183 	mutex_unlock(&vb->balloon_lock);
184 
185 	return num_allocated_pages;
186 }
187 
188 static void release_pages_balloon(struct virtio_balloon *vb,
189 				 struct list_head *pages)
190 {
191 	struct page *page, *next;
192 
193 	list_for_each_entry_safe(page, next, pages, lru) {
194 		if (!virtio_has_feature(vb->vdev,
195 					VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
196 			adjust_managed_page_count(page, 1);
197 		list_del(&page->lru);
198 		put_page(page); /* balloon reference */
199 	}
200 }
201 
202 static unsigned leak_balloon(struct virtio_balloon *vb, size_t num)
203 {
204 	unsigned num_freed_pages;
205 	struct page *page;
206 	struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
207 	LIST_HEAD(pages);
208 
209 	/* We can only do one array worth at a time. */
210 	num = min(num, ARRAY_SIZE(vb->pfns));
211 
212 	mutex_lock(&vb->balloon_lock);
213 	/* We can't release more pages than taken */
214 	num = min(num, (size_t)vb->num_pages);
215 	for (vb->num_pfns = 0; vb->num_pfns < num;
216 	     vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
217 		page = balloon_page_dequeue(vb_dev_info);
218 		if (!page)
219 			break;
220 		set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
221 		list_add(&page->lru, &pages);
222 		vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
223 	}
224 
225 	num_freed_pages = vb->num_pfns;
226 	/*
227 	 * Note that if
228 	 * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
229 	 * is true, we *have* to do it in this order
230 	 */
231 	if (vb->num_pfns != 0)
232 		tell_host(vb, vb->deflate_vq);
233 	release_pages_balloon(vb, &pages);
234 	mutex_unlock(&vb->balloon_lock);
235 	return num_freed_pages;
236 }
237 
238 static inline void update_stat(struct virtio_balloon *vb, int idx,
239 			       u16 tag, u64 val)
240 {
241 	BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
242 	vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag);
243 	vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val);
244 }
245 
246 #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
247 
248 static unsigned int update_balloon_stats(struct virtio_balloon *vb)
249 {
250 	unsigned long events[NR_VM_EVENT_ITEMS];
251 	struct sysinfo i;
252 	unsigned int idx = 0;
253 	long available;
254 	unsigned long caches;
255 
256 	all_vm_events(events);
257 	si_meminfo(&i);
258 
259 	available = si_mem_available();
260 	caches = global_node_page_state(NR_FILE_PAGES);
261 
262 #ifdef CONFIG_VM_EVENT_COUNTERS
263 	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
264 				pages_to_bytes(events[PSWPIN]));
265 	update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
266 				pages_to_bytes(events[PSWPOUT]));
267 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
268 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
269 #ifdef CONFIG_HUGETLB_PAGE
270 	update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGALLOC,
271 		    events[HTLB_BUDDY_PGALLOC]);
272 	update_stat(vb, idx++, VIRTIO_BALLOON_S_HTLB_PGFAIL,
273 		    events[HTLB_BUDDY_PGALLOC_FAIL]);
274 #endif
275 #endif
276 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
277 				pages_to_bytes(i.freeram));
278 	update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
279 				pages_to_bytes(i.totalram));
280 	update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
281 				pages_to_bytes(available));
282 	update_stat(vb, idx++, VIRTIO_BALLOON_S_CACHES,
283 				pages_to_bytes(caches));
284 
285 	return idx;
286 }
287 
288 /*
289  * While most virtqueues communicate guest-initiated requests to the hypervisor,
290  * the stats queue operates in reverse.  The driver initializes the virtqueue
291  * with a single buffer.  From that point forward, all conversations consist of
292  * a hypervisor request (a call to this function) which directs us to refill
293  * the virtqueue with a fresh stats buffer.  Since stats collection can sleep,
294  * we delegate the job to a freezable workqueue that will do the actual work via
295  * stats_handle_request().
296  */
297 static void stats_request(struct virtqueue *vq)
298 {
299 	struct virtio_balloon *vb = vq->vdev->priv;
300 
301 	spin_lock(&vb->stop_update_lock);
302 	if (!vb->stop_update)
303 		queue_work(system_freezable_wq, &vb->update_balloon_stats_work);
304 	spin_unlock(&vb->stop_update_lock);
305 }
306 
307 static void stats_handle_request(struct virtio_balloon *vb)
308 {
309 	struct virtqueue *vq;
310 	struct scatterlist sg;
311 	unsigned int len, num_stats;
312 
313 	num_stats = update_balloon_stats(vb);
314 
315 	vq = vb->stats_vq;
316 	if (!virtqueue_get_buf(vq, &len))
317 		return;
318 	sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
319 	virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
320 	virtqueue_kick(vq);
321 }
322 
323 static void virtballoon_changed(struct virtio_device *vdev)
324 {
325 	struct virtio_balloon *vb = vdev->priv;
326 	unsigned long flags;
327 
328 	spin_lock_irqsave(&vb->stop_update_lock, flags);
329 	if (!vb->stop_update)
330 		queue_work(system_freezable_wq, &vb->update_balloon_size_work);
331 	spin_unlock_irqrestore(&vb->stop_update_lock, flags);
332 }
333 
334 static inline s64 towards_target(struct virtio_balloon *vb)
335 {
336 	s64 target;
337 	u32 num_pages;
338 
339 	virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages,
340 		     &num_pages);
341 
342 	/* Legacy balloon config space is LE, unlike all other devices. */
343 	if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1))
344 		num_pages = le32_to_cpu((__force __le32)num_pages);
345 
346 	target = num_pages;
347 	return target - vb->num_pages;
348 }
349 
350 static void update_balloon_size(struct virtio_balloon *vb)
351 {
352 	u32 actual = vb->num_pages;
353 
354 	/* Legacy balloon config space is LE, unlike all other devices. */
355 	if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1))
356 		actual = (__force u32)cpu_to_le32(actual);
357 
358 	virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual,
359 		      &actual);
360 }
361 
362 static void update_balloon_stats_func(struct work_struct *work)
363 {
364 	struct virtio_balloon *vb;
365 
366 	vb = container_of(work, struct virtio_balloon,
367 			  update_balloon_stats_work);
368 	stats_handle_request(vb);
369 }
370 
371 static void update_balloon_size_func(struct work_struct *work)
372 {
373 	struct virtio_balloon *vb;
374 	s64 diff;
375 
376 	vb = container_of(work, struct virtio_balloon,
377 			  update_balloon_size_work);
378 	diff = towards_target(vb);
379 
380 	if (diff > 0)
381 		diff -= fill_balloon(vb, diff);
382 	else if (diff < 0)
383 		diff += leak_balloon(vb, -diff);
384 	update_balloon_size(vb);
385 
386 	if (diff)
387 		queue_work(system_freezable_wq, work);
388 }
389 
390 static int init_vqs(struct virtio_balloon *vb)
391 {
392 	struct virtqueue *vqs[3];
393 	vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request };
394 	static const char * const names[] = { "inflate", "deflate", "stats" };
395 	int err, nvqs;
396 
397 	/*
398 	 * We expect two virtqueues: inflate and deflate, and
399 	 * optionally stat.
400 	 */
401 	nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
402 	err = virtio_find_vqs(vb->vdev, nvqs, vqs, callbacks, names, NULL);
403 	if (err)
404 		return err;
405 
406 	vb->inflate_vq = vqs[0];
407 	vb->deflate_vq = vqs[1];
408 	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
409 		struct scatterlist sg;
410 		unsigned int num_stats;
411 		vb->stats_vq = vqs[2];
412 
413 		/*
414 		 * Prime this virtqueue with one buffer so the hypervisor can
415 		 * use it to signal us later (it can't be broken yet!).
416 		 */
417 		num_stats = update_balloon_stats(vb);
418 
419 		sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
420 		err = virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb,
421 					   GFP_KERNEL);
422 		if (err) {
423 			dev_warn(&vb->vdev->dev, "%s: add stat_vq failed\n",
424 				 __func__);
425 			return err;
426 		}
427 		virtqueue_kick(vb->stats_vq);
428 	}
429 	return 0;
430 }
431 
432 #ifdef CONFIG_BALLOON_COMPACTION
433 /*
434  * virtballoon_migratepage - perform the balloon page migration on behalf of
435  *			     a compation thread.     (called under page lock)
436  * @vb_dev_info: the balloon device
437  * @newpage: page that will replace the isolated page after migration finishes.
438  * @page   : the isolated (old) page that is about to be migrated to newpage.
439  * @mode   : compaction mode -- not used for balloon page migration.
440  *
441  * After a ballooned page gets isolated by compaction procedures, this is the
442  * function that performs the page migration on behalf of a compaction thread
443  * The page migration for virtio balloon is done in a simple swap fashion which
444  * follows these two macro steps:
445  *  1) insert newpage into vb->pages list and update the host about it;
446  *  2) update the host about the old page removed from vb->pages list;
447  *
448  * This function preforms the balloon page migration task.
449  * Called through balloon_mapping->a_ops->migratepage
450  */
451 static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info,
452 		struct page *newpage, struct page *page, enum migrate_mode mode)
453 {
454 	struct virtio_balloon *vb = container_of(vb_dev_info,
455 			struct virtio_balloon, vb_dev_info);
456 	unsigned long flags;
457 
458 	/*
459 	 * In order to avoid lock contention while migrating pages concurrently
460 	 * to leak_balloon() or fill_balloon() we just give up the balloon_lock
461 	 * this turn, as it is easier to retry the page migration later.
462 	 * This also prevents fill_balloon() getting stuck into a mutex
463 	 * recursion in the case it ends up triggering memory compaction
464 	 * while it is attempting to inflate the ballon.
465 	 */
466 	if (!mutex_trylock(&vb->balloon_lock))
467 		return -EAGAIN;
468 
469 	get_page(newpage); /* balloon reference */
470 
471 	/* balloon's page migration 1st step  -- inflate "newpage" */
472 	spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
473 	balloon_page_insert(vb_dev_info, newpage);
474 	vb_dev_info->isolated_pages--;
475 	__count_vm_event(BALLOON_MIGRATE);
476 	spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
477 	vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
478 	set_page_pfns(vb, vb->pfns, newpage);
479 	tell_host(vb, vb->inflate_vq);
480 
481 	/* balloon's page migration 2nd step -- deflate "page" */
482 	spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
483 	balloon_page_delete(page);
484 	spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
485 	vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
486 	set_page_pfns(vb, vb->pfns, page);
487 	tell_host(vb, vb->deflate_vq);
488 
489 	mutex_unlock(&vb->balloon_lock);
490 
491 	put_page(page); /* balloon reference */
492 
493 	return MIGRATEPAGE_SUCCESS;
494 }
495 
496 static struct dentry *balloon_mount(struct file_system_type *fs_type,
497 		int flags, const char *dev_name, void *data)
498 {
499 	static const struct dentry_operations ops = {
500 		.d_dname = simple_dname,
501 	};
502 
503 	return mount_pseudo(fs_type, "balloon-kvm:", NULL, &ops,
504 				BALLOON_KVM_MAGIC);
505 }
506 
507 static struct file_system_type balloon_fs = {
508 	.name           = "balloon-kvm",
509 	.mount          = balloon_mount,
510 	.kill_sb        = kill_anon_super,
511 };
512 
513 #endif /* CONFIG_BALLOON_COMPACTION */
514 
515 static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,
516 						  struct shrink_control *sc)
517 {
518 	unsigned long pages_to_free, pages_freed = 0;
519 	struct virtio_balloon *vb = container_of(shrinker,
520 					struct virtio_balloon, shrinker);
521 
522 	pages_to_free = sc->nr_to_scan * VIRTIO_BALLOON_PAGES_PER_PAGE;
523 
524 	/*
525 	 * One invocation of leak_balloon can deflate at most
526 	 * VIRTIO_BALLOON_ARRAY_PFNS_MAX balloon pages, so we call it
527 	 * multiple times to deflate pages till reaching pages_to_free.
528 	 */
529 	while (vb->num_pages && pages_to_free) {
530 		pages_to_free -= pages_freed;
531 		pages_freed += leak_balloon(vb, pages_to_free);
532 	}
533 	update_balloon_size(vb);
534 
535 	return pages_freed / VIRTIO_BALLOON_PAGES_PER_PAGE;
536 }
537 
538 static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker,
539 						   struct shrink_control *sc)
540 {
541 	struct virtio_balloon *vb = container_of(shrinker,
542 					struct virtio_balloon, shrinker);
543 
544 	return vb->num_pages / VIRTIO_BALLOON_PAGES_PER_PAGE;
545 }
546 
547 static void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb)
548 {
549 	unregister_shrinker(&vb->shrinker);
550 }
551 
552 static int virtio_balloon_register_shrinker(struct virtio_balloon *vb)
553 {
554 	vb->shrinker.scan_objects = virtio_balloon_shrinker_scan;
555 	vb->shrinker.count_objects = virtio_balloon_shrinker_count;
556 	vb->shrinker.seeks = DEFAULT_SEEKS;
557 
558 	return register_shrinker(&vb->shrinker);
559 }
560 
561 static int virtballoon_probe(struct virtio_device *vdev)
562 {
563 	struct virtio_balloon *vb;
564 	int err;
565 
566 	if (!vdev->config->get) {
567 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
568 			__func__);
569 		return -EINVAL;
570 	}
571 
572 	vdev->priv = vb = kzalloc(sizeof(*vb), GFP_KERNEL);
573 	if (!vb) {
574 		err = -ENOMEM;
575 		goto out;
576 	}
577 
578 	INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
579 	INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
580 	spin_lock_init(&vb->stop_update_lock);
581 	mutex_init(&vb->balloon_lock);
582 	init_waitqueue_head(&vb->acked);
583 	vb->vdev = vdev;
584 
585 	balloon_devinfo_init(&vb->vb_dev_info);
586 
587 	err = init_vqs(vb);
588 	if (err)
589 		goto out_free_vb;
590 
591 #ifdef CONFIG_BALLOON_COMPACTION
592 	balloon_mnt = kern_mount(&balloon_fs);
593 	if (IS_ERR(balloon_mnt)) {
594 		err = PTR_ERR(balloon_mnt);
595 		goto out_del_vqs;
596 	}
597 
598 	vb->vb_dev_info.migratepage = virtballoon_migratepage;
599 	vb->vb_dev_info.inode = alloc_anon_inode(balloon_mnt->mnt_sb);
600 	if (IS_ERR(vb->vb_dev_info.inode)) {
601 		err = PTR_ERR(vb->vb_dev_info.inode);
602 		kern_unmount(balloon_mnt);
603 		goto out_del_vqs;
604 	}
605 	vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops;
606 #endif
607 	/*
608 	 * We continue to use VIRTIO_BALLOON_F_DEFLATE_ON_OOM to decide if a
609 	 * shrinker needs to be registered to relieve memory pressure.
610 	 */
611 	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) {
612 		err = virtio_balloon_register_shrinker(vb);
613 		if (err)
614 			goto out_del_vqs;
615 	}
616 	virtio_device_ready(vdev);
617 
618 	if (towards_target(vb))
619 		virtballoon_changed(vdev);
620 	return 0;
621 
622 out_del_vqs:
623 	vdev->config->del_vqs(vdev);
624 out_free_vb:
625 	kfree(vb);
626 out:
627 	return err;
628 }
629 
630 static void remove_common(struct virtio_balloon *vb)
631 {
632 	/* There might be pages left in the balloon: free them. */
633 	while (vb->num_pages)
634 		leak_balloon(vb, vb->num_pages);
635 	update_balloon_size(vb);
636 
637 	/* Now we reset the device so we can clean up the queues. */
638 	vb->vdev->config->reset(vb->vdev);
639 
640 	vb->vdev->config->del_vqs(vb->vdev);
641 }
642 
643 static void virtballoon_remove(struct virtio_device *vdev)
644 {
645 	struct virtio_balloon *vb = vdev->priv;
646 
647 	if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
648 		virtio_balloon_unregister_shrinker(vb);
649 	spin_lock_irq(&vb->stop_update_lock);
650 	vb->stop_update = true;
651 	spin_unlock_irq(&vb->stop_update_lock);
652 	cancel_work_sync(&vb->update_balloon_size_work);
653 	cancel_work_sync(&vb->update_balloon_stats_work);
654 
655 	remove_common(vb);
656 #ifdef CONFIG_BALLOON_COMPACTION
657 	if (vb->vb_dev_info.inode)
658 		iput(vb->vb_dev_info.inode);
659 
660 	kern_unmount(balloon_mnt);
661 #endif
662 	kfree(vb);
663 }
664 
665 #ifdef CONFIG_PM_SLEEP
666 static int virtballoon_freeze(struct virtio_device *vdev)
667 {
668 	struct virtio_balloon *vb = vdev->priv;
669 
670 	/*
671 	 * The workqueue is already frozen by the PM core before this
672 	 * function is called.
673 	 */
674 	remove_common(vb);
675 	return 0;
676 }
677 
678 static int virtballoon_restore(struct virtio_device *vdev)
679 {
680 	struct virtio_balloon *vb = vdev->priv;
681 	int ret;
682 
683 	ret = init_vqs(vdev->priv);
684 	if (ret)
685 		return ret;
686 
687 	virtio_device_ready(vdev);
688 
689 	if (towards_target(vb))
690 		virtballoon_changed(vdev);
691 	update_balloon_size(vb);
692 	return 0;
693 }
694 #endif
695 
696 static int virtballoon_validate(struct virtio_device *vdev)
697 {
698 	__virtio_clear_bit(vdev, VIRTIO_F_IOMMU_PLATFORM);
699 	return 0;
700 }
701 
702 static unsigned int features[] = {
703 	VIRTIO_BALLOON_F_MUST_TELL_HOST,
704 	VIRTIO_BALLOON_F_STATS_VQ,
705 	VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
706 };
707 
708 static struct virtio_driver virtio_balloon_driver = {
709 	.feature_table = features,
710 	.feature_table_size = ARRAY_SIZE(features),
711 	.driver.name =	KBUILD_MODNAME,
712 	.driver.owner =	THIS_MODULE,
713 	.id_table =	id_table,
714 	.validate =	virtballoon_validate,
715 	.probe =	virtballoon_probe,
716 	.remove =	virtballoon_remove,
717 	.config_changed = virtballoon_changed,
718 #ifdef CONFIG_PM_SLEEP
719 	.freeze	=	virtballoon_freeze,
720 	.restore =	virtballoon_restore,
721 #endif
722 };
723 
724 module_virtio_driver(virtio_balloon_driver);
725 MODULE_DEVICE_TABLE(virtio, id_table);
726 MODULE_DESCRIPTION("Virtio balloon driver");
727 MODULE_LICENSE("GPL");
728