Loading...
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
26static bool unplug_online = true;
27module_param(unplug_online, bool, 0644);
28MODULE_PARM_DESC(unplug_online, "Try to unplug online memory");
29
30enum 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 (!ZONE_MOVABLE). */
40 VIRTIO_MEM_MB_STATE_ONLINE,
41 /* Partially plugged, fully added to Linux, online (!ZONE_MOVABLE). */
42 VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL,
43 /*
44 * Fully plugged, fully added to Linux, online (ZONE_MOVABLE).
45 * We are not allowed to allocate (unplug) parts of this block that
46 * are not movable (similar to gigantic pages). We will never allow
47 * to online OFFLINE_PARTIAL to ZONE_MOVABLE (as they would contain
48 * unmovable parts).
49 */
50 VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE,
51 VIRTIO_MEM_MB_STATE_COUNT
52};
53
54struct virtio_mem {
55 struct virtio_device *vdev;
56
57 /* We might first have to unplug all memory when starting up. */
58 bool unplug_all_required;
59
60 /* Workqueue that processes the plug/unplug requests. */
61 struct work_struct wq;
62 atomic_t config_changed;
63
64 /* Virtqueue for guest->host requests. */
65 struct virtqueue *vq;
66
67 /* Wait for a host response to a guest request. */
68 wait_queue_head_t host_resp;
69
70 /* Space for one guest request and the host response. */
71 struct virtio_mem_req req;
72 struct virtio_mem_resp resp;
73
74 /* The current size of the device. */
75 uint64_t plugged_size;
76 /* The requested size of the device. */
77 uint64_t requested_size;
78
79 /* The device block size (for communicating with the device). */
80 uint64_t device_block_size;
81 /* The translated node id. NUMA_NO_NODE in case not specified. */
82 int nid;
83 /* Physical start address of the memory region. */
84 uint64_t addr;
85 /* Maximum region size in bytes. */
86 uint64_t region_size;
87
88 /* The subblock size. */
89 uint64_t subblock_size;
90 /* The number of subblocks per memory block. */
91 uint32_t nb_sb_per_mb;
92
93 /* Id of the first memory block of this device. */
94 unsigned long first_mb_id;
95 /* Id of the last memory block of this device. */
96 unsigned long last_mb_id;
97 /* Id of the last usable memory block of this device. */
98 unsigned long last_usable_mb_id;
99 /* Id of the next memory bock to prepare when needed. */
100 unsigned long next_mb_id;
101
102 /* The parent resource for all memory added via this device. */
103 struct resource *parent_resource;
104 /*
105 * Copy of "System RAM (virtio_mem)" to be used for
106 * add_memory_driver_managed().
107 */
108 const char *resource_name;
109
110 /* Summary of all memory block states. */
111 unsigned long nb_mb_state[VIRTIO_MEM_MB_STATE_COUNT];
112#define VIRTIO_MEM_NB_OFFLINE_THRESHOLD 10
113
114 /*
115 * One byte state per memory block.
116 *
117 * Allocated via vmalloc(). When preparing new blocks, resized
118 * (alloc+copy+free) when needed (crossing pages with the next mb).
119 * (when crossing pages).
120 *
121 * With 128MB memory blocks, we have states for 512GB of memory in one
122 * page.
123 */
124 uint8_t *mb_state;
125
126 /*
127 * $nb_sb_per_mb bit per memory block. Handled similar to mb_state.
128 *
129 * With 4MB subblocks, we manage 128GB of memory in one page.
130 */
131 unsigned long *sb_bitmap;
132
133 /*
134 * Mutex that protects the nb_mb_state, mb_state, and sb_bitmap.
135 *
136 * When this lock is held the pointers can't change, ONLINE and
137 * OFFLINE blocks can't change the state and no subblocks will get
138 * plugged/unplugged.
139 */
140 struct mutex hotplug_mutex;
141 bool hotplug_active;
142
143 /* An error occurred we cannot handle - stop processing requests. */
144 bool broken;
145
146 /* The driver is being removed. */
147 spinlock_t removal_lock;
148 bool removing;
149
150 /* Timer for retrying to plug/unplug memory. */
151 struct hrtimer retry_timer;
152 unsigned int retry_timer_ms;
153#define VIRTIO_MEM_RETRY_TIMER_MIN_MS 50000
154#define VIRTIO_MEM_RETRY_TIMER_MAX_MS 300000
155
156 /* Memory notifier (online/offline events). */
157 struct notifier_block memory_notifier;
158
159 /* Next device in the list of virtio-mem devices. */
160 struct list_head next;
161};
162
163/*
164 * We have to share a single online_page callback among all virtio-mem
165 * devices. We use RCU to iterate the list in the callback.
166 */
167static DEFINE_MUTEX(virtio_mem_mutex);
168static LIST_HEAD(virtio_mem_devices);
169
170static void virtio_mem_online_page_cb(struct page *page, unsigned int order);
171
172/*
173 * Register a virtio-mem device so it will be considered for the online_page
174 * callback.
175 */
176static int register_virtio_mem_device(struct virtio_mem *vm)
177{
178 int rc = 0;
179
180 /* First device registers the callback. */
181 mutex_lock(&virtio_mem_mutex);
182 if (list_empty(&virtio_mem_devices))
183 rc = set_online_page_callback(&virtio_mem_online_page_cb);
184 if (!rc)
185 list_add_rcu(&vm->next, &virtio_mem_devices);
186 mutex_unlock(&virtio_mem_mutex);
187
188 return rc;
189}
190
191/*
192 * Unregister a virtio-mem device so it will no longer be considered for the
193 * online_page callback.
194 */
195static void unregister_virtio_mem_device(struct virtio_mem *vm)
196{
197 /* Last device unregisters the callback. */
198 mutex_lock(&virtio_mem_mutex);
199 list_del_rcu(&vm->next);
200 if (list_empty(&virtio_mem_devices))
201 restore_online_page_callback(&virtio_mem_online_page_cb);
202 mutex_unlock(&virtio_mem_mutex);
203
204 synchronize_rcu();
205}
206
207/*
208 * Calculate the memory block id of a given address.
209 */
210static unsigned long virtio_mem_phys_to_mb_id(unsigned long addr)
211{
212 return addr / memory_block_size_bytes();
213}
214
215/*
216 * Calculate the physical start address of a given memory block id.
217 */
218static unsigned long virtio_mem_mb_id_to_phys(unsigned long mb_id)
219{
220 return mb_id * memory_block_size_bytes();
221}
222
223/*
224 * Calculate the subblock id of a given address.
225 */
226static unsigned long virtio_mem_phys_to_sb_id(struct virtio_mem *vm,
227 unsigned long addr)
228{
229 const unsigned long mb_id = virtio_mem_phys_to_mb_id(addr);
230 const unsigned long mb_addr = virtio_mem_mb_id_to_phys(mb_id);
231
232 return (addr - mb_addr) / vm->subblock_size;
233}
234
235/*
236 * Set the state of a memory block, taking care of the state counter.
237 */
238static void virtio_mem_mb_set_state(struct virtio_mem *vm, unsigned long mb_id,
239 enum virtio_mem_mb_state state)
240{
241 const unsigned long idx = mb_id - vm->first_mb_id;
242 enum virtio_mem_mb_state old_state;
243
244 old_state = vm->mb_state[idx];
245 vm->mb_state[idx] = state;
246
247 BUG_ON(vm->nb_mb_state[old_state] == 0);
248 vm->nb_mb_state[old_state]--;
249 vm->nb_mb_state[state]++;
250}
251
252/*
253 * Get the state of a memory block.
254 */
255static enum virtio_mem_mb_state virtio_mem_mb_get_state(struct virtio_mem *vm,
256 unsigned long mb_id)
257{
258 const unsigned long idx = mb_id - vm->first_mb_id;
259
260 return vm->mb_state[idx];
261}
262
263/*
264 * Prepare the state array for the next memory block.
265 */
266static int virtio_mem_mb_state_prepare_next_mb(struct virtio_mem *vm)
267{
268 unsigned long old_bytes = vm->next_mb_id - vm->first_mb_id + 1;
269 unsigned long new_bytes = vm->next_mb_id - vm->first_mb_id + 2;
270 int old_pages = PFN_UP(old_bytes);
271 int new_pages = PFN_UP(new_bytes);
272 uint8_t *new_mb_state;
273
274 if (vm->mb_state && old_pages == new_pages)
275 return 0;
276
277 new_mb_state = vzalloc(new_pages * PAGE_SIZE);
278 if (!new_mb_state)
279 return -ENOMEM;
280
281 mutex_lock(&vm->hotplug_mutex);
282 if (vm->mb_state)
283 memcpy(new_mb_state, vm->mb_state, old_pages * PAGE_SIZE);
284 vfree(vm->mb_state);
285 vm->mb_state = new_mb_state;
286 mutex_unlock(&vm->hotplug_mutex);
287
288 return 0;
289}
290
291#define virtio_mem_for_each_mb_state(_vm, _mb_id, _state) \
292 for (_mb_id = _vm->first_mb_id; \
293 _mb_id < _vm->next_mb_id && _vm->nb_mb_state[_state]; \
294 _mb_id++) \
295 if (virtio_mem_mb_get_state(_vm, _mb_id) == _state)
296
297#define virtio_mem_for_each_mb_state_rev(_vm, _mb_id, _state) \
298 for (_mb_id = _vm->next_mb_id - 1; \
299 _mb_id >= _vm->first_mb_id && _vm->nb_mb_state[_state]; \
300 _mb_id--) \
301 if (virtio_mem_mb_get_state(_vm, _mb_id) == _state)
302
303/*
304 * Mark all selected subblocks plugged.
305 *
306 * Will not modify the state of the memory block.
307 */
308static void virtio_mem_mb_set_sb_plugged(struct virtio_mem *vm,
309 unsigned long mb_id, int sb_id,
310 int count)
311{
312 const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id;
313
314 __bitmap_set(vm->sb_bitmap, bit, count);
315}
316
317/*
318 * Mark all selected subblocks unplugged.
319 *
320 * Will not modify the state of the memory block.
321 */
322static void virtio_mem_mb_set_sb_unplugged(struct virtio_mem *vm,
323 unsigned long mb_id, int sb_id,
324 int count)
325{
326 const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id;
327
328 __bitmap_clear(vm->sb_bitmap, bit, count);
329}
330
331/*
332 * Test if all selected subblocks are plugged.
333 */
334static bool virtio_mem_mb_test_sb_plugged(struct virtio_mem *vm,
335 unsigned long mb_id, int sb_id,
336 int count)
337{
338 const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id;
339
340 if (count == 1)
341 return test_bit(bit, vm->sb_bitmap);
342
343 /* TODO: Helper similar to bitmap_set() */
344 return find_next_zero_bit(vm->sb_bitmap, bit + count, bit) >=
345 bit + count;
346}
347
348/*
349 * Test if all selected subblocks are unplugged.
350 */
351static bool virtio_mem_mb_test_sb_unplugged(struct virtio_mem *vm,
352 unsigned long mb_id, int sb_id,
353 int count)
354{
355 const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id;
356
357 /* TODO: Helper similar to bitmap_set() */
358 return find_next_bit(vm->sb_bitmap, bit + count, bit) >= bit + count;
359}
360
361/*
362 * Find the first unplugged subblock. Returns vm->nb_sb_per_mb in case there is
363 * none.
364 */
365static int virtio_mem_mb_first_unplugged_sb(struct virtio_mem *vm,
366 unsigned long mb_id)
367{
368 const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb;
369
370 return find_next_zero_bit(vm->sb_bitmap, bit + vm->nb_sb_per_mb, bit) -
371 bit;
372}
373
374/*
375 * Prepare the subblock bitmap for the next memory block.
376 */
377static int virtio_mem_sb_bitmap_prepare_next_mb(struct virtio_mem *vm)
378{
379 const unsigned long old_nb_mb = vm->next_mb_id - vm->first_mb_id;
380 const unsigned long old_nb_bits = old_nb_mb * vm->nb_sb_per_mb;
381 const unsigned long new_nb_bits = (old_nb_mb + 1) * vm->nb_sb_per_mb;
382 int old_pages = PFN_UP(BITS_TO_LONGS(old_nb_bits) * sizeof(long));
383 int new_pages = PFN_UP(BITS_TO_LONGS(new_nb_bits) * sizeof(long));
384 unsigned long *new_sb_bitmap, *old_sb_bitmap;
385
386 if (vm->sb_bitmap && old_pages == new_pages)
387 return 0;
388
389 new_sb_bitmap = vzalloc(new_pages * PAGE_SIZE);
390 if (!new_sb_bitmap)
391 return -ENOMEM;
392
393 mutex_lock(&vm->hotplug_mutex);
394 if (new_sb_bitmap)
395 memcpy(new_sb_bitmap, vm->sb_bitmap, old_pages * PAGE_SIZE);
396
397 old_sb_bitmap = vm->sb_bitmap;
398 vm->sb_bitmap = new_sb_bitmap;
399 mutex_unlock(&vm->hotplug_mutex);
400
401 vfree(old_sb_bitmap);
402 return 0;
403}
404
405/*
406 * Try to add a memory block to Linux. This will usually only fail
407 * if out of memory.
408 *
409 * Must not be called with the vm->hotplug_mutex held (possible deadlock with
410 * onlining code).
411 *
412 * Will not modify the state of the memory block.
413 */
414static int virtio_mem_mb_add(struct virtio_mem *vm, unsigned long mb_id)
415{
416 const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
417 int nid = vm->nid;
418
419 if (nid == NUMA_NO_NODE)
420 nid = memory_add_physaddr_to_nid(addr);
421
422 /*
423 * When force-unloading the driver and we still have memory added to
424 * Linux, the resource name has to stay.
425 */
426 if (!vm->resource_name) {
427 vm->resource_name = kstrdup_const("System RAM (virtio_mem)",
428 GFP_KERNEL);
429 if (!vm->resource_name)
430 return -ENOMEM;
431 }
432
433 dev_dbg(&vm->vdev->dev, "adding memory block: %lu\n", mb_id);
434 return add_memory_driver_managed(nid, addr, memory_block_size_bytes(),
435 vm->resource_name);
436}
437
438/*
439 * Try to remove a memory block from Linux. Will only fail if the memory block
440 * is not offline.
441 *
442 * Must not be called with the vm->hotplug_mutex held (possible deadlock with
443 * onlining code).
444 *
445 * Will not modify the state of the memory block.
446 */
447static int virtio_mem_mb_remove(struct virtio_mem *vm, unsigned long mb_id)
448{
449 const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
450 int nid = vm->nid;
451
452 if (nid == NUMA_NO_NODE)
453 nid = memory_add_physaddr_to_nid(addr);
454
455 dev_dbg(&vm->vdev->dev, "removing memory block: %lu\n", mb_id);
456 return remove_memory(nid, addr, memory_block_size_bytes());
457}
458
459/*
460 * Try to offline and remove a memory block from Linux.
461 *
462 * Must not be called with the vm->hotplug_mutex held (possible deadlock with
463 * onlining code).
464 *
465 * Will not modify the state of the memory block.
466 */
467static int virtio_mem_mb_offline_and_remove(struct virtio_mem *vm,
468 unsigned long mb_id)
469{
470 const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
471 int nid = vm->nid;
472
473 if (nid == NUMA_NO_NODE)
474 nid = memory_add_physaddr_to_nid(addr);
475
476 dev_dbg(&vm->vdev->dev, "offlining and removing memory block: %lu\n",
477 mb_id);
478 return offline_and_remove_memory(nid, addr, memory_block_size_bytes());
479}
480
481/*
482 * Trigger the workqueue so the device can perform its magic.
483 */
484static void virtio_mem_retry(struct virtio_mem *vm)
485{
486 unsigned long flags;
487
488 spin_lock_irqsave(&vm->removal_lock, flags);
489 if (!vm->removing)
490 queue_work(system_freezable_wq, &vm->wq);
491 spin_unlock_irqrestore(&vm->removal_lock, flags);
492}
493
494static int virtio_mem_translate_node_id(struct virtio_mem *vm, uint16_t node_id)
495{
496 int node = NUMA_NO_NODE;
497
498#if defined(CONFIG_ACPI_NUMA)
499 if (virtio_has_feature(vm->vdev, VIRTIO_MEM_F_ACPI_PXM))
500 node = pxm_to_node(node_id);
501#endif
502 return node;
503}
504
505/*
506 * Test if a virtio-mem device overlaps with the given range. Can be called
507 * from (notifier) callbacks lockless.
508 */
509static bool virtio_mem_overlaps_range(struct virtio_mem *vm,
510 unsigned long start, unsigned long size)
511{
512 unsigned long dev_start = virtio_mem_mb_id_to_phys(vm->first_mb_id);
513 unsigned long dev_end = virtio_mem_mb_id_to_phys(vm->last_mb_id) +
514 memory_block_size_bytes();
515
516 return start < dev_end && dev_start < start + size;
517}
518
519/*
520 * Test if a virtio-mem device owns a memory block. Can be called from
521 * (notifier) callbacks lockless.
522 */
523static bool virtio_mem_owned_mb(struct virtio_mem *vm, unsigned long mb_id)
524{
525 return mb_id >= vm->first_mb_id && mb_id <= vm->last_mb_id;
526}
527
528static int virtio_mem_notify_going_online(struct virtio_mem *vm,
529 unsigned long mb_id,
530 enum zone_type zone)
531{
532 switch (virtio_mem_mb_get_state(vm, mb_id)) {
533 case VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL:
534 /*
535 * We won't allow to online a partially plugged memory block
536 * to the MOVABLE zone - it would contain unmovable parts.
537 */
538 if (zone == ZONE_MOVABLE) {
539 dev_warn_ratelimited(&vm->vdev->dev,
540 "memory block has holes, MOVABLE not supported\n");
541 return NOTIFY_BAD;
542 }
543 return NOTIFY_OK;
544 case VIRTIO_MEM_MB_STATE_OFFLINE:
545 return NOTIFY_OK;
546 default:
547 break;
548 }
549 dev_warn_ratelimited(&vm->vdev->dev,
550 "memory block onlining denied\n");
551 return NOTIFY_BAD;
552}
553
554static void virtio_mem_notify_offline(struct virtio_mem *vm,
555 unsigned long mb_id)
556{
557 switch (virtio_mem_mb_get_state(vm, mb_id)) {
558 case VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL:
559 virtio_mem_mb_set_state(vm, mb_id,
560 VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL);
561 break;
562 case VIRTIO_MEM_MB_STATE_ONLINE:
563 case VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE:
564 virtio_mem_mb_set_state(vm, mb_id,
565 VIRTIO_MEM_MB_STATE_OFFLINE);
566 break;
567 default:
568 BUG();
569 break;
570 }
571
572 /*
573 * Trigger the workqueue, maybe we can now unplug memory. Also,
574 * when we offline and remove a memory block, this will re-trigger
575 * us immediately - which is often nice because the removal of
576 * the memory block (e.g., memmap) might have freed up memory
577 * on other memory blocks we manage.
578 */
579 virtio_mem_retry(vm);
580}
581
582static void virtio_mem_notify_online(struct virtio_mem *vm, unsigned long mb_id,
583 enum zone_type zone)
584{
585 unsigned long nb_offline;
586
587 switch (virtio_mem_mb_get_state(vm, mb_id)) {
588 case VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL:
589 BUG_ON(zone == ZONE_MOVABLE);
590 virtio_mem_mb_set_state(vm, mb_id,
591 VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL);
592 break;
593 case VIRTIO_MEM_MB_STATE_OFFLINE:
594 if (zone == ZONE_MOVABLE)
595 virtio_mem_mb_set_state(vm, mb_id,
596 VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE);
597 else
598 virtio_mem_mb_set_state(vm, mb_id,
599 VIRTIO_MEM_MB_STATE_ONLINE);
600 break;
601 default:
602 BUG();
603 break;
604 }
605 nb_offline = vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE] +
606 vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL];
607
608 /* see if we can add new blocks now that we onlined one block */
609 if (nb_offline == VIRTIO_MEM_NB_OFFLINE_THRESHOLD - 1)
610 virtio_mem_retry(vm);
611}
612
613static void virtio_mem_notify_going_offline(struct virtio_mem *vm,
614 unsigned long mb_id)
615{
616 const unsigned long nr_pages = PFN_DOWN(vm->subblock_size);
617 struct page *page;
618 unsigned long pfn;
619 int sb_id, i;
620
621 for (sb_id = 0; sb_id < vm->nb_sb_per_mb; sb_id++) {
622 if (virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
623 continue;
624 /*
625 * Drop our reference to the pages so the memory can get
626 * offlined and add the unplugged pages to the managed
627 * page counters (so offlining code can correctly subtract
628 * them again).
629 */
630 pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
631 sb_id * vm->subblock_size);
632 adjust_managed_page_count(pfn_to_page(pfn), nr_pages);
633 for (i = 0; i < nr_pages; i++) {
634 page = pfn_to_page(pfn + i);
635 if (WARN_ON(!page_ref_dec_and_test(page)))
636 dump_page(page, "unplugged page referenced");
637 }
638 }
639}
640
641static void virtio_mem_notify_cancel_offline(struct virtio_mem *vm,
642 unsigned long mb_id)
643{
644 const unsigned long nr_pages = PFN_DOWN(vm->subblock_size);
645 unsigned long pfn;
646 int sb_id, i;
647
648 for (sb_id = 0; sb_id < vm->nb_sb_per_mb; sb_id++) {
649 if (virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
650 continue;
651 /*
652 * Get the reference we dropped when going offline and
653 * subtract the unplugged pages from the managed page
654 * counters.
655 */
656 pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
657 sb_id * vm->subblock_size);
658 adjust_managed_page_count(pfn_to_page(pfn), -nr_pages);
659 for (i = 0; i < nr_pages; i++)
660 page_ref_inc(pfn_to_page(pfn + i));
661 }
662}
663
664/*
665 * This callback will either be called synchronously from add_memory() or
666 * asynchronously (e.g., triggered via user space). We have to be careful
667 * with locking when calling add_memory().
668 */
669static int virtio_mem_memory_notifier_cb(struct notifier_block *nb,
670 unsigned long action, void *arg)
671{
672 struct virtio_mem *vm = container_of(nb, struct virtio_mem,
673 memory_notifier);
674 struct memory_notify *mhp = arg;
675 const unsigned long start = PFN_PHYS(mhp->start_pfn);
676 const unsigned long size = PFN_PHYS(mhp->nr_pages);
677 const unsigned long mb_id = virtio_mem_phys_to_mb_id(start);
678 enum zone_type zone;
679 int rc = NOTIFY_OK;
680
681 if (!virtio_mem_overlaps_range(vm, start, size))
682 return NOTIFY_DONE;
683
684 /*
685 * Memory is onlined/offlined in memory block granularity. We cannot
686 * cross virtio-mem device boundaries and memory block boundaries. Bail
687 * out if this ever changes.
688 */
689 if (WARN_ON_ONCE(size != memory_block_size_bytes() ||
690 !IS_ALIGNED(start, memory_block_size_bytes())))
691 return NOTIFY_BAD;
692
693 /*
694 * Avoid circular locking lockdep warnings. We lock the mutex
695 * e.g., in MEM_GOING_ONLINE and unlock it in MEM_ONLINE. The
696 * blocking_notifier_call_chain() has it's own lock, which gets unlocked
697 * between both notifier calls and will bail out. False positive.
698 */
699 lockdep_off();
700
701 switch (action) {
702 case MEM_GOING_OFFLINE:
703 mutex_lock(&vm->hotplug_mutex);
704 if (vm->removing) {
705 rc = notifier_from_errno(-EBUSY);
706 mutex_unlock(&vm->hotplug_mutex);
707 break;
708 }
709 vm->hotplug_active = true;
710 virtio_mem_notify_going_offline(vm, mb_id);
711 break;
712 case MEM_GOING_ONLINE:
713 mutex_lock(&vm->hotplug_mutex);
714 if (vm->removing) {
715 rc = notifier_from_errno(-EBUSY);
716 mutex_unlock(&vm->hotplug_mutex);
717 break;
718 }
719 vm->hotplug_active = true;
720 zone = page_zonenum(pfn_to_page(mhp->start_pfn));
721 rc = virtio_mem_notify_going_online(vm, mb_id, zone);
722 break;
723 case MEM_OFFLINE:
724 virtio_mem_notify_offline(vm, mb_id);
725 vm->hotplug_active = false;
726 mutex_unlock(&vm->hotplug_mutex);
727 break;
728 case MEM_ONLINE:
729 zone = page_zonenum(pfn_to_page(mhp->start_pfn));
730 virtio_mem_notify_online(vm, mb_id, zone);
731 vm->hotplug_active = false;
732 mutex_unlock(&vm->hotplug_mutex);
733 break;
734 case MEM_CANCEL_OFFLINE:
735 if (!vm->hotplug_active)
736 break;
737 virtio_mem_notify_cancel_offline(vm, mb_id);
738 vm->hotplug_active = false;
739 mutex_unlock(&vm->hotplug_mutex);
740 break;
741 case MEM_CANCEL_ONLINE:
742 if (!vm->hotplug_active)
743 break;
744 vm->hotplug_active = false;
745 mutex_unlock(&vm->hotplug_mutex);
746 break;
747 default:
748 break;
749 }
750
751 lockdep_on();
752
753 return rc;
754}
755
756/*
757 * Set a range of pages PG_offline. Remember pages that were never onlined
758 * (via generic_online_page()) using PageDirty().
759 */
760static void virtio_mem_set_fake_offline(unsigned long pfn,
761 unsigned int nr_pages, bool onlined)
762{
763 for (; nr_pages--; pfn++) {
764 struct page *page = pfn_to_page(pfn);
765
766 __SetPageOffline(page);
767 if (!onlined) {
768 SetPageDirty(page);
769 /* FIXME: remove after cleanups */
770 ClearPageReserved(page);
771 }
772 }
773}
774
775/*
776 * Clear PG_offline from a range of pages. If the pages were never onlined,
777 * (via generic_online_page()), clear PageDirty().
778 */
779static void virtio_mem_clear_fake_offline(unsigned long pfn,
780 unsigned int nr_pages, bool onlined)
781{
782 for (; nr_pages--; pfn++) {
783 struct page *page = pfn_to_page(pfn);
784
785 __ClearPageOffline(page);
786 if (!onlined)
787 ClearPageDirty(page);
788 }
789}
790
791/*
792 * Release a range of fake-offline pages to the buddy, effectively
793 * fake-onlining them.
794 */
795static void virtio_mem_fake_online(unsigned long pfn, unsigned int nr_pages)
796{
797 const int order = MAX_ORDER - 1;
798 int i;
799
800 /*
801 * We are always called with subblock granularity, which is at least
802 * aligned to MAX_ORDER - 1.
803 */
804 for (i = 0; i < nr_pages; i += 1 << order) {
805 struct page *page = pfn_to_page(pfn + i);
806
807 /*
808 * If the page is PageDirty(), it was kept fake-offline when
809 * onlining the memory block. Otherwise, it was allocated
810 * using alloc_contig_range(). All pages in a subblock are
811 * alike.
812 */
813 if (PageDirty(page)) {
814 virtio_mem_clear_fake_offline(pfn + i, 1 << order,
815 false);
816 generic_online_page(page, order);
817 } else {
818 virtio_mem_clear_fake_offline(pfn + i, 1 << order,
819 true);
820 free_contig_range(pfn + i, 1 << order);
821 adjust_managed_page_count(page, 1 << order);
822 }
823 }
824}
825
826static void virtio_mem_online_page_cb(struct page *page, unsigned int order)
827{
828 const unsigned long addr = page_to_phys(page);
829 const unsigned long mb_id = virtio_mem_phys_to_mb_id(addr);
830 struct virtio_mem *vm;
831 int sb_id;
832
833 /*
834 * We exploit here that subblocks have at least MAX_ORDER - 1
835 * size/alignment and that this callback is is called with such a
836 * size/alignment. So we cannot cross subblocks and therefore
837 * also not memory blocks.
838 */
839 rcu_read_lock();
840 list_for_each_entry_rcu(vm, &virtio_mem_devices, next) {
841 if (!virtio_mem_owned_mb(vm, mb_id))
842 continue;
843
844 sb_id = virtio_mem_phys_to_sb_id(vm, addr);
845 /*
846 * If plugged, online the pages, otherwise, set them fake
847 * offline (PageOffline).
848 */
849 if (virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
850 generic_online_page(page, order);
851 else
852 virtio_mem_set_fake_offline(PFN_DOWN(addr), 1 << order,
853 false);
854 rcu_read_unlock();
855 return;
856 }
857 rcu_read_unlock();
858
859 /* not virtio-mem memory, but e.g., a DIMM. online it */
860 generic_online_page(page, order);
861}
862
863static uint64_t virtio_mem_send_request(struct virtio_mem *vm,
864 const struct virtio_mem_req *req)
865{
866 struct scatterlist *sgs[2], sg_req, sg_resp;
867 unsigned int len;
868 int rc;
869
870 /* don't use the request residing on the stack (vaddr) */
871 vm->req = *req;
872
873 /* out: buffer for request */
874 sg_init_one(&sg_req, &vm->req, sizeof(vm->req));
875 sgs[0] = &sg_req;
876
877 /* in: buffer for response */
878 sg_init_one(&sg_resp, &vm->resp, sizeof(vm->resp));
879 sgs[1] = &sg_resp;
880
881 rc = virtqueue_add_sgs(vm->vq, sgs, 1, 1, vm, GFP_KERNEL);
882 if (rc < 0)
883 return rc;
884
885 virtqueue_kick(vm->vq);
886
887 /* wait for a response */
888 wait_event(vm->host_resp, virtqueue_get_buf(vm->vq, &len));
889
890 return virtio16_to_cpu(vm->vdev, vm->resp.type);
891}
892
893static int virtio_mem_send_plug_request(struct virtio_mem *vm, uint64_t addr,
894 uint64_t size)
895{
896 const uint64_t nb_vm_blocks = size / vm->device_block_size;
897 const struct virtio_mem_req req = {
898 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_PLUG),
899 .u.plug.addr = cpu_to_virtio64(vm->vdev, addr),
900 .u.plug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
901 };
902
903 if (atomic_read(&vm->config_changed))
904 return -EAGAIN;
905
906 switch (virtio_mem_send_request(vm, &req)) {
907 case VIRTIO_MEM_RESP_ACK:
908 vm->plugged_size += size;
909 return 0;
910 case VIRTIO_MEM_RESP_NACK:
911 return -EAGAIN;
912 case VIRTIO_MEM_RESP_BUSY:
913 return -ETXTBSY;
914 case VIRTIO_MEM_RESP_ERROR:
915 return -EINVAL;
916 default:
917 return -ENOMEM;
918 }
919}
920
921static int virtio_mem_send_unplug_request(struct virtio_mem *vm, uint64_t addr,
922 uint64_t size)
923{
924 const uint64_t nb_vm_blocks = size / vm->device_block_size;
925 const struct virtio_mem_req req = {
926 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG),
927 .u.unplug.addr = cpu_to_virtio64(vm->vdev, addr),
928 .u.unplug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
929 };
930
931 if (atomic_read(&vm->config_changed))
932 return -EAGAIN;
933
934 switch (virtio_mem_send_request(vm, &req)) {
935 case VIRTIO_MEM_RESP_ACK:
936 vm->plugged_size -= size;
937 return 0;
938 case VIRTIO_MEM_RESP_BUSY:
939 return -ETXTBSY;
940 case VIRTIO_MEM_RESP_ERROR:
941 return -EINVAL;
942 default:
943 return -ENOMEM;
944 }
945}
946
947static int virtio_mem_send_unplug_all_request(struct virtio_mem *vm)
948{
949 const struct virtio_mem_req req = {
950 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG_ALL),
951 };
952
953 switch (virtio_mem_send_request(vm, &req)) {
954 case VIRTIO_MEM_RESP_ACK:
955 vm->unplug_all_required = false;
956 vm->plugged_size = 0;
957 /* usable region might have shrunk */
958 atomic_set(&vm->config_changed, 1);
959 return 0;
960 case VIRTIO_MEM_RESP_BUSY:
961 return -ETXTBSY;
962 default:
963 return -ENOMEM;
964 }
965}
966
967/*
968 * Plug selected subblocks. Updates the plugged state, but not the state
969 * of the memory block.
970 */
971static int virtio_mem_mb_plug_sb(struct virtio_mem *vm, unsigned long mb_id,
972 int sb_id, int count)
973{
974 const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id) +
975 sb_id * vm->subblock_size;
976 const uint64_t size = count * vm->subblock_size;
977 int rc;
978
979 dev_dbg(&vm->vdev->dev, "plugging memory block: %lu : %i - %i\n", mb_id,
980 sb_id, sb_id + count - 1);
981
982 rc = virtio_mem_send_plug_request(vm, addr, size);
983 if (!rc)
984 virtio_mem_mb_set_sb_plugged(vm, mb_id, sb_id, count);
985 return rc;
986}
987
988/*
989 * Unplug selected subblocks. Updates the plugged state, but not the state
990 * of the memory block.
991 */
992static int virtio_mem_mb_unplug_sb(struct virtio_mem *vm, unsigned long mb_id,
993 int sb_id, int count)
994{
995 const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id) +
996 sb_id * vm->subblock_size;
997 const uint64_t size = count * vm->subblock_size;
998 int rc;
999
1000 dev_dbg(&vm->vdev->dev, "unplugging memory block: %lu : %i - %i\n",
1001 mb_id, sb_id, sb_id + count - 1);
1002
1003 rc = virtio_mem_send_unplug_request(vm, addr, size);
1004 if (!rc)
1005 virtio_mem_mb_set_sb_unplugged(vm, mb_id, sb_id, count);
1006 return rc;
1007}
1008
1009/*
1010 * Unplug the desired number of plugged subblocks of a offline or not-added
1011 * memory block. Will fail if any subblock cannot get unplugged (instead of
1012 * skipping it).
1013 *
1014 * Will not modify the state of the memory block.
1015 *
1016 * Note: can fail after some subblocks were unplugged.
1017 */
1018static int virtio_mem_mb_unplug_any_sb(struct virtio_mem *vm,
1019 unsigned long mb_id, uint64_t *nb_sb)
1020{
1021 int sb_id, count;
1022 int rc;
1023
1024 sb_id = vm->nb_sb_per_mb - 1;
1025 while (*nb_sb) {
1026 /* Find the next candidate subblock */
1027 while (sb_id >= 0 &&
1028 virtio_mem_mb_test_sb_unplugged(vm, mb_id, sb_id, 1))
1029 sb_id--;
1030 if (sb_id < 0)
1031 break;
1032 /* Try to unplug multiple subblocks at a time */
1033 count = 1;
1034 while (count < *nb_sb && sb_id > 0 &&
1035 virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id - 1, 1)) {
1036 count++;
1037 sb_id--;
1038 }
1039
1040 rc = virtio_mem_mb_unplug_sb(vm, mb_id, sb_id, count);
1041 if (rc)
1042 return rc;
1043 *nb_sb -= count;
1044 sb_id--;
1045 }
1046
1047 return 0;
1048}
1049
1050/*
1051 * Unplug all plugged subblocks of an offline or not-added memory block.
1052 *
1053 * Will not modify the state of the memory block.
1054 *
1055 * Note: can fail after some subblocks were unplugged.
1056 */
1057static int virtio_mem_mb_unplug(struct virtio_mem *vm, unsigned long mb_id)
1058{
1059 uint64_t nb_sb = vm->nb_sb_per_mb;
1060
1061 return virtio_mem_mb_unplug_any_sb(vm, mb_id, &nb_sb);
1062}
1063
1064/*
1065 * Prepare tracking data for the next memory block.
1066 */
1067static int virtio_mem_prepare_next_mb(struct virtio_mem *vm,
1068 unsigned long *mb_id)
1069{
1070 int rc;
1071
1072 if (vm->next_mb_id > vm->last_usable_mb_id)
1073 return -ENOSPC;
1074
1075 /* Resize the state array if required. */
1076 rc = virtio_mem_mb_state_prepare_next_mb(vm);
1077 if (rc)
1078 return rc;
1079
1080 /* Resize the subblock bitmap if required. */
1081 rc = virtio_mem_sb_bitmap_prepare_next_mb(vm);
1082 if (rc)
1083 return rc;
1084
1085 vm->nb_mb_state[VIRTIO_MEM_MB_STATE_UNUSED]++;
1086 *mb_id = vm->next_mb_id++;
1087 return 0;
1088}
1089
1090/*
1091 * Don't add too many blocks that are not onlined yet to avoid running OOM.
1092 */
1093static bool virtio_mem_too_many_mb_offline(struct virtio_mem *vm)
1094{
1095 unsigned long nb_offline;
1096
1097 nb_offline = vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE] +
1098 vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL];
1099 return nb_offline >= VIRTIO_MEM_NB_OFFLINE_THRESHOLD;
1100}
1101
1102/*
1103 * Try to plug the desired number of subblocks and add the memory block
1104 * to Linux.
1105 *
1106 * Will modify the state of the memory block.
1107 */
1108static int virtio_mem_mb_plug_and_add(struct virtio_mem *vm,
1109 unsigned long mb_id,
1110 uint64_t *nb_sb)
1111{
1112 const int count = min_t(int, *nb_sb, vm->nb_sb_per_mb);
1113 int rc, rc2;
1114
1115 if (WARN_ON_ONCE(!count))
1116 return -EINVAL;
1117
1118 /*
1119 * Plug the requested number of subblocks before adding it to linux,
1120 * so that onlining will directly online all plugged subblocks.
1121 */
1122 rc = virtio_mem_mb_plug_sb(vm, mb_id, 0, count);
1123 if (rc)
1124 return rc;
1125
1126 /*
1127 * Mark the block properly offline before adding it to Linux,
1128 * so the memory notifiers will find the block in the right state.
1129 */
1130 if (count == vm->nb_sb_per_mb)
1131 virtio_mem_mb_set_state(vm, mb_id,
1132 VIRTIO_MEM_MB_STATE_OFFLINE);
1133 else
1134 virtio_mem_mb_set_state(vm, mb_id,
1135 VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL);
1136
1137 /* Add the memory block to linux - if that fails, try to unplug. */
1138 rc = virtio_mem_mb_add(vm, mb_id);
1139 if (rc) {
1140 enum virtio_mem_mb_state new_state = VIRTIO_MEM_MB_STATE_UNUSED;
1141
1142 dev_err(&vm->vdev->dev,
1143 "adding memory block %lu failed with %d\n", mb_id, rc);
1144 rc2 = virtio_mem_mb_unplug_sb(vm, mb_id, 0, count);
1145
1146 /*
1147 * TODO: Linux MM does not properly clean up yet in all cases
1148 * where adding of memory failed - especially on -ENOMEM.
1149 */
1150 if (rc2)
1151 new_state = VIRTIO_MEM_MB_STATE_PLUGGED;
1152 virtio_mem_mb_set_state(vm, mb_id, new_state);
1153 return rc;
1154 }
1155
1156 *nb_sb -= count;
1157 return 0;
1158}
1159
1160/*
1161 * Try to plug the desired number of subblocks of a memory block that
1162 * is already added to Linux.
1163 *
1164 * Will modify the state of the memory block.
1165 *
1166 * Note: Can fail after some subblocks were successfully plugged.
1167 */
1168static int virtio_mem_mb_plug_any_sb(struct virtio_mem *vm, unsigned long mb_id,
1169 uint64_t *nb_sb, bool online)
1170{
1171 unsigned long pfn, nr_pages;
1172 int sb_id, count;
1173 int rc;
1174
1175 if (WARN_ON_ONCE(!*nb_sb))
1176 return -EINVAL;
1177
1178 while (*nb_sb) {
1179 sb_id = virtio_mem_mb_first_unplugged_sb(vm, mb_id);
1180 if (sb_id >= vm->nb_sb_per_mb)
1181 break;
1182 count = 1;
1183 while (count < *nb_sb &&
1184 sb_id + count < vm->nb_sb_per_mb &&
1185 !virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id + count,
1186 1))
1187 count++;
1188
1189 rc = virtio_mem_mb_plug_sb(vm, mb_id, sb_id, count);
1190 if (rc)
1191 return rc;
1192 *nb_sb -= count;
1193 if (!online)
1194 continue;
1195
1196 /* fake-online the pages if the memory block is online */
1197 pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
1198 sb_id * vm->subblock_size);
1199 nr_pages = PFN_DOWN(count * vm->subblock_size);
1200 virtio_mem_fake_online(pfn, nr_pages);
1201 }
1202
1203 if (virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1204 if (online)
1205 virtio_mem_mb_set_state(vm, mb_id,
1206 VIRTIO_MEM_MB_STATE_ONLINE);
1207 else
1208 virtio_mem_mb_set_state(vm, mb_id,
1209 VIRTIO_MEM_MB_STATE_OFFLINE);
1210 }
1211
1212 return 0;
1213}
1214
1215/*
1216 * Try to plug the requested amount of memory.
1217 */
1218static int virtio_mem_plug_request(struct virtio_mem *vm, uint64_t diff)
1219{
1220 uint64_t nb_sb = diff / vm->subblock_size;
1221 unsigned long mb_id;
1222 int rc;
1223
1224 if (!nb_sb)
1225 return 0;
1226
1227 /* Don't race with onlining/offlining */
1228 mutex_lock(&vm->hotplug_mutex);
1229
1230 /* Try to plug subblocks of partially plugged online blocks. */
1231 virtio_mem_for_each_mb_state(vm, mb_id,
1232 VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL) {
1233 rc = virtio_mem_mb_plug_any_sb(vm, mb_id, &nb_sb, true);
1234 if (rc || !nb_sb)
1235 goto out_unlock;
1236 cond_resched();
1237 }
1238
1239 /* Try to plug subblocks of partially plugged offline blocks. */
1240 virtio_mem_for_each_mb_state(vm, mb_id,
1241 VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) {
1242 rc = virtio_mem_mb_plug_any_sb(vm, mb_id, &nb_sb, false);
1243 if (rc || !nb_sb)
1244 goto out_unlock;
1245 cond_resched();
1246 }
1247
1248 /*
1249 * We won't be working on online/offline memory blocks from this point,
1250 * so we can't race with memory onlining/offlining. Drop the mutex.
1251 */
1252 mutex_unlock(&vm->hotplug_mutex);
1253
1254 /* Try to plug and add unused blocks */
1255 virtio_mem_for_each_mb_state(vm, mb_id, VIRTIO_MEM_MB_STATE_UNUSED) {
1256 if (virtio_mem_too_many_mb_offline(vm))
1257 return -ENOSPC;
1258
1259 rc = virtio_mem_mb_plug_and_add(vm, mb_id, &nb_sb);
1260 if (rc || !nb_sb)
1261 return rc;
1262 cond_resched();
1263 }
1264
1265 /* Try to prepare, plug and add new blocks */
1266 while (nb_sb) {
1267 if (virtio_mem_too_many_mb_offline(vm))
1268 return -ENOSPC;
1269
1270 rc = virtio_mem_prepare_next_mb(vm, &mb_id);
1271 if (rc)
1272 return rc;
1273 rc = virtio_mem_mb_plug_and_add(vm, mb_id, &nb_sb);
1274 if (rc)
1275 return rc;
1276 cond_resched();
1277 }
1278
1279 return 0;
1280out_unlock:
1281 mutex_unlock(&vm->hotplug_mutex);
1282 return rc;
1283}
1284
1285/*
1286 * Unplug the desired number of plugged subblocks of an offline memory block.
1287 * Will fail if any subblock cannot get unplugged (instead of skipping it).
1288 *
1289 * Will modify the state of the memory block. Might temporarily drop the
1290 * hotplug_mutex.
1291 *
1292 * Note: Can fail after some subblocks were successfully unplugged.
1293 */
1294static int virtio_mem_mb_unplug_any_sb_offline(struct virtio_mem *vm,
1295 unsigned long mb_id,
1296 uint64_t *nb_sb)
1297{
1298 int rc;
1299
1300 rc = virtio_mem_mb_unplug_any_sb(vm, mb_id, nb_sb);
1301
1302 /* some subblocks might have been unplugged even on failure */
1303 if (!virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb))
1304 virtio_mem_mb_set_state(vm, mb_id,
1305 VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL);
1306 if (rc)
1307 return rc;
1308
1309 if (virtio_mem_mb_test_sb_unplugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1310 /*
1311 * Remove the block from Linux - this should never fail.
1312 * Hinder the block from getting onlined by marking it
1313 * unplugged. Temporarily drop the mutex, so
1314 * any pending GOING_ONLINE requests can be serviced/rejected.
1315 */
1316 virtio_mem_mb_set_state(vm, mb_id,
1317 VIRTIO_MEM_MB_STATE_UNUSED);
1318
1319 mutex_unlock(&vm->hotplug_mutex);
1320 rc = virtio_mem_mb_remove(vm, mb_id);
1321 BUG_ON(rc);
1322 mutex_lock(&vm->hotplug_mutex);
1323 }
1324 return 0;
1325}
1326
1327/*
1328 * Unplug the given plugged subblocks of an online memory block.
1329 *
1330 * Will modify the state of the memory block.
1331 */
1332static int virtio_mem_mb_unplug_sb_online(struct virtio_mem *vm,
1333 unsigned long mb_id, int sb_id,
1334 int count)
1335{
1336 const unsigned long nr_pages = PFN_DOWN(vm->subblock_size) * count;
1337 unsigned long start_pfn;
1338 int rc;
1339
1340 start_pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
1341 sb_id * vm->subblock_size);
1342 rc = alloc_contig_range(start_pfn, start_pfn + nr_pages,
1343 MIGRATE_MOVABLE, GFP_KERNEL);
1344 if (rc == -ENOMEM)
1345 /* whoops, out of memory */
1346 return rc;
1347 if (rc)
1348 return -EBUSY;
1349
1350 /* Mark it as fake-offline before unplugging it */
1351 virtio_mem_set_fake_offline(start_pfn, nr_pages, true);
1352 adjust_managed_page_count(pfn_to_page(start_pfn), -nr_pages);
1353
1354 /* Try to unplug the allocated memory */
1355 rc = virtio_mem_mb_unplug_sb(vm, mb_id, sb_id, count);
1356 if (rc) {
1357 /* Return the memory to the buddy. */
1358 virtio_mem_fake_online(start_pfn, nr_pages);
1359 return rc;
1360 }
1361
1362 virtio_mem_mb_set_state(vm, mb_id,
1363 VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL);
1364 return 0;
1365}
1366
1367/*
1368 * Unplug the desired number of plugged subblocks of an online memory block.
1369 * Will skip subblock that are busy.
1370 *
1371 * Will modify the state of the memory block. Might temporarily drop the
1372 * hotplug_mutex.
1373 *
1374 * Note: Can fail after some subblocks were successfully unplugged. Can
1375 * return 0 even if subblocks were busy and could not get unplugged.
1376 */
1377static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm,
1378 unsigned long mb_id,
1379 uint64_t *nb_sb)
1380{
1381 int rc, sb_id;
1382
1383 /* If possible, try to unplug the complete block in one shot. */
1384 if (*nb_sb >= vm->nb_sb_per_mb &&
1385 virtio_mem_mb_test_sb_plugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1386 rc = virtio_mem_mb_unplug_sb_online(vm, mb_id, 0,
1387 vm->nb_sb_per_mb);
1388 if (!rc) {
1389 *nb_sb -= vm->nb_sb_per_mb;
1390 goto unplugged;
1391 } else if (rc != -EBUSY)
1392 return rc;
1393 }
1394
1395 /* Fallback to single subblocks. */
1396 for (sb_id = vm->nb_sb_per_mb - 1; sb_id >= 0 && *nb_sb; sb_id--) {
1397 /* Find the next candidate subblock */
1398 while (sb_id >= 0 &&
1399 !virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
1400 sb_id--;
1401 if (sb_id < 0)
1402 break;
1403
1404 rc = virtio_mem_mb_unplug_sb_online(vm, mb_id, sb_id, 1);
1405 if (rc == -EBUSY)
1406 continue;
1407 else if (rc)
1408 return rc;
1409 *nb_sb -= 1;
1410 }
1411
1412unplugged:
1413 /*
1414 * Once all subblocks of a memory block were unplugged, offline and
1415 * remove it. This will usually not fail, as no memory is in use
1416 * anymore - however some other notifiers might NACK the request.
1417 */
1418 if (virtio_mem_mb_test_sb_unplugged(vm, mb_id, 0, vm->nb_sb_per_mb)) {
1419 mutex_unlock(&vm->hotplug_mutex);
1420 rc = virtio_mem_mb_offline_and_remove(vm, mb_id);
1421 mutex_lock(&vm->hotplug_mutex);
1422 if (!rc)
1423 virtio_mem_mb_set_state(vm, mb_id,
1424 VIRTIO_MEM_MB_STATE_UNUSED);
1425 }
1426
1427 return 0;
1428}
1429
1430/*
1431 * Try to unplug the requested amount of memory.
1432 */
1433static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff)
1434{
1435 uint64_t nb_sb = diff / vm->subblock_size;
1436 unsigned long mb_id;
1437 int rc;
1438
1439 if (!nb_sb)
1440 return 0;
1441
1442 /*
1443 * We'll drop the mutex a couple of times when it is safe to do so.
1444 * This might result in some blocks switching the state (online/offline)
1445 * and we could miss them in this run - we will retry again later.
1446 */
1447 mutex_lock(&vm->hotplug_mutex);
1448
1449 /* Try to unplug subblocks of partially plugged offline blocks. */
1450 virtio_mem_for_each_mb_state_rev(vm, mb_id,
1451 VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) {
1452 rc = virtio_mem_mb_unplug_any_sb_offline(vm, mb_id,
1453 &nb_sb);
1454 if (rc || !nb_sb)
1455 goto out_unlock;
1456 cond_resched();
1457 }
1458
1459 /* Try to unplug subblocks of plugged offline blocks. */
1460 virtio_mem_for_each_mb_state_rev(vm, mb_id,
1461 VIRTIO_MEM_MB_STATE_OFFLINE) {
1462 rc = virtio_mem_mb_unplug_any_sb_offline(vm, mb_id,
1463 &nb_sb);
1464 if (rc || !nb_sb)
1465 goto out_unlock;
1466 cond_resched();
1467 }
1468
1469 if (!unplug_online) {
1470 mutex_unlock(&vm->hotplug_mutex);
1471 return 0;
1472 }
1473
1474 /* Try to unplug subblocks of partially plugged online blocks. */
1475 virtio_mem_for_each_mb_state_rev(vm, mb_id,
1476 VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL) {
1477 rc = virtio_mem_mb_unplug_any_sb_online(vm, mb_id,
1478 &nb_sb);
1479 if (rc || !nb_sb)
1480 goto out_unlock;
1481 mutex_unlock(&vm->hotplug_mutex);
1482 cond_resched();
1483 mutex_lock(&vm->hotplug_mutex);
1484 }
1485
1486 /* Try to unplug subblocks of plugged online blocks. */
1487 virtio_mem_for_each_mb_state_rev(vm, mb_id,
1488 VIRTIO_MEM_MB_STATE_ONLINE) {
1489 rc = virtio_mem_mb_unplug_any_sb_online(vm, mb_id,
1490 &nb_sb);
1491 if (rc || !nb_sb)
1492 goto out_unlock;
1493 mutex_unlock(&vm->hotplug_mutex);
1494 cond_resched();
1495 mutex_lock(&vm->hotplug_mutex);
1496 }
1497
1498 mutex_unlock(&vm->hotplug_mutex);
1499 return nb_sb ? -EBUSY : 0;
1500out_unlock:
1501 mutex_unlock(&vm->hotplug_mutex);
1502 return rc;
1503}
1504
1505/*
1506 * Try to unplug all blocks that couldn't be unplugged before, for example,
1507 * because the hypervisor was busy.
1508 */
1509static int virtio_mem_unplug_pending_mb(struct virtio_mem *vm)
1510{
1511 unsigned long mb_id;
1512 int rc;
1513
1514 virtio_mem_for_each_mb_state(vm, mb_id, VIRTIO_MEM_MB_STATE_PLUGGED) {
1515 rc = virtio_mem_mb_unplug(vm, mb_id);
1516 if (rc)
1517 return rc;
1518 virtio_mem_mb_set_state(vm, mb_id, VIRTIO_MEM_MB_STATE_UNUSED);
1519 }
1520
1521 return 0;
1522}
1523
1524/*
1525 * Update all parts of the config that could have changed.
1526 */
1527static void virtio_mem_refresh_config(struct virtio_mem *vm)
1528{
1529 const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
1530 uint64_t new_plugged_size, usable_region_size, end_addr;
1531
1532 /* the plugged_size is just a reflection of what _we_ did previously */
1533 virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
1534 &new_plugged_size);
1535 if (WARN_ON_ONCE(new_plugged_size != vm->plugged_size))
1536 vm->plugged_size = new_plugged_size;
1537
1538 /* calculate the last usable memory block id */
1539 virtio_cread_le(vm->vdev, struct virtio_mem_config,
1540 usable_region_size, &usable_region_size);
1541 end_addr = vm->addr + usable_region_size;
1542 end_addr = min(end_addr, phys_limit);
1543 vm->last_usable_mb_id = virtio_mem_phys_to_mb_id(end_addr) - 1;
1544
1545 /* see if there is a request to change the size */
1546 virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size,
1547 &vm->requested_size);
1548
1549 dev_info(&vm->vdev->dev, "plugged size: 0x%llx", vm->plugged_size);
1550 dev_info(&vm->vdev->dev, "requested size: 0x%llx", vm->requested_size);
1551}
1552
1553/*
1554 * Workqueue function for handling plug/unplug requests and config updates.
1555 */
1556static void virtio_mem_run_wq(struct work_struct *work)
1557{
1558 struct virtio_mem *vm = container_of(work, struct virtio_mem, wq);
1559 uint64_t diff;
1560 int rc;
1561
1562 hrtimer_cancel(&vm->retry_timer);
1563
1564 if (vm->broken)
1565 return;
1566
1567retry:
1568 rc = 0;
1569
1570 /* Make sure we start with a clean state if there are leftovers. */
1571 if (unlikely(vm->unplug_all_required))
1572 rc = virtio_mem_send_unplug_all_request(vm);
1573
1574 if (atomic_read(&vm->config_changed)) {
1575 atomic_set(&vm->config_changed, 0);
1576 virtio_mem_refresh_config(vm);
1577 }
1578
1579 /* Unplug any leftovers from previous runs */
1580 if (!rc)
1581 rc = virtio_mem_unplug_pending_mb(vm);
1582
1583 if (!rc && vm->requested_size != vm->plugged_size) {
1584 if (vm->requested_size > vm->plugged_size) {
1585 diff = vm->requested_size - vm->plugged_size;
1586 rc = virtio_mem_plug_request(vm, diff);
1587 } else {
1588 diff = vm->plugged_size - vm->requested_size;
1589 rc = virtio_mem_unplug_request(vm, diff);
1590 }
1591 }
1592
1593 switch (rc) {
1594 case 0:
1595 vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS;
1596 break;
1597 case -ENOSPC:
1598 /*
1599 * We cannot add any more memory (alignment, physical limit)
1600 * or we have too many offline memory blocks.
1601 */
1602 break;
1603 case -ETXTBSY:
1604 /*
1605 * The hypervisor cannot process our request right now
1606 * (e.g., out of memory, migrating);
1607 */
1608 case -EBUSY:
1609 /*
1610 * We cannot free up any memory to unplug it (all plugged memory
1611 * is busy).
1612 */
1613 case -ENOMEM:
1614 /* Out of memory, try again later. */
1615 hrtimer_start(&vm->retry_timer, ms_to_ktime(vm->retry_timer_ms),
1616 HRTIMER_MODE_REL);
1617 break;
1618 case -EAGAIN:
1619 /* Retry immediately (e.g., the config changed). */
1620 goto retry;
1621 default:
1622 /* Unknown error, mark as broken */
1623 dev_err(&vm->vdev->dev,
1624 "unknown error, marking device broken: %d\n", rc);
1625 vm->broken = true;
1626 }
1627}
1628
1629static enum hrtimer_restart virtio_mem_timer_expired(struct hrtimer *timer)
1630{
1631 struct virtio_mem *vm = container_of(timer, struct virtio_mem,
1632 retry_timer);
1633
1634 virtio_mem_retry(vm);
1635 vm->retry_timer_ms = min_t(unsigned int, vm->retry_timer_ms * 2,
1636 VIRTIO_MEM_RETRY_TIMER_MAX_MS);
1637 return HRTIMER_NORESTART;
1638}
1639
1640static void virtio_mem_handle_response(struct virtqueue *vq)
1641{
1642 struct virtio_mem *vm = vq->vdev->priv;
1643
1644 wake_up(&vm->host_resp);
1645}
1646
1647static int virtio_mem_init_vq(struct virtio_mem *vm)
1648{
1649 struct virtqueue *vq;
1650
1651 vq = virtio_find_single_vq(vm->vdev, virtio_mem_handle_response,
1652 "guest-request");
1653 if (IS_ERR(vq))
1654 return PTR_ERR(vq);
1655 vm->vq = vq;
1656
1657 return 0;
1658}
1659
1660static int virtio_mem_init(struct virtio_mem *vm)
1661{
1662 const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
1663 uint16_t node_id;
1664
1665 if (!vm->vdev->config->get) {
1666 dev_err(&vm->vdev->dev, "config access disabled\n");
1667 return -EINVAL;
1668 }
1669
1670 /*
1671 * We don't want to (un)plug or reuse any memory when in kdump. The
1672 * memory is still accessible (but not mapped).
1673 */
1674 if (is_kdump_kernel()) {
1675 dev_warn(&vm->vdev->dev, "disabled in kdump kernel\n");
1676 return -EBUSY;
1677 }
1678
1679 /* Fetch all properties that can't change. */
1680 virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
1681 &vm->plugged_size);
1682 virtio_cread_le(vm->vdev, struct virtio_mem_config, block_size,
1683 &vm->device_block_size);
1684 virtio_cread_le(vm->vdev, struct virtio_mem_config, node_id,
1685 &node_id);
1686 vm->nid = virtio_mem_translate_node_id(vm, node_id);
1687 virtio_cread_le(vm->vdev, struct virtio_mem_config, addr, &vm->addr);
1688 virtio_cread_le(vm->vdev, struct virtio_mem_config, region_size,
1689 &vm->region_size);
1690
1691 /*
1692 * We always hotplug memory in memory block granularity. This way,
1693 * we have to wait for exactly one memory block to online.
1694 */
1695 if (vm->device_block_size > memory_block_size_bytes()) {
1696 dev_err(&vm->vdev->dev,
1697 "The block size is not supported (too big).\n");
1698 return -EINVAL;
1699 }
1700
1701 /* bad device setup - warn only */
1702 if (!IS_ALIGNED(vm->addr, memory_block_size_bytes()))
1703 dev_warn(&vm->vdev->dev,
1704 "The alignment of the physical start address can make some memory unusable.\n");
1705 if (!IS_ALIGNED(vm->addr + vm->region_size, memory_block_size_bytes()))
1706 dev_warn(&vm->vdev->dev,
1707 "The alignment of the physical end address can make some memory unusable.\n");
1708 if (vm->addr + vm->region_size > phys_limit)
1709 dev_warn(&vm->vdev->dev,
1710 "Some memory is not addressable. This can make some memory unusable.\n");
1711
1712 /*
1713 * Calculate the subblock size:
1714 * - At least MAX_ORDER - 1 / pageblock_order.
1715 * - At least the device block size.
1716 * In the worst case, a single subblock per memory block.
1717 */
1718 vm->subblock_size = PAGE_SIZE * 1ul << max_t(uint32_t, MAX_ORDER - 1,
1719 pageblock_order);
1720 vm->subblock_size = max_t(uint64_t, vm->device_block_size,
1721 vm->subblock_size);
1722 vm->nb_sb_per_mb = memory_block_size_bytes() / vm->subblock_size;
1723
1724 /* Round up to the next full memory block */
1725 vm->first_mb_id = virtio_mem_phys_to_mb_id(vm->addr - 1 +
1726 memory_block_size_bytes());
1727 vm->next_mb_id = vm->first_mb_id;
1728 vm->last_mb_id = virtio_mem_phys_to_mb_id(vm->addr +
1729 vm->region_size) - 1;
1730
1731 dev_info(&vm->vdev->dev, "start address: 0x%llx", vm->addr);
1732 dev_info(&vm->vdev->dev, "region size: 0x%llx", vm->region_size);
1733 dev_info(&vm->vdev->dev, "device block size: 0x%llx",
1734 (unsigned long long)vm->device_block_size);
1735 dev_info(&vm->vdev->dev, "memory block size: 0x%lx",
1736 memory_block_size_bytes());
1737 dev_info(&vm->vdev->dev, "subblock size: 0x%llx",
1738 (unsigned long long)vm->subblock_size);
1739 if (vm->nid != NUMA_NO_NODE)
1740 dev_info(&vm->vdev->dev, "nid: %d", vm->nid);
1741
1742 return 0;
1743}
1744
1745static int virtio_mem_create_resource(struct virtio_mem *vm)
1746{
1747 /*
1748 * When force-unloading the driver and removing the device, we
1749 * could have a garbage pointer. Duplicate the string.
1750 */
1751 const char *name = kstrdup(dev_name(&vm->vdev->dev), GFP_KERNEL);
1752
1753 if (!name)
1754 return -ENOMEM;
1755
1756 vm->parent_resource = __request_mem_region(vm->addr, vm->region_size,
1757 name, IORESOURCE_SYSTEM_RAM);
1758 if (!vm->parent_resource) {
1759 kfree(name);
1760 dev_warn(&vm->vdev->dev, "could not reserve device region\n");
1761 dev_info(&vm->vdev->dev,
1762 "reloading the driver is not supported\n");
1763 return -EBUSY;
1764 }
1765
1766 /* The memory is not actually busy - make add_memory() work. */
1767 vm->parent_resource->flags &= ~IORESOURCE_BUSY;
1768 return 0;
1769}
1770
1771static void virtio_mem_delete_resource(struct virtio_mem *vm)
1772{
1773 const char *name;
1774
1775 if (!vm->parent_resource)
1776 return;
1777
1778 name = vm->parent_resource->name;
1779 release_resource(vm->parent_resource);
1780 kfree(vm->parent_resource);
1781 kfree(name);
1782 vm->parent_resource = NULL;
1783}
1784
1785static int virtio_mem_probe(struct virtio_device *vdev)
1786{
1787 struct virtio_mem *vm;
1788 int rc;
1789
1790 BUILD_BUG_ON(sizeof(struct virtio_mem_req) != 24);
1791 BUILD_BUG_ON(sizeof(struct virtio_mem_resp) != 10);
1792
1793 vdev->priv = vm = kzalloc(sizeof(*vm), GFP_KERNEL);
1794 if (!vm)
1795 return -ENOMEM;
1796
1797 init_waitqueue_head(&vm->host_resp);
1798 vm->vdev = vdev;
1799 INIT_WORK(&vm->wq, virtio_mem_run_wq);
1800 mutex_init(&vm->hotplug_mutex);
1801 INIT_LIST_HEAD(&vm->next);
1802 spin_lock_init(&vm->removal_lock);
1803 hrtimer_init(&vm->retry_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1804 vm->retry_timer.function = virtio_mem_timer_expired;
1805 vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS;
1806
1807 /* register the virtqueue */
1808 rc = virtio_mem_init_vq(vm);
1809 if (rc)
1810 goto out_free_vm;
1811
1812 /* initialize the device by querying the config */
1813 rc = virtio_mem_init(vm);
1814 if (rc)
1815 goto out_del_vq;
1816
1817 /* create the parent resource for all memory */
1818 rc = virtio_mem_create_resource(vm);
1819 if (rc)
1820 goto out_del_vq;
1821
1822 /*
1823 * If we still have memory plugged, we have to unplug all memory first.
1824 * Registering our parent resource makes sure that this memory isn't
1825 * actually in use (e.g., trying to reload the driver).
1826 */
1827 if (vm->plugged_size) {
1828 vm->unplug_all_required = 1;
1829 dev_info(&vm->vdev->dev, "unplugging all memory is required\n");
1830 }
1831
1832 /* register callbacks */
1833 vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb;
1834 rc = register_memory_notifier(&vm->memory_notifier);
1835 if (rc)
1836 goto out_del_resource;
1837 rc = register_virtio_mem_device(vm);
1838 if (rc)
1839 goto out_unreg_mem;
1840
1841 virtio_device_ready(vdev);
1842
1843 /* trigger a config update to start processing the requested_size */
1844 atomic_set(&vm->config_changed, 1);
1845 queue_work(system_freezable_wq, &vm->wq);
1846
1847 return 0;
1848out_unreg_mem:
1849 unregister_memory_notifier(&vm->memory_notifier);
1850out_del_resource:
1851 virtio_mem_delete_resource(vm);
1852out_del_vq:
1853 vdev->config->del_vqs(vdev);
1854out_free_vm:
1855 kfree(vm);
1856 vdev->priv = NULL;
1857
1858 return rc;
1859}
1860
1861static void virtio_mem_remove(struct virtio_device *vdev)
1862{
1863 struct virtio_mem *vm = vdev->priv;
1864 unsigned long mb_id;
1865 int rc;
1866
1867 /*
1868 * Make sure the workqueue won't be triggered anymore and no memory
1869 * blocks can be onlined/offlined until we're finished here.
1870 */
1871 mutex_lock(&vm->hotplug_mutex);
1872 spin_lock_irq(&vm->removal_lock);
1873 vm->removing = true;
1874 spin_unlock_irq(&vm->removal_lock);
1875 mutex_unlock(&vm->hotplug_mutex);
1876
1877 /* wait until the workqueue stopped */
1878 cancel_work_sync(&vm->wq);
1879 hrtimer_cancel(&vm->retry_timer);
1880
1881 /*
1882 * After we unregistered our callbacks, user space can online partially
1883 * plugged offline blocks. Make sure to remove them.
1884 */
1885 virtio_mem_for_each_mb_state(vm, mb_id,
1886 VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL) {
1887 rc = virtio_mem_mb_remove(vm, mb_id);
1888 BUG_ON(rc);
1889 virtio_mem_mb_set_state(vm, mb_id, VIRTIO_MEM_MB_STATE_UNUSED);
1890 }
1891 /*
1892 * After we unregistered our callbacks, user space can no longer
1893 * offline partially plugged online memory blocks. No need to worry
1894 * about them.
1895 */
1896
1897 /* unregister callbacks */
1898 unregister_virtio_mem_device(vm);
1899 unregister_memory_notifier(&vm->memory_notifier);
1900
1901 /*
1902 * There is no way we could reliably remove all memory we have added to
1903 * the system. And there is no way to stop the driver/device from going
1904 * away. Warn at least.
1905 */
1906 if (vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE] ||
1907 vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL] ||
1908 vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE] ||
1909 vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL] ||
1910 vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE]) {
1911 dev_warn(&vdev->dev, "device still has system memory added\n");
1912 } else {
1913 virtio_mem_delete_resource(vm);
1914 kfree_const(vm->resource_name);
1915 }
1916
1917 /* remove all tracking data - no locking needed */
1918 vfree(vm->mb_state);
1919 vfree(vm->sb_bitmap);
1920
1921 /* reset the device and cleanup the queues */
1922 vdev->config->reset(vdev);
1923 vdev->config->del_vqs(vdev);
1924
1925 kfree(vm);
1926 vdev->priv = NULL;
1927}
1928
1929static void virtio_mem_config_changed(struct virtio_device *vdev)
1930{
1931 struct virtio_mem *vm = vdev->priv;
1932
1933 atomic_set(&vm->config_changed, 1);
1934 virtio_mem_retry(vm);
1935}
1936
1937#ifdef CONFIG_PM_SLEEP
1938static int virtio_mem_freeze(struct virtio_device *vdev)
1939{
1940 /*
1941 * When restarting the VM, all memory is usually unplugged. Don't
1942 * allow to suspend/hibernate.
1943 */
1944 dev_err(&vdev->dev, "save/restore not supported.\n");
1945 return -EPERM;
1946}
1947
1948static int virtio_mem_restore(struct virtio_device *vdev)
1949{
1950 return -EPERM;
1951}
1952#endif
1953
1954static unsigned int virtio_mem_features[] = {
1955#if defined(CONFIG_NUMA) && defined(CONFIG_ACPI_NUMA)
1956 VIRTIO_MEM_F_ACPI_PXM,
1957#endif
1958};
1959
1960static struct virtio_device_id virtio_mem_id_table[] = {
1961 { VIRTIO_ID_MEM, VIRTIO_DEV_ANY_ID },
1962 { 0 },
1963};
1964
1965static struct virtio_driver virtio_mem_driver = {
1966 .feature_table = virtio_mem_features,
1967 .feature_table_size = ARRAY_SIZE(virtio_mem_features),
1968 .driver.name = KBUILD_MODNAME,
1969 .driver.owner = THIS_MODULE,
1970 .id_table = virtio_mem_id_table,
1971 .probe = virtio_mem_probe,
1972 .remove = virtio_mem_remove,
1973 .config_changed = virtio_mem_config_changed,
1974#ifdef CONFIG_PM_SLEEP
1975 .freeze = virtio_mem_freeze,
1976 .restore = virtio_mem_restore,
1977#endif
1978};
1979
1980module_virtio_driver(virtio_mem_driver);
1981MODULE_DEVICE_TABLE(virtio, virtio_mem_id_table);
1982MODULE_AUTHOR("David Hildenbrand <david@redhat.com>");
1983MODULE_DESCRIPTION("Virtio-mem driver");
1984MODULE_LICENSE("GPL");
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#include <linux/log2.h>
24
25#include <acpi/acpi_numa.h>
26
27static bool unplug_online = true;
28module_param(unplug_online, bool, 0644);
29MODULE_PARM_DESC(unplug_online, "Try to unplug online memory");
30
31static bool force_bbm;
32module_param(force_bbm, bool, 0444);
33MODULE_PARM_DESC(force_bbm,
34 "Force Big Block Mode. Default is 0 (auto-selection)");
35
36static unsigned long bbm_block_size;
37module_param(bbm_block_size, ulong, 0444);
38MODULE_PARM_DESC(bbm_block_size,
39 "Big Block size in bytes. Default is 0 (auto-detection).");
40
41static bool bbm_safe_unplug = true;
42module_param(bbm_safe_unplug, bool, 0444);
43MODULE_PARM_DESC(bbm_safe_unplug,
44 "Use a safe unplug mechanism in BBM, avoiding long/endless loops");
45
46/*
47 * virtio-mem currently supports the following modes of operation:
48 *
49 * * Sub Block Mode (SBM): A Linux memory block spans 2..X subblocks (SB). The
50 * size of a Sub Block (SB) is determined based on the device block size, the
51 * pageblock size, and the maximum allocation granularity of the buddy.
52 * Subblocks within a Linux memory block might either be plugged or unplugged.
53 * Memory is added/removed to Linux MM in Linux memory block granularity.
54 *
55 * * Big Block Mode (BBM): A Big Block (BB) spans 1..X Linux memory blocks.
56 * Memory is added/removed to Linux MM in Big Block granularity.
57 *
58 * The mode is determined automatically based on the Linux memory block size
59 * and the device block size.
60 *
61 * User space / core MM (auto onlining) is responsible for onlining added
62 * Linux memory blocks - and for selecting a zone. Linux Memory Blocks are
63 * always onlined separately, and all memory within a Linux memory block is
64 * onlined to the same zone - virtio-mem relies on this behavior.
65 */
66
67/*
68 * State of a Linux memory block in SBM.
69 */
70enum virtio_mem_sbm_mb_state {
71 /* Unplugged, not added to Linux. Can be reused later. */
72 VIRTIO_MEM_SBM_MB_UNUSED = 0,
73 /* (Partially) plugged, not added to Linux. Error on add_memory(). */
74 VIRTIO_MEM_SBM_MB_PLUGGED,
75 /* Fully plugged, fully added to Linux, offline. */
76 VIRTIO_MEM_SBM_MB_OFFLINE,
77 /* Partially plugged, fully added to Linux, offline. */
78 VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL,
79 /* Fully plugged, fully added to Linux, onlined to a kernel zone. */
80 VIRTIO_MEM_SBM_MB_KERNEL,
81 /* Partially plugged, fully added to Linux, online to a kernel zone */
82 VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL,
83 /* Fully plugged, fully added to Linux, onlined to ZONE_MOVABLE. */
84 VIRTIO_MEM_SBM_MB_MOVABLE,
85 /* Partially plugged, fully added to Linux, onlined to ZONE_MOVABLE. */
86 VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL,
87 VIRTIO_MEM_SBM_MB_COUNT
88};
89
90/*
91 * State of a Big Block (BB) in BBM, covering 1..X Linux memory blocks.
92 */
93enum virtio_mem_bbm_bb_state {
94 /* Unplugged, not added to Linux. Can be reused later. */
95 VIRTIO_MEM_BBM_BB_UNUSED = 0,
96 /* Plugged, not added to Linux. Error on add_memory(). */
97 VIRTIO_MEM_BBM_BB_PLUGGED,
98 /* Plugged and added to Linux. */
99 VIRTIO_MEM_BBM_BB_ADDED,
100 /* All online parts are fake-offline, ready to remove. */
101 VIRTIO_MEM_BBM_BB_FAKE_OFFLINE,
102 VIRTIO_MEM_BBM_BB_COUNT
103};
104
105struct virtio_mem {
106 struct virtio_device *vdev;
107
108 /* We might first have to unplug all memory when starting up. */
109 bool unplug_all_required;
110
111 /* Workqueue that processes the plug/unplug requests. */
112 struct work_struct wq;
113 atomic_t wq_active;
114 atomic_t config_changed;
115
116 /* Virtqueue for guest->host requests. */
117 struct virtqueue *vq;
118
119 /* Wait for a host response to a guest request. */
120 wait_queue_head_t host_resp;
121
122 /* Space for one guest request and the host response. */
123 struct virtio_mem_req req;
124 struct virtio_mem_resp resp;
125
126 /* The current size of the device. */
127 uint64_t plugged_size;
128 /* The requested size of the device. */
129 uint64_t requested_size;
130
131 /* The device block size (for communicating with the device). */
132 uint64_t device_block_size;
133 /* The determined node id for all memory of the device. */
134 int nid;
135 /* Physical start address of the memory region. */
136 uint64_t addr;
137 /* Maximum region size in bytes. */
138 uint64_t region_size;
139
140 /* The parent resource for all memory added via this device. */
141 struct resource *parent_resource;
142 /*
143 * Copy of "System RAM (virtio_mem)" to be used for
144 * add_memory_driver_managed().
145 */
146 const char *resource_name;
147 /* Memory group identification. */
148 int mgid;
149
150 /*
151 * We don't want to add too much memory if it's not getting onlined,
152 * to avoid running OOM. Besides this threshold, we allow to have at
153 * least two offline blocks at a time (whatever is bigger).
154 */
155#define VIRTIO_MEM_DEFAULT_OFFLINE_THRESHOLD (1024 * 1024 * 1024)
156 atomic64_t offline_size;
157 uint64_t offline_threshold;
158
159 /* If set, the driver is in SBM, otherwise in BBM. */
160 bool in_sbm;
161
162 union {
163 struct {
164 /* Id of the first memory block of this device. */
165 unsigned long first_mb_id;
166 /* Id of the last usable memory block of this device. */
167 unsigned long last_usable_mb_id;
168 /* Id of the next memory bock to prepare when needed. */
169 unsigned long next_mb_id;
170
171 /* The subblock size. */
172 uint64_t sb_size;
173 /* The number of subblocks per Linux memory block. */
174 uint32_t sbs_per_mb;
175
176 /* Summary of all memory block states. */
177 unsigned long mb_count[VIRTIO_MEM_SBM_MB_COUNT];
178
179 /*
180 * One byte state per memory block. Allocated via
181 * vmalloc(). Resized (alloc+copy+free) on demand.
182 *
183 * With 128 MiB memory blocks, we have states for 512
184 * GiB of memory in one 4 KiB page.
185 */
186 uint8_t *mb_states;
187
188 /*
189 * Bitmap: one bit per subblock. Allocated similar to
190 * sbm.mb_states.
191 *
192 * A set bit means the corresponding subblock is
193 * plugged, otherwise it's unblocked.
194 *
195 * With 4 MiB subblocks, we manage 128 GiB of memory
196 * in one 4 KiB page.
197 */
198 unsigned long *sb_states;
199 } sbm;
200
201 struct {
202 /* Id of the first big block of this device. */
203 unsigned long first_bb_id;
204 /* Id of the last usable big block of this device. */
205 unsigned long last_usable_bb_id;
206 /* Id of the next device bock to prepare when needed. */
207 unsigned long next_bb_id;
208
209 /* Summary of all big block states. */
210 unsigned long bb_count[VIRTIO_MEM_BBM_BB_COUNT];
211
212 /* One byte state per big block. See sbm.mb_states. */
213 uint8_t *bb_states;
214
215 /* The block size used for plugging/adding/removing. */
216 uint64_t bb_size;
217 } bbm;
218 };
219
220 /*
221 * Mutex that protects the sbm.mb_count, sbm.mb_states,
222 * sbm.sb_states, bbm.bb_count, and bbm.bb_states
223 *
224 * When this lock is held the pointers can't change, ONLINE and
225 * OFFLINE blocks can't change the state and no subblocks will get
226 * plugged/unplugged.
227 *
228 * In kdump mode, used to serialize requests, last_block_addr and
229 * last_block_plugged.
230 */
231 struct mutex hotplug_mutex;
232 bool hotplug_active;
233
234 /* An error occurred we cannot handle - stop processing requests. */
235 bool broken;
236
237 /* Cached valued of is_kdump_kernel() when the device was probed. */
238 bool in_kdump;
239
240 /* The driver is being removed. */
241 spinlock_t removal_lock;
242 bool removing;
243
244 /* Timer for retrying to plug/unplug memory. */
245 struct hrtimer retry_timer;
246 unsigned int retry_timer_ms;
247#define VIRTIO_MEM_RETRY_TIMER_MIN_MS 50000
248#define VIRTIO_MEM_RETRY_TIMER_MAX_MS 300000
249
250 /* Memory notifier (online/offline events). */
251 struct notifier_block memory_notifier;
252
253#ifdef CONFIG_PROC_VMCORE
254 /* vmcore callback for /proc/vmcore handling in kdump mode */
255 struct vmcore_cb vmcore_cb;
256 uint64_t last_block_addr;
257 bool last_block_plugged;
258#endif /* CONFIG_PROC_VMCORE */
259
260 /* Next device in the list of virtio-mem devices. */
261 struct list_head next;
262};
263
264/*
265 * We have to share a single online_page callback among all virtio-mem
266 * devices. We use RCU to iterate the list in the callback.
267 */
268static DEFINE_MUTEX(virtio_mem_mutex);
269static LIST_HEAD(virtio_mem_devices);
270
271static void virtio_mem_online_page_cb(struct page *page, unsigned int order);
272static void virtio_mem_fake_offline_going_offline(unsigned long pfn,
273 unsigned long nr_pages);
274static void virtio_mem_fake_offline_cancel_offline(unsigned long pfn,
275 unsigned long nr_pages);
276static void virtio_mem_retry(struct virtio_mem *vm);
277static int virtio_mem_create_resource(struct virtio_mem *vm);
278static void virtio_mem_delete_resource(struct virtio_mem *vm);
279
280/*
281 * Register a virtio-mem device so it will be considered for the online_page
282 * callback.
283 */
284static int register_virtio_mem_device(struct virtio_mem *vm)
285{
286 int rc = 0;
287
288 /* First device registers the callback. */
289 mutex_lock(&virtio_mem_mutex);
290 if (list_empty(&virtio_mem_devices))
291 rc = set_online_page_callback(&virtio_mem_online_page_cb);
292 if (!rc)
293 list_add_rcu(&vm->next, &virtio_mem_devices);
294 mutex_unlock(&virtio_mem_mutex);
295
296 return rc;
297}
298
299/*
300 * Unregister a virtio-mem device so it will no longer be considered for the
301 * online_page callback.
302 */
303static void unregister_virtio_mem_device(struct virtio_mem *vm)
304{
305 /* Last device unregisters the callback. */
306 mutex_lock(&virtio_mem_mutex);
307 list_del_rcu(&vm->next);
308 if (list_empty(&virtio_mem_devices))
309 restore_online_page_callback(&virtio_mem_online_page_cb);
310 mutex_unlock(&virtio_mem_mutex);
311
312 synchronize_rcu();
313}
314
315/*
316 * Calculate the memory block id of a given address.
317 */
318static unsigned long virtio_mem_phys_to_mb_id(unsigned long addr)
319{
320 return addr / memory_block_size_bytes();
321}
322
323/*
324 * Calculate the physical start address of a given memory block id.
325 */
326static unsigned long virtio_mem_mb_id_to_phys(unsigned long mb_id)
327{
328 return mb_id * memory_block_size_bytes();
329}
330
331/*
332 * Calculate the big block id of a given address.
333 */
334static unsigned long virtio_mem_phys_to_bb_id(struct virtio_mem *vm,
335 uint64_t addr)
336{
337 return addr / vm->bbm.bb_size;
338}
339
340/*
341 * Calculate the physical start address of a given big block id.
342 */
343static uint64_t virtio_mem_bb_id_to_phys(struct virtio_mem *vm,
344 unsigned long bb_id)
345{
346 return bb_id * vm->bbm.bb_size;
347}
348
349/*
350 * Calculate the subblock id of a given address.
351 */
352static unsigned long virtio_mem_phys_to_sb_id(struct virtio_mem *vm,
353 unsigned long addr)
354{
355 const unsigned long mb_id = virtio_mem_phys_to_mb_id(addr);
356 const unsigned long mb_addr = virtio_mem_mb_id_to_phys(mb_id);
357
358 return (addr - mb_addr) / vm->sbm.sb_size;
359}
360
361/*
362 * Set the state of a big block, taking care of the state counter.
363 */
364static void virtio_mem_bbm_set_bb_state(struct virtio_mem *vm,
365 unsigned long bb_id,
366 enum virtio_mem_bbm_bb_state state)
367{
368 const unsigned long idx = bb_id - vm->bbm.first_bb_id;
369 enum virtio_mem_bbm_bb_state old_state;
370
371 old_state = vm->bbm.bb_states[idx];
372 vm->bbm.bb_states[idx] = state;
373
374 BUG_ON(vm->bbm.bb_count[old_state] == 0);
375 vm->bbm.bb_count[old_state]--;
376 vm->bbm.bb_count[state]++;
377}
378
379/*
380 * Get the state of a big block.
381 */
382static enum virtio_mem_bbm_bb_state virtio_mem_bbm_get_bb_state(struct virtio_mem *vm,
383 unsigned long bb_id)
384{
385 return vm->bbm.bb_states[bb_id - vm->bbm.first_bb_id];
386}
387
388/*
389 * Prepare the big block state array for the next big block.
390 */
391static int virtio_mem_bbm_bb_states_prepare_next_bb(struct virtio_mem *vm)
392{
393 unsigned long old_bytes = vm->bbm.next_bb_id - vm->bbm.first_bb_id;
394 unsigned long new_bytes = old_bytes + 1;
395 int old_pages = PFN_UP(old_bytes);
396 int new_pages = PFN_UP(new_bytes);
397 uint8_t *new_array;
398
399 if (vm->bbm.bb_states && old_pages == new_pages)
400 return 0;
401
402 new_array = vzalloc(new_pages * PAGE_SIZE);
403 if (!new_array)
404 return -ENOMEM;
405
406 mutex_lock(&vm->hotplug_mutex);
407 if (vm->bbm.bb_states)
408 memcpy(new_array, vm->bbm.bb_states, old_pages * PAGE_SIZE);
409 vfree(vm->bbm.bb_states);
410 vm->bbm.bb_states = new_array;
411 mutex_unlock(&vm->hotplug_mutex);
412
413 return 0;
414}
415
416#define virtio_mem_bbm_for_each_bb(_vm, _bb_id, _state) \
417 for (_bb_id = vm->bbm.first_bb_id; \
418 _bb_id < vm->bbm.next_bb_id && _vm->bbm.bb_count[_state]; \
419 _bb_id++) \
420 if (virtio_mem_bbm_get_bb_state(_vm, _bb_id) == _state)
421
422#define virtio_mem_bbm_for_each_bb_rev(_vm, _bb_id, _state) \
423 for (_bb_id = vm->bbm.next_bb_id - 1; \
424 _bb_id >= vm->bbm.first_bb_id && _vm->bbm.bb_count[_state]; \
425 _bb_id--) \
426 if (virtio_mem_bbm_get_bb_state(_vm, _bb_id) == _state)
427
428/*
429 * Set the state of a memory block, taking care of the state counter.
430 */
431static void virtio_mem_sbm_set_mb_state(struct virtio_mem *vm,
432 unsigned long mb_id, uint8_t state)
433{
434 const unsigned long idx = mb_id - vm->sbm.first_mb_id;
435 uint8_t old_state;
436
437 old_state = vm->sbm.mb_states[idx];
438 vm->sbm.mb_states[idx] = state;
439
440 BUG_ON(vm->sbm.mb_count[old_state] == 0);
441 vm->sbm.mb_count[old_state]--;
442 vm->sbm.mb_count[state]++;
443}
444
445/*
446 * Get the state of a memory block.
447 */
448static uint8_t virtio_mem_sbm_get_mb_state(struct virtio_mem *vm,
449 unsigned long mb_id)
450{
451 const unsigned long idx = mb_id - vm->sbm.first_mb_id;
452
453 return vm->sbm.mb_states[idx];
454}
455
456/*
457 * Prepare the state array for the next memory block.
458 */
459static int virtio_mem_sbm_mb_states_prepare_next_mb(struct virtio_mem *vm)
460{
461 int old_pages = PFN_UP(vm->sbm.next_mb_id - vm->sbm.first_mb_id);
462 int new_pages = PFN_UP(vm->sbm.next_mb_id - vm->sbm.first_mb_id + 1);
463 uint8_t *new_array;
464
465 if (vm->sbm.mb_states && old_pages == new_pages)
466 return 0;
467
468 new_array = vzalloc(new_pages * PAGE_SIZE);
469 if (!new_array)
470 return -ENOMEM;
471
472 mutex_lock(&vm->hotplug_mutex);
473 if (vm->sbm.mb_states)
474 memcpy(new_array, vm->sbm.mb_states, old_pages * PAGE_SIZE);
475 vfree(vm->sbm.mb_states);
476 vm->sbm.mb_states = new_array;
477 mutex_unlock(&vm->hotplug_mutex);
478
479 return 0;
480}
481
482#define virtio_mem_sbm_for_each_mb(_vm, _mb_id, _state) \
483 for (_mb_id = _vm->sbm.first_mb_id; \
484 _mb_id < _vm->sbm.next_mb_id && _vm->sbm.mb_count[_state]; \
485 _mb_id++) \
486 if (virtio_mem_sbm_get_mb_state(_vm, _mb_id) == _state)
487
488#define virtio_mem_sbm_for_each_mb_rev(_vm, _mb_id, _state) \
489 for (_mb_id = _vm->sbm.next_mb_id - 1; \
490 _mb_id >= _vm->sbm.first_mb_id && _vm->sbm.mb_count[_state]; \
491 _mb_id--) \
492 if (virtio_mem_sbm_get_mb_state(_vm, _mb_id) == _state)
493
494/*
495 * Calculate the bit number in the subblock bitmap for the given subblock
496 * inside the given memory block.
497 */
498static int virtio_mem_sbm_sb_state_bit_nr(struct virtio_mem *vm,
499 unsigned long mb_id, int sb_id)
500{
501 return (mb_id - vm->sbm.first_mb_id) * vm->sbm.sbs_per_mb + sb_id;
502}
503
504/*
505 * Mark all selected subblocks plugged.
506 *
507 * Will not modify the state of the memory block.
508 */
509static void virtio_mem_sbm_set_sb_plugged(struct virtio_mem *vm,
510 unsigned long mb_id, int sb_id,
511 int count)
512{
513 const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id);
514
515 __bitmap_set(vm->sbm.sb_states, bit, count);
516}
517
518/*
519 * Mark all selected subblocks unplugged.
520 *
521 * Will not modify the state of the memory block.
522 */
523static void virtio_mem_sbm_set_sb_unplugged(struct virtio_mem *vm,
524 unsigned long mb_id, int sb_id,
525 int count)
526{
527 const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id);
528
529 __bitmap_clear(vm->sbm.sb_states, bit, count);
530}
531
532/*
533 * Test if all selected subblocks are plugged.
534 */
535static bool virtio_mem_sbm_test_sb_plugged(struct virtio_mem *vm,
536 unsigned long mb_id, int sb_id,
537 int count)
538{
539 const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id);
540
541 if (count == 1)
542 return test_bit(bit, vm->sbm.sb_states);
543
544 /* TODO: Helper similar to bitmap_set() */
545 return find_next_zero_bit(vm->sbm.sb_states, bit + count, bit) >=
546 bit + count;
547}
548
549/*
550 * Test if all selected subblocks are unplugged.
551 */
552static bool virtio_mem_sbm_test_sb_unplugged(struct virtio_mem *vm,
553 unsigned long mb_id, int sb_id,
554 int count)
555{
556 const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id);
557
558 /* TODO: Helper similar to bitmap_set() */
559 return find_next_bit(vm->sbm.sb_states, bit + count, bit) >=
560 bit + count;
561}
562
563/*
564 * Find the first unplugged subblock. Returns vm->sbm.sbs_per_mb in case there is
565 * none.
566 */
567static int virtio_mem_sbm_first_unplugged_sb(struct virtio_mem *vm,
568 unsigned long mb_id)
569{
570 const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, 0);
571
572 return find_next_zero_bit(vm->sbm.sb_states,
573 bit + vm->sbm.sbs_per_mb, bit) - bit;
574}
575
576/*
577 * Prepare the subblock bitmap for the next memory block.
578 */
579static int virtio_mem_sbm_sb_states_prepare_next_mb(struct virtio_mem *vm)
580{
581 const unsigned long old_nb_mb = vm->sbm.next_mb_id - vm->sbm.first_mb_id;
582 const unsigned long old_nb_bits = old_nb_mb * vm->sbm.sbs_per_mb;
583 const unsigned long new_nb_bits = (old_nb_mb + 1) * vm->sbm.sbs_per_mb;
584 int old_pages = PFN_UP(BITS_TO_LONGS(old_nb_bits) * sizeof(long));
585 int new_pages = PFN_UP(BITS_TO_LONGS(new_nb_bits) * sizeof(long));
586 unsigned long *new_bitmap, *old_bitmap;
587
588 if (vm->sbm.sb_states && old_pages == new_pages)
589 return 0;
590
591 new_bitmap = vzalloc(new_pages * PAGE_SIZE);
592 if (!new_bitmap)
593 return -ENOMEM;
594
595 mutex_lock(&vm->hotplug_mutex);
596 if (vm->sbm.sb_states)
597 memcpy(new_bitmap, vm->sbm.sb_states, old_pages * PAGE_SIZE);
598
599 old_bitmap = vm->sbm.sb_states;
600 vm->sbm.sb_states = new_bitmap;
601 mutex_unlock(&vm->hotplug_mutex);
602
603 vfree(old_bitmap);
604 return 0;
605}
606
607/*
608 * Test if we could add memory without creating too much offline memory -
609 * to avoid running OOM if memory is getting onlined deferred.
610 */
611static bool virtio_mem_could_add_memory(struct virtio_mem *vm, uint64_t size)
612{
613 if (WARN_ON_ONCE(size > vm->offline_threshold))
614 return false;
615
616 return atomic64_read(&vm->offline_size) + size <= vm->offline_threshold;
617}
618
619/*
620 * Try adding memory to Linux. Will usually only fail if out of memory.
621 *
622 * Must not be called with the vm->hotplug_mutex held (possible deadlock with
623 * onlining code).
624 *
625 * Will not modify the state of memory blocks in virtio-mem.
626 */
627static int virtio_mem_add_memory(struct virtio_mem *vm, uint64_t addr,
628 uint64_t size)
629{
630 int rc;
631
632 /*
633 * When force-unloading the driver and we still have memory added to
634 * Linux, the resource name has to stay.
635 */
636 if (!vm->resource_name) {
637 vm->resource_name = kstrdup_const("System RAM (virtio_mem)",
638 GFP_KERNEL);
639 if (!vm->resource_name)
640 return -ENOMEM;
641 }
642
643 dev_dbg(&vm->vdev->dev, "adding memory: 0x%llx - 0x%llx\n", addr,
644 addr + size - 1);
645 /* Memory might get onlined immediately. */
646 atomic64_add(size, &vm->offline_size);
647 rc = add_memory_driver_managed(vm->mgid, addr, size, vm->resource_name,
648 MHP_MERGE_RESOURCE | MHP_NID_IS_MGID);
649 if (rc) {
650 atomic64_sub(size, &vm->offline_size);
651 dev_warn(&vm->vdev->dev, "adding memory failed: %d\n", rc);
652 /*
653 * TODO: Linux MM does not properly clean up yet in all cases
654 * where adding of memory failed - especially on -ENOMEM.
655 */
656 }
657 return rc;
658}
659
660/*
661 * See virtio_mem_add_memory(): Try adding a single Linux memory block.
662 */
663static int virtio_mem_sbm_add_mb(struct virtio_mem *vm, unsigned long mb_id)
664{
665 const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
666 const uint64_t size = memory_block_size_bytes();
667
668 return virtio_mem_add_memory(vm, addr, size);
669}
670
671/*
672 * See virtio_mem_add_memory(): Try adding a big block.
673 */
674static int virtio_mem_bbm_add_bb(struct virtio_mem *vm, unsigned long bb_id)
675{
676 const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id);
677 const uint64_t size = vm->bbm.bb_size;
678
679 return virtio_mem_add_memory(vm, addr, size);
680}
681
682/*
683 * Try removing memory from Linux. Will only fail if memory blocks aren't
684 * offline.
685 *
686 * Must not be called with the vm->hotplug_mutex held (possible deadlock with
687 * onlining code).
688 *
689 * Will not modify the state of memory blocks in virtio-mem.
690 */
691static int virtio_mem_remove_memory(struct virtio_mem *vm, uint64_t addr,
692 uint64_t size)
693{
694 int rc;
695
696 dev_dbg(&vm->vdev->dev, "removing memory: 0x%llx - 0x%llx\n", addr,
697 addr + size - 1);
698 rc = remove_memory(addr, size);
699 if (!rc) {
700 atomic64_sub(size, &vm->offline_size);
701 /*
702 * We might have freed up memory we can now unplug, retry
703 * immediately instead of waiting.
704 */
705 virtio_mem_retry(vm);
706 } else {
707 dev_dbg(&vm->vdev->dev, "removing memory failed: %d\n", rc);
708 }
709 return rc;
710}
711
712/*
713 * See virtio_mem_remove_memory(): Try removing a single Linux memory block.
714 */
715static int virtio_mem_sbm_remove_mb(struct virtio_mem *vm, unsigned long mb_id)
716{
717 const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
718 const uint64_t size = memory_block_size_bytes();
719
720 return virtio_mem_remove_memory(vm, addr, size);
721}
722
723/*
724 * Try offlining and removing memory from Linux.
725 *
726 * Must not be called with the vm->hotplug_mutex held (possible deadlock with
727 * onlining code).
728 *
729 * Will not modify the state of memory blocks in virtio-mem.
730 */
731static int virtio_mem_offline_and_remove_memory(struct virtio_mem *vm,
732 uint64_t addr,
733 uint64_t size)
734{
735 int rc;
736
737 dev_dbg(&vm->vdev->dev,
738 "offlining and removing memory: 0x%llx - 0x%llx\n", addr,
739 addr + size - 1);
740
741 rc = offline_and_remove_memory(addr, size);
742 if (!rc) {
743 atomic64_sub(size, &vm->offline_size);
744 /*
745 * We might have freed up memory we can now unplug, retry
746 * immediately instead of waiting.
747 */
748 virtio_mem_retry(vm);
749 } else {
750 dev_dbg(&vm->vdev->dev,
751 "offlining and removing memory failed: %d\n", rc);
752 }
753 return rc;
754}
755
756/*
757 * See virtio_mem_offline_and_remove_memory(): Try offlining and removing
758 * a single Linux memory block.
759 */
760static int virtio_mem_sbm_offline_and_remove_mb(struct virtio_mem *vm,
761 unsigned long mb_id)
762{
763 const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id);
764 const uint64_t size = memory_block_size_bytes();
765
766 return virtio_mem_offline_and_remove_memory(vm, addr, size);
767}
768
769/*
770 * See virtio_mem_offline_and_remove_memory(): Try to offline and remove a
771 * all Linux memory blocks covered by the big block.
772 */
773static int virtio_mem_bbm_offline_and_remove_bb(struct virtio_mem *vm,
774 unsigned long bb_id)
775{
776 const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id);
777 const uint64_t size = vm->bbm.bb_size;
778
779 return virtio_mem_offline_and_remove_memory(vm, addr, size);
780}
781
782/*
783 * Trigger the workqueue so the device can perform its magic.
784 */
785static void virtio_mem_retry(struct virtio_mem *vm)
786{
787 unsigned long flags;
788
789 spin_lock_irqsave(&vm->removal_lock, flags);
790 if (!vm->removing)
791 queue_work(system_freezable_wq, &vm->wq);
792 spin_unlock_irqrestore(&vm->removal_lock, flags);
793}
794
795static int virtio_mem_translate_node_id(struct virtio_mem *vm, uint16_t node_id)
796{
797 int node = NUMA_NO_NODE;
798
799#if defined(CONFIG_ACPI_NUMA)
800 if (virtio_has_feature(vm->vdev, VIRTIO_MEM_F_ACPI_PXM))
801 node = pxm_to_node(node_id);
802#endif
803 return node;
804}
805
806/*
807 * Test if a virtio-mem device overlaps with the given range. Can be called
808 * from (notifier) callbacks lockless.
809 */
810static bool virtio_mem_overlaps_range(struct virtio_mem *vm, uint64_t start,
811 uint64_t size)
812{
813 return start < vm->addr + vm->region_size && vm->addr < start + size;
814}
815
816/*
817 * Test if a virtio-mem device contains a given range. Can be called from
818 * (notifier) callbacks lockless.
819 */
820static bool virtio_mem_contains_range(struct virtio_mem *vm, uint64_t start,
821 uint64_t size)
822{
823 return start >= vm->addr && start + size <= vm->addr + vm->region_size;
824}
825
826static int virtio_mem_sbm_notify_going_online(struct virtio_mem *vm,
827 unsigned long mb_id)
828{
829 switch (virtio_mem_sbm_get_mb_state(vm, mb_id)) {
830 case VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL:
831 case VIRTIO_MEM_SBM_MB_OFFLINE:
832 return NOTIFY_OK;
833 default:
834 break;
835 }
836 dev_warn_ratelimited(&vm->vdev->dev,
837 "memory block onlining denied\n");
838 return NOTIFY_BAD;
839}
840
841static void virtio_mem_sbm_notify_offline(struct virtio_mem *vm,
842 unsigned long mb_id)
843{
844 switch (virtio_mem_sbm_get_mb_state(vm, mb_id)) {
845 case VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL:
846 case VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL:
847 virtio_mem_sbm_set_mb_state(vm, mb_id,
848 VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL);
849 break;
850 case VIRTIO_MEM_SBM_MB_KERNEL:
851 case VIRTIO_MEM_SBM_MB_MOVABLE:
852 virtio_mem_sbm_set_mb_state(vm, mb_id,
853 VIRTIO_MEM_SBM_MB_OFFLINE);
854 break;
855 default:
856 BUG();
857 break;
858 }
859}
860
861static void virtio_mem_sbm_notify_online(struct virtio_mem *vm,
862 unsigned long mb_id,
863 unsigned long start_pfn)
864{
865 const bool is_movable = is_zone_movable_page(pfn_to_page(start_pfn));
866 int new_state;
867
868 switch (virtio_mem_sbm_get_mb_state(vm, mb_id)) {
869 case VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL:
870 new_state = VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL;
871 if (is_movable)
872 new_state = VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL;
873 break;
874 case VIRTIO_MEM_SBM_MB_OFFLINE:
875 new_state = VIRTIO_MEM_SBM_MB_KERNEL;
876 if (is_movable)
877 new_state = VIRTIO_MEM_SBM_MB_MOVABLE;
878 break;
879 default:
880 BUG();
881 break;
882 }
883 virtio_mem_sbm_set_mb_state(vm, mb_id, new_state);
884}
885
886static void virtio_mem_sbm_notify_going_offline(struct virtio_mem *vm,
887 unsigned long mb_id)
888{
889 const unsigned long nr_pages = PFN_DOWN(vm->sbm.sb_size);
890 unsigned long pfn;
891 int sb_id;
892
893 for (sb_id = 0; sb_id < vm->sbm.sbs_per_mb; sb_id++) {
894 if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id, 1))
895 continue;
896 pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
897 sb_id * vm->sbm.sb_size);
898 virtio_mem_fake_offline_going_offline(pfn, nr_pages);
899 }
900}
901
902static void virtio_mem_sbm_notify_cancel_offline(struct virtio_mem *vm,
903 unsigned long mb_id)
904{
905 const unsigned long nr_pages = PFN_DOWN(vm->sbm.sb_size);
906 unsigned long pfn;
907 int sb_id;
908
909 for (sb_id = 0; sb_id < vm->sbm.sbs_per_mb; sb_id++) {
910 if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id, 1))
911 continue;
912 pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
913 sb_id * vm->sbm.sb_size);
914 virtio_mem_fake_offline_cancel_offline(pfn, nr_pages);
915 }
916}
917
918static void virtio_mem_bbm_notify_going_offline(struct virtio_mem *vm,
919 unsigned long bb_id,
920 unsigned long pfn,
921 unsigned long nr_pages)
922{
923 /*
924 * When marked as "fake-offline", all online memory of this device block
925 * is allocated by us. Otherwise, we don't have any memory allocated.
926 */
927 if (virtio_mem_bbm_get_bb_state(vm, bb_id) !=
928 VIRTIO_MEM_BBM_BB_FAKE_OFFLINE)
929 return;
930 virtio_mem_fake_offline_going_offline(pfn, nr_pages);
931}
932
933static void virtio_mem_bbm_notify_cancel_offline(struct virtio_mem *vm,
934 unsigned long bb_id,
935 unsigned long pfn,
936 unsigned long nr_pages)
937{
938 if (virtio_mem_bbm_get_bb_state(vm, bb_id) !=
939 VIRTIO_MEM_BBM_BB_FAKE_OFFLINE)
940 return;
941 virtio_mem_fake_offline_cancel_offline(pfn, nr_pages);
942}
943
944/*
945 * This callback will either be called synchronously from add_memory() or
946 * asynchronously (e.g., triggered via user space). We have to be careful
947 * with locking when calling add_memory().
948 */
949static int virtio_mem_memory_notifier_cb(struct notifier_block *nb,
950 unsigned long action, void *arg)
951{
952 struct virtio_mem *vm = container_of(nb, struct virtio_mem,
953 memory_notifier);
954 struct memory_notify *mhp = arg;
955 const unsigned long start = PFN_PHYS(mhp->start_pfn);
956 const unsigned long size = PFN_PHYS(mhp->nr_pages);
957 int rc = NOTIFY_OK;
958 unsigned long id;
959
960 if (!virtio_mem_overlaps_range(vm, start, size))
961 return NOTIFY_DONE;
962
963 if (vm->in_sbm) {
964 id = virtio_mem_phys_to_mb_id(start);
965 /*
966 * In SBM, we add memory in separate memory blocks - we expect
967 * it to be onlined/offlined in the same granularity. Bail out
968 * if this ever changes.
969 */
970 if (WARN_ON_ONCE(size != memory_block_size_bytes() ||
971 !IS_ALIGNED(start, memory_block_size_bytes())))
972 return NOTIFY_BAD;
973 } else {
974 id = virtio_mem_phys_to_bb_id(vm, start);
975 /*
976 * In BBM, we only care about onlining/offlining happening
977 * within a single big block, we don't care about the
978 * actual granularity as we don't track individual Linux
979 * memory blocks.
980 */
981 if (WARN_ON_ONCE(id != virtio_mem_phys_to_bb_id(vm, start + size - 1)))
982 return NOTIFY_BAD;
983 }
984
985 /*
986 * Avoid circular locking lockdep warnings. We lock the mutex
987 * e.g., in MEM_GOING_ONLINE and unlock it in MEM_ONLINE. The
988 * blocking_notifier_call_chain() has it's own lock, which gets unlocked
989 * between both notifier calls and will bail out. False positive.
990 */
991 lockdep_off();
992
993 switch (action) {
994 case MEM_GOING_OFFLINE:
995 mutex_lock(&vm->hotplug_mutex);
996 if (vm->removing) {
997 rc = notifier_from_errno(-EBUSY);
998 mutex_unlock(&vm->hotplug_mutex);
999 break;
1000 }
1001 vm->hotplug_active = true;
1002 if (vm->in_sbm)
1003 virtio_mem_sbm_notify_going_offline(vm, id);
1004 else
1005 virtio_mem_bbm_notify_going_offline(vm, id,
1006 mhp->start_pfn,
1007 mhp->nr_pages);
1008 break;
1009 case MEM_GOING_ONLINE:
1010 mutex_lock(&vm->hotplug_mutex);
1011 if (vm->removing) {
1012 rc = notifier_from_errno(-EBUSY);
1013 mutex_unlock(&vm->hotplug_mutex);
1014 break;
1015 }
1016 vm->hotplug_active = true;
1017 if (vm->in_sbm)
1018 rc = virtio_mem_sbm_notify_going_online(vm, id);
1019 break;
1020 case MEM_OFFLINE:
1021 if (vm->in_sbm)
1022 virtio_mem_sbm_notify_offline(vm, id);
1023
1024 atomic64_add(size, &vm->offline_size);
1025 /*
1026 * Trigger the workqueue. Now that we have some offline memory,
1027 * maybe we can handle pending unplug requests.
1028 */
1029 if (!unplug_online)
1030 virtio_mem_retry(vm);
1031
1032 vm->hotplug_active = false;
1033 mutex_unlock(&vm->hotplug_mutex);
1034 break;
1035 case MEM_ONLINE:
1036 if (vm->in_sbm)
1037 virtio_mem_sbm_notify_online(vm, id, mhp->start_pfn);
1038
1039 atomic64_sub(size, &vm->offline_size);
1040 /*
1041 * Start adding more memory once we onlined half of our
1042 * threshold. Don't trigger if it's possibly due to our actipn
1043 * (e.g., us adding memory which gets onlined immediately from
1044 * the core).
1045 */
1046 if (!atomic_read(&vm->wq_active) &&
1047 virtio_mem_could_add_memory(vm, vm->offline_threshold / 2))
1048 virtio_mem_retry(vm);
1049
1050 vm->hotplug_active = false;
1051 mutex_unlock(&vm->hotplug_mutex);
1052 break;
1053 case MEM_CANCEL_OFFLINE:
1054 if (!vm->hotplug_active)
1055 break;
1056 if (vm->in_sbm)
1057 virtio_mem_sbm_notify_cancel_offline(vm, id);
1058 else
1059 virtio_mem_bbm_notify_cancel_offline(vm, id,
1060 mhp->start_pfn,
1061 mhp->nr_pages);
1062 vm->hotplug_active = false;
1063 mutex_unlock(&vm->hotplug_mutex);
1064 break;
1065 case MEM_CANCEL_ONLINE:
1066 if (!vm->hotplug_active)
1067 break;
1068 vm->hotplug_active = false;
1069 mutex_unlock(&vm->hotplug_mutex);
1070 break;
1071 default:
1072 break;
1073 }
1074
1075 lockdep_on();
1076
1077 return rc;
1078}
1079
1080/*
1081 * Set a range of pages PG_offline. Remember pages that were never onlined
1082 * (via generic_online_page()) using PageDirty().
1083 */
1084static void virtio_mem_set_fake_offline(unsigned long pfn,
1085 unsigned long nr_pages, bool onlined)
1086{
1087 page_offline_begin();
1088 for (; nr_pages--; pfn++) {
1089 struct page *page = pfn_to_page(pfn);
1090
1091 __SetPageOffline(page);
1092 if (!onlined) {
1093 SetPageDirty(page);
1094 /* FIXME: remove after cleanups */
1095 ClearPageReserved(page);
1096 }
1097 }
1098 page_offline_end();
1099}
1100
1101/*
1102 * Clear PG_offline from a range of pages. If the pages were never onlined,
1103 * (via generic_online_page()), clear PageDirty().
1104 */
1105static void virtio_mem_clear_fake_offline(unsigned long pfn,
1106 unsigned long nr_pages, bool onlined)
1107{
1108 for (; nr_pages--; pfn++) {
1109 struct page *page = pfn_to_page(pfn);
1110
1111 __ClearPageOffline(page);
1112 if (!onlined)
1113 ClearPageDirty(page);
1114 }
1115}
1116
1117/*
1118 * Release a range of fake-offline pages to the buddy, effectively
1119 * fake-onlining them.
1120 */
1121static void virtio_mem_fake_online(unsigned long pfn, unsigned long nr_pages)
1122{
1123 unsigned long order = MAX_ORDER - 1;
1124 unsigned long i;
1125
1126 /*
1127 * We might get called for ranges that don't cover properly aligned
1128 * MAX_ORDER - 1 pages; however, we can only online properly aligned
1129 * pages with an order of MAX_ORDER - 1 at maximum.
1130 */
1131 while (!IS_ALIGNED(pfn | nr_pages, 1 << order))
1132 order--;
1133
1134 for (i = 0; i < nr_pages; i += 1 << order) {
1135 struct page *page = pfn_to_page(pfn + i);
1136
1137 /*
1138 * If the page is PageDirty(), it was kept fake-offline when
1139 * onlining the memory block. Otherwise, it was allocated
1140 * using alloc_contig_range(). All pages in a subblock are
1141 * alike.
1142 */
1143 if (PageDirty(page)) {
1144 virtio_mem_clear_fake_offline(pfn + i, 1 << order, false);
1145 generic_online_page(page, order);
1146 } else {
1147 virtio_mem_clear_fake_offline(pfn + i, 1 << order, true);
1148 free_contig_range(pfn + i, 1 << order);
1149 adjust_managed_page_count(page, 1 << order);
1150 }
1151 }
1152}
1153
1154/*
1155 * Try to allocate a range, marking pages fake-offline, effectively
1156 * fake-offlining them.
1157 */
1158static int virtio_mem_fake_offline(unsigned long pfn, unsigned long nr_pages)
1159{
1160 const bool is_movable = is_zone_movable_page(pfn_to_page(pfn));
1161 int rc, retry_count;
1162
1163 /*
1164 * TODO: We want an alloc_contig_range() mode that tries to allocate
1165 * harder (e.g., dealing with temporarily pinned pages, PCP), especially
1166 * with ZONE_MOVABLE. So for now, retry a couple of times with
1167 * ZONE_MOVABLE before giving up - because that zone is supposed to give
1168 * some guarantees.
1169 */
1170 for (retry_count = 0; retry_count < 5; retry_count++) {
1171 rc = alloc_contig_range(pfn, pfn + nr_pages, MIGRATE_MOVABLE,
1172 GFP_KERNEL);
1173 if (rc == -ENOMEM)
1174 /* whoops, out of memory */
1175 return rc;
1176 else if (rc && !is_movable)
1177 break;
1178 else if (rc)
1179 continue;
1180
1181 virtio_mem_set_fake_offline(pfn, nr_pages, true);
1182 adjust_managed_page_count(pfn_to_page(pfn), -nr_pages);
1183 return 0;
1184 }
1185
1186 return -EBUSY;
1187}
1188
1189/*
1190 * Handle fake-offline pages when memory is going offline - such that the
1191 * pages can be skipped by mm-core when offlining.
1192 */
1193static void virtio_mem_fake_offline_going_offline(unsigned long pfn,
1194 unsigned long nr_pages)
1195{
1196 struct page *page;
1197 unsigned long i;
1198
1199 /*
1200 * Drop our reference to the pages so the memory can get offlined
1201 * and add the unplugged pages to the managed page counters (so
1202 * offlining code can correctly subtract them again).
1203 */
1204 adjust_managed_page_count(pfn_to_page(pfn), nr_pages);
1205 /* Drop our reference to the pages so the memory can get offlined. */
1206 for (i = 0; i < nr_pages; i++) {
1207 page = pfn_to_page(pfn + i);
1208 if (WARN_ON(!page_ref_dec_and_test(page)))
1209 dump_page(page, "fake-offline page referenced");
1210 }
1211}
1212
1213/*
1214 * Handle fake-offline pages when memory offlining is canceled - to undo
1215 * what we did in virtio_mem_fake_offline_going_offline().
1216 */
1217static void virtio_mem_fake_offline_cancel_offline(unsigned long pfn,
1218 unsigned long nr_pages)
1219{
1220 unsigned long i;
1221
1222 /*
1223 * Get the reference we dropped when going offline and subtract the
1224 * unplugged pages from the managed page counters.
1225 */
1226 adjust_managed_page_count(pfn_to_page(pfn), -nr_pages);
1227 for (i = 0; i < nr_pages; i++)
1228 page_ref_inc(pfn_to_page(pfn + i));
1229}
1230
1231static void virtio_mem_online_page(struct virtio_mem *vm,
1232 struct page *page, unsigned int order)
1233{
1234 const unsigned long start = page_to_phys(page);
1235 const unsigned long end = start + PFN_PHYS(1 << order);
1236 unsigned long addr, next, id, sb_id, count;
1237 bool do_online;
1238
1239 /*
1240 * We can get called with any order up to MAX_ORDER - 1. If our
1241 * subblock size is smaller than that and we have a mixture of plugged
1242 * and unplugged subblocks within such a page, we have to process in
1243 * smaller granularity. In that case we'll adjust the order exactly once
1244 * within the loop.
1245 */
1246 for (addr = start; addr < end; ) {
1247 next = addr + PFN_PHYS(1 << order);
1248
1249 if (vm->in_sbm) {
1250 id = virtio_mem_phys_to_mb_id(addr);
1251 sb_id = virtio_mem_phys_to_sb_id(vm, addr);
1252 count = virtio_mem_phys_to_sb_id(vm, next - 1) - sb_id + 1;
1253
1254 if (virtio_mem_sbm_test_sb_plugged(vm, id, sb_id, count)) {
1255 /* Fully plugged. */
1256 do_online = true;
1257 } else if (count == 1 ||
1258 virtio_mem_sbm_test_sb_unplugged(vm, id, sb_id, count)) {
1259 /* Fully unplugged. */
1260 do_online = false;
1261 } else {
1262 /*
1263 * Mixture, process sub-blocks instead. This
1264 * will be at least the size of a pageblock.
1265 * We'll run into this case exactly once.
1266 */
1267 order = ilog2(vm->sbm.sb_size) - PAGE_SHIFT;
1268 do_online = virtio_mem_sbm_test_sb_plugged(vm, id, sb_id, 1);
1269 continue;
1270 }
1271 } else {
1272 /*
1273 * If the whole block is marked fake offline, keep
1274 * everything that way.
1275 */
1276 id = virtio_mem_phys_to_bb_id(vm, addr);
1277 do_online = virtio_mem_bbm_get_bb_state(vm, id) !=
1278 VIRTIO_MEM_BBM_BB_FAKE_OFFLINE;
1279 }
1280
1281 if (do_online)
1282 generic_online_page(pfn_to_page(PFN_DOWN(addr)), order);
1283 else
1284 virtio_mem_set_fake_offline(PFN_DOWN(addr), 1 << order,
1285 false);
1286 addr = next;
1287 }
1288}
1289
1290static void virtio_mem_online_page_cb(struct page *page, unsigned int order)
1291{
1292 const unsigned long addr = page_to_phys(page);
1293 struct virtio_mem *vm;
1294
1295 rcu_read_lock();
1296 list_for_each_entry_rcu(vm, &virtio_mem_devices, next) {
1297 /*
1298 * Pages we're onlining will never cross memory blocks and,
1299 * therefore, not virtio-mem devices.
1300 */
1301 if (!virtio_mem_contains_range(vm, addr, PFN_PHYS(1 << order)))
1302 continue;
1303
1304 /*
1305 * virtio_mem_set_fake_offline() might sleep. We can safely
1306 * drop the RCU lock at this point because the device
1307 * cannot go away. See virtio_mem_remove() how races
1308 * between memory onlining and device removal are handled.
1309 */
1310 rcu_read_unlock();
1311
1312 virtio_mem_online_page(vm, page, order);
1313 return;
1314 }
1315 rcu_read_unlock();
1316
1317 /* not virtio-mem memory, but e.g., a DIMM. online it */
1318 generic_online_page(page, order);
1319}
1320
1321static uint64_t virtio_mem_send_request(struct virtio_mem *vm,
1322 const struct virtio_mem_req *req)
1323{
1324 struct scatterlist *sgs[2], sg_req, sg_resp;
1325 unsigned int len;
1326 int rc;
1327
1328 /* don't use the request residing on the stack (vaddr) */
1329 vm->req = *req;
1330
1331 /* out: buffer for request */
1332 sg_init_one(&sg_req, &vm->req, sizeof(vm->req));
1333 sgs[0] = &sg_req;
1334
1335 /* in: buffer for response */
1336 sg_init_one(&sg_resp, &vm->resp, sizeof(vm->resp));
1337 sgs[1] = &sg_resp;
1338
1339 rc = virtqueue_add_sgs(vm->vq, sgs, 1, 1, vm, GFP_KERNEL);
1340 if (rc < 0)
1341 return rc;
1342
1343 virtqueue_kick(vm->vq);
1344
1345 /* wait for a response */
1346 wait_event(vm->host_resp, virtqueue_get_buf(vm->vq, &len));
1347
1348 return virtio16_to_cpu(vm->vdev, vm->resp.type);
1349}
1350
1351static int virtio_mem_send_plug_request(struct virtio_mem *vm, uint64_t addr,
1352 uint64_t size)
1353{
1354 const uint64_t nb_vm_blocks = size / vm->device_block_size;
1355 const struct virtio_mem_req req = {
1356 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_PLUG),
1357 .u.plug.addr = cpu_to_virtio64(vm->vdev, addr),
1358 .u.plug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
1359 };
1360 int rc = -ENOMEM;
1361
1362 if (atomic_read(&vm->config_changed))
1363 return -EAGAIN;
1364
1365 dev_dbg(&vm->vdev->dev, "plugging memory: 0x%llx - 0x%llx\n", addr,
1366 addr + size - 1);
1367
1368 switch (virtio_mem_send_request(vm, &req)) {
1369 case VIRTIO_MEM_RESP_ACK:
1370 vm->plugged_size += size;
1371 return 0;
1372 case VIRTIO_MEM_RESP_NACK:
1373 rc = -EAGAIN;
1374 break;
1375 case VIRTIO_MEM_RESP_BUSY:
1376 rc = -ETXTBSY;
1377 break;
1378 case VIRTIO_MEM_RESP_ERROR:
1379 rc = -EINVAL;
1380 break;
1381 default:
1382 break;
1383 }
1384
1385 dev_dbg(&vm->vdev->dev, "plugging memory failed: %d\n", rc);
1386 return rc;
1387}
1388
1389static int virtio_mem_send_unplug_request(struct virtio_mem *vm, uint64_t addr,
1390 uint64_t size)
1391{
1392 const uint64_t nb_vm_blocks = size / vm->device_block_size;
1393 const struct virtio_mem_req req = {
1394 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG),
1395 .u.unplug.addr = cpu_to_virtio64(vm->vdev, addr),
1396 .u.unplug.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
1397 };
1398 int rc = -ENOMEM;
1399
1400 if (atomic_read(&vm->config_changed))
1401 return -EAGAIN;
1402
1403 dev_dbg(&vm->vdev->dev, "unplugging memory: 0x%llx - 0x%llx\n", addr,
1404 addr + size - 1);
1405
1406 switch (virtio_mem_send_request(vm, &req)) {
1407 case VIRTIO_MEM_RESP_ACK:
1408 vm->plugged_size -= size;
1409 return 0;
1410 case VIRTIO_MEM_RESP_BUSY:
1411 rc = -ETXTBSY;
1412 break;
1413 case VIRTIO_MEM_RESP_ERROR:
1414 rc = -EINVAL;
1415 break;
1416 default:
1417 break;
1418 }
1419
1420 dev_dbg(&vm->vdev->dev, "unplugging memory failed: %d\n", rc);
1421 return rc;
1422}
1423
1424static int virtio_mem_send_unplug_all_request(struct virtio_mem *vm)
1425{
1426 const struct virtio_mem_req req = {
1427 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_UNPLUG_ALL),
1428 };
1429 int rc = -ENOMEM;
1430
1431 dev_dbg(&vm->vdev->dev, "unplugging all memory");
1432
1433 switch (virtio_mem_send_request(vm, &req)) {
1434 case VIRTIO_MEM_RESP_ACK:
1435 vm->unplug_all_required = false;
1436 vm->plugged_size = 0;
1437 /* usable region might have shrunk */
1438 atomic_set(&vm->config_changed, 1);
1439 return 0;
1440 case VIRTIO_MEM_RESP_BUSY:
1441 rc = -ETXTBSY;
1442 break;
1443 default:
1444 break;
1445 }
1446
1447 dev_dbg(&vm->vdev->dev, "unplugging all memory failed: %d\n", rc);
1448 return rc;
1449}
1450
1451/*
1452 * Plug selected subblocks. Updates the plugged state, but not the state
1453 * of the memory block.
1454 */
1455static int virtio_mem_sbm_plug_sb(struct virtio_mem *vm, unsigned long mb_id,
1456 int sb_id, int count)
1457{
1458 const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id) +
1459 sb_id * vm->sbm.sb_size;
1460 const uint64_t size = count * vm->sbm.sb_size;
1461 int rc;
1462
1463 rc = virtio_mem_send_plug_request(vm, addr, size);
1464 if (!rc)
1465 virtio_mem_sbm_set_sb_plugged(vm, mb_id, sb_id, count);
1466 return rc;
1467}
1468
1469/*
1470 * Unplug selected subblocks. Updates the plugged state, but not the state
1471 * of the memory block.
1472 */
1473static int virtio_mem_sbm_unplug_sb(struct virtio_mem *vm, unsigned long mb_id,
1474 int sb_id, int count)
1475{
1476 const uint64_t addr = virtio_mem_mb_id_to_phys(mb_id) +
1477 sb_id * vm->sbm.sb_size;
1478 const uint64_t size = count * vm->sbm.sb_size;
1479 int rc;
1480
1481 rc = virtio_mem_send_unplug_request(vm, addr, size);
1482 if (!rc)
1483 virtio_mem_sbm_set_sb_unplugged(vm, mb_id, sb_id, count);
1484 return rc;
1485}
1486
1487/*
1488 * Request to unplug a big block.
1489 *
1490 * Will not modify the state of the big block.
1491 */
1492static int virtio_mem_bbm_unplug_bb(struct virtio_mem *vm, unsigned long bb_id)
1493{
1494 const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id);
1495 const uint64_t size = vm->bbm.bb_size;
1496
1497 return virtio_mem_send_unplug_request(vm, addr, size);
1498}
1499
1500/*
1501 * Request to plug a big block.
1502 *
1503 * Will not modify the state of the big block.
1504 */
1505static int virtio_mem_bbm_plug_bb(struct virtio_mem *vm, unsigned long bb_id)
1506{
1507 const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id);
1508 const uint64_t size = vm->bbm.bb_size;
1509
1510 return virtio_mem_send_plug_request(vm, addr, size);
1511}
1512
1513/*
1514 * Unplug the desired number of plugged subblocks of a offline or not-added
1515 * memory block. Will fail if any subblock cannot get unplugged (instead of
1516 * skipping it).
1517 *
1518 * Will not modify the state of the memory block.
1519 *
1520 * Note: can fail after some subblocks were unplugged.
1521 */
1522static int virtio_mem_sbm_unplug_any_sb_raw(struct virtio_mem *vm,
1523 unsigned long mb_id, uint64_t *nb_sb)
1524{
1525 int sb_id, count;
1526 int rc;
1527
1528 sb_id = vm->sbm.sbs_per_mb - 1;
1529 while (*nb_sb) {
1530 /* Find the next candidate subblock */
1531 while (sb_id >= 0 &&
1532 virtio_mem_sbm_test_sb_unplugged(vm, mb_id, sb_id, 1))
1533 sb_id--;
1534 if (sb_id < 0)
1535 break;
1536 /* Try to unplug multiple subblocks at a time */
1537 count = 1;
1538 while (count < *nb_sb && sb_id > 0 &&
1539 virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id - 1, 1)) {
1540 count++;
1541 sb_id--;
1542 }
1543
1544 rc = virtio_mem_sbm_unplug_sb(vm, mb_id, sb_id, count);
1545 if (rc)
1546 return rc;
1547 *nb_sb -= count;
1548 sb_id--;
1549 }
1550
1551 return 0;
1552}
1553
1554/*
1555 * Unplug all plugged subblocks of an offline or not-added memory block.
1556 *
1557 * Will not modify the state of the memory block.
1558 *
1559 * Note: can fail after some subblocks were unplugged.
1560 */
1561static int virtio_mem_sbm_unplug_mb(struct virtio_mem *vm, unsigned long mb_id)
1562{
1563 uint64_t nb_sb = vm->sbm.sbs_per_mb;
1564
1565 return virtio_mem_sbm_unplug_any_sb_raw(vm, mb_id, &nb_sb);
1566}
1567
1568/*
1569 * Prepare tracking data for the next memory block.
1570 */
1571static int virtio_mem_sbm_prepare_next_mb(struct virtio_mem *vm,
1572 unsigned long *mb_id)
1573{
1574 int rc;
1575
1576 if (vm->sbm.next_mb_id > vm->sbm.last_usable_mb_id)
1577 return -ENOSPC;
1578
1579 /* Resize the state array if required. */
1580 rc = virtio_mem_sbm_mb_states_prepare_next_mb(vm);
1581 if (rc)
1582 return rc;
1583
1584 /* Resize the subblock bitmap if required. */
1585 rc = virtio_mem_sbm_sb_states_prepare_next_mb(vm);
1586 if (rc)
1587 return rc;
1588
1589 vm->sbm.mb_count[VIRTIO_MEM_SBM_MB_UNUSED]++;
1590 *mb_id = vm->sbm.next_mb_id++;
1591 return 0;
1592}
1593
1594/*
1595 * Try to plug the desired number of subblocks and add the memory block
1596 * to Linux.
1597 *
1598 * Will modify the state of the memory block.
1599 */
1600static int virtio_mem_sbm_plug_and_add_mb(struct virtio_mem *vm,
1601 unsigned long mb_id, uint64_t *nb_sb)
1602{
1603 const int count = min_t(int, *nb_sb, vm->sbm.sbs_per_mb);
1604 int rc;
1605
1606 if (WARN_ON_ONCE(!count))
1607 return -EINVAL;
1608
1609 /*
1610 * Plug the requested number of subblocks before adding it to linux,
1611 * so that onlining will directly online all plugged subblocks.
1612 */
1613 rc = virtio_mem_sbm_plug_sb(vm, mb_id, 0, count);
1614 if (rc)
1615 return rc;
1616
1617 /*
1618 * Mark the block properly offline before adding it to Linux,
1619 * so the memory notifiers will find the block in the right state.
1620 */
1621 if (count == vm->sbm.sbs_per_mb)
1622 virtio_mem_sbm_set_mb_state(vm, mb_id,
1623 VIRTIO_MEM_SBM_MB_OFFLINE);
1624 else
1625 virtio_mem_sbm_set_mb_state(vm, mb_id,
1626 VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL);
1627
1628 /* Add the memory block to linux - if that fails, try to unplug. */
1629 rc = virtio_mem_sbm_add_mb(vm, mb_id);
1630 if (rc) {
1631 int new_state = VIRTIO_MEM_SBM_MB_UNUSED;
1632
1633 if (virtio_mem_sbm_unplug_sb(vm, mb_id, 0, count))
1634 new_state = VIRTIO_MEM_SBM_MB_PLUGGED;
1635 virtio_mem_sbm_set_mb_state(vm, mb_id, new_state);
1636 return rc;
1637 }
1638
1639 *nb_sb -= count;
1640 return 0;
1641}
1642
1643/*
1644 * Try to plug the desired number of subblocks of a memory block that
1645 * is already added to Linux.
1646 *
1647 * Will modify the state of the memory block.
1648 *
1649 * Note: Can fail after some subblocks were successfully plugged.
1650 */
1651static int virtio_mem_sbm_plug_any_sb(struct virtio_mem *vm,
1652 unsigned long mb_id, uint64_t *nb_sb)
1653{
1654 const int old_state = virtio_mem_sbm_get_mb_state(vm, mb_id);
1655 unsigned long pfn, nr_pages;
1656 int sb_id, count;
1657 int rc;
1658
1659 if (WARN_ON_ONCE(!*nb_sb))
1660 return -EINVAL;
1661
1662 while (*nb_sb) {
1663 sb_id = virtio_mem_sbm_first_unplugged_sb(vm, mb_id);
1664 if (sb_id >= vm->sbm.sbs_per_mb)
1665 break;
1666 count = 1;
1667 while (count < *nb_sb &&
1668 sb_id + count < vm->sbm.sbs_per_mb &&
1669 !virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id + count, 1))
1670 count++;
1671
1672 rc = virtio_mem_sbm_plug_sb(vm, mb_id, sb_id, count);
1673 if (rc)
1674 return rc;
1675 *nb_sb -= count;
1676 if (old_state == VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL)
1677 continue;
1678
1679 /* fake-online the pages if the memory block is online */
1680 pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
1681 sb_id * vm->sbm.sb_size);
1682 nr_pages = PFN_DOWN(count * vm->sbm.sb_size);
1683 virtio_mem_fake_online(pfn, nr_pages);
1684 }
1685
1686 if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb))
1687 virtio_mem_sbm_set_mb_state(vm, mb_id, old_state - 1);
1688
1689 return 0;
1690}
1691
1692static int virtio_mem_sbm_plug_request(struct virtio_mem *vm, uint64_t diff)
1693{
1694 const int mb_states[] = {
1695 VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL,
1696 VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL,
1697 VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL,
1698 };
1699 uint64_t nb_sb = diff / vm->sbm.sb_size;
1700 unsigned long mb_id;
1701 int rc, i;
1702
1703 if (!nb_sb)
1704 return 0;
1705
1706 /* Don't race with onlining/offlining */
1707 mutex_lock(&vm->hotplug_mutex);
1708
1709 for (i = 0; i < ARRAY_SIZE(mb_states); i++) {
1710 virtio_mem_sbm_for_each_mb(vm, mb_id, mb_states[i]) {
1711 rc = virtio_mem_sbm_plug_any_sb(vm, mb_id, &nb_sb);
1712 if (rc || !nb_sb)
1713 goto out_unlock;
1714 cond_resched();
1715 }
1716 }
1717
1718 /*
1719 * We won't be working on online/offline memory blocks from this point,
1720 * so we can't race with memory onlining/offlining. Drop the mutex.
1721 */
1722 mutex_unlock(&vm->hotplug_mutex);
1723
1724 /* Try to plug and add unused blocks */
1725 virtio_mem_sbm_for_each_mb(vm, mb_id, VIRTIO_MEM_SBM_MB_UNUSED) {
1726 if (!virtio_mem_could_add_memory(vm, memory_block_size_bytes()))
1727 return -ENOSPC;
1728
1729 rc = virtio_mem_sbm_plug_and_add_mb(vm, mb_id, &nb_sb);
1730 if (rc || !nb_sb)
1731 return rc;
1732 cond_resched();
1733 }
1734
1735 /* Try to prepare, plug and add new blocks */
1736 while (nb_sb) {
1737 if (!virtio_mem_could_add_memory(vm, memory_block_size_bytes()))
1738 return -ENOSPC;
1739
1740 rc = virtio_mem_sbm_prepare_next_mb(vm, &mb_id);
1741 if (rc)
1742 return rc;
1743 rc = virtio_mem_sbm_plug_and_add_mb(vm, mb_id, &nb_sb);
1744 if (rc)
1745 return rc;
1746 cond_resched();
1747 }
1748
1749 return 0;
1750out_unlock:
1751 mutex_unlock(&vm->hotplug_mutex);
1752 return rc;
1753}
1754
1755/*
1756 * Plug a big block and add it to Linux.
1757 *
1758 * Will modify the state of the big block.
1759 */
1760static int virtio_mem_bbm_plug_and_add_bb(struct virtio_mem *vm,
1761 unsigned long bb_id)
1762{
1763 int rc;
1764
1765 if (WARN_ON_ONCE(virtio_mem_bbm_get_bb_state(vm, bb_id) !=
1766 VIRTIO_MEM_BBM_BB_UNUSED))
1767 return -EINVAL;
1768
1769 rc = virtio_mem_bbm_plug_bb(vm, bb_id);
1770 if (rc)
1771 return rc;
1772 virtio_mem_bbm_set_bb_state(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED);
1773
1774 rc = virtio_mem_bbm_add_bb(vm, bb_id);
1775 if (rc) {
1776 if (!virtio_mem_bbm_unplug_bb(vm, bb_id))
1777 virtio_mem_bbm_set_bb_state(vm, bb_id,
1778 VIRTIO_MEM_BBM_BB_UNUSED);
1779 else
1780 /* Retry from the main loop. */
1781 virtio_mem_bbm_set_bb_state(vm, bb_id,
1782 VIRTIO_MEM_BBM_BB_PLUGGED);
1783 return rc;
1784 }
1785 return 0;
1786}
1787
1788/*
1789 * Prepare tracking data for the next big block.
1790 */
1791static int virtio_mem_bbm_prepare_next_bb(struct virtio_mem *vm,
1792 unsigned long *bb_id)
1793{
1794 int rc;
1795
1796 if (vm->bbm.next_bb_id > vm->bbm.last_usable_bb_id)
1797 return -ENOSPC;
1798
1799 /* Resize the big block state array if required. */
1800 rc = virtio_mem_bbm_bb_states_prepare_next_bb(vm);
1801 if (rc)
1802 return rc;
1803
1804 vm->bbm.bb_count[VIRTIO_MEM_BBM_BB_UNUSED]++;
1805 *bb_id = vm->bbm.next_bb_id;
1806 vm->bbm.next_bb_id++;
1807 return 0;
1808}
1809
1810static int virtio_mem_bbm_plug_request(struct virtio_mem *vm, uint64_t diff)
1811{
1812 uint64_t nb_bb = diff / vm->bbm.bb_size;
1813 unsigned long bb_id;
1814 int rc;
1815
1816 if (!nb_bb)
1817 return 0;
1818
1819 /* Try to plug and add unused big blocks */
1820 virtio_mem_bbm_for_each_bb(vm, bb_id, VIRTIO_MEM_BBM_BB_UNUSED) {
1821 if (!virtio_mem_could_add_memory(vm, vm->bbm.bb_size))
1822 return -ENOSPC;
1823
1824 rc = virtio_mem_bbm_plug_and_add_bb(vm, bb_id);
1825 if (!rc)
1826 nb_bb--;
1827 if (rc || !nb_bb)
1828 return rc;
1829 cond_resched();
1830 }
1831
1832 /* Try to prepare, plug and add new big blocks */
1833 while (nb_bb) {
1834 if (!virtio_mem_could_add_memory(vm, vm->bbm.bb_size))
1835 return -ENOSPC;
1836
1837 rc = virtio_mem_bbm_prepare_next_bb(vm, &bb_id);
1838 if (rc)
1839 return rc;
1840 rc = virtio_mem_bbm_plug_and_add_bb(vm, bb_id);
1841 if (!rc)
1842 nb_bb--;
1843 if (rc)
1844 return rc;
1845 cond_resched();
1846 }
1847
1848 return 0;
1849}
1850
1851/*
1852 * Try to plug the requested amount of memory.
1853 */
1854static int virtio_mem_plug_request(struct virtio_mem *vm, uint64_t diff)
1855{
1856 if (vm->in_sbm)
1857 return virtio_mem_sbm_plug_request(vm, diff);
1858 return virtio_mem_bbm_plug_request(vm, diff);
1859}
1860
1861/*
1862 * Unplug the desired number of plugged subblocks of an offline memory block.
1863 * Will fail if any subblock cannot get unplugged (instead of skipping it).
1864 *
1865 * Will modify the state of the memory block. Might temporarily drop the
1866 * hotplug_mutex.
1867 *
1868 * Note: Can fail after some subblocks were successfully unplugged.
1869 */
1870static int virtio_mem_sbm_unplug_any_sb_offline(struct virtio_mem *vm,
1871 unsigned long mb_id,
1872 uint64_t *nb_sb)
1873{
1874 int rc;
1875
1876 rc = virtio_mem_sbm_unplug_any_sb_raw(vm, mb_id, nb_sb);
1877
1878 /* some subblocks might have been unplugged even on failure */
1879 if (!virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb))
1880 virtio_mem_sbm_set_mb_state(vm, mb_id,
1881 VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL);
1882 if (rc)
1883 return rc;
1884
1885 if (virtio_mem_sbm_test_sb_unplugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) {
1886 /*
1887 * Remove the block from Linux - this should never fail.
1888 * Hinder the block from getting onlined by marking it
1889 * unplugged. Temporarily drop the mutex, so
1890 * any pending GOING_ONLINE requests can be serviced/rejected.
1891 */
1892 virtio_mem_sbm_set_mb_state(vm, mb_id,
1893 VIRTIO_MEM_SBM_MB_UNUSED);
1894
1895 mutex_unlock(&vm->hotplug_mutex);
1896 rc = virtio_mem_sbm_remove_mb(vm, mb_id);
1897 BUG_ON(rc);
1898 mutex_lock(&vm->hotplug_mutex);
1899 }
1900 return 0;
1901}
1902
1903/*
1904 * Unplug the given plugged subblocks of an online memory block.
1905 *
1906 * Will modify the state of the memory block.
1907 */
1908static int virtio_mem_sbm_unplug_sb_online(struct virtio_mem *vm,
1909 unsigned long mb_id, int sb_id,
1910 int count)
1911{
1912 const unsigned long nr_pages = PFN_DOWN(vm->sbm.sb_size) * count;
1913 const int old_state = virtio_mem_sbm_get_mb_state(vm, mb_id);
1914 unsigned long start_pfn;
1915 int rc;
1916
1917 start_pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
1918 sb_id * vm->sbm.sb_size);
1919
1920 rc = virtio_mem_fake_offline(start_pfn, nr_pages);
1921 if (rc)
1922 return rc;
1923
1924 /* Try to unplug the allocated memory */
1925 rc = virtio_mem_sbm_unplug_sb(vm, mb_id, sb_id, count);
1926 if (rc) {
1927 /* Return the memory to the buddy. */
1928 virtio_mem_fake_online(start_pfn, nr_pages);
1929 return rc;
1930 }
1931
1932 switch (old_state) {
1933 case VIRTIO_MEM_SBM_MB_KERNEL:
1934 virtio_mem_sbm_set_mb_state(vm, mb_id,
1935 VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL);
1936 break;
1937 case VIRTIO_MEM_SBM_MB_MOVABLE:
1938 virtio_mem_sbm_set_mb_state(vm, mb_id,
1939 VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL);
1940 break;
1941 }
1942
1943 return 0;
1944}
1945
1946/*
1947 * Unplug the desired number of plugged subblocks of an online memory block.
1948 * Will skip subblock that are busy.
1949 *
1950 * Will modify the state of the memory block. Might temporarily drop the
1951 * hotplug_mutex.
1952 *
1953 * Note: Can fail after some subblocks were successfully unplugged. Can
1954 * return 0 even if subblocks were busy and could not get unplugged.
1955 */
1956static int virtio_mem_sbm_unplug_any_sb_online(struct virtio_mem *vm,
1957 unsigned long mb_id,
1958 uint64_t *nb_sb)
1959{
1960 int rc, sb_id;
1961
1962 /* If possible, try to unplug the complete block in one shot. */
1963 if (*nb_sb >= vm->sbm.sbs_per_mb &&
1964 virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) {
1965 rc = virtio_mem_sbm_unplug_sb_online(vm, mb_id, 0,
1966 vm->sbm.sbs_per_mb);
1967 if (!rc) {
1968 *nb_sb -= vm->sbm.sbs_per_mb;
1969 goto unplugged;
1970 } else if (rc != -EBUSY)
1971 return rc;
1972 }
1973
1974 /* Fallback to single subblocks. */
1975 for (sb_id = vm->sbm.sbs_per_mb - 1; sb_id >= 0 && *nb_sb; sb_id--) {
1976 /* Find the next candidate subblock */
1977 while (sb_id >= 0 &&
1978 !virtio_mem_sbm_test_sb_plugged(vm, mb_id, sb_id, 1))
1979 sb_id--;
1980 if (sb_id < 0)
1981 break;
1982
1983 rc = virtio_mem_sbm_unplug_sb_online(vm, mb_id, sb_id, 1);
1984 if (rc == -EBUSY)
1985 continue;
1986 else if (rc)
1987 return rc;
1988 *nb_sb -= 1;
1989 }
1990
1991unplugged:
1992 /*
1993 * Once all subblocks of a memory block were unplugged, offline and
1994 * remove it. This will usually not fail, as no memory is in use
1995 * anymore - however some other notifiers might NACK the request.
1996 */
1997 if (virtio_mem_sbm_test_sb_unplugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) {
1998 mutex_unlock(&vm->hotplug_mutex);
1999 rc = virtio_mem_sbm_offline_and_remove_mb(vm, mb_id);
2000 mutex_lock(&vm->hotplug_mutex);
2001 if (!rc)
2002 virtio_mem_sbm_set_mb_state(vm, mb_id,
2003 VIRTIO_MEM_SBM_MB_UNUSED);
2004 }
2005
2006 return 0;
2007}
2008
2009/*
2010 * Unplug the desired number of plugged subblocks of a memory block that is
2011 * already added to Linux. Will skip subblock of online memory blocks that are
2012 * busy (by the OS). Will fail if any subblock that's not busy cannot get
2013 * unplugged.
2014 *
2015 * Will modify the state of the memory block. Might temporarily drop the
2016 * hotplug_mutex.
2017 *
2018 * Note: Can fail after some subblocks were successfully unplugged. Can
2019 * return 0 even if subblocks were busy and could not get unplugged.
2020 */
2021static int virtio_mem_sbm_unplug_any_sb(struct virtio_mem *vm,
2022 unsigned long mb_id,
2023 uint64_t *nb_sb)
2024{
2025 const int old_state = virtio_mem_sbm_get_mb_state(vm, mb_id);
2026
2027 switch (old_state) {
2028 case VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL:
2029 case VIRTIO_MEM_SBM_MB_KERNEL:
2030 case VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL:
2031 case VIRTIO_MEM_SBM_MB_MOVABLE:
2032 return virtio_mem_sbm_unplug_any_sb_online(vm, mb_id, nb_sb);
2033 case VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL:
2034 case VIRTIO_MEM_SBM_MB_OFFLINE:
2035 return virtio_mem_sbm_unplug_any_sb_offline(vm, mb_id, nb_sb);
2036 }
2037 return -EINVAL;
2038}
2039
2040static int virtio_mem_sbm_unplug_request(struct virtio_mem *vm, uint64_t diff)
2041{
2042 const int mb_states[] = {
2043 VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL,
2044 VIRTIO_MEM_SBM_MB_OFFLINE,
2045 VIRTIO_MEM_SBM_MB_MOVABLE_PARTIAL,
2046 VIRTIO_MEM_SBM_MB_KERNEL_PARTIAL,
2047 VIRTIO_MEM_SBM_MB_MOVABLE,
2048 VIRTIO_MEM_SBM_MB_KERNEL,
2049 };
2050 uint64_t nb_sb = diff / vm->sbm.sb_size;
2051 unsigned long mb_id;
2052 int rc, i;
2053
2054 if (!nb_sb)
2055 return 0;
2056
2057 /*
2058 * We'll drop the mutex a couple of times when it is safe to do so.
2059 * This might result in some blocks switching the state (online/offline)
2060 * and we could miss them in this run - we will retry again later.
2061 */
2062 mutex_lock(&vm->hotplug_mutex);
2063
2064 /*
2065 * We try unplug from partially plugged blocks first, to try removing
2066 * whole memory blocks along with metadata. We prioritize ZONE_MOVABLE
2067 * as it's more reliable to unplug memory and remove whole memory
2068 * blocks, and we don't want to trigger a zone imbalances by
2069 * accidentially removing too much kernel memory.
2070 */
2071 for (i = 0; i < ARRAY_SIZE(mb_states); i++) {
2072 virtio_mem_sbm_for_each_mb_rev(vm, mb_id, mb_states[i]) {
2073 rc = virtio_mem_sbm_unplug_any_sb(vm, mb_id, &nb_sb);
2074 if (rc || !nb_sb)
2075 goto out_unlock;
2076 mutex_unlock(&vm->hotplug_mutex);
2077 cond_resched();
2078 mutex_lock(&vm->hotplug_mutex);
2079 }
2080 if (!unplug_online && i == 1) {
2081 mutex_unlock(&vm->hotplug_mutex);
2082 return 0;
2083 }
2084 }
2085
2086 mutex_unlock(&vm->hotplug_mutex);
2087 return nb_sb ? -EBUSY : 0;
2088out_unlock:
2089 mutex_unlock(&vm->hotplug_mutex);
2090 return rc;
2091}
2092
2093/*
2094 * Try to offline and remove a big block from Linux and unplug it. Will fail
2095 * with -EBUSY if some memory is busy and cannot get unplugged.
2096 *
2097 * Will modify the state of the memory block. Might temporarily drop the
2098 * hotplug_mutex.
2099 */
2100static int virtio_mem_bbm_offline_remove_and_unplug_bb(struct virtio_mem *vm,
2101 unsigned long bb_id)
2102{
2103 const unsigned long start_pfn = PFN_DOWN(virtio_mem_bb_id_to_phys(vm, bb_id));
2104 const unsigned long nr_pages = PFN_DOWN(vm->bbm.bb_size);
2105 unsigned long end_pfn = start_pfn + nr_pages;
2106 unsigned long pfn;
2107 struct page *page;
2108 int rc;
2109
2110 if (WARN_ON_ONCE(virtio_mem_bbm_get_bb_state(vm, bb_id) !=
2111 VIRTIO_MEM_BBM_BB_ADDED))
2112 return -EINVAL;
2113
2114 if (bbm_safe_unplug) {
2115 /*
2116 * Start by fake-offlining all memory. Once we marked the device
2117 * block as fake-offline, all newly onlined memory will
2118 * automatically be kept fake-offline. Protect from concurrent
2119 * onlining/offlining until we have a consistent state.
2120 */
2121 mutex_lock(&vm->hotplug_mutex);
2122 virtio_mem_bbm_set_bb_state(vm, bb_id,
2123 VIRTIO_MEM_BBM_BB_FAKE_OFFLINE);
2124
2125 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2126 page = pfn_to_online_page(pfn);
2127 if (!page)
2128 continue;
2129
2130 rc = virtio_mem_fake_offline(pfn, PAGES_PER_SECTION);
2131 if (rc) {
2132 end_pfn = pfn;
2133 goto rollback_safe_unplug;
2134 }
2135 }
2136 mutex_unlock(&vm->hotplug_mutex);
2137 }
2138
2139 rc = virtio_mem_bbm_offline_and_remove_bb(vm, bb_id);
2140 if (rc) {
2141 if (bbm_safe_unplug) {
2142 mutex_lock(&vm->hotplug_mutex);
2143 goto rollback_safe_unplug;
2144 }
2145 return rc;
2146 }
2147
2148 rc = virtio_mem_bbm_unplug_bb(vm, bb_id);
2149 if (rc)
2150 virtio_mem_bbm_set_bb_state(vm, bb_id,
2151 VIRTIO_MEM_BBM_BB_PLUGGED);
2152 else
2153 virtio_mem_bbm_set_bb_state(vm, bb_id,
2154 VIRTIO_MEM_BBM_BB_UNUSED);
2155 return rc;
2156
2157rollback_safe_unplug:
2158 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
2159 page = pfn_to_online_page(pfn);
2160 if (!page)
2161 continue;
2162 virtio_mem_fake_online(pfn, PAGES_PER_SECTION);
2163 }
2164 virtio_mem_bbm_set_bb_state(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED);
2165 mutex_unlock(&vm->hotplug_mutex);
2166 return rc;
2167}
2168
2169/*
2170 * Test if a big block is completely offline.
2171 */
2172static bool virtio_mem_bbm_bb_is_offline(struct virtio_mem *vm,
2173 unsigned long bb_id)
2174{
2175 const unsigned long start_pfn = PFN_DOWN(virtio_mem_bb_id_to_phys(vm, bb_id));
2176 const unsigned long nr_pages = PFN_DOWN(vm->bbm.bb_size);
2177 unsigned long pfn;
2178
2179 for (pfn = start_pfn; pfn < start_pfn + nr_pages;
2180 pfn += PAGES_PER_SECTION) {
2181 if (pfn_to_online_page(pfn))
2182 return false;
2183 }
2184
2185 return true;
2186}
2187
2188/*
2189 * Test if a big block is completely onlined to ZONE_MOVABLE (or offline).
2190 */
2191static bool virtio_mem_bbm_bb_is_movable(struct virtio_mem *vm,
2192 unsigned long bb_id)
2193{
2194 const unsigned long start_pfn = PFN_DOWN(virtio_mem_bb_id_to_phys(vm, bb_id));
2195 const unsigned long nr_pages = PFN_DOWN(vm->bbm.bb_size);
2196 struct page *page;
2197 unsigned long pfn;
2198
2199 for (pfn = start_pfn; pfn < start_pfn + nr_pages;
2200 pfn += PAGES_PER_SECTION) {
2201 page = pfn_to_online_page(pfn);
2202 if (!page)
2203 continue;
2204 if (page_zonenum(page) != ZONE_MOVABLE)
2205 return false;
2206 }
2207
2208 return true;
2209}
2210
2211static int virtio_mem_bbm_unplug_request(struct virtio_mem *vm, uint64_t diff)
2212{
2213 uint64_t nb_bb = diff / vm->bbm.bb_size;
2214 uint64_t bb_id;
2215 int rc, i;
2216
2217 if (!nb_bb)
2218 return 0;
2219
2220 /*
2221 * Try to unplug big blocks. Similar to SBM, start with offline
2222 * big blocks.
2223 */
2224 for (i = 0; i < 3; i++) {
2225 virtio_mem_bbm_for_each_bb_rev(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED) {
2226 cond_resched();
2227
2228 /*
2229 * As we're holding no locks, these checks are racy,
2230 * but we don't care.
2231 */
2232 if (i == 0 && !virtio_mem_bbm_bb_is_offline(vm, bb_id))
2233 continue;
2234 if (i == 1 && !virtio_mem_bbm_bb_is_movable(vm, bb_id))
2235 continue;
2236 rc = virtio_mem_bbm_offline_remove_and_unplug_bb(vm, bb_id);
2237 if (rc == -EBUSY)
2238 continue;
2239 if (!rc)
2240 nb_bb--;
2241 if (rc || !nb_bb)
2242 return rc;
2243 }
2244 if (i == 0 && !unplug_online)
2245 return 0;
2246 }
2247
2248 return nb_bb ? -EBUSY : 0;
2249}
2250
2251/*
2252 * Try to unplug the requested amount of memory.
2253 */
2254static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff)
2255{
2256 if (vm->in_sbm)
2257 return virtio_mem_sbm_unplug_request(vm, diff);
2258 return virtio_mem_bbm_unplug_request(vm, diff);
2259}
2260
2261/*
2262 * Try to unplug all blocks that couldn't be unplugged before, for example,
2263 * because the hypervisor was busy.
2264 */
2265static int virtio_mem_unplug_pending_mb(struct virtio_mem *vm)
2266{
2267 unsigned long id;
2268 int rc;
2269
2270 if (!vm->in_sbm) {
2271 virtio_mem_bbm_for_each_bb(vm, id,
2272 VIRTIO_MEM_BBM_BB_PLUGGED) {
2273 rc = virtio_mem_bbm_unplug_bb(vm, id);
2274 if (rc)
2275 return rc;
2276 virtio_mem_bbm_set_bb_state(vm, id,
2277 VIRTIO_MEM_BBM_BB_UNUSED);
2278 }
2279 return 0;
2280 }
2281
2282 virtio_mem_sbm_for_each_mb(vm, id, VIRTIO_MEM_SBM_MB_PLUGGED) {
2283 rc = virtio_mem_sbm_unplug_mb(vm, id);
2284 if (rc)
2285 return rc;
2286 virtio_mem_sbm_set_mb_state(vm, id,
2287 VIRTIO_MEM_SBM_MB_UNUSED);
2288 }
2289
2290 return 0;
2291}
2292
2293/*
2294 * Update all parts of the config that could have changed.
2295 */
2296static void virtio_mem_refresh_config(struct virtio_mem *vm)
2297{
2298 const struct range pluggable_range = mhp_get_pluggable_range(true);
2299 uint64_t new_plugged_size, usable_region_size, end_addr;
2300
2301 /* the plugged_size is just a reflection of what _we_ did previously */
2302 virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
2303 &new_plugged_size);
2304 if (WARN_ON_ONCE(new_plugged_size != vm->plugged_size))
2305 vm->plugged_size = new_plugged_size;
2306
2307 /* calculate the last usable memory block id */
2308 virtio_cread_le(vm->vdev, struct virtio_mem_config,
2309 usable_region_size, &usable_region_size);
2310 end_addr = min(vm->addr + usable_region_size - 1,
2311 pluggable_range.end);
2312
2313 if (vm->in_sbm) {
2314 vm->sbm.last_usable_mb_id = virtio_mem_phys_to_mb_id(end_addr);
2315 if (!IS_ALIGNED(end_addr + 1, memory_block_size_bytes()))
2316 vm->sbm.last_usable_mb_id--;
2317 } else {
2318 vm->bbm.last_usable_bb_id = virtio_mem_phys_to_bb_id(vm,
2319 end_addr);
2320 if (!IS_ALIGNED(end_addr + 1, vm->bbm.bb_size))
2321 vm->bbm.last_usable_bb_id--;
2322 }
2323 /*
2324 * If we cannot plug any of our device memory (e.g., nothing in the
2325 * usable region is addressable), the last usable memory block id will
2326 * be smaller than the first usable memory block id. We'll stop
2327 * attempting to add memory with -ENOSPC from our main loop.
2328 */
2329
2330 /* see if there is a request to change the size */
2331 virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size,
2332 &vm->requested_size);
2333
2334 dev_info(&vm->vdev->dev, "plugged size: 0x%llx", vm->plugged_size);
2335 dev_info(&vm->vdev->dev, "requested size: 0x%llx", vm->requested_size);
2336}
2337
2338/*
2339 * Workqueue function for handling plug/unplug requests and config updates.
2340 */
2341static void virtio_mem_run_wq(struct work_struct *work)
2342{
2343 struct virtio_mem *vm = container_of(work, struct virtio_mem, wq);
2344 uint64_t diff;
2345 int rc;
2346
2347 if (unlikely(vm->in_kdump)) {
2348 dev_warn_once(&vm->vdev->dev,
2349 "unexpected workqueue run in kdump kernel\n");
2350 return;
2351 }
2352
2353 hrtimer_cancel(&vm->retry_timer);
2354
2355 if (vm->broken)
2356 return;
2357
2358 atomic_set(&vm->wq_active, 1);
2359retry:
2360 rc = 0;
2361
2362 /* Make sure we start with a clean state if there are leftovers. */
2363 if (unlikely(vm->unplug_all_required))
2364 rc = virtio_mem_send_unplug_all_request(vm);
2365
2366 if (atomic_read(&vm->config_changed)) {
2367 atomic_set(&vm->config_changed, 0);
2368 virtio_mem_refresh_config(vm);
2369 }
2370
2371 /* Unplug any leftovers from previous runs */
2372 if (!rc)
2373 rc = virtio_mem_unplug_pending_mb(vm);
2374
2375 if (!rc && vm->requested_size != vm->plugged_size) {
2376 if (vm->requested_size > vm->plugged_size) {
2377 diff = vm->requested_size - vm->plugged_size;
2378 rc = virtio_mem_plug_request(vm, diff);
2379 } else {
2380 diff = vm->plugged_size - vm->requested_size;
2381 rc = virtio_mem_unplug_request(vm, diff);
2382 }
2383 }
2384
2385 switch (rc) {
2386 case 0:
2387 vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS;
2388 break;
2389 case -ENOSPC:
2390 /*
2391 * We cannot add any more memory (alignment, physical limit)
2392 * or we have too many offline memory blocks.
2393 */
2394 break;
2395 case -ETXTBSY:
2396 /*
2397 * The hypervisor cannot process our request right now
2398 * (e.g., out of memory, migrating);
2399 */
2400 case -EBUSY:
2401 /*
2402 * We cannot free up any memory to unplug it (all plugged memory
2403 * is busy).
2404 */
2405 case -ENOMEM:
2406 /* Out of memory, try again later. */
2407 hrtimer_start(&vm->retry_timer, ms_to_ktime(vm->retry_timer_ms),
2408 HRTIMER_MODE_REL);
2409 break;
2410 case -EAGAIN:
2411 /* Retry immediately (e.g., the config changed). */
2412 goto retry;
2413 default:
2414 /* Unknown error, mark as broken */
2415 dev_err(&vm->vdev->dev,
2416 "unknown error, marking device broken: %d\n", rc);
2417 vm->broken = true;
2418 }
2419
2420 atomic_set(&vm->wq_active, 0);
2421}
2422
2423static enum hrtimer_restart virtio_mem_timer_expired(struct hrtimer *timer)
2424{
2425 struct virtio_mem *vm = container_of(timer, struct virtio_mem,
2426 retry_timer);
2427
2428 virtio_mem_retry(vm);
2429 vm->retry_timer_ms = min_t(unsigned int, vm->retry_timer_ms * 2,
2430 VIRTIO_MEM_RETRY_TIMER_MAX_MS);
2431 return HRTIMER_NORESTART;
2432}
2433
2434static void virtio_mem_handle_response(struct virtqueue *vq)
2435{
2436 struct virtio_mem *vm = vq->vdev->priv;
2437
2438 wake_up(&vm->host_resp);
2439}
2440
2441static int virtio_mem_init_vq(struct virtio_mem *vm)
2442{
2443 struct virtqueue *vq;
2444
2445 vq = virtio_find_single_vq(vm->vdev, virtio_mem_handle_response,
2446 "guest-request");
2447 if (IS_ERR(vq))
2448 return PTR_ERR(vq);
2449 vm->vq = vq;
2450
2451 return 0;
2452}
2453
2454static int virtio_mem_init_hotplug(struct virtio_mem *vm)
2455{
2456 const struct range pluggable_range = mhp_get_pluggable_range(true);
2457 uint64_t unit_pages, sb_size, addr;
2458 int rc;
2459
2460 /* bad device setup - warn only */
2461 if (!IS_ALIGNED(vm->addr, memory_block_size_bytes()))
2462 dev_warn(&vm->vdev->dev,
2463 "The alignment of the physical start address can make some memory unusable.\n");
2464 if (!IS_ALIGNED(vm->addr + vm->region_size, memory_block_size_bytes()))
2465 dev_warn(&vm->vdev->dev,
2466 "The alignment of the physical end address can make some memory unusable.\n");
2467 if (vm->addr < pluggable_range.start ||
2468 vm->addr + vm->region_size - 1 > pluggable_range.end)
2469 dev_warn(&vm->vdev->dev,
2470 "Some device memory is not addressable/pluggable. This can make some memory unusable.\n");
2471
2472 /* Prepare the offline threshold - make sure we can add two blocks. */
2473 vm->offline_threshold = max_t(uint64_t, 2 * memory_block_size_bytes(),
2474 VIRTIO_MEM_DEFAULT_OFFLINE_THRESHOLD);
2475
2476 /*
2477 * alloc_contig_range() works reliably with pageblock
2478 * granularity on ZONE_NORMAL, use pageblock_nr_pages.
2479 */
2480 sb_size = PAGE_SIZE * pageblock_nr_pages;
2481 sb_size = max_t(uint64_t, vm->device_block_size, sb_size);
2482
2483 if (sb_size < memory_block_size_bytes() && !force_bbm) {
2484 /* SBM: At least two subblocks per Linux memory block. */
2485 vm->in_sbm = true;
2486 vm->sbm.sb_size = sb_size;
2487 vm->sbm.sbs_per_mb = memory_block_size_bytes() /
2488 vm->sbm.sb_size;
2489
2490 /* Round up to the next full memory block */
2491 addr = max_t(uint64_t, vm->addr, pluggable_range.start) +
2492 memory_block_size_bytes() - 1;
2493 vm->sbm.first_mb_id = virtio_mem_phys_to_mb_id(addr);
2494 vm->sbm.next_mb_id = vm->sbm.first_mb_id;
2495 } else {
2496 /* BBM: At least one Linux memory block. */
2497 vm->bbm.bb_size = max_t(uint64_t, vm->device_block_size,
2498 memory_block_size_bytes());
2499
2500 if (bbm_block_size) {
2501 if (!is_power_of_2(bbm_block_size)) {
2502 dev_warn(&vm->vdev->dev,
2503 "bbm_block_size is not a power of 2");
2504 } else if (bbm_block_size < vm->bbm.bb_size) {
2505 dev_warn(&vm->vdev->dev,
2506 "bbm_block_size is too small");
2507 } else {
2508 vm->bbm.bb_size = bbm_block_size;
2509 }
2510 }
2511
2512 /* Round up to the next aligned big block */
2513 addr = max_t(uint64_t, vm->addr, pluggable_range.start) +
2514 vm->bbm.bb_size - 1;
2515 vm->bbm.first_bb_id = virtio_mem_phys_to_bb_id(vm, addr);
2516 vm->bbm.next_bb_id = vm->bbm.first_bb_id;
2517
2518 /* Make sure we can add two big blocks. */
2519 vm->offline_threshold = max_t(uint64_t, 2 * vm->bbm.bb_size,
2520 vm->offline_threshold);
2521 }
2522
2523 dev_info(&vm->vdev->dev, "memory block size: 0x%lx",
2524 memory_block_size_bytes());
2525 if (vm->in_sbm)
2526 dev_info(&vm->vdev->dev, "subblock size: 0x%llx",
2527 (unsigned long long)vm->sbm.sb_size);
2528 else
2529 dev_info(&vm->vdev->dev, "big block size: 0x%llx",
2530 (unsigned long long)vm->bbm.bb_size);
2531
2532 /* create the parent resource for all memory */
2533 rc = virtio_mem_create_resource(vm);
2534 if (rc)
2535 return rc;
2536
2537 /* use a single dynamic memory group to cover the whole memory device */
2538 if (vm->in_sbm)
2539 unit_pages = PHYS_PFN(memory_block_size_bytes());
2540 else
2541 unit_pages = PHYS_PFN(vm->bbm.bb_size);
2542 rc = memory_group_register_dynamic(vm->nid, unit_pages);
2543 if (rc < 0)
2544 goto out_del_resource;
2545 vm->mgid = rc;
2546
2547 /*
2548 * If we still have memory plugged, we have to unplug all memory first.
2549 * Registering our parent resource makes sure that this memory isn't
2550 * actually in use (e.g., trying to reload the driver).
2551 */
2552 if (vm->plugged_size) {
2553 vm->unplug_all_required = true;
2554 dev_info(&vm->vdev->dev, "unplugging all memory is required\n");
2555 }
2556
2557 /* register callbacks */
2558 vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb;
2559 rc = register_memory_notifier(&vm->memory_notifier);
2560 if (rc)
2561 goto out_unreg_group;
2562 rc = register_virtio_mem_device(vm);
2563 if (rc)
2564 goto out_unreg_mem;
2565
2566 return 0;
2567out_unreg_mem:
2568 unregister_memory_notifier(&vm->memory_notifier);
2569out_unreg_group:
2570 memory_group_unregister(vm->mgid);
2571out_del_resource:
2572 virtio_mem_delete_resource(vm);
2573 return rc;
2574}
2575
2576#ifdef CONFIG_PROC_VMCORE
2577static int virtio_mem_send_state_request(struct virtio_mem *vm, uint64_t addr,
2578 uint64_t size)
2579{
2580 const uint64_t nb_vm_blocks = size / vm->device_block_size;
2581 const struct virtio_mem_req req = {
2582 .type = cpu_to_virtio16(vm->vdev, VIRTIO_MEM_REQ_STATE),
2583 .u.state.addr = cpu_to_virtio64(vm->vdev, addr),
2584 .u.state.nb_blocks = cpu_to_virtio16(vm->vdev, nb_vm_blocks),
2585 };
2586 int rc = -ENOMEM;
2587
2588 dev_dbg(&vm->vdev->dev, "requesting state: 0x%llx - 0x%llx\n", addr,
2589 addr + size - 1);
2590
2591 switch (virtio_mem_send_request(vm, &req)) {
2592 case VIRTIO_MEM_RESP_ACK:
2593 return virtio16_to_cpu(vm->vdev, vm->resp.u.state.state);
2594 case VIRTIO_MEM_RESP_ERROR:
2595 rc = -EINVAL;
2596 break;
2597 default:
2598 break;
2599 }
2600
2601 dev_dbg(&vm->vdev->dev, "requesting state failed: %d\n", rc);
2602 return rc;
2603}
2604
2605static bool virtio_mem_vmcore_pfn_is_ram(struct vmcore_cb *cb,
2606 unsigned long pfn)
2607{
2608 struct virtio_mem *vm = container_of(cb, struct virtio_mem,
2609 vmcore_cb);
2610 uint64_t addr = PFN_PHYS(pfn);
2611 bool is_ram;
2612 int rc;
2613
2614 if (!virtio_mem_contains_range(vm, addr, PAGE_SIZE))
2615 return true;
2616 if (!vm->plugged_size)
2617 return false;
2618
2619 /*
2620 * We have to serialize device requests and access to the information
2621 * about the block queried last.
2622 */
2623 mutex_lock(&vm->hotplug_mutex);
2624
2625 addr = ALIGN_DOWN(addr, vm->device_block_size);
2626 if (addr != vm->last_block_addr) {
2627 rc = virtio_mem_send_state_request(vm, addr,
2628 vm->device_block_size);
2629 /* On any kind of error, we're going to signal !ram. */
2630 if (rc == VIRTIO_MEM_STATE_PLUGGED)
2631 vm->last_block_plugged = true;
2632 else
2633 vm->last_block_plugged = false;
2634 vm->last_block_addr = addr;
2635 }
2636
2637 is_ram = vm->last_block_plugged;
2638 mutex_unlock(&vm->hotplug_mutex);
2639 return is_ram;
2640}
2641#endif /* CONFIG_PROC_VMCORE */
2642
2643static int virtio_mem_init_kdump(struct virtio_mem *vm)
2644{
2645#ifdef CONFIG_PROC_VMCORE
2646 dev_info(&vm->vdev->dev, "memory hot(un)plug disabled in kdump kernel\n");
2647 vm->vmcore_cb.pfn_is_ram = virtio_mem_vmcore_pfn_is_ram;
2648 register_vmcore_cb(&vm->vmcore_cb);
2649 return 0;
2650#else /* CONFIG_PROC_VMCORE */
2651 dev_warn(&vm->vdev->dev, "disabled in kdump kernel without vmcore\n");
2652 return -EBUSY;
2653#endif /* CONFIG_PROC_VMCORE */
2654}
2655
2656static int virtio_mem_init(struct virtio_mem *vm)
2657{
2658 uint16_t node_id;
2659
2660 if (!vm->vdev->config->get) {
2661 dev_err(&vm->vdev->dev, "config access disabled\n");
2662 return -EINVAL;
2663 }
2664
2665 /* Fetch all properties that can't change. */
2666 virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
2667 &vm->plugged_size);
2668 virtio_cread_le(vm->vdev, struct virtio_mem_config, block_size,
2669 &vm->device_block_size);
2670 virtio_cread_le(vm->vdev, struct virtio_mem_config, node_id,
2671 &node_id);
2672 vm->nid = virtio_mem_translate_node_id(vm, node_id);
2673 virtio_cread_le(vm->vdev, struct virtio_mem_config, addr, &vm->addr);
2674 virtio_cread_le(vm->vdev, struct virtio_mem_config, region_size,
2675 &vm->region_size);
2676
2677 /* Determine the nid for the device based on the lowest address. */
2678 if (vm->nid == NUMA_NO_NODE)
2679 vm->nid = memory_add_physaddr_to_nid(vm->addr);
2680
2681 dev_info(&vm->vdev->dev, "start address: 0x%llx", vm->addr);
2682 dev_info(&vm->vdev->dev, "region size: 0x%llx", vm->region_size);
2683 dev_info(&vm->vdev->dev, "device block size: 0x%llx",
2684 (unsigned long long)vm->device_block_size);
2685 if (vm->nid != NUMA_NO_NODE && IS_ENABLED(CONFIG_NUMA))
2686 dev_info(&vm->vdev->dev, "nid: %d", vm->nid);
2687
2688 /*
2689 * We don't want to (un)plug or reuse any memory when in kdump. The
2690 * memory is still accessible (but not exposed to Linux).
2691 */
2692 if (vm->in_kdump)
2693 return virtio_mem_init_kdump(vm);
2694 return virtio_mem_init_hotplug(vm);
2695}
2696
2697static int virtio_mem_create_resource(struct virtio_mem *vm)
2698{
2699 /*
2700 * When force-unloading the driver and removing the device, we
2701 * could have a garbage pointer. Duplicate the string.
2702 */
2703 const char *name = kstrdup(dev_name(&vm->vdev->dev), GFP_KERNEL);
2704
2705 if (!name)
2706 return -ENOMEM;
2707
2708 /* Disallow mapping device memory via /dev/mem completely. */
2709 vm->parent_resource = __request_mem_region(vm->addr, vm->region_size,
2710 name, IORESOURCE_SYSTEM_RAM |
2711 IORESOURCE_EXCLUSIVE);
2712 if (!vm->parent_resource) {
2713 kfree(name);
2714 dev_warn(&vm->vdev->dev, "could not reserve device region\n");
2715 dev_info(&vm->vdev->dev,
2716 "reloading the driver is not supported\n");
2717 return -EBUSY;
2718 }
2719
2720 /* The memory is not actually busy - make add_memory() work. */
2721 vm->parent_resource->flags &= ~IORESOURCE_BUSY;
2722 return 0;
2723}
2724
2725static void virtio_mem_delete_resource(struct virtio_mem *vm)
2726{
2727 const char *name;
2728
2729 if (!vm->parent_resource)
2730 return;
2731
2732 name = vm->parent_resource->name;
2733 release_resource(vm->parent_resource);
2734 kfree(vm->parent_resource);
2735 kfree(name);
2736 vm->parent_resource = NULL;
2737}
2738
2739static int virtio_mem_range_has_system_ram(struct resource *res, void *arg)
2740{
2741 return 1;
2742}
2743
2744static bool virtio_mem_has_memory_added(struct virtio_mem *vm)
2745{
2746 const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
2747
2748 return walk_iomem_res_desc(IORES_DESC_NONE, flags, vm->addr,
2749 vm->addr + vm->region_size, NULL,
2750 virtio_mem_range_has_system_ram) == 1;
2751}
2752
2753static int virtio_mem_probe(struct virtio_device *vdev)
2754{
2755 struct virtio_mem *vm;
2756 int rc;
2757
2758 BUILD_BUG_ON(sizeof(struct virtio_mem_req) != 24);
2759 BUILD_BUG_ON(sizeof(struct virtio_mem_resp) != 10);
2760
2761 vdev->priv = vm = kzalloc(sizeof(*vm), GFP_KERNEL);
2762 if (!vm)
2763 return -ENOMEM;
2764
2765 init_waitqueue_head(&vm->host_resp);
2766 vm->vdev = vdev;
2767 INIT_WORK(&vm->wq, virtio_mem_run_wq);
2768 mutex_init(&vm->hotplug_mutex);
2769 INIT_LIST_HEAD(&vm->next);
2770 spin_lock_init(&vm->removal_lock);
2771 hrtimer_init(&vm->retry_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2772 vm->retry_timer.function = virtio_mem_timer_expired;
2773 vm->retry_timer_ms = VIRTIO_MEM_RETRY_TIMER_MIN_MS;
2774 vm->in_kdump = is_kdump_kernel();
2775
2776 /* register the virtqueue */
2777 rc = virtio_mem_init_vq(vm);
2778 if (rc)
2779 goto out_free_vm;
2780
2781 /* initialize the device by querying the config */
2782 rc = virtio_mem_init(vm);
2783 if (rc)
2784 goto out_del_vq;
2785
2786 virtio_device_ready(vdev);
2787
2788 /* trigger a config update to start processing the requested_size */
2789 if (!vm->in_kdump) {
2790 atomic_set(&vm->config_changed, 1);
2791 queue_work(system_freezable_wq, &vm->wq);
2792 }
2793
2794 return 0;
2795out_del_vq:
2796 vdev->config->del_vqs(vdev);
2797out_free_vm:
2798 kfree(vm);
2799 vdev->priv = NULL;
2800
2801 return rc;
2802}
2803
2804static void virtio_mem_deinit_hotplug(struct virtio_mem *vm)
2805{
2806 unsigned long mb_id;
2807 int rc;
2808
2809 /*
2810 * Make sure the workqueue won't be triggered anymore and no memory
2811 * blocks can be onlined/offlined until we're finished here.
2812 */
2813 mutex_lock(&vm->hotplug_mutex);
2814 spin_lock_irq(&vm->removal_lock);
2815 vm->removing = true;
2816 spin_unlock_irq(&vm->removal_lock);
2817 mutex_unlock(&vm->hotplug_mutex);
2818
2819 /* wait until the workqueue stopped */
2820 cancel_work_sync(&vm->wq);
2821 hrtimer_cancel(&vm->retry_timer);
2822
2823 if (vm->in_sbm) {
2824 /*
2825 * After we unregistered our callbacks, user space can online
2826 * partially plugged offline blocks. Make sure to remove them.
2827 */
2828 virtio_mem_sbm_for_each_mb(vm, mb_id,
2829 VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL) {
2830 rc = virtio_mem_sbm_remove_mb(vm, mb_id);
2831 BUG_ON(rc);
2832 virtio_mem_sbm_set_mb_state(vm, mb_id,
2833 VIRTIO_MEM_SBM_MB_UNUSED);
2834 }
2835 /*
2836 * After we unregistered our callbacks, user space can no longer
2837 * offline partially plugged online memory blocks. No need to
2838 * worry about them.
2839 */
2840 }
2841
2842 /* unregister callbacks */
2843 unregister_virtio_mem_device(vm);
2844 unregister_memory_notifier(&vm->memory_notifier);
2845
2846 /*
2847 * There is no way we could reliably remove all memory we have added to
2848 * the system. And there is no way to stop the driver/device from going
2849 * away. Warn at least.
2850 */
2851 if (virtio_mem_has_memory_added(vm)) {
2852 dev_warn(&vm->vdev->dev,
2853 "device still has system memory added\n");
2854 } else {
2855 virtio_mem_delete_resource(vm);
2856 kfree_const(vm->resource_name);
2857 memory_group_unregister(vm->mgid);
2858 }
2859
2860 /* remove all tracking data - no locking needed */
2861 if (vm->in_sbm) {
2862 vfree(vm->sbm.mb_states);
2863 vfree(vm->sbm.sb_states);
2864 } else {
2865 vfree(vm->bbm.bb_states);
2866 }
2867}
2868
2869static void virtio_mem_deinit_kdump(struct virtio_mem *vm)
2870{
2871#ifdef CONFIG_PROC_VMCORE
2872 unregister_vmcore_cb(&vm->vmcore_cb);
2873#endif /* CONFIG_PROC_VMCORE */
2874}
2875
2876static void virtio_mem_remove(struct virtio_device *vdev)
2877{
2878 struct virtio_mem *vm = vdev->priv;
2879
2880 if (vm->in_kdump)
2881 virtio_mem_deinit_kdump(vm);
2882 else
2883 virtio_mem_deinit_hotplug(vm);
2884
2885 /* reset the device and cleanup the queues */
2886 virtio_reset_device(vdev);
2887 vdev->config->del_vqs(vdev);
2888
2889 kfree(vm);
2890 vdev->priv = NULL;
2891}
2892
2893static void virtio_mem_config_changed(struct virtio_device *vdev)
2894{
2895 struct virtio_mem *vm = vdev->priv;
2896
2897 if (unlikely(vm->in_kdump))
2898 return;
2899
2900 atomic_set(&vm->config_changed, 1);
2901 virtio_mem_retry(vm);
2902}
2903
2904#ifdef CONFIG_PM_SLEEP
2905static int virtio_mem_freeze(struct virtio_device *vdev)
2906{
2907 /*
2908 * When restarting the VM, all memory is usually unplugged. Don't
2909 * allow to suspend/hibernate.
2910 */
2911 dev_err(&vdev->dev, "save/restore not supported.\n");
2912 return -EPERM;
2913}
2914
2915static int virtio_mem_restore(struct virtio_device *vdev)
2916{
2917 return -EPERM;
2918}
2919#endif
2920
2921static unsigned int virtio_mem_features[] = {
2922#if defined(CONFIG_NUMA) && defined(CONFIG_ACPI_NUMA)
2923 VIRTIO_MEM_F_ACPI_PXM,
2924#endif
2925 VIRTIO_MEM_F_UNPLUGGED_INACCESSIBLE,
2926};
2927
2928static const struct virtio_device_id virtio_mem_id_table[] = {
2929 { VIRTIO_ID_MEM, VIRTIO_DEV_ANY_ID },
2930 { 0 },
2931};
2932
2933static struct virtio_driver virtio_mem_driver = {
2934 .feature_table = virtio_mem_features,
2935 .feature_table_size = ARRAY_SIZE(virtio_mem_features),
2936 .driver.name = KBUILD_MODNAME,
2937 .driver.owner = THIS_MODULE,
2938 .id_table = virtio_mem_id_table,
2939 .probe = virtio_mem_probe,
2940 .remove = virtio_mem_remove,
2941 .config_changed = virtio_mem_config_changed,
2942#ifdef CONFIG_PM_SLEEP
2943 .freeze = virtio_mem_freeze,
2944 .restore = virtio_mem_restore,
2945#endif
2946};
2947
2948module_virtio_driver(virtio_mem_driver);
2949MODULE_DEVICE_TABLE(virtio, virtio_mem_id_table);
2950MODULE_AUTHOR("David Hildenbrand <david@redhat.com>");
2951MODULE_DESCRIPTION("Virtio-mem driver");
2952MODULE_LICENSE("GPL");