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