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