xref: /linux/drivers/virtio/virtio_mem.c (revision f43d3870cafa2a0f3854c1819c8385733db8f9ae)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Virtio-mem device driver.
4  *
5  * Copyright Red Hat, Inc. 2020
6  *
7  * Author(s): David Hildenbrand <david@redhat.com>
8  */
9 
10 #include <linux/virtio.h>
11 #include <linux/virtio_mem.h>
12 #include <linux/workqueue.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/mm.h>
16 #include <linux/memory_hotplug.h>
17 #include <linux/memory.h>
18 #include <linux/hrtimer.h>
19 #include <linux/crash_dump.h>
20 #include <linux/mutex.h>
21 #include <linux/bitmap.h>
22 #include <linux/lockdep.h>
23 
24 #include <acpi/acpi_numa.h>
25 
26 static bool unplug_online = true;
27 module_param(unplug_online, bool, 0644);
28 MODULE_PARM_DESC(unplug_online, "Try to unplug online memory");
29 
30 enum virtio_mem_mb_state {
31 	/* Unplugged, not added to Linux. Can be reused later. */
32 	VIRTIO_MEM_MB_STATE_UNUSED = 0,
33 	/* (Partially) plugged, not added to Linux. Error on add_memory(). */
34 	VIRTIO_MEM_MB_STATE_PLUGGED,
35 	/* Fully plugged, fully added to Linux, offline. */
36 	VIRTIO_MEM_MB_STATE_OFFLINE,
37 	/* Partially plugged, fully added to Linux, offline. */
38 	VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL,
39 	/* Fully plugged, fully added to Linux, online. */
40 	VIRTIO_MEM_MB_STATE_ONLINE,
41 	/* Partially plugged, fully added to Linux, online. */
42 	VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL,
43 	VIRTIO_MEM_MB_STATE_COUNT
44 };
45 
46 struct virtio_mem {
47 	struct virtio_device *vdev;
48 
49 	/* We might first have to unplug all memory when starting up. */
50 	bool unplug_all_required;
51 
52 	/* Workqueue that processes the plug/unplug requests. */
53 	struct work_struct wq;
54 	atomic_t config_changed;
55 
56 	/* Virtqueue for guest->host requests. */
57 	struct virtqueue *vq;
58 
59 	/* Wait for a host response to a guest request. */
60 	wait_queue_head_t host_resp;
61 
62 	/* Space for one guest request and the host response. */
63 	struct virtio_mem_req req;
64 	struct virtio_mem_resp resp;
65 
66 	/* The current size of the device. */
67 	uint64_t plugged_size;
68 	/* The requested size of the device. */
69 	uint64_t requested_size;
70 
71 	/* The device block size (for communicating with the device). */
72 	uint64_t device_block_size;
73 	/* The translated node id. NUMA_NO_NODE in case not specified. */
74 	int nid;
75 	/* Physical start address of the memory region. */
76 	uint64_t addr;
77 	/* Maximum region size in bytes. */
78 	uint64_t region_size;
79 
80 	/* The subblock size. */
81 	uint64_t subblock_size;
82 	/* The number of subblocks per memory block. */
83 	uint32_t nb_sb_per_mb;
84 
85 	/* Id of the first memory block of this device. */
86 	unsigned long first_mb_id;
87 	/* Id of the last memory block of this device. */
88 	unsigned long last_mb_id;
89 	/* Id of the last usable memory block of this device. */
90 	unsigned long last_usable_mb_id;
91 	/* Id of the next memory bock to prepare when needed. */
92 	unsigned long next_mb_id;
93 
94 	/* The parent resource for all memory added via this device. */
95 	struct resource *parent_resource;
96 	/*
97 	 * Copy of "System RAM (virtio_mem)" to be used for
98 	 * add_memory_driver_managed().
99 	 */
100 	const char *resource_name;
101 
102 	/* Summary of all memory block states. */
103 	unsigned long nb_mb_state[VIRTIO_MEM_MB_STATE_COUNT];
104 #define VIRTIO_MEM_NB_OFFLINE_THRESHOLD		10
105 
106 	/*
107 	 * One byte state per memory block.
108 	 *
109 	 * Allocated via vmalloc(). When preparing new blocks, resized
110 	 * (alloc+copy+free) when needed (crossing pages with the next mb).
111 	 * (when crossing pages).
112 	 *
113 	 * With 128MB memory blocks, we have states for 512GB of memory in one
114 	 * page.
115 	 */
116 	uint8_t *mb_state;
117 
118 	/*
119 	 * $nb_sb_per_mb bit per memory block. Handled similar to mb_state.
120 	 *
121 	 * With 4MB subblocks, we manage 128GB of memory in one page.
122 	 */
123 	unsigned long *sb_bitmap;
124 
125 	/*
126 	 * Mutex that protects the nb_mb_state, mb_state, and sb_bitmap.
127 	 *
128 	 * When this lock is held the pointers can't change, ONLINE and
129 	 * OFFLINE blocks can't change the state and no subblocks will get
130 	 * plugged/unplugged.
131 	 */
132 	struct mutex hotplug_mutex;
133 	bool hotplug_active;
134 
135 	/* An error occurred we cannot handle - stop processing requests. */
136 	bool broken;
137 
138 	/* The driver is being removed. */
139 	spinlock_t removal_lock;
140 	bool removing;
141 
142 	/* Timer for retrying to plug/unplug memory. */
143 	struct hrtimer retry_timer;
144 	unsigned int retry_timer_ms;
145 #define VIRTIO_MEM_RETRY_TIMER_MIN_MS		50000
146 #define VIRTIO_MEM_RETRY_TIMER_MAX_MS		300000
147 
148 	/* Memory notifier (online/offline events). */
149 	struct notifier_block memory_notifier;
150 
151 	/* Next device in the list of virtio-mem devices. */
152 	struct list_head next;
153 };
154 
155 /*
156  * We have to share a single online_page callback among all virtio-mem
157  * devices. We use RCU to iterate the list in the callback.
158  */
159 static DEFINE_MUTEX(virtio_mem_mutex);
160 static LIST_HEAD(virtio_mem_devices);
161 
162 static void virtio_mem_online_page_cb(struct page *page, unsigned int order);
163 
164 /*
165  * Register a virtio-mem device so it will be considered for the online_page
166  * callback.
167  */
168 static int register_virtio_mem_device(struct virtio_mem *vm)
169 {
170 	int rc = 0;
171 
172 	/* First device registers the callback. */
173 	mutex_lock(&virtio_mem_mutex);
174 	if (list_empty(&virtio_mem_devices))
175 		rc = set_online_page_callback(&virtio_mem_online_page_cb);
176 	if (!rc)
177 		list_add_rcu(&vm->next, &virtio_mem_devices);
178 	mutex_unlock(&virtio_mem_mutex);
179 
180 	return rc;
181 }
182 
183 /*
184  * Unregister a virtio-mem device so it will no longer be considered for the
185  * online_page callback.
186  */
187 static void unregister_virtio_mem_device(struct virtio_mem *vm)
188 {
189 	/* Last device unregisters the callback. */
190 	mutex_lock(&virtio_mem_mutex);
191 	list_del_rcu(&vm->next);
192 	if (list_empty(&virtio_mem_devices))
193 		restore_online_page_callback(&virtio_mem_online_page_cb);
194 	mutex_unlock(&virtio_mem_mutex);
195 
196 	synchronize_rcu();
197 }
198 
199 /*
200  * Calculate the memory block id of a given address.
201  */
202 static unsigned long virtio_mem_phys_to_mb_id(unsigned long addr)
203 {
204 	return addr / memory_block_size_bytes();
205 }
206 
207 /*
208  * Calculate the physical start address of a given memory block id.
209  */
210 static unsigned long virtio_mem_mb_id_to_phys(unsigned long mb_id)
211 {
212 	return mb_id * memory_block_size_bytes();
213 }
214 
215 /*
216  * Calculate the subblock id of a given address.
217  */
218 static unsigned long virtio_mem_phys_to_sb_id(struct virtio_mem *vm,
219 					      unsigned long addr)
220 {
221 	const unsigned long mb_id = virtio_mem_phys_to_mb_id(addr);
222 	const unsigned long mb_addr = virtio_mem_mb_id_to_phys(mb_id);
223 
224 	return (addr - mb_addr) / vm->subblock_size;
225 }
226 
227 /*
228  * Set the state of a memory block, taking care of the state counter.
229  */
230 static void virtio_mem_mb_set_state(struct virtio_mem *vm, unsigned long mb_id,
231 				    enum virtio_mem_mb_state state)
232 {
233 	const unsigned long idx = mb_id - vm->first_mb_id;
234 	enum virtio_mem_mb_state old_state;
235 
236 	old_state = vm->mb_state[idx];
237 	vm->mb_state[idx] = state;
238 
239 	BUG_ON(vm->nb_mb_state[old_state] == 0);
240 	vm->nb_mb_state[old_state]--;
241 	vm->nb_mb_state[state]++;
242 }
243 
244 /*
245  * Get the state of a memory block.
246  */
247 static enum virtio_mem_mb_state virtio_mem_mb_get_state(struct virtio_mem *vm,
248 							unsigned long mb_id)
249 {
250 	const unsigned long idx = mb_id - vm->first_mb_id;
251 
252 	return vm->mb_state[idx];
253 }
254 
255 /*
256  * Prepare the state array for the next memory block.
257  */
258 static int virtio_mem_mb_state_prepare_next_mb(struct virtio_mem *vm)
259 {
260 	unsigned long old_bytes = vm->next_mb_id - vm->first_mb_id + 1;
261 	unsigned long new_bytes = vm->next_mb_id - vm->first_mb_id + 2;
262 	int old_pages = PFN_UP(old_bytes);
263 	int new_pages = PFN_UP(new_bytes);
264 	uint8_t *new_mb_state;
265 
266 	if (vm->mb_state && old_pages == new_pages)
267 		return 0;
268 
269 	new_mb_state = vzalloc(new_pages * PAGE_SIZE);
270 	if (!new_mb_state)
271 		return -ENOMEM;
272 
273 	mutex_lock(&vm->hotplug_mutex);
274 	if (vm->mb_state)
275 		memcpy(new_mb_state, vm->mb_state, old_pages * PAGE_SIZE);
276 	vfree(vm->mb_state);
277 	vm->mb_state = new_mb_state;
278 	mutex_unlock(&vm->hotplug_mutex);
279 
280 	return 0;
281 }
282 
283 #define virtio_mem_for_each_mb_state(_vm, _mb_id, _state) \
284 	for (_mb_id = _vm->first_mb_id; \
285 	     _mb_id < _vm->next_mb_id && _vm->nb_mb_state[_state]; \
286 	     _mb_id++) \
287 		if (virtio_mem_mb_get_state(_vm, _mb_id) == _state)
288 
289 #define virtio_mem_for_each_mb_state_rev(_vm, _mb_id, _state) \
290 	for (_mb_id = _vm->next_mb_id - 1; \
291 	     _mb_id >= _vm->first_mb_id && _vm->nb_mb_state[_state]; \
292 	     _mb_id--) \
293 		if (virtio_mem_mb_get_state(_vm, _mb_id) == _state)
294 
295 /*
296  * Mark all selected subblocks plugged.
297  *
298  * Will not modify the state of the memory block.
299  */
300 static void virtio_mem_mb_set_sb_plugged(struct virtio_mem *vm,
301 					 unsigned long mb_id, int sb_id,
302 					 int count)
303 {
304 	const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id;
305 
306 	__bitmap_set(vm->sb_bitmap, bit, count);
307 }
308 
309 /*
310  * Mark all selected subblocks unplugged.
311  *
312  * Will not modify the state of the memory block.
313  */
314 static void virtio_mem_mb_set_sb_unplugged(struct virtio_mem *vm,
315 					   unsigned long mb_id, int sb_id,
316 					   int count)
317 {
318 	const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id;
319 
320 	__bitmap_clear(vm->sb_bitmap, bit, count);
321 }
322 
323 /*
324  * Test if all selected subblocks are plugged.
325  */
326 static bool virtio_mem_mb_test_sb_plugged(struct virtio_mem *vm,
327 					  unsigned long mb_id, int sb_id,
328 					  int count)
329 {
330 	const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id;
331 
332 	if (count == 1)
333 		return test_bit(bit, vm->sb_bitmap);
334 
335 	/* TODO: Helper similar to bitmap_set() */
336 	return find_next_zero_bit(vm->sb_bitmap, bit + count, bit) >=
337 	       bit + count;
338 }
339 
340 /*
341  * Test if all selected subblocks are unplugged.
342  */
343 static bool virtio_mem_mb_test_sb_unplugged(struct virtio_mem *vm,
344 					    unsigned long mb_id, int sb_id,
345 					    int count)
346 {
347 	const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id;
348 
349 	/* TODO: Helper similar to bitmap_set() */
350 	return find_next_bit(vm->sb_bitmap, bit + count, bit) >= bit + count;
351 }
352 
353 /*
354  * Find the first unplugged subblock. Returns vm->nb_sb_per_mb in case there is
355  * none.
356  */
357 static int virtio_mem_mb_first_unplugged_sb(struct virtio_mem *vm,
358 					    unsigned long mb_id)
359 {
360 	const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb;
361 
362 	return find_next_zero_bit(vm->sb_bitmap, bit + vm->nb_sb_per_mb, bit) -
363 	       bit;
364 }
365 
366 /*
367  * Prepare the subblock bitmap for the next memory block.
368  */
369 static int virtio_mem_sb_bitmap_prepare_next_mb(struct virtio_mem *vm)
370 {
371 	const unsigned long old_nb_mb = vm->next_mb_id - vm->first_mb_id;
372 	const unsigned long old_nb_bits = old_nb_mb * vm->nb_sb_per_mb;
373 	const unsigned long new_nb_bits = (old_nb_mb + 1) * vm->nb_sb_per_mb;
374 	int old_pages = PFN_UP(BITS_TO_LONGS(old_nb_bits) * sizeof(long));
375 	int new_pages = PFN_UP(BITS_TO_LONGS(new_nb_bits) * sizeof(long));
376 	unsigned long *new_sb_bitmap, *old_sb_bitmap;
377 
378 	if (vm->sb_bitmap && old_pages == new_pages)
379 		return 0;
380 
381 	new_sb_bitmap = vzalloc(new_pages * PAGE_SIZE);
382 	if (!new_sb_bitmap)
383 		return -ENOMEM;
384 
385 	mutex_lock(&vm->hotplug_mutex);
386 	if (new_sb_bitmap)
387 		memcpy(new_sb_bitmap, vm->sb_bitmap, old_pages * PAGE_SIZE);
388 
389 	old_sb_bitmap = vm->sb_bitmap;
390 	vm->sb_bitmap = new_sb_bitmap;
391 	mutex_unlock(&vm->hotplug_mutex);
392 
393 	vfree(old_sb_bitmap);
394 	return 0;
395 }
396 
397 /*
398  * Try to add a memory block to Linux. This will usually only fail
399  * if out of memory.
400  *
401  * Must not be called with the vm->hotplug_mutex held (possible deadlock with
402  * onlining code).
403  *
404  * Will not modify the state of the memory block.
405  */
406 static int virtio_mem_mb_add(struct virtio_mem *vm, unsigned long mb_id)
407 {
408 	const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
409 	int nid = vm->nid;
410 
411 	if (nid == NUMA_NO_NODE)
412 		nid = memory_add_physaddr_to_nid(addr);
413 
414 	/*
415 	 * When force-unloading the driver and we still have memory added to
416 	 * Linux, the resource name has to stay.
417 	 */
418 	if (!vm->resource_name) {
419 		vm->resource_name = kstrdup_const("System RAM (virtio_mem)",
420 						  GFP_KERNEL);
421 		if (!vm->resource_name)
422 			return -ENOMEM;
423 	}
424 
425 	dev_dbg(&vm->vdev->dev, "adding memory block: %lu\n", mb_id);
426 	return add_memory_driver_managed(nid, addr, memory_block_size_bytes(),
427 					 vm->resource_name);
428 }
429 
430 /*
431  * Try to remove a memory block from Linux. Will only fail if the memory block
432  * is not offline.
433  *
434  * Must not be called with the vm->hotplug_mutex held (possible deadlock with
435  * onlining code).
436  *
437  * Will not modify the state of the memory block.
438  */
439 static int virtio_mem_mb_remove(struct virtio_mem *vm, unsigned long mb_id)
440 {
441 	const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
442 	int nid = vm->nid;
443 
444 	if (nid == NUMA_NO_NODE)
445 		nid = memory_add_physaddr_to_nid(addr);
446 
447 	dev_dbg(&vm->vdev->dev, "removing memory block: %lu\n", mb_id);
448 	return remove_memory(nid, addr, memory_block_size_bytes());
449 }
450 
451 /*
452  * Try to offline and remove a memory block from Linux.
453  *
454  * Must not be called with the vm->hotplug_mutex held (possible deadlock with
455  * onlining code).
456  *
457  * Will not modify the state of the memory block.
458  */
459 static int virtio_mem_mb_offline_and_remove(struct virtio_mem *vm,
460 					    unsigned long mb_id)
461 {
462 	const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
463 	int nid = vm->nid;
464 
465 	if (nid == NUMA_NO_NODE)
466 		nid = memory_add_physaddr_to_nid(addr);
467 
468 	dev_dbg(&vm->vdev->dev, "offlining and removing memory block: %lu\n",
469 		mb_id);
470 	return offline_and_remove_memory(nid, addr, memory_block_size_bytes());
471 }
472 
473 /*
474  * Trigger the workqueue so the device can perform its magic.
475  */
476 static void virtio_mem_retry(struct virtio_mem *vm)
477 {
478 	unsigned long flags;
479 
480 	spin_lock_irqsave(&vm->removal_lock, flags);
481 	if (!vm->removing)
482 		queue_work(system_freezable_wq, &vm->wq);
483 	spin_unlock_irqrestore(&vm->removal_lock, flags);
484 }
485 
486 static int virtio_mem_translate_node_id(struct virtio_mem *vm, uint16_t node_id)
487 {
488 	int node = NUMA_NO_NODE;
489 
490 #if defined(CONFIG_ACPI_NUMA)
491 	if (virtio_has_feature(vm->vdev, VIRTIO_MEM_F_ACPI_PXM))
492 		node = pxm_to_node(node_id);
493 #endif
494 	return node;
495 }
496 
497 /*
498  * Test if a virtio-mem device overlaps with the given range. Can be called
499  * from (notifier) callbacks lockless.
500  */
501 static bool virtio_mem_overlaps_range(struct virtio_mem *vm,
502 				      unsigned long start, unsigned long size)
503 {
504 	unsigned long dev_start = virtio_mem_mb_id_to_phys(vm->first_mb_id);
505 	unsigned long dev_end = virtio_mem_mb_id_to_phys(vm->last_mb_id) +
506 				memory_block_size_bytes();
507 
508 	return start < dev_end && dev_start < start + size;
509 }
510 
511 /*
512  * Test if a virtio-mem device owns a memory block. Can be called from
513  * (notifier) callbacks lockless.
514  */
515 static bool virtio_mem_owned_mb(struct virtio_mem *vm, unsigned long mb_id)
516 {
517 	return mb_id >= vm->first_mb_id && mb_id <= vm->last_mb_id;
518 }
519 
520 static int virtio_mem_notify_going_online(struct virtio_mem *vm,
521 					  unsigned long mb_id)
522 {
523 	switch (virtio_mem_mb_get_state(vm, mb_id)) {
524 	case VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL:
525 	case VIRTIO_MEM_MB_STATE_OFFLINE:
526 		return NOTIFY_OK;
527 	default:
528 		break;
529 	}
530 	dev_warn_ratelimited(&vm->vdev->dev,
531 			     "memory block onlining denied\n");
532 	return NOTIFY_BAD;
533 }
534 
535 static void virtio_mem_notify_offline(struct virtio_mem *vm,
536 				      unsigned long mb_id)
537 {
538 	switch (virtio_mem_mb_get_state(vm, mb_id)) {
539 	case VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL:
540 		virtio_mem_mb_set_state(vm, mb_id,
541 					VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL);
542 		break;
543 	case VIRTIO_MEM_MB_STATE_ONLINE:
544 		virtio_mem_mb_set_state(vm, mb_id,
545 					VIRTIO_MEM_MB_STATE_OFFLINE);
546 		break;
547 	default:
548 		BUG();
549 		break;
550 	}
551 
552 	/*
553 	 * Trigger the workqueue, maybe we can now unplug memory. Also,
554 	 * when we offline and remove a memory block, this will re-trigger
555 	 * us immediately - which is often nice because the removal of
556 	 * the memory block (e.g., memmap) might have freed up memory
557 	 * on other memory blocks we manage.
558 	 */
559 	virtio_mem_retry(vm);
560 }
561 
562 static void virtio_mem_notify_online(struct virtio_mem *vm, unsigned long mb_id)
563 {
564 	unsigned long nb_offline;
565 
566 	switch (virtio_mem_mb_get_state(vm, mb_id)) {
567 	case VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL:
568 		virtio_mem_mb_set_state(vm, mb_id,
569 					VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL);
570 		break;
571 	case VIRTIO_MEM_MB_STATE_OFFLINE:
572 		virtio_mem_mb_set_state(vm, mb_id, VIRTIO_MEM_MB_STATE_ONLINE);
573 		break;
574 	default:
575 		BUG();
576 		break;
577 	}
578 	nb_offline = vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE] +
579 		     vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL];
580 
581 	/* see if we can add new blocks now that we onlined one block */
582 	if (nb_offline == VIRTIO_MEM_NB_OFFLINE_THRESHOLD - 1)
583 		virtio_mem_retry(vm);
584 }
585 
586 static void virtio_mem_notify_going_offline(struct virtio_mem *vm,
587 					    unsigned long mb_id)
588 {
589 	const unsigned long nr_pages = PFN_DOWN(vm->subblock_size);
590 	struct page *page;
591 	unsigned long pfn;
592 	int sb_id, i;
593 
594 	for (sb_id = 0; sb_id < vm->nb_sb_per_mb; sb_id++) {
595 		if (virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
596 			continue;
597 		/*
598 		 * Drop our reference to the pages so the memory can get
599 		 * offlined and add the unplugged pages to the managed
600 		 * page counters (so offlining code can correctly subtract
601 		 * them again).
602 		 */
603 		pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
604 			       sb_id * vm->subblock_size);
605 		adjust_managed_page_count(pfn_to_page(pfn), nr_pages);
606 		for (i = 0; i < nr_pages; i++) {
607 			page = pfn_to_page(pfn + i);
608 			if (WARN_ON(!page_ref_dec_and_test(page)))
609 				dump_page(page, "unplugged page referenced");
610 		}
611 	}
612 }
613 
614 static void virtio_mem_notify_cancel_offline(struct virtio_mem *vm,
615 					     unsigned long mb_id)
616 {
617 	const unsigned long nr_pages = PFN_DOWN(vm->subblock_size);
618 	unsigned long pfn;
619 	int sb_id, i;
620 
621 	for (sb_id = 0; sb_id < vm->nb_sb_per_mb; sb_id++) {
622 		if (virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
623 			continue;
624 		/*
625 		 * Get the reference we dropped when going offline and
626 		 * subtract the unplugged pages from the managed page
627 		 * counters.
628 		 */
629 		pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
630 			       sb_id * vm->subblock_size);
631 		adjust_managed_page_count(pfn_to_page(pfn), -nr_pages);
632 		for (i = 0; i < nr_pages; i++)
633 			page_ref_inc(pfn_to_page(pfn + i));
634 	}
635 }
636 
637 /*
638  * This callback will either be called synchronously from add_memory() or
639  * asynchronously (e.g., triggered via user space). We have to be careful
640  * with locking when calling add_memory().
641  */
642 static int virtio_mem_memory_notifier_cb(struct notifier_block *nb,
643 					 unsigned long action, void *arg)
644 {
645 	struct virtio_mem *vm = container_of(nb, struct virtio_mem,
646 					     memory_notifier);
647 	struct memory_notify *mhp = arg;
648 	const unsigned long start = PFN_PHYS(mhp->start_pfn);
649 	const unsigned long size = PFN_PHYS(mhp->nr_pages);
650 	const unsigned long mb_id = virtio_mem_phys_to_mb_id(start);
651 	int rc = NOTIFY_OK;
652 
653 	if (!virtio_mem_overlaps_range(vm, start, size))
654 		return NOTIFY_DONE;
655 
656 	/*
657 	 * Memory is onlined/offlined in memory block granularity. We cannot
658 	 * cross virtio-mem device boundaries and memory block boundaries. Bail
659 	 * out if this ever changes.
660 	 */
661 	if (WARN_ON_ONCE(size != memory_block_size_bytes() ||
662 			 !IS_ALIGNED(start, memory_block_size_bytes())))
663 		return NOTIFY_BAD;
664 
665 	/*
666 	 * Avoid circular locking lockdep warnings. We lock the mutex
667 	 * e.g., in MEM_GOING_ONLINE and unlock it in MEM_ONLINE. The
668 	 * blocking_notifier_call_chain() has it's own lock, which gets unlocked
669 	 * between both notifier calls and will bail out. False positive.
670 	 */
671 	lockdep_off();
672 
673 	switch (action) {
674 	case MEM_GOING_OFFLINE:
675 		mutex_lock(&vm->hotplug_mutex);
676 		if (vm->removing) {
677 			rc = notifier_from_errno(-EBUSY);
678 			mutex_unlock(&vm->hotplug_mutex);
679 			break;
680 		}
681 		vm->hotplug_active = true;
682 		virtio_mem_notify_going_offline(vm, mb_id);
683 		break;
684 	case MEM_GOING_ONLINE:
685 		mutex_lock(&vm->hotplug_mutex);
686 		if (vm->removing) {
687 			rc = notifier_from_errno(-EBUSY);
688 			mutex_unlock(&vm->hotplug_mutex);
689 			break;
690 		}
691 		vm->hotplug_active = true;
692 		rc = virtio_mem_notify_going_online(vm, mb_id);
693 		break;
694 	case MEM_OFFLINE:
695 		virtio_mem_notify_offline(vm, mb_id);
696 		vm->hotplug_active = false;
697 		mutex_unlock(&vm->hotplug_mutex);
698 		break;
699 	case MEM_ONLINE:
700 		virtio_mem_notify_online(vm, mb_id);
701 		vm->hotplug_active = false;
702 		mutex_unlock(&vm->hotplug_mutex);
703 		break;
704 	case MEM_CANCEL_OFFLINE:
705 		if (!vm->hotplug_active)
706 			break;
707 		virtio_mem_notify_cancel_offline(vm, mb_id);
708 		vm->hotplug_active = false;
709 		mutex_unlock(&vm->hotplug_mutex);
710 		break;
711 	case MEM_CANCEL_ONLINE:
712 		if (!vm->hotplug_active)
713 			break;
714 		vm->hotplug_active = false;
715 		mutex_unlock(&vm->hotplug_mutex);
716 		break;
717 	default:
718 		break;
719 	}
720 
721 	lockdep_on();
722 
723 	return rc;
724 }
725 
726 /*
727  * Set a range of pages PG_offline. Remember pages that were never onlined
728  * (via generic_online_page()) using PageDirty().
729  */
730 static void virtio_mem_set_fake_offline(unsigned long pfn,
731 					unsigned int nr_pages, bool onlined)
732 {
733 	for (; nr_pages--; pfn++) {
734 		struct page *page = pfn_to_page(pfn);
735 
736 		__SetPageOffline(page);
737 		if (!onlined) {
738 			SetPageDirty(page);
739 			/* FIXME: remove after cleanups */
740 			ClearPageReserved(page);
741 		}
742 	}
743 }
744 
745 /*
746  * Clear PG_offline from a range of pages. If the pages were never onlined,
747  * (via generic_online_page()), clear PageDirty().
748  */
749 static void virtio_mem_clear_fake_offline(unsigned long pfn,
750 					  unsigned int nr_pages, bool onlined)
751 {
752 	for (; nr_pages--; pfn++) {
753 		struct page *page = pfn_to_page(pfn);
754 
755 		__ClearPageOffline(page);
756 		if (!onlined)
757 			ClearPageDirty(page);
758 	}
759 }
760 
761 /*
762  * Release a range of fake-offline pages to the buddy, effectively
763  * fake-onlining them.
764  */
765 static void virtio_mem_fake_online(unsigned long pfn, unsigned int nr_pages)
766 {
767 	const int order = MAX_ORDER - 1;
768 	int i;
769 
770 	/*
771 	 * We are always called with subblock granularity, which is at least
772 	 * aligned to MAX_ORDER - 1.
773 	 */
774 	for (i = 0; i < nr_pages; i += 1 << order) {
775 		struct page *page = pfn_to_page(pfn + i);
776 
777 		/*
778 		 * If the page is PageDirty(), it was kept fake-offline when
779 		 * onlining the memory block. Otherwise, it was allocated
780 		 * using alloc_contig_range(). All pages in a subblock are
781 		 * alike.
782 		 */
783 		if (PageDirty(page)) {
784 			virtio_mem_clear_fake_offline(pfn + i, 1 << order,
785 						      false);
786 			generic_online_page(page, order);
787 		} else {
788 			virtio_mem_clear_fake_offline(pfn + i, 1 << order,
789 						      true);
790 			free_contig_range(pfn + i, 1 << order);
791 			adjust_managed_page_count(page, 1 << order);
792 		}
793 	}
794 }
795 
796 static void virtio_mem_online_page_cb(struct page *page, unsigned int order)
797 {
798 	const unsigned long addr = page_to_phys(page);
799 	const unsigned long mb_id = virtio_mem_phys_to_mb_id(addr);
800 	struct virtio_mem *vm;
801 	int sb_id;
802 
803 	/*
804 	 * We exploit here that subblocks have at least MAX_ORDER - 1
805 	 * size/alignment and that this callback is is called with such a
806 	 * size/alignment. So we cannot cross subblocks and therefore
807 	 * also not memory blocks.
808 	 */
809 	rcu_read_lock();
810 	list_for_each_entry_rcu(vm, &virtio_mem_devices, next) {
811 		if (!virtio_mem_owned_mb(vm, mb_id))
812 			continue;
813 
814 		sb_id = virtio_mem_phys_to_sb_id(vm, addr);
815 		/*
816 		 * If plugged, online the pages, otherwise, set them fake
817 		 * offline (PageOffline).
818 		 */
819 		if (virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
820 			generic_online_page(page, order);
821 		else
822 			virtio_mem_set_fake_offline(PFN_DOWN(addr), 1 << order,
823 						    false);
824 		rcu_read_unlock();
825 		return;
826 	}
827 	rcu_read_unlock();
828 
829 	/* not virtio-mem memory, but e.g., a DIMM. online it */
830 	generic_online_page(page, order);
831 }
832 
833 static uint64_t virtio_mem_send_request(struct virtio_mem *vm,
834 					const struct virtio_mem_req *req)
835 {
836 	struct scatterlist *sgs[2], sg_req, sg_resp;
837 	unsigned int len;
838 	int rc;
839 
840 	/* don't use the request residing on the stack (vaddr) */
841 	vm->req = *req;
842 
843 	/* out: buffer for request */
844 	sg_init_one(&sg_req, &vm->req, sizeof(vm->req));
845 	sgs[0] = &sg_req;
846 
847 	/* in: buffer for response */
848 	sg_init_one(&sg_resp, &vm->resp, sizeof(vm->resp));
849 	sgs[1] = &sg_resp;
850 
851 	rc = virtqueue_add_sgs(vm->vq, sgs, 1, 1, vm, GFP_KERNEL);
852 	if (rc < 0)
853 		return rc;
854 
855 	virtqueue_kick(vm->vq);
856 
857 	/* wait for a response */
858 	wait_event(vm->host_resp, virtqueue_get_buf(vm->vq, &len));
859 
860 	return virtio16_to_cpu(vm->vdev, vm->resp.type);
861 }
862 
863 static int virtio_mem_send_plug_request(struct virtio_mem *vm, uint64_t addr,
864 					uint64_t size)
865 {
866 	const uint64_t nb_vm_blocks = size / vm->device_block_size;
867 	const struct virtio_mem_req req = {
868 		.type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_PLUG),
869 		.u.plug.addr = cpu_to_virtio64(vm->vdev, addr),
870 		.u.plug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
871 	};
872 
873 	if (atomic_read(&vm->config_changed))
874 		return -EAGAIN;
875 
876 	switch (virtio_mem_send_request(vm, &req)) {
877 	case VIRTIO_MEM_RESP_ACK:
878 		vm->plugged_size += size;
879 		return 0;
880 	case VIRTIO_MEM_RESP_NACK:
881 		return -EAGAIN;
882 	case VIRTIO_MEM_RESP_BUSY:
883 		return -ETXTBSY;
884 	case VIRTIO_MEM_RESP_ERROR:
885 		return -EINVAL;
886 	default:
887 		return -ENOMEM;
888 	}
889 }
890 
891 static int virtio_mem_send_unplug_request(struct virtio_mem *vm, uint64_t addr,
892 					  uint64_t size)
893 {
894 	const uint64_t nb_vm_blocks = size / vm->device_block_size;
895 	const struct virtio_mem_req req = {
896 		.type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG),
897 		.u.unplug.addr = cpu_to_virtio64(vm->vdev, addr),
898 		.u.unplug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
899 	};
900 
901 	if (atomic_read(&vm->config_changed))
902 		return -EAGAIN;
903 
904 	switch (virtio_mem_send_request(vm, &req)) {
905 	case VIRTIO_MEM_RESP_ACK:
906 		vm->plugged_size -= size;
907 		return 0;
908 	case VIRTIO_MEM_RESP_BUSY:
909 		return -ETXTBSY;
910 	case VIRTIO_MEM_RESP_ERROR:
911 		return -EINVAL;
912 	default:
913 		return -ENOMEM;
914 	}
915 }
916 
917 static int virtio_mem_send_unplug_all_request(struct virtio_mem *vm)
918 {
919 	const struct virtio_mem_req req = {
920 		.type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG_ALL),
921 	};
922 
923 	switch (virtio_mem_send_request(vm, &req)) {
924 	case VIRTIO_MEM_RESP_ACK:
925 		vm->unplug_all_required = false;
926 		vm->plugged_size = 0;
927 		/* usable region might have shrunk */
928 		atomic_set(&vm->config_changed, 1);
929 		return 0;
930 	case VIRTIO_MEM_RESP_BUSY:
931 		return -ETXTBSY;
932 	default:
933 		return -ENOMEM;
934 	}
935 }
936 
937 /*
938  * Plug selected subblocks. Updates the plugged state, but not the state
939  * of the memory block.
940  */
941 static int virtio_mem_mb_plug_sb(struct virtio_mem *vm, unsigned long mb_id,
942 				 int sb_id, int count)
943 {
944 	const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id) +
945 			      sb_id * vm->subblock_size;
946 	const uint64_t size = count * vm->subblock_size;
947 	int rc;
948 
949 	dev_dbg(&vm->vdev->dev, "plugging memory block: %lu : %i - %i\n", mb_id,
950 		sb_id, sb_id + count - 1);
951 
952 	rc = virtio_mem_send_plug_request(vm, addr, size);
953 	if (!rc)
954 		virtio_mem_mb_set_sb_plugged(vm, mb_id, sb_id, count);
955 	return rc;
956 }
957 
958 /*
959  * Unplug selected subblocks. Updates the plugged state, but not the state
960  * of the memory block.
961  */
962 static int virtio_mem_mb_unplug_sb(struct virtio_mem *vm, unsigned long mb_id,
963 				   int sb_id, int count)
964 {
965 	const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id) +
966 			      sb_id * vm->subblock_size;
967 	const uint64_t size = count * vm->subblock_size;
968 	int rc;
969 
970 	dev_dbg(&vm->vdev->dev, "unplugging memory block: %lu : %i - %i\n",
971 		mb_id, sb_id, sb_id + count - 1);
972 
973 	rc = virtio_mem_send_unplug_request(vm, addr, size);
974 	if (!rc)
975 		virtio_mem_mb_set_sb_unplugged(vm, mb_id, sb_id, count);
976 	return rc;
977 }
978 
979 /*
980  * Unplug the desired number of plugged subblocks of a offline or not-added
981  * memory block. Will fail if any subblock cannot get unplugged (instead of
982  * skipping it).
983  *
984  * Will not modify the state of the memory block.
985  *
986  * Note: can fail after some subblocks were unplugged.
987  */
988 static int virtio_mem_mb_unplug_any_sb(struct virtio_mem *vm,
989 				       unsigned long mb_id, uint64_t *nb_sb)
990 {
991 	int sb_id, count;
992 	int rc;
993 
994 	sb_id = vm->nb_sb_per_mb - 1;
995 	while (*nb_sb) {
996 		/* Find the next candidate subblock */
997 		while (sb_id >= 0 &&
998 		       virtio_mem_mb_test_sb_unplugged(vm, mb_id, sb_id, 1))
999 			sb_id--;
1000 		if (sb_id < 0)
1001 			break;
1002 		/* Try to unplug multiple subblocks at a time */
1003 		count = 1;
1004 		while (count < *nb_sb && sb_id > 0 &&
1005 		       virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id - 1, 1)) {
1006 			count++;
1007 			sb_id--;
1008 		}
1009 
1010 		rc = virtio_mem_mb_unplug_sb(vm, mb_id, sb_id, count);
1011 		if (rc)
1012 			return rc;
1013 		*nb_sb -= count;
1014 		sb_id--;
1015 	}
1016 
1017 	return 0;
1018 }
1019 
1020 /*
1021  * Unplug all plugged subblocks of an offline or not-added memory block.
1022  *
1023  * Will not modify the state of the memory block.
1024  *
1025  * Note: can fail after some subblocks were unplugged.
1026  */
1027 static int virtio_mem_mb_unplug(struct virtio_mem *vm, unsigned long mb_id)
1028 {
1029 	uint64_t nb_sb = vm->nb_sb_per_mb;
1030 
1031 	return virtio_mem_mb_unplug_any_sb(vm, mb_id, &nb_sb);
1032 }
1033 
1034 /*
1035  * Prepare tracking data for the next memory block.
1036  */
1037 static int virtio_mem_prepare_next_mb(struct virtio_mem *vm,
1038 				      unsigned long *mb_id)
1039 {
1040 	int rc;
1041 
1042 	if (vm->next_mb_id > vm->last_usable_mb_id)
1043 		return -ENOSPC;
1044 
1045 	/* Resize the state array if required. */
1046 	rc = virtio_mem_mb_state_prepare_next_mb(vm);
1047 	if (rc)
1048 		return rc;
1049 
1050 	/* Resize the subblock bitmap if required. */
1051 	rc = virtio_mem_sb_bitmap_prepare_next_mb(vm);
1052 	if (rc)
1053 		return rc;
1054 
1055 	vm->nb_mb_state[VIRTIO_MEM_MB_STATE_UNUSED]++;
1056 	*mb_id = vm->next_mb_id++;
1057 	return 0;
1058 }
1059 
1060 /*
1061  * Don't add too many blocks that are not onlined yet to avoid running OOM.
1062  */
1063 static bool virtio_mem_too_many_mb_offline(struct virtio_mem *vm)
1064 {
1065 	unsigned long nb_offline;
1066 
1067 	nb_offline = vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE] +
1068 		     vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL];
1069 	return nb_offline >= VIRTIO_MEM_NB_OFFLINE_THRESHOLD;
1070 }
1071 
1072 /*
1073  * Try to plug the desired number of subblocks and add the memory block
1074  * to Linux.
1075  *
1076  * Will modify the state of the memory block.
1077  */
1078 static int virtio_mem_mb_plug_and_add(struct virtio_mem *vm,
1079 				      unsigned long mb_id,
1080 				      uint64_t *nb_sb)
1081 {
1082 	const int count = min_t(int, *nb_sb, vm->nb_sb_per_mb);
1083 	int rc, rc2;
1084 
1085 	if (WARN_ON_ONCE(!count))
1086 		return -EINVAL;
1087 
1088 	/*
1089 	 * Plug the requested number of subblocks before adding it to linux,
1090 	 * so that onlining will directly online all plugged subblocks.
1091 	 */
1092 	rc = virtio_mem_mb_plug_sb(vm, mb_id, 0, count);
1093 	if (rc)
1094 		return rc;
1095 
1096 	/*
1097 	 * Mark the block properly offline before adding it to Linux,
1098 	 * so the memory notifiers will find the block in the right state.
1099 	 */
1100 	if (count == vm->nb_sb_per_mb)
1101 		virtio_mem_mb_set_state(vm, mb_id,
1102 					VIRTIO_MEM_MB_STATE_OFFLINE);
1103 	else
1104 		virtio_mem_mb_set_state(vm, mb_id,
1105 					VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL);
1106 
1107 	/* Add the memory block to linux - if that fails, try to unplug. */
1108 	rc = virtio_mem_mb_add(vm, mb_id);
1109 	if (rc) {
1110 		enum virtio_mem_mb_state new_state = VIRTIO_MEM_MB_STATE_UNUSED;
1111 
1112 		dev_err(&vm->vdev->dev,
1113 			"adding memory block %lu failed with %d\n", mb_id, rc);
1114 		rc2 = virtio_mem_mb_unplug_sb(vm, mb_id, 0, count);
1115 
1116 		/*
1117 		 * TODO: Linux MM does not properly clean up yet in all cases
1118 		 * where adding of memory failed - especially on -ENOMEM.
1119 		 */
1120 		if (rc2)
1121 			new_state = VIRTIO_MEM_MB_STATE_PLUGGED;
1122 		virtio_mem_mb_set_state(vm, mb_id, new_state);
1123 		return rc;
1124 	}
1125 
1126 	*nb_sb -= count;
1127 	return 0;
1128 }
1129 
1130 /*
1131  * Try to plug the desired number of subblocks of a memory block that
1132  * is already added to Linux.
1133  *
1134  * Will modify the state of the memory block.
1135  *
1136  * Note: Can fail after some subblocks were successfully plugged.
1137  */
1138 static int virtio_mem_mb_plug_any_sb(struct virtio_mem *vm, unsigned long mb_id,
1139 				     uint64_t *nb_sb, bool online)
1140 {
1141 	unsigned long pfn, nr_pages;
1142 	int sb_id, count;
1143 	int rc;
1144 
1145 	if (WARN_ON_ONCE(!*nb_sb))
1146 		return -EINVAL;
1147 
1148 	while (*nb_sb) {
1149 		sb_id = virtio_mem_mb_first_unplugged_sb(vm, mb_id);
1150 		if (sb_id >= vm->nb_sb_per_mb)
1151 			break;
1152 		count = 1;
1153 		while (count < *nb_sb &&
1154 		       sb_id + count < vm->nb_sb_per_mb &&
1155 		       !virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id + count,
1156 						      1))
1157 			count++;
1158 
1159 		rc = virtio_mem_mb_plug_sb(vm, mb_id, sb_id, count);
1160 		if (rc)
1161 			return rc;
1162 		*nb_sb -= count;
1163 		if (!online)
1164 			continue;
1165 
1166 		/* fake-online the pages if the memory block is online */
1167 		pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
1168 			       sb_id * vm->subblock_size);
1169 		nr_pages = PFN_DOWN(count * vm->subblock_size);
1170 		virtio_mem_fake_online(pfn, nr_pages);
1171 	}
1172 
1173 	if (virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1174 		if (online)
1175 			virtio_mem_mb_set_state(vm, mb_id,
1176 						VIRTIO_MEM_MB_STATE_ONLINE);
1177 		else
1178 			virtio_mem_mb_set_state(vm, mb_id,
1179 						VIRTIO_MEM_MB_STATE_OFFLINE);
1180 	}
1181 
1182 	return 0;
1183 }
1184 
1185 /*
1186  * Try to plug the requested amount of memory.
1187  */
1188 static int virtio_mem_plug_request(struct virtio_mem *vm, uint64_t diff)
1189 {
1190 	uint64_t nb_sb = diff / vm->subblock_size;
1191 	unsigned long mb_id;
1192 	int rc;
1193 
1194 	if (!nb_sb)
1195 		return 0;
1196 
1197 	/* Don't race with onlining/offlining */
1198 	mutex_lock(&vm->hotplug_mutex);
1199 
1200 	/* Try to plug subblocks of partially plugged online blocks. */
1201 	virtio_mem_for_each_mb_state(vm, mb_id,
1202 				     VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL) {
1203 		rc = virtio_mem_mb_plug_any_sb(vm, mb_id, &nb_sb, true);
1204 		if (rc || !nb_sb)
1205 			goto out_unlock;
1206 		cond_resched();
1207 	}
1208 
1209 	/* Try to plug subblocks of partially plugged offline blocks. */
1210 	virtio_mem_for_each_mb_state(vm, mb_id,
1211 				     VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) {
1212 		rc = virtio_mem_mb_plug_any_sb(vm, mb_id, &nb_sb, false);
1213 		if (rc || !nb_sb)
1214 			goto out_unlock;
1215 		cond_resched();
1216 	}
1217 
1218 	/*
1219 	 * We won't be working on online/offline memory blocks from this point,
1220 	 * so we can't race with memory onlining/offlining. Drop the mutex.
1221 	 */
1222 	mutex_unlock(&vm->hotplug_mutex);
1223 
1224 	/* Try to plug and add unused blocks */
1225 	virtio_mem_for_each_mb_state(vm, mb_id, VIRTIO_MEM_MB_STATE_UNUSED) {
1226 		if (virtio_mem_too_many_mb_offline(vm))
1227 			return -ENOSPC;
1228 
1229 		rc = virtio_mem_mb_plug_and_add(vm, mb_id, &nb_sb);
1230 		if (rc || !nb_sb)
1231 			return rc;
1232 		cond_resched();
1233 	}
1234 
1235 	/* Try to prepare, plug and add new blocks */
1236 	while (nb_sb) {
1237 		if (virtio_mem_too_many_mb_offline(vm))
1238 			return -ENOSPC;
1239 
1240 		rc = virtio_mem_prepare_next_mb(vm, &mb_id);
1241 		if (rc)
1242 			return rc;
1243 		rc = virtio_mem_mb_plug_and_add(vm, mb_id, &nb_sb);
1244 		if (rc)
1245 			return rc;
1246 		cond_resched();
1247 	}
1248 
1249 	return 0;
1250 out_unlock:
1251 	mutex_unlock(&vm->hotplug_mutex);
1252 	return rc;
1253 }
1254 
1255 /*
1256  * Unplug the desired number of plugged subblocks of an offline memory block.
1257  * Will fail if any subblock cannot get unplugged (instead of skipping it).
1258  *
1259  * Will modify the state of the memory block. Might temporarily drop the
1260  * hotplug_mutex.
1261  *
1262  * Note: Can fail after some subblocks were successfully unplugged.
1263  */
1264 static int virtio_mem_mb_unplug_any_sb_offline(struct virtio_mem *vm,
1265 					       unsigned long mb_id,
1266 					       uint64_t *nb_sb)
1267 {
1268 	int rc;
1269 
1270 	rc = virtio_mem_mb_unplug_any_sb(vm, mb_id, nb_sb);
1271 
1272 	/* some subblocks might have been unplugged even on failure */
1273 	if (!virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb))
1274 		virtio_mem_mb_set_state(vm, mb_id,
1275 					VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL);
1276 	if (rc)
1277 		return rc;
1278 
1279 	if (virtio_mem_mb_test_sb_unplugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1280 		/*
1281 		 * Remove the block from Linux - this should never fail.
1282 		 * Hinder the block from getting onlined by marking it
1283 		 * unplugged. Temporarily drop the mutex, so
1284 		 * any pending GOING_ONLINE requests can be serviced/rejected.
1285 		 */
1286 		virtio_mem_mb_set_state(vm, mb_id,
1287 					VIRTIO_MEM_MB_STATE_UNUSED);
1288 
1289 		mutex_unlock(&vm->hotplug_mutex);
1290 		rc = virtio_mem_mb_remove(vm, mb_id);
1291 		BUG_ON(rc);
1292 		mutex_lock(&vm->hotplug_mutex);
1293 	}
1294 	return 0;
1295 }
1296 
1297 /*
1298  * Unplug the given plugged subblocks of an online memory block.
1299  *
1300  * Will modify the state of the memory block.
1301  */
1302 static int virtio_mem_mb_unplug_sb_online(struct virtio_mem *vm,
1303 					  unsigned long mb_id, int sb_id,
1304 					  int count)
1305 {
1306 	const unsigned long nr_pages = PFN_DOWN(vm->subblock_size) * count;
1307 	unsigned long start_pfn;
1308 	int rc;
1309 
1310 	start_pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
1311 			     sb_id * vm->subblock_size);
1312 	rc = alloc_contig_range(start_pfn, start_pfn + nr_pages,
1313 				MIGRATE_MOVABLE, GFP_KERNEL);
1314 	if (rc == -ENOMEM)
1315 		/* whoops, out of memory */
1316 		return rc;
1317 	if (rc)
1318 		return -EBUSY;
1319 
1320 	/* Mark it as fake-offline before unplugging it */
1321 	virtio_mem_set_fake_offline(start_pfn, nr_pages, true);
1322 	adjust_managed_page_count(pfn_to_page(start_pfn), -nr_pages);
1323 
1324 	/* Try to unplug the allocated memory */
1325 	rc = virtio_mem_mb_unplug_sb(vm, mb_id, sb_id, count);
1326 	if (rc) {
1327 		/* Return the memory to the buddy. */
1328 		virtio_mem_fake_online(start_pfn, nr_pages);
1329 		return rc;
1330 	}
1331 
1332 	virtio_mem_mb_set_state(vm, mb_id,
1333 				VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL);
1334 	return 0;
1335 }
1336 
1337 /*
1338  * Unplug the desired number of plugged subblocks of an online memory block.
1339  * Will skip subblock that are busy.
1340  *
1341  * Will modify the state of the memory block. Might temporarily drop the
1342  * hotplug_mutex.
1343  *
1344  * Note: Can fail after some subblocks were successfully unplugged. Can
1345  *       return 0 even if subblocks were busy and could not get unplugged.
1346  */
1347 static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm,
1348 					      unsigned long mb_id,
1349 					      uint64_t *nb_sb)
1350 {
1351 	int rc, sb_id;
1352 
1353 	/* If possible, try to unplug the complete block in one shot. */
1354 	if (*nb_sb >= vm->nb_sb_per_mb &&
1355 	    virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1356 		rc = virtio_mem_mb_unplug_sb_online(vm, mb_id, 0,
1357 						    vm->nb_sb_per_mb);
1358 		if (!rc) {
1359 			*nb_sb -= vm->nb_sb_per_mb;
1360 			goto unplugged;
1361 		} else if (rc != -EBUSY)
1362 			return rc;
1363 	}
1364 
1365 	/* Fallback to single subblocks. */
1366 	for (sb_id = vm->nb_sb_per_mb - 1; sb_id >= 0 && *nb_sb; sb_id--) {
1367 		/* Find the next candidate subblock */
1368 		while (sb_id >= 0 &&
1369 		       !virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
1370 			sb_id--;
1371 		if (sb_id < 0)
1372 			break;
1373 
1374 		rc = virtio_mem_mb_unplug_sb_online(vm, mb_id, sb_id, 1);
1375 		if (rc == -EBUSY)
1376 			continue;
1377 		else if (rc)
1378 			return rc;
1379 		*nb_sb -= 1;
1380 	}
1381 
1382 unplugged:
1383 	/*
1384 	 * Once all subblocks of a memory block were unplugged, offline and
1385 	 * remove it. This will usually not fail, as no memory is in use
1386 	 * anymore - however some other notifiers might NACK the request.
1387 	 */
1388 	if (virtio_mem_mb_test_sb_unplugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1389 		mutex_unlock(&vm->hotplug_mutex);
1390 		rc = virtio_mem_mb_offline_and_remove(vm, mb_id);
1391 		mutex_lock(&vm->hotplug_mutex);
1392 		if (!rc)
1393 			virtio_mem_mb_set_state(vm, mb_id,
1394 						VIRTIO_MEM_MB_STATE_UNUSED);
1395 	}
1396 
1397 	return 0;
1398 }
1399 
1400 /*
1401  * Try to unplug the requested amount of memory.
1402  */
1403 static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff)
1404 {
1405 	uint64_t nb_sb = diff / vm->subblock_size;
1406 	unsigned long mb_id;
1407 	int rc;
1408 
1409 	if (!nb_sb)
1410 		return 0;
1411 
1412 	/*
1413 	 * We'll drop the mutex a couple of times when it is safe to do so.
1414 	 * This might result in some blocks switching the state (online/offline)
1415 	 * and we could miss them in this run - we will retry again later.
1416 	 */
1417 	mutex_lock(&vm->hotplug_mutex);
1418 
1419 	/* Try to unplug subblocks of partially plugged offline blocks. */
1420 	virtio_mem_for_each_mb_state_rev(vm, mb_id,
1421 					 VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) {
1422 		rc = virtio_mem_mb_unplug_any_sb_offline(vm, mb_id,
1423 							 &nb_sb);
1424 		if (rc || !nb_sb)
1425 			goto out_unlock;
1426 		cond_resched();
1427 	}
1428 
1429 	/* Try to unplug subblocks of plugged offline blocks. */
1430 	virtio_mem_for_each_mb_state_rev(vm, mb_id,
1431 					 VIRTIO_MEM_MB_STATE_OFFLINE) {
1432 		rc = virtio_mem_mb_unplug_any_sb_offline(vm, mb_id,
1433 							 &nb_sb);
1434 		if (rc || !nb_sb)
1435 			goto out_unlock;
1436 		cond_resched();
1437 	}
1438 
1439 	if (!unplug_online) {
1440 		mutex_unlock(&vm->hotplug_mutex);
1441 		return 0;
1442 	}
1443 
1444 	/* Try to unplug subblocks of partially plugged online blocks. */
1445 	virtio_mem_for_each_mb_state_rev(vm, mb_id,
1446 					 VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL) {
1447 		rc = virtio_mem_mb_unplug_any_sb_online(vm, mb_id,
1448 							&nb_sb);
1449 		if (rc || !nb_sb)
1450 			goto out_unlock;
1451 		mutex_unlock(&vm->hotplug_mutex);
1452 		cond_resched();
1453 		mutex_lock(&vm->hotplug_mutex);
1454 	}
1455 
1456 	/* Try to unplug subblocks of plugged online blocks. */
1457 	virtio_mem_for_each_mb_state_rev(vm, mb_id,
1458 					 VIRTIO_MEM_MB_STATE_ONLINE) {
1459 		rc = virtio_mem_mb_unplug_any_sb_online(vm, mb_id,
1460 							&nb_sb);
1461 		if (rc || !nb_sb)
1462 			goto out_unlock;
1463 		mutex_unlock(&vm->hotplug_mutex);
1464 		cond_resched();
1465 		mutex_lock(&vm->hotplug_mutex);
1466 	}
1467 
1468 	mutex_unlock(&vm->hotplug_mutex);
1469 	return nb_sb ? -EBUSY : 0;
1470 out_unlock:
1471 	mutex_unlock(&vm->hotplug_mutex);
1472 	return rc;
1473 }
1474 
1475 /*
1476  * Try to unplug all blocks that couldn't be unplugged before, for example,
1477  * because the hypervisor was busy.
1478  */
1479 static int virtio_mem_unplug_pending_mb(struct virtio_mem *vm)
1480 {
1481 	unsigned long mb_id;
1482 	int rc;
1483 
1484 	virtio_mem_for_each_mb_state(vm, mb_id, VIRTIO_MEM_MB_STATE_PLUGGED) {
1485 		rc = virtio_mem_mb_unplug(vm, mb_id);
1486 		if (rc)
1487 			return rc;
1488 		virtio_mem_mb_set_state(vm, mb_id, VIRTIO_MEM_MB_STATE_UNUSED);
1489 	}
1490 
1491 	return 0;
1492 }
1493 
1494 /*
1495  * Update all parts of the config that could have changed.
1496  */
1497 static void virtio_mem_refresh_config(struct virtio_mem *vm)
1498 {
1499 	const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
1500 	uint64_t new_plugged_size, usable_region_size, end_addr;
1501 
1502 	/* the plugged_size is just a reflection of what _we_ did previously */
1503 	virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
1504 			&new_plugged_size);
1505 	if (WARN_ON_ONCE(new_plugged_size != vm->plugged_size))
1506 		vm->plugged_size = new_plugged_size;
1507 
1508 	/* calculate the last usable memory block id */
1509 	virtio_cread_le(vm->vdev, struct virtio_mem_config,
1510 			usable_region_size, &usable_region_size);
1511 	end_addr = vm->addr + usable_region_size;
1512 	end_addr = min(end_addr, phys_limit);
1513 	vm->last_usable_mb_id = virtio_mem_phys_to_mb_id(end_addr) - 1;
1514 
1515 	/* see if there is a request to change the size */
1516 	virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size,
1517 			&vm->requested_size);
1518 
1519 	dev_info(&vm->vdev->dev, "plugged size: 0x%llx", vm->plugged_size);
1520 	dev_info(&vm->vdev->dev, "requested size: 0x%llx", vm->requested_size);
1521 }
1522 
1523 /*
1524  * Workqueue function for handling plug/unplug requests and config updates.
1525  */
1526 static void virtio_mem_run_wq(struct work_struct *work)
1527 {
1528 	struct virtio_mem *vm = container_of(work, struct virtio_mem, wq);
1529 	uint64_t diff;
1530 	int rc;
1531 
1532 	hrtimer_cancel(&vm->retry_timer);
1533 
1534 	if (vm->broken)
1535 		return;
1536 
1537 retry:
1538 	rc = 0;
1539 
1540 	/* Make sure we start with a clean state if there are leftovers. */
1541 	if (unlikely(vm->unplug_all_required))
1542 		rc = virtio_mem_send_unplug_all_request(vm);
1543 
1544 	if (atomic_read(&vm->config_changed)) {
1545 		atomic_set(&vm->config_changed, 0);
1546 		virtio_mem_refresh_config(vm);
1547 	}
1548 
1549 	/* Unplug any leftovers from previous runs */
1550 	if (!rc)
1551 		rc = virtio_mem_unplug_pending_mb(vm);
1552 
1553 	if (!rc && vm->requested_size != vm->plugged_size) {
1554 		if (vm->requested_size > vm->plugged_size) {
1555 			diff = vm->requested_size - vm->plugged_size;
1556 			rc = virtio_mem_plug_request(vm, diff);
1557 		} else {
1558 			diff = vm->plugged_size - vm->requested_size;
1559 			rc = virtio_mem_unplug_request(vm, diff);
1560 		}
1561 	}
1562 
1563 	switch (rc) {
1564 	case 0:
1565 		vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS;
1566 		break;
1567 	case -ENOSPC:
1568 		/*
1569 		 * We cannot add any more memory (alignment, physical limit)
1570 		 * or we have too many offline memory blocks.
1571 		 */
1572 		break;
1573 	case -ETXTBSY:
1574 		/*
1575 		 * The hypervisor cannot process our request right now
1576 		 * (e.g., out of memory, migrating);
1577 		 */
1578 	case -EBUSY:
1579 		/*
1580 		 * We cannot free up any memory to unplug it (all plugged memory
1581 		 * is busy).
1582 		 */
1583 	case -ENOMEM:
1584 		/* Out of memory, try again later. */
1585 		hrtimer_start(&vm->retry_timer, ms_to_ktime(vm->retry_timer_ms),
1586 			      HRTIMER_MODE_REL);
1587 		break;
1588 	case -EAGAIN:
1589 		/* Retry immediately (e.g., the config changed). */
1590 		goto retry;
1591 	default:
1592 		/* Unknown error, mark as broken */
1593 		dev_err(&vm->vdev->dev,
1594 			"unknown error, marking device broken: %d\n", rc);
1595 		vm->broken = true;
1596 	}
1597 }
1598 
1599 static enum hrtimer_restart virtio_mem_timer_expired(struct hrtimer *timer)
1600 {
1601 	struct virtio_mem *vm = container_of(timer, struct virtio_mem,
1602 					     retry_timer);
1603 
1604 	virtio_mem_retry(vm);
1605 	vm->retry_timer_ms = min_t(unsigned int, vm->retry_timer_ms * 2,
1606 				   VIRTIO_MEM_RETRY_TIMER_MAX_MS);
1607 	return HRTIMER_NORESTART;
1608 }
1609 
1610 static void virtio_mem_handle_response(struct virtqueue *vq)
1611 {
1612 	struct virtio_mem *vm = vq->vdev->priv;
1613 
1614 	wake_up(&vm->host_resp);
1615 }
1616 
1617 static int virtio_mem_init_vq(struct virtio_mem *vm)
1618 {
1619 	struct virtqueue *vq;
1620 
1621 	vq = virtio_find_single_vq(vm->vdev, virtio_mem_handle_response,
1622 				   "guest-request");
1623 	if (IS_ERR(vq))
1624 		return PTR_ERR(vq);
1625 	vm->vq = vq;
1626 
1627 	return 0;
1628 }
1629 
1630 static int virtio_mem_init(struct virtio_mem *vm)
1631 {
1632 	const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
1633 	uint16_t node_id;
1634 
1635 	if (!vm->vdev->config->get) {
1636 		dev_err(&vm->vdev->dev, "config access disabled\n");
1637 		return -EINVAL;
1638 	}
1639 
1640 	/*
1641 	 * We don't want to (un)plug or reuse any memory when in kdump. The
1642 	 * memory is still accessible (but not mapped).
1643 	 */
1644 	if (is_kdump_kernel()) {
1645 		dev_warn(&vm->vdev->dev, "disabled in kdump kernel\n");
1646 		return -EBUSY;
1647 	}
1648 
1649 	/* Fetch all properties that can't change. */
1650 	virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
1651 			&vm->plugged_size);
1652 	virtio_cread_le(vm->vdev, struct virtio_mem_config, block_size,
1653 			&vm->device_block_size);
1654 	virtio_cread_le(vm->vdev, struct virtio_mem_config, node_id,
1655 			&node_id);
1656 	vm->nid = virtio_mem_translate_node_id(vm, node_id);
1657 	virtio_cread_le(vm->vdev, struct virtio_mem_config, addr, &vm->addr);
1658 	virtio_cread_le(vm->vdev, struct virtio_mem_config, region_size,
1659 			&vm->region_size);
1660 
1661 	/*
1662 	 * We always hotplug memory in memory block granularity. This way,
1663 	 * we have to wait for exactly one memory block to online.
1664 	 */
1665 	if (vm->device_block_size > memory_block_size_bytes()) {
1666 		dev_err(&vm->vdev->dev,
1667 			"The block size is not supported (too big).\n");
1668 		return -EINVAL;
1669 	}
1670 
1671 	/* bad device setup - warn only */
1672 	if (!IS_ALIGNED(vm->addr, memory_block_size_bytes()))
1673 		dev_warn(&vm->vdev->dev,
1674 			 "The alignment of the physical start address can make some memory unusable.\n");
1675 	if (!IS_ALIGNED(vm->addr + vm->region_size, memory_block_size_bytes()))
1676 		dev_warn(&vm->vdev->dev,
1677 			 "The alignment of the physical end address can make some memory unusable.\n");
1678 	if (vm->addr + vm->region_size > phys_limit)
1679 		dev_warn(&vm->vdev->dev,
1680 			 "Some memory is not addressable. This can make some memory unusable.\n");
1681 
1682 	/*
1683 	 * Calculate the subblock size:
1684 	 * - At least MAX_ORDER - 1 / pageblock_order.
1685 	 * - At least the device block size.
1686 	 * In the worst case, a single subblock per memory block.
1687 	 */
1688 	vm->subblock_size = PAGE_SIZE * 1ul << max_t(uint32_t, MAX_ORDER - 1,
1689 						     pageblock_order);
1690 	vm->subblock_size = max_t(uint64_t, vm->device_block_size,
1691 				  vm->subblock_size);
1692 	vm->nb_sb_per_mb = memory_block_size_bytes() / vm->subblock_size;
1693 
1694 	/* Round up to the next full memory block */
1695 	vm->first_mb_id = virtio_mem_phys_to_mb_id(vm->addr - 1 +
1696 						   memory_block_size_bytes());
1697 	vm->next_mb_id = vm->first_mb_id;
1698 	vm->last_mb_id = virtio_mem_phys_to_mb_id(vm->addr +
1699 			 vm->region_size) - 1;
1700 
1701 	dev_info(&vm->vdev->dev, "start address: 0x%llx", vm->addr);
1702 	dev_info(&vm->vdev->dev, "region size: 0x%llx", vm->region_size);
1703 	dev_info(&vm->vdev->dev, "device block size: 0x%llx",
1704 		 (unsigned long long)vm->device_block_size);
1705 	dev_info(&vm->vdev->dev, "memory block size: 0x%lx",
1706 		 memory_block_size_bytes());
1707 	dev_info(&vm->vdev->dev, "subblock size: 0x%llx",
1708 		 (unsigned long long)vm->subblock_size);
1709 	if (vm->nid != NUMA_NO_NODE)
1710 		dev_info(&vm->vdev->dev, "nid: %d", vm->nid);
1711 
1712 	return 0;
1713 }
1714 
1715 static int virtio_mem_create_resource(struct virtio_mem *vm)
1716 {
1717 	/*
1718 	 * When force-unloading the driver and removing the device, we
1719 	 * could have a garbage pointer. Duplicate the string.
1720 	 */
1721 	const char *name = kstrdup(dev_name(&vm->vdev->dev), GFP_KERNEL);
1722 
1723 	if (!name)
1724 		return -ENOMEM;
1725 
1726 	vm->parent_resource = __request_mem_region(vm->addr, vm->region_size,
1727 						   name, IORESOURCE_SYSTEM_RAM);
1728 	if (!vm->parent_resource) {
1729 		kfree(name);
1730 		dev_warn(&vm->vdev->dev, "could not reserve device region\n");
1731 		dev_info(&vm->vdev->dev,
1732 			 "reloading the driver is not supported\n");
1733 		return -EBUSY;
1734 	}
1735 
1736 	/* The memory is not actually busy - make add_memory() work. */
1737 	vm->parent_resource->flags &= ~IORESOURCE_BUSY;
1738 	return 0;
1739 }
1740 
1741 static void virtio_mem_delete_resource(struct virtio_mem *vm)
1742 {
1743 	const char *name;
1744 
1745 	if (!vm->parent_resource)
1746 		return;
1747 
1748 	name = vm->parent_resource->name;
1749 	release_resource(vm->parent_resource);
1750 	kfree(vm->parent_resource);
1751 	kfree(name);
1752 	vm->parent_resource = NULL;
1753 }
1754 
1755 static int virtio_mem_probe(struct virtio_device *vdev)
1756 {
1757 	struct virtio_mem *vm;
1758 	int rc;
1759 
1760 	BUILD_BUG_ON(sizeof(struct virtio_mem_req) != 24);
1761 	BUILD_BUG_ON(sizeof(struct virtio_mem_resp) != 10);
1762 
1763 	vdev->priv = vm = kzalloc(sizeof(*vm), GFP_KERNEL);
1764 	if (!vm)
1765 		return -ENOMEM;
1766 
1767 	init_waitqueue_head(&vm->host_resp);
1768 	vm->vdev = vdev;
1769 	INIT_WORK(&vm->wq, virtio_mem_run_wq);
1770 	mutex_init(&vm->hotplug_mutex);
1771 	INIT_LIST_HEAD(&vm->next);
1772 	spin_lock_init(&vm->removal_lock);
1773 	hrtimer_init(&vm->retry_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1774 	vm->retry_timer.function = virtio_mem_timer_expired;
1775 	vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS;
1776 
1777 	/* register the virtqueue */
1778 	rc = virtio_mem_init_vq(vm);
1779 	if (rc)
1780 		goto out_free_vm;
1781 
1782 	/* initialize the device by querying the config */
1783 	rc = virtio_mem_init(vm);
1784 	if (rc)
1785 		goto out_del_vq;
1786 
1787 	/* create the parent resource for all memory */
1788 	rc = virtio_mem_create_resource(vm);
1789 	if (rc)
1790 		goto out_del_vq;
1791 
1792 	/*
1793 	 * If we still have memory plugged, we have to unplug all memory first.
1794 	 * Registering our parent resource makes sure that this memory isn't
1795 	 * actually in use (e.g., trying to reload the driver).
1796 	 */
1797 	if (vm->plugged_size) {
1798 		vm->unplug_all_required = 1;
1799 		dev_info(&vm->vdev->dev, "unplugging all memory is required\n");
1800 	}
1801 
1802 	/* register callbacks */
1803 	vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb;
1804 	rc = register_memory_notifier(&vm->memory_notifier);
1805 	if (rc)
1806 		goto out_del_resource;
1807 	rc = register_virtio_mem_device(vm);
1808 	if (rc)
1809 		goto out_unreg_mem;
1810 
1811 	virtio_device_ready(vdev);
1812 
1813 	/* trigger a config update to start processing the requested_size */
1814 	atomic_set(&vm->config_changed, 1);
1815 	queue_work(system_freezable_wq, &vm->wq);
1816 
1817 	return 0;
1818 out_unreg_mem:
1819 	unregister_memory_notifier(&vm->memory_notifier);
1820 out_del_resource:
1821 	virtio_mem_delete_resource(vm);
1822 out_del_vq:
1823 	vdev->config->del_vqs(vdev);
1824 out_free_vm:
1825 	kfree(vm);
1826 	vdev->priv = NULL;
1827 
1828 	return rc;
1829 }
1830 
1831 static void virtio_mem_remove(struct virtio_device *vdev)
1832 {
1833 	struct virtio_mem *vm = vdev->priv;
1834 	unsigned long mb_id;
1835 	int rc;
1836 
1837 	/*
1838 	 * Make sure the workqueue won't be triggered anymore and no memory
1839 	 * blocks can be onlined/offlined until we're finished here.
1840 	 */
1841 	mutex_lock(&vm->hotplug_mutex);
1842 	spin_lock_irq(&vm->removal_lock);
1843 	vm->removing = true;
1844 	spin_unlock_irq(&vm->removal_lock);
1845 	mutex_unlock(&vm->hotplug_mutex);
1846 
1847 	/* wait until the workqueue stopped */
1848 	cancel_work_sync(&vm->wq);
1849 	hrtimer_cancel(&vm->retry_timer);
1850 
1851 	/*
1852 	 * After we unregistered our callbacks, user space can online partially
1853 	 * plugged offline blocks. Make sure to remove them.
1854 	 */
1855 	virtio_mem_for_each_mb_state(vm, mb_id,
1856 				     VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) {
1857 		rc = virtio_mem_mb_remove(vm, mb_id);
1858 		BUG_ON(rc);
1859 		virtio_mem_mb_set_state(vm, mb_id, VIRTIO_MEM_MB_STATE_UNUSED);
1860 	}
1861 	/*
1862 	 * After we unregistered our callbacks, user space can no longer
1863 	 * offline partially plugged online memory blocks. No need to worry
1864 	 * about them.
1865 	 */
1866 
1867 	/* unregister callbacks */
1868 	unregister_virtio_mem_device(vm);
1869 	unregister_memory_notifier(&vm->memory_notifier);
1870 
1871 	/*
1872 	 * There is no way we could reliably remove all memory we have added to
1873 	 * the system. And there is no way to stop the driver/device from going
1874 	 * away. Warn at least.
1875 	 */
1876 	if (vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE] ||
1877 	    vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL] ||
1878 	    vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE] ||
1879 	    vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL]) {
1880 		dev_warn(&vdev->dev, "device still has system memory added\n");
1881 	} else {
1882 		virtio_mem_delete_resource(vm);
1883 		kfree_const(vm->resource_name);
1884 	}
1885 
1886 	/* remove all tracking data - no locking needed */
1887 	vfree(vm->mb_state);
1888 	vfree(vm->sb_bitmap);
1889 
1890 	/* reset the device and cleanup the queues */
1891 	vdev->config->reset(vdev);
1892 	vdev->config->del_vqs(vdev);
1893 
1894 	kfree(vm);
1895 	vdev->priv = NULL;
1896 }
1897 
1898 static void virtio_mem_config_changed(struct virtio_device *vdev)
1899 {
1900 	struct virtio_mem *vm = vdev->priv;
1901 
1902 	atomic_set(&vm->config_changed, 1);
1903 	virtio_mem_retry(vm);
1904 }
1905 
1906 #ifdef CONFIG_PM_SLEEP
1907 static int virtio_mem_freeze(struct virtio_device *vdev)
1908 {
1909 	/*
1910 	 * When restarting the VM, all memory is usually unplugged. Don't
1911 	 * allow to suspend/hibernate.
1912 	 */
1913 	dev_err(&vdev->dev, "save/restore not supported.\n");
1914 	return -EPERM;
1915 }
1916 
1917 static int virtio_mem_restore(struct virtio_device *vdev)
1918 {
1919 	return -EPERM;
1920 }
1921 #endif
1922 
1923 static unsigned int virtio_mem_features[] = {
1924 #if defined(CONFIG_NUMA) && defined(CONFIG_ACPI_NUMA)
1925 	VIRTIO_MEM_F_ACPI_PXM,
1926 #endif
1927 };
1928 
1929 static struct virtio_device_id virtio_mem_id_table[] = {
1930 	{ VIRTIO_ID_MEM, VIRTIO_DEV_ANY_ID },
1931 	{ 0 },
1932 };
1933 
1934 static struct virtio_driver virtio_mem_driver = {
1935 	.feature_table = virtio_mem_features,
1936 	.feature_table_size = ARRAY_SIZE(virtio_mem_features),
1937 	.driver.name = KBUILD_MODNAME,
1938 	.driver.owner = THIS_MODULE,
1939 	.id_table = virtio_mem_id_table,
1940 	.probe = virtio_mem_probe,
1941 	.remove = virtio_mem_remove,
1942 	.config_changed = virtio_mem_config_changed,
1943 #ifdef CONFIG_PM_SLEEP
1944 	.freeze	=	virtio_mem_freeze,
1945 	.restore =	virtio_mem_restore,
1946 #endif
1947 };
1948 
1949 module_virtio_driver(virtio_mem_driver);
1950 MODULE_DEVICE_TABLE(virtio, virtio_mem_id_table);
1951 MODULE_AUTHOR("David Hildenbrand <david@redhat.com>");
1952 MODULE_DESCRIPTION("Virtio-mem driver");
1953 MODULE_LICENSE("GPL");
1954