virtio-mem: more precise calculation in virtio_mem_mb_state_prepare_next_mb()
[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 int order = MAX_ORDER - 1;
757         int i;
758
759         /*
760          * We are always called with subblock granularity, which is at least
761          * aligned to MAX_ORDER - 1.
762          */
763         for (i = 0; i < nr_pages; i += 1 << order) {
764                 struct page *page = pfn_to_page(pfn + i);
765
766                 /*
767                  * If the page is PageDirty(), it was kept fake-offline when
768                  * onlining the memory block. Otherwise, it was allocated
769                  * using alloc_contig_range(). All pages in a subblock are
770                  * alike.
771                  */
772                 if (PageDirty(page)) {
773                         virtio_mem_clear_fake_offline(pfn + i, 1 << order,
774                                                       false);
775                         generic_online_page(page, order);
776                 } else {
777                         virtio_mem_clear_fake_offline(pfn + i, 1 << order,
778                                                       true);
779                         free_contig_range(pfn + i, 1 << order);
780                         adjust_managed_page_count(page, 1 << order);
781                 }
782         }
783 }
784
785 static void virtio_mem_online_page_cb(struct page *page, unsigned int order)
786 {
787         const unsigned long addr = page_to_phys(page);
788         const unsigned long mb_id = virtio_mem_phys_to_mb_id(addr);
789         struct virtio_mem *vm;
790         int sb_id;
791
792         /*
793          * We exploit here that subblocks have at least MAX_ORDER - 1
794          * size/alignment and that this callback is is called with such a
795          * size/alignment. So we cannot cross subblocks and therefore
796          * also not memory blocks.
797          */
798         rcu_read_lock();
799         list_for_each_entry_rcu(vm, &virtio_mem_devices, next) {
800                 if (!virtio_mem_owned_mb(vm, mb_id))
801                         continue;
802
803                 sb_id = virtio_mem_phys_to_sb_id(vm, addr);
804                 /*
805                  * If plugged, online the pages, otherwise, set them fake
806                  * offline (PageOffline).
807                  */
808                 if (virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
809                         generic_online_page(page, order);
810                 else
811                         virtio_mem_set_fake_offline(PFN_DOWN(addr), 1 << order,
812                                                     false);
813                 rcu_read_unlock();
814                 return;
815         }
816         rcu_read_unlock();
817
818         /* not virtio-mem memory, but e.g., a DIMM. online it */
819         generic_online_page(page, order);
820 }
821
822 static uint64_t virtio_mem_send_request(struct virtio_mem *vm,
823                                         const struct virtio_mem_req *req)
824 {
825         struct scatterlist *sgs[2], sg_req, sg_resp;
826         unsigned int len;
827         int rc;
828
829         /* don't use the request residing on the stack (vaddr) */
830         vm->req = *req;
831
832         /* out: buffer for request */
833         sg_init_one(&sg_req, &vm->req, sizeof(vm->req));
834         sgs[0] = &sg_req;
835
836         /* in: buffer for response */
837         sg_init_one(&sg_resp, &vm->resp, sizeof(vm->resp));
838         sgs[1] = &sg_resp;
839
840         rc = virtqueue_add_sgs(vm->vq, sgs, 1, 1, vm, GFP_KERNEL);
841         if (rc < 0)
842                 return rc;
843
844         virtqueue_kick(vm->vq);
845
846         /* wait for a response */
847         wait_event(vm->host_resp, virtqueue_get_buf(vm->vq, &len));
848
849         return virtio16_to_cpu(vm->vdev, vm->resp.type);
850 }
851
852 static int virtio_mem_send_plug_request(struct virtio_mem *vm, uint64_t addr,
853                                         uint64_t size)
854 {
855         const uint64_t nb_vm_blocks = size / vm->device_block_size;
856         const struct virtio_mem_req req = {
857                 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_PLUG),
858                 .u.plug.addr = cpu_to_virtio64(vm->vdev, addr),
859                 .u.plug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
860         };
861
862         if (atomic_read(&vm->config_changed))
863                 return -EAGAIN;
864
865         switch (virtio_mem_send_request(vm, &req)) {
866         case VIRTIO_MEM_RESP_ACK:
867                 vm->plugged_size += size;
868                 return 0;
869         case VIRTIO_MEM_RESP_NACK:
870                 return -EAGAIN;
871         case VIRTIO_MEM_RESP_BUSY:
872                 return -ETXTBSY;
873         case VIRTIO_MEM_RESP_ERROR:
874                 return -EINVAL;
875         default:
876                 return -ENOMEM;
877         }
878 }
879
880 static int virtio_mem_send_unplug_request(struct virtio_mem *vm, uint64_t addr,
881                                           uint64_t size)
882 {
883         const uint64_t nb_vm_blocks = size / vm->device_block_size;
884         const struct virtio_mem_req req = {
885                 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG),
886                 .u.unplug.addr = cpu_to_virtio64(vm->vdev, addr),
887                 .u.unplug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
888         };
889
890         if (atomic_read(&vm->config_changed))
891                 return -EAGAIN;
892
893         switch (virtio_mem_send_request(vm, &req)) {
894         case VIRTIO_MEM_RESP_ACK:
895                 vm->plugged_size -= size;
896                 return 0;
897         case VIRTIO_MEM_RESP_BUSY:
898                 return -ETXTBSY;
899         case VIRTIO_MEM_RESP_ERROR:
900                 return -EINVAL;
901         default:
902                 return -ENOMEM;
903         }
904 }
905
906 static int virtio_mem_send_unplug_all_request(struct virtio_mem *vm)
907 {
908         const struct virtio_mem_req req = {
909                 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG_ALL),
910         };
911
912         switch (virtio_mem_send_request(vm, &req)) {
913         case VIRTIO_MEM_RESP_ACK:
914                 vm->unplug_all_required = false;
915                 vm->plugged_size = 0;
916                 /* usable region might have shrunk */
917                 atomic_set(&vm->config_changed, 1);
918                 return 0;
919         case VIRTIO_MEM_RESP_BUSY:
920                 return -ETXTBSY;
921         default:
922                 return -ENOMEM;
923         }
924 }
925
926 /*
927  * Plug selected subblocks. Updates the plugged state, but not the state
928  * of the memory block.
929  */
930 static int virtio_mem_mb_plug_sb(struct virtio_mem *vm, unsigned long mb_id,
931                                  int sb_id, int count)
932 {
933         const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id) +
934                               sb_id * vm->subblock_size;
935         const uint64_t size = count * vm->subblock_size;
936         int rc;
937
938         dev_dbg(&vm->vdev->dev, "plugging memory block: %lu : %i - %i\n", mb_id,
939                 sb_id, sb_id + count - 1);
940
941         rc = virtio_mem_send_plug_request(vm, addr, size);
942         if (!rc)
943                 virtio_mem_mb_set_sb_plugged(vm, mb_id, sb_id, count);
944         return rc;
945 }
946
947 /*
948  * Unplug selected subblocks. Updates the plugged state, but not the state
949  * of the memory block.
950  */
951 static int virtio_mem_mb_unplug_sb(struct virtio_mem *vm, unsigned long mb_id,
952                                    int sb_id, int count)
953 {
954         const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id) +
955                               sb_id * vm->subblock_size;
956         const uint64_t size = count * vm->subblock_size;
957         int rc;
958
959         dev_dbg(&vm->vdev->dev, "unplugging memory block: %lu : %i - %i\n",
960                 mb_id, sb_id, sb_id + count - 1);
961
962         rc = virtio_mem_send_unplug_request(vm, addr, size);
963         if (!rc)
964                 virtio_mem_mb_set_sb_unplugged(vm, mb_id, sb_id, count);
965         return rc;
966 }
967
968 /*
969  * Unplug the desired number of plugged subblocks of a offline or not-added
970  * memory block. Will fail if any subblock cannot get unplugged (instead of
971  * skipping it).
972  *
973  * Will not modify the state of the memory block.
974  *
975  * Note: can fail after some subblocks were unplugged.
976  */
977 static int virtio_mem_mb_unplug_any_sb(struct virtio_mem *vm,
978                                        unsigned long mb_id, uint64_t *nb_sb)
979 {
980         int sb_id, count;
981         int rc;
982
983         sb_id = vm->nb_sb_per_mb - 1;
984         while (*nb_sb) {
985                 /* Find the next candidate subblock */
986                 while (sb_id >= 0 &&
987                        virtio_mem_mb_test_sb_unplugged(vm, mb_id, sb_id, 1))
988                         sb_id--;
989                 if (sb_id < 0)
990                         break;
991                 /* Try to unplug multiple subblocks at a time */
992                 count = 1;
993                 while (count < *nb_sb && sb_id > 0 &&
994                        virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id - 1, 1)) {
995                         count++;
996                         sb_id--;
997                 }
998
999                 rc = virtio_mem_mb_unplug_sb(vm, mb_id, sb_id, count);
1000                 if (rc)
1001                         return rc;
1002                 *nb_sb -= count;
1003                 sb_id--;
1004         }
1005
1006         return 0;
1007 }
1008
1009 /*
1010  * Unplug all plugged subblocks of an offline or not-added memory block.
1011  *
1012  * Will not modify the state of the memory block.
1013  *
1014  * Note: can fail after some subblocks were unplugged.
1015  */
1016 static int virtio_mem_mb_unplug(struct virtio_mem *vm, unsigned long mb_id)
1017 {
1018         uint64_t nb_sb = vm->nb_sb_per_mb;
1019
1020         return virtio_mem_mb_unplug_any_sb(vm, mb_id, &nb_sb);
1021 }
1022
1023 /*
1024  * Prepare tracking data for the next memory block.
1025  */
1026 static int virtio_mem_prepare_next_mb(struct virtio_mem *vm,
1027                                       unsigned long *mb_id)
1028 {
1029         int rc;
1030
1031         if (vm->next_mb_id > vm->last_usable_mb_id)
1032                 return -ENOSPC;
1033
1034         /* Resize the state array if required. */
1035         rc = virtio_mem_mb_state_prepare_next_mb(vm);
1036         if (rc)
1037                 return rc;
1038
1039         /* Resize the subblock bitmap if required. */
1040         rc = virtio_mem_sb_bitmap_prepare_next_mb(vm);
1041         if (rc)
1042                 return rc;
1043
1044         vm->nb_mb_state[VIRTIO_MEM_MB_STATE_UNUSED]++;
1045         *mb_id = vm->next_mb_id++;
1046         return 0;
1047 }
1048
1049 /*
1050  * Don't add too many blocks that are not onlined yet to avoid running OOM.
1051  */
1052 static bool virtio_mem_too_many_mb_offline(struct virtio_mem *vm)
1053 {
1054         unsigned long nb_offline;
1055
1056         nb_offline = vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE] +
1057                      vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL];
1058         return nb_offline >= VIRTIO_MEM_NB_OFFLINE_THRESHOLD;
1059 }
1060
1061 /*
1062  * Try to plug the desired number of subblocks and add the memory block
1063  * to Linux.
1064  *
1065  * Will modify the state of the memory block.
1066  */
1067 static int virtio_mem_mb_plug_and_add(struct virtio_mem *vm,
1068                                       unsigned long mb_id,
1069                                       uint64_t *nb_sb)
1070 {
1071         const int count = min_t(int, *nb_sb, vm->nb_sb_per_mb);
1072         int rc, rc2;
1073
1074         if (WARN_ON_ONCE(!count))
1075                 return -EINVAL;
1076
1077         /*
1078          * Plug the requested number of subblocks before adding it to linux,
1079          * so that onlining will directly online all plugged subblocks.
1080          */
1081         rc = virtio_mem_mb_plug_sb(vm, mb_id, 0, count);
1082         if (rc)
1083                 return rc;
1084
1085         /*
1086          * Mark the block properly offline before adding it to Linux,
1087          * so the memory notifiers will find the block in the right state.
1088          */
1089         if (count == vm->nb_sb_per_mb)
1090                 virtio_mem_mb_set_state(vm, mb_id,
1091                                         VIRTIO_MEM_MB_STATE_OFFLINE);
1092         else
1093                 virtio_mem_mb_set_state(vm, mb_id,
1094                                         VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL);
1095
1096         /* Add the memory block to linux - if that fails, try to unplug. */
1097         rc = virtio_mem_mb_add(vm, mb_id);
1098         if (rc) {
1099                 enum virtio_mem_mb_state new_state = VIRTIO_MEM_MB_STATE_UNUSED;
1100
1101                 dev_err(&vm->vdev->dev,
1102                         "adding memory block %lu failed with %d\n", mb_id, rc);
1103                 rc2 = virtio_mem_mb_unplug_sb(vm, mb_id, 0, count);
1104
1105                 /*
1106                  * TODO: Linux MM does not properly clean up yet in all cases
1107                  * where adding of memory failed - especially on -ENOMEM.
1108                  */
1109                 if (rc2)
1110                         new_state = VIRTIO_MEM_MB_STATE_PLUGGED;
1111                 virtio_mem_mb_set_state(vm, mb_id, new_state);
1112                 return rc;
1113         }
1114
1115         *nb_sb -= count;
1116         return 0;
1117 }
1118
1119 /*
1120  * Try to plug the desired number of subblocks of a memory block that
1121  * is already added to Linux.
1122  *
1123  * Will modify the state of the memory block.
1124  *
1125  * Note: Can fail after some subblocks were successfully plugged.
1126  */
1127 static int virtio_mem_mb_plug_any_sb(struct virtio_mem *vm, unsigned long mb_id,
1128                                      uint64_t *nb_sb, bool online)
1129 {
1130         unsigned long pfn, nr_pages;
1131         int sb_id, count;
1132         int rc;
1133
1134         if (WARN_ON_ONCE(!*nb_sb))
1135                 return -EINVAL;
1136
1137         while (*nb_sb) {
1138                 sb_id = virtio_mem_mb_first_unplugged_sb(vm, mb_id);
1139                 if (sb_id >= vm->nb_sb_per_mb)
1140                         break;
1141                 count = 1;
1142                 while (count < *nb_sb &&
1143                        sb_id + count < vm->nb_sb_per_mb &&
1144                        !virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id + count,
1145                                                       1))
1146                         count++;
1147
1148                 rc = virtio_mem_mb_plug_sb(vm, mb_id, sb_id, count);
1149                 if (rc)
1150                         return rc;
1151                 *nb_sb -= count;
1152                 if (!online)
1153                         continue;
1154
1155                 /* fake-online the pages if the memory block is online */
1156                 pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
1157                                sb_id * vm->subblock_size);
1158                 nr_pages = PFN_DOWN(count * vm->subblock_size);
1159                 virtio_mem_fake_online(pfn, nr_pages);
1160         }
1161
1162         if (virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1163                 if (online)
1164                         virtio_mem_mb_set_state(vm, mb_id,
1165                                                 VIRTIO_MEM_MB_STATE_ONLINE);
1166                 else
1167                         virtio_mem_mb_set_state(vm, mb_id,
1168                                                 VIRTIO_MEM_MB_STATE_OFFLINE);
1169         }
1170
1171         return 0;
1172 }
1173
1174 /*
1175  * Try to plug the requested amount of memory.
1176  */
1177 static int virtio_mem_plug_request(struct virtio_mem *vm, uint64_t diff)
1178 {
1179         uint64_t nb_sb = diff / vm->subblock_size;
1180         unsigned long mb_id;
1181         int rc;
1182
1183         if (!nb_sb)
1184                 return 0;
1185
1186         /* Don't race with onlining/offlining */
1187         mutex_lock(&vm->hotplug_mutex);
1188
1189         /* Try to plug subblocks of partially plugged online blocks. */
1190         virtio_mem_for_each_mb_state(vm, mb_id,
1191                                      VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL) {
1192                 rc = virtio_mem_mb_plug_any_sb(vm, mb_id, &nb_sb, true);
1193                 if (rc || !nb_sb)
1194                         goto out_unlock;
1195                 cond_resched();
1196         }
1197
1198         /* Try to plug subblocks of partially plugged offline blocks. */
1199         virtio_mem_for_each_mb_state(vm, mb_id,
1200                                      VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) {
1201                 rc = virtio_mem_mb_plug_any_sb(vm, mb_id, &nb_sb, false);
1202                 if (rc || !nb_sb)
1203                         goto out_unlock;
1204                 cond_resched();
1205         }
1206
1207         /*
1208          * We won't be working on online/offline memory blocks from this point,
1209          * so we can't race with memory onlining/offlining. Drop the mutex.
1210          */
1211         mutex_unlock(&vm->hotplug_mutex);
1212
1213         /* Try to plug and add unused blocks */
1214         virtio_mem_for_each_mb_state(vm, mb_id, VIRTIO_MEM_MB_STATE_UNUSED) {
1215                 if (virtio_mem_too_many_mb_offline(vm))
1216                         return -ENOSPC;
1217
1218                 rc = virtio_mem_mb_plug_and_add(vm, mb_id, &nb_sb);
1219                 if (rc || !nb_sb)
1220                         return rc;
1221                 cond_resched();
1222         }
1223
1224         /* Try to prepare, plug and add new blocks */
1225         while (nb_sb) {
1226                 if (virtio_mem_too_many_mb_offline(vm))
1227                         return -ENOSPC;
1228
1229                 rc = virtio_mem_prepare_next_mb(vm, &mb_id);
1230                 if (rc)
1231                         return rc;
1232                 rc = virtio_mem_mb_plug_and_add(vm, mb_id, &nb_sb);
1233                 if (rc)
1234                         return rc;
1235                 cond_resched();
1236         }
1237
1238         return 0;
1239 out_unlock:
1240         mutex_unlock(&vm->hotplug_mutex);
1241         return rc;
1242 }
1243
1244 /*
1245  * Unplug the desired number of plugged subblocks of an offline memory block.
1246  * Will fail if any subblock cannot get unplugged (instead of skipping it).
1247  *
1248  * Will modify the state of the memory block. Might temporarily drop the
1249  * hotplug_mutex.
1250  *
1251  * Note: Can fail after some subblocks were successfully unplugged.
1252  */
1253 static int virtio_mem_mb_unplug_any_sb_offline(struct virtio_mem *vm,
1254                                                unsigned long mb_id,
1255                                                uint64_t *nb_sb)
1256 {
1257         int rc;
1258
1259         rc = virtio_mem_mb_unplug_any_sb(vm, mb_id, nb_sb);
1260
1261         /* some subblocks might have been unplugged even on failure */
1262         if (!virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb))
1263                 virtio_mem_mb_set_state(vm, mb_id,
1264                                         VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL);
1265         if (rc)
1266                 return rc;
1267
1268         if (virtio_mem_mb_test_sb_unplugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1269                 /*
1270                  * Remove the block from Linux - this should never fail.
1271                  * Hinder the block from getting onlined by marking it
1272                  * unplugged. Temporarily drop the mutex, so
1273                  * any pending GOING_ONLINE requests can be serviced/rejected.
1274                  */
1275                 virtio_mem_mb_set_state(vm, mb_id,
1276                                         VIRTIO_MEM_MB_STATE_UNUSED);
1277
1278                 mutex_unlock(&vm->hotplug_mutex);
1279                 rc = virtio_mem_mb_remove(vm, mb_id);
1280                 BUG_ON(rc);
1281                 mutex_lock(&vm->hotplug_mutex);
1282         }
1283         return 0;
1284 }
1285
1286 /*
1287  * Unplug the given plugged subblocks of an online memory block.
1288  *
1289  * Will modify the state of the memory block.
1290  */
1291 static int virtio_mem_mb_unplug_sb_online(struct virtio_mem *vm,
1292                                           unsigned long mb_id, int sb_id,
1293                                           int count)
1294 {
1295         const unsigned long nr_pages = PFN_DOWN(vm->subblock_size) * count;
1296         unsigned long start_pfn;
1297         int rc;
1298
1299         start_pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
1300                              sb_id * vm->subblock_size);
1301         rc = alloc_contig_range(start_pfn, start_pfn + nr_pages,
1302                                 MIGRATE_MOVABLE, GFP_KERNEL);
1303         if (rc == -ENOMEM)
1304                 /* whoops, out of memory */
1305                 return rc;
1306         if (rc)
1307                 return -EBUSY;
1308
1309         /* Mark it as fake-offline before unplugging it */
1310         virtio_mem_set_fake_offline(start_pfn, nr_pages, true);
1311         adjust_managed_page_count(pfn_to_page(start_pfn), -nr_pages);
1312
1313         /* Try to unplug the allocated memory */
1314         rc = virtio_mem_mb_unplug_sb(vm, mb_id, sb_id, count);
1315         if (rc) {
1316                 /* Return the memory to the buddy. */
1317                 virtio_mem_fake_online(start_pfn, nr_pages);
1318                 return rc;
1319         }
1320
1321         virtio_mem_mb_set_state(vm, mb_id,
1322                                 VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL);
1323         return 0;
1324 }
1325
1326 /*
1327  * Unplug the desired number of plugged subblocks of an online memory block.
1328  * Will skip subblock that are busy.
1329  *
1330  * Will modify the state of the memory block. Might temporarily drop the
1331  * hotplug_mutex.
1332  *
1333  * Note: Can fail after some subblocks were successfully unplugged. Can
1334  *       return 0 even if subblocks were busy and could not get unplugged.
1335  */
1336 static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm,
1337                                               unsigned long mb_id,
1338                                               uint64_t *nb_sb)
1339 {
1340         int rc, sb_id;
1341
1342         /* If possible, try to unplug the complete block in one shot. */
1343         if (*nb_sb >= vm->nb_sb_per_mb &&
1344             virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1345                 rc = virtio_mem_mb_unplug_sb_online(vm, mb_id, 0,
1346                                                     vm->nb_sb_per_mb);
1347                 if (!rc) {
1348                         *nb_sb -= vm->nb_sb_per_mb;
1349                         goto unplugged;
1350                 } else if (rc != -EBUSY)
1351                         return rc;
1352         }
1353
1354         /* Fallback to single subblocks. */
1355         for (sb_id = vm->nb_sb_per_mb - 1; sb_id >= 0 && *nb_sb; sb_id--) {
1356                 /* Find the next candidate subblock */
1357                 while (sb_id >= 0 &&
1358                        !virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
1359                         sb_id--;
1360                 if (sb_id < 0)
1361                         break;
1362
1363                 rc = virtio_mem_mb_unplug_sb_online(vm, mb_id, sb_id, 1);
1364                 if (rc == -EBUSY)
1365                         continue;
1366                 else if (rc)
1367                         return rc;
1368                 *nb_sb -= 1;
1369         }
1370
1371 unplugged:
1372         /*
1373          * Once all subblocks of a memory block were unplugged, offline and
1374          * remove it. This will usually not fail, as no memory is in use
1375          * anymore - however some other notifiers might NACK the request.
1376          */
1377         if (virtio_mem_mb_test_sb_unplugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1378                 mutex_unlock(&vm->hotplug_mutex);
1379                 rc = virtio_mem_mb_offline_and_remove(vm, mb_id);
1380                 mutex_lock(&vm->hotplug_mutex);
1381                 if (!rc)
1382                         virtio_mem_mb_set_state(vm, mb_id,
1383                                                 VIRTIO_MEM_MB_STATE_UNUSED);
1384         }
1385
1386         return 0;
1387 }
1388
1389 /*
1390  * Try to unplug the requested amount of memory.
1391  */
1392 static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff)
1393 {
1394         uint64_t nb_sb = diff / vm->subblock_size;
1395         unsigned long mb_id;
1396         int rc;
1397
1398         if (!nb_sb)
1399                 return 0;
1400
1401         /*
1402          * We'll drop the mutex a couple of times when it is safe to do so.
1403          * This might result in some blocks switching the state (online/offline)
1404          * and we could miss them in this run - we will retry again later.
1405          */
1406         mutex_lock(&vm->hotplug_mutex);
1407
1408         /* Try to unplug subblocks of partially plugged offline blocks. */
1409         virtio_mem_for_each_mb_state_rev(vm, mb_id,
1410                                          VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) {
1411                 rc = virtio_mem_mb_unplug_any_sb_offline(vm, mb_id,
1412                                                          &nb_sb);
1413                 if (rc || !nb_sb)
1414                         goto out_unlock;
1415                 cond_resched();
1416         }
1417
1418         /* Try to unplug subblocks of plugged offline blocks. */
1419         virtio_mem_for_each_mb_state_rev(vm, mb_id,
1420                                          VIRTIO_MEM_MB_STATE_OFFLINE) {
1421                 rc = virtio_mem_mb_unplug_any_sb_offline(vm, mb_id,
1422                                                          &nb_sb);
1423                 if (rc || !nb_sb)
1424                         goto out_unlock;
1425                 cond_resched();
1426         }
1427
1428         if (!unplug_online) {
1429                 mutex_unlock(&vm->hotplug_mutex);
1430                 return 0;
1431         }
1432
1433         /* Try to unplug subblocks of partially plugged online blocks. */
1434         virtio_mem_for_each_mb_state_rev(vm, mb_id,
1435                                          VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL) {
1436                 rc = virtio_mem_mb_unplug_any_sb_online(vm, mb_id,
1437                                                         &nb_sb);
1438                 if (rc || !nb_sb)
1439                         goto out_unlock;
1440                 mutex_unlock(&vm->hotplug_mutex);
1441                 cond_resched();
1442                 mutex_lock(&vm->hotplug_mutex);
1443         }
1444
1445         /* Try to unplug subblocks of plugged online blocks. */
1446         virtio_mem_for_each_mb_state_rev(vm, mb_id,
1447                                          VIRTIO_MEM_MB_STATE_ONLINE) {
1448                 rc = virtio_mem_mb_unplug_any_sb_online(vm, mb_id,
1449                                                         &nb_sb);
1450                 if (rc || !nb_sb)
1451                         goto out_unlock;
1452                 mutex_unlock(&vm->hotplug_mutex);
1453                 cond_resched();
1454                 mutex_lock(&vm->hotplug_mutex);
1455         }
1456
1457         mutex_unlock(&vm->hotplug_mutex);
1458         return nb_sb ? -EBUSY : 0;
1459 out_unlock:
1460         mutex_unlock(&vm->hotplug_mutex);
1461         return rc;
1462 }
1463
1464 /*
1465  * Try to unplug all blocks that couldn't be unplugged before, for example,
1466  * because the hypervisor was busy.
1467  */
1468 static int virtio_mem_unplug_pending_mb(struct virtio_mem *vm)
1469 {
1470         unsigned long mb_id;
1471         int rc;
1472
1473         virtio_mem_for_each_mb_state(vm, mb_id, VIRTIO_MEM_MB_STATE_PLUGGED) {
1474                 rc = virtio_mem_mb_unplug(vm, mb_id);
1475                 if (rc)
1476                         return rc;
1477                 virtio_mem_mb_set_state(vm, mb_id, VIRTIO_MEM_MB_STATE_UNUSED);
1478         }
1479
1480         return 0;
1481 }
1482
1483 /*
1484  * Update all parts of the config that could have changed.
1485  */
1486 static void virtio_mem_refresh_config(struct virtio_mem *vm)
1487 {
1488         const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
1489         uint64_t new_plugged_size, usable_region_size, end_addr;
1490
1491         /* the plugged_size is just a reflection of what _we_ did previously */
1492         virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
1493                         &new_plugged_size);
1494         if (WARN_ON_ONCE(new_plugged_size != vm->plugged_size))
1495                 vm->plugged_size = new_plugged_size;
1496
1497         /* calculate the last usable memory block id */
1498         virtio_cread_le(vm->vdev, struct virtio_mem_config,
1499                         usable_region_size, &usable_region_size);
1500         end_addr = vm->addr + usable_region_size;
1501         end_addr = min(end_addr, phys_limit);
1502         vm->last_usable_mb_id = virtio_mem_phys_to_mb_id(end_addr) - 1;
1503
1504         /* see if there is a request to change the size */
1505         virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size,
1506                         &vm->requested_size);
1507
1508         dev_info(&vm->vdev->dev, "plugged size: 0x%llx", vm->plugged_size);
1509         dev_info(&vm->vdev->dev, "requested size: 0x%llx", vm->requested_size);
1510 }
1511
1512 /*
1513  * Workqueue function for handling plug/unplug requests and config updates.
1514  */
1515 static void virtio_mem_run_wq(struct work_struct *work)
1516 {
1517         struct virtio_mem *vm = container_of(work, struct virtio_mem, wq);
1518         uint64_t diff;
1519         int rc;
1520
1521         hrtimer_cancel(&vm->retry_timer);
1522
1523         if (vm->broken)
1524                 return;
1525
1526 retry:
1527         rc = 0;
1528
1529         /* Make sure we start with a clean state if there are leftovers. */
1530         if (unlikely(vm->unplug_all_required))
1531                 rc = virtio_mem_send_unplug_all_request(vm);
1532
1533         if (atomic_read(&vm->config_changed)) {
1534                 atomic_set(&vm->config_changed, 0);
1535                 virtio_mem_refresh_config(vm);
1536         }
1537
1538         /* Unplug any leftovers from previous runs */
1539         if (!rc)
1540                 rc = virtio_mem_unplug_pending_mb(vm);
1541
1542         if (!rc && vm->requested_size != vm->plugged_size) {
1543                 if (vm->requested_size > vm->plugged_size) {
1544                         diff = vm->requested_size - vm->plugged_size;
1545                         rc = virtio_mem_plug_request(vm, diff);
1546                 } else {
1547                         diff = vm->plugged_size - vm->requested_size;
1548                         rc = virtio_mem_unplug_request(vm, diff);
1549                 }
1550         }
1551
1552         switch (rc) {
1553         case 0:
1554                 vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS;
1555                 break;
1556         case -ENOSPC:
1557                 /*
1558                  * We cannot add any more memory (alignment, physical limit)
1559                  * or we have too many offline memory blocks.
1560                  */
1561                 break;
1562         case -ETXTBSY:
1563                 /*
1564                  * The hypervisor cannot process our request right now
1565                  * (e.g., out of memory, migrating);
1566                  */
1567         case -EBUSY:
1568                 /*
1569                  * We cannot free up any memory to unplug it (all plugged memory
1570                  * is busy).
1571                  */
1572         case -ENOMEM:
1573                 /* Out of memory, try again later. */
1574                 hrtimer_start(&vm->retry_timer, ms_to_ktime(vm->retry_timer_ms),
1575                               HRTIMER_MODE_REL);
1576                 break;
1577         case -EAGAIN:
1578                 /* Retry immediately (e.g., the config changed). */
1579                 goto retry;
1580         default:
1581                 /* Unknown error, mark as broken */
1582                 dev_err(&vm->vdev->dev,
1583                         "unknown error, marking device broken: %d\n", rc);
1584                 vm->broken = true;
1585         }
1586 }
1587
1588 static enum hrtimer_restart virtio_mem_timer_expired(struct hrtimer *timer)
1589 {
1590         struct virtio_mem *vm = container_of(timer, struct virtio_mem,
1591                                              retry_timer);
1592
1593         virtio_mem_retry(vm);
1594         vm->retry_timer_ms = min_t(unsigned int, vm->retry_timer_ms * 2,
1595                                    VIRTIO_MEM_RETRY_TIMER_MAX_MS);
1596         return HRTIMER_NORESTART;
1597 }
1598
1599 static void virtio_mem_handle_response(struct virtqueue *vq)
1600 {
1601         struct virtio_mem *vm = vq->vdev->priv;
1602
1603         wake_up(&vm->host_resp);
1604 }
1605
1606 static int virtio_mem_init_vq(struct virtio_mem *vm)
1607 {
1608         struct virtqueue *vq;
1609
1610         vq = virtio_find_single_vq(vm->vdev, virtio_mem_handle_response,
1611                                    "guest-request");
1612         if (IS_ERR(vq))
1613                 return PTR_ERR(vq);
1614         vm->vq = vq;
1615
1616         return 0;
1617 }
1618
1619 static int virtio_mem_init(struct virtio_mem *vm)
1620 {
1621         const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
1622         uint16_t node_id;
1623
1624         if (!vm->vdev->config->get) {
1625                 dev_err(&vm->vdev->dev, "config access disabled\n");
1626                 return -EINVAL;
1627         }
1628
1629         /*
1630          * We don't want to (un)plug or reuse any memory when in kdump. The
1631          * memory is still accessible (but not mapped).
1632          */
1633         if (is_kdump_kernel()) {
1634                 dev_warn(&vm->vdev->dev, "disabled in kdump kernel\n");
1635                 return -EBUSY;
1636         }
1637
1638         /* Fetch all properties that can't change. */
1639         virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
1640                         &vm->plugged_size);
1641         virtio_cread_le(vm->vdev, struct virtio_mem_config, block_size,
1642                         &vm->device_block_size);
1643         virtio_cread_le(vm->vdev, struct virtio_mem_config, node_id,
1644                         &node_id);
1645         vm->nid = virtio_mem_translate_node_id(vm, node_id);
1646         virtio_cread_le(vm->vdev, struct virtio_mem_config, addr, &vm->addr);
1647         virtio_cread_le(vm->vdev, struct virtio_mem_config, region_size,
1648                         &vm->region_size);
1649
1650         /* Determine the nid for the device based on the lowest address. */
1651         if (vm->nid == NUMA_NO_NODE)
1652                 vm->nid = memory_add_physaddr_to_nid(vm->addr);
1653
1654         /*
1655          * We always hotplug memory in memory block granularity. This way,
1656          * we have to wait for exactly one memory block to online.
1657          */
1658         if (vm->device_block_size > memory_block_size_bytes()) {
1659                 dev_err(&vm->vdev->dev,
1660                         "The block size is not supported (too big).\n");
1661                 return -EINVAL;
1662         }
1663
1664         /* bad device setup - warn only */
1665         if (!IS_ALIGNED(vm->addr, memory_block_size_bytes()))
1666                 dev_warn(&vm->vdev->dev,
1667                          "The alignment of the physical start address can make some memory unusable.\n");
1668         if (!IS_ALIGNED(vm->addr + vm->region_size, memory_block_size_bytes()))
1669                 dev_warn(&vm->vdev->dev,
1670                          "The alignment of the physical end address can make some memory unusable.\n");
1671         if (vm->addr + vm->region_size > phys_limit)
1672                 dev_warn(&vm->vdev->dev,
1673                          "Some memory is not addressable. This can make some memory unusable.\n");
1674
1675         /*
1676          * Calculate the subblock size:
1677          * - At least MAX_ORDER - 1 / pageblock_order.
1678          * - At least the device block size.
1679          * In the worst case, a single subblock per memory block.
1680          */
1681         vm->subblock_size = PAGE_SIZE * 1ul << max_t(uint32_t, MAX_ORDER - 1,
1682                                                      pageblock_order);
1683         vm->subblock_size = max_t(uint64_t, vm->device_block_size,
1684                                   vm->subblock_size);
1685         vm->nb_sb_per_mb = memory_block_size_bytes() / vm->subblock_size;
1686
1687         /* Round up to the next full memory block */
1688         vm->first_mb_id = virtio_mem_phys_to_mb_id(vm->addr - 1 +
1689                                                    memory_block_size_bytes());
1690         vm->next_mb_id = vm->first_mb_id;
1691         vm->last_mb_id = virtio_mem_phys_to_mb_id(vm->addr +
1692                          vm->region_size) - 1;
1693
1694         dev_info(&vm->vdev->dev, "start address: 0x%llx", vm->addr);
1695         dev_info(&vm->vdev->dev, "region size: 0x%llx", vm->region_size);
1696         dev_info(&vm->vdev->dev, "device block size: 0x%llx",
1697                  (unsigned long long)vm->device_block_size);
1698         dev_info(&vm->vdev->dev, "memory block size: 0x%lx",
1699                  memory_block_size_bytes());
1700         dev_info(&vm->vdev->dev, "subblock size: 0x%llx",
1701                  (unsigned long long)vm->subblock_size);
1702         if (vm->nid != NUMA_NO_NODE && IS_ENABLED(CONFIG_NUMA))
1703                 dev_info(&vm->vdev->dev, "nid: %d", vm->nid);
1704
1705         return 0;
1706 }
1707
1708 static int virtio_mem_create_resource(struct virtio_mem *vm)
1709 {
1710         /*
1711          * When force-unloading the driver and removing the device, we
1712          * could have a garbage pointer. Duplicate the string.
1713          */
1714         const char *name = kstrdup(dev_name(&vm->vdev->dev), GFP_KERNEL);
1715
1716         if (!name)
1717                 return -ENOMEM;
1718
1719         vm->parent_resource = __request_mem_region(vm->addr, vm->region_size,
1720                                                    name, IORESOURCE_SYSTEM_RAM);
1721         if (!vm->parent_resource) {
1722                 kfree(name);
1723                 dev_warn(&vm->vdev->dev, "could not reserve device region\n");
1724                 dev_info(&vm->vdev->dev,
1725                          "reloading the driver is not supported\n");
1726                 return -EBUSY;
1727         }
1728
1729         /* The memory is not actually busy - make add_memory() work. */
1730         vm->parent_resource->flags &= ~IORESOURCE_BUSY;
1731         return 0;
1732 }
1733
1734 static void virtio_mem_delete_resource(struct virtio_mem *vm)
1735 {
1736         const char *name;
1737
1738         if (!vm->parent_resource)
1739                 return;
1740
1741         name = vm->parent_resource->name;
1742         release_resource(vm->parent_resource);
1743         kfree(vm->parent_resource);
1744         kfree(name);
1745         vm->parent_resource = NULL;
1746 }
1747
1748 static int virtio_mem_probe(struct virtio_device *vdev)
1749 {
1750         struct virtio_mem *vm;
1751         int rc;
1752
1753         BUILD_BUG_ON(sizeof(struct virtio_mem_req) != 24);
1754         BUILD_BUG_ON(sizeof(struct virtio_mem_resp) != 10);
1755
1756         vdev->priv = vm = kzalloc(sizeof(*vm), GFP_KERNEL);
1757         if (!vm)
1758                 return -ENOMEM;
1759
1760         init_waitqueue_head(&vm->host_resp);
1761         vm->vdev = vdev;
1762         INIT_WORK(&vm->wq, virtio_mem_run_wq);
1763         mutex_init(&vm->hotplug_mutex);
1764         INIT_LIST_HEAD(&vm->next);
1765         spin_lock_init(&vm->removal_lock);
1766         hrtimer_init(&vm->retry_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1767         vm->retry_timer.function = virtio_mem_timer_expired;
1768         vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS;
1769
1770         /* register the virtqueue */
1771         rc = virtio_mem_init_vq(vm);
1772         if (rc)
1773                 goto out_free_vm;
1774
1775         /* initialize the device by querying the config */
1776         rc = virtio_mem_init(vm);
1777         if (rc)
1778                 goto out_del_vq;
1779
1780         /* create the parent resource for all memory */
1781         rc = virtio_mem_create_resource(vm);
1782         if (rc)
1783                 goto out_del_vq;
1784
1785         /*
1786          * If we still have memory plugged, we have to unplug all memory first.
1787          * Registering our parent resource makes sure that this memory isn't
1788          * actually in use (e.g., trying to reload the driver).
1789          */
1790         if (vm->plugged_size) {
1791                 vm->unplug_all_required = 1;
1792                 dev_info(&vm->vdev->dev, "unplugging all memory is required\n");
1793         }
1794
1795         /* register callbacks */
1796         vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb;
1797         rc = register_memory_notifier(&vm->memory_notifier);
1798         if (rc)
1799                 goto out_del_resource;
1800         rc = register_virtio_mem_device(vm);
1801         if (rc)
1802                 goto out_unreg_mem;
1803
1804         virtio_device_ready(vdev);
1805
1806         /* trigger a config update to start processing the requested_size */
1807         atomic_set(&vm->config_changed, 1);
1808         queue_work(system_freezable_wq, &vm->wq);
1809
1810         return 0;
1811 out_unreg_mem:
1812         unregister_memory_notifier(&vm->memory_notifier);
1813 out_del_resource:
1814         virtio_mem_delete_resource(vm);
1815 out_del_vq:
1816         vdev->config->del_vqs(vdev);
1817 out_free_vm:
1818         kfree(vm);
1819         vdev->priv = NULL;
1820
1821         return rc;
1822 }
1823
1824 static void virtio_mem_remove(struct virtio_device *vdev)
1825 {
1826         struct virtio_mem *vm = vdev->priv;
1827         unsigned long mb_id;
1828         int rc;
1829
1830         /*
1831          * Make sure the workqueue won't be triggered anymore and no memory
1832          * blocks can be onlined/offlined until we're finished here.
1833          */
1834         mutex_lock(&vm->hotplug_mutex);
1835         spin_lock_irq(&vm->removal_lock);
1836         vm->removing = true;
1837         spin_unlock_irq(&vm->removal_lock);
1838         mutex_unlock(&vm->hotplug_mutex);
1839
1840         /* wait until the workqueue stopped */
1841         cancel_work_sync(&vm->wq);
1842         hrtimer_cancel(&vm->retry_timer);
1843
1844         /*
1845          * After we unregistered our callbacks, user space can online partially
1846          * plugged offline blocks. Make sure to remove them.
1847          */
1848         virtio_mem_for_each_mb_state(vm, mb_id,
1849                                      VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) {
1850                 rc = virtio_mem_mb_remove(vm, mb_id);
1851                 BUG_ON(rc);
1852                 virtio_mem_mb_set_state(vm, mb_id, VIRTIO_MEM_MB_STATE_UNUSED);
1853         }
1854         /*
1855          * After we unregistered our callbacks, user space can no longer
1856          * offline partially plugged online memory blocks. No need to worry
1857          * about them.
1858          */
1859
1860         /* unregister callbacks */
1861         unregister_virtio_mem_device(vm);
1862         unregister_memory_notifier(&vm->memory_notifier);
1863
1864         /*
1865          * There is no way we could reliably remove all memory we have added to
1866          * the system. And there is no way to stop the driver/device from going
1867          * away. Warn at least.
1868          */
1869         if (vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE] ||
1870             vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL] ||
1871             vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE] ||
1872             vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL]) {
1873                 dev_warn(&vdev->dev, "device still has system memory added\n");
1874         } else {
1875                 virtio_mem_delete_resource(vm);
1876                 kfree_const(vm->resource_name);
1877         }
1878
1879         /* remove all tracking data - no locking needed */
1880         vfree(vm->mb_state);
1881         vfree(vm->sb_bitmap);
1882
1883         /* reset the device and cleanup the queues */
1884         vdev->config->reset(vdev);
1885         vdev->config->del_vqs(vdev);
1886
1887         kfree(vm);
1888         vdev->priv = NULL;
1889 }
1890
1891 static void virtio_mem_config_changed(struct virtio_device *vdev)
1892 {
1893         struct virtio_mem *vm = vdev->priv;
1894
1895         atomic_set(&vm->config_changed, 1);
1896         virtio_mem_retry(vm);
1897 }
1898
1899 #ifdef CONFIG_PM_SLEEP
1900 static int virtio_mem_freeze(struct virtio_device *vdev)
1901 {
1902         /*
1903          * When restarting the VM, all memory is usually unplugged. Don't
1904          * allow to suspend/hibernate.
1905          */
1906         dev_err(&vdev->dev, "save/restore not supported.\n");
1907         return -EPERM;
1908 }
1909
1910 static int virtio_mem_restore(struct virtio_device *vdev)
1911 {
1912         return -EPERM;
1913 }
1914 #endif
1915
1916 static unsigned int virtio_mem_features[] = {
1917 #if defined(CONFIG_NUMA) && defined(CONFIG_ACPI_NUMA)
1918         VIRTIO_MEM_F_ACPI_PXM,
1919 #endif
1920 };
1921
1922 static const struct virtio_device_id virtio_mem_id_table[] = {
1923         { VIRTIO_ID_MEM, VIRTIO_DEV_ANY_ID },
1924         { 0 },
1925 };
1926
1927 static struct virtio_driver virtio_mem_driver = {
1928         .feature_table = virtio_mem_features,
1929         .feature_table_size = ARRAY_SIZE(virtio_mem_features),
1930         .driver.name = KBUILD_MODNAME,
1931         .driver.owner = THIS_MODULE,
1932         .id_table = virtio_mem_id_table,
1933         .probe = virtio_mem_probe,
1934         .remove = virtio_mem_remove,
1935         .config_changed = virtio_mem_config_changed,
1936 #ifdef CONFIG_PM_SLEEP
1937         .freeze =       virtio_mem_freeze,
1938         .restore =      virtio_mem_restore,
1939 #endif
1940 };
1941
1942 module_virtio_driver(virtio_mem_driver);
1943 MODULE_DEVICE_TABLE(virtio, virtio_mem_id_table);
1944 MODULE_AUTHOR("David Hildenbrand <david@redhat.com>");
1945 MODULE_DESCRIPTION("Virtio-mem driver");
1946 MODULE_LICENSE("GPL");