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