Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

virtio-mem: add memory via add_memory_driver_managed()

Virtio-mem managed memory is always detected and added by the virtio-mem
driver, never using something like the firmware-provided memory map.
This is the case after an ordinary system reboot, and has to be guaranteed
after kexec. Especially, virtio-mem added memory resources can contain
inaccessible parts ("unblocked memory blocks"), blindly forwarding them
to a kexec kernel is dangerous, as unplugged memory will get accessed
(esp. written).

Let's use the new way of adding special driver-managed memory introduced
in commit 7b7b27214bba ("mm/memory_hotplug: introduce
add_memory_driver_managed()").

This will result in no entries in /sys/firmware/memmap ("raw firmware-
provided memory map"), the memory resource will be flagged
IORESOURCE_MEM_DRIVER_MANAGED (esp., kexec_file_load() will not place
kexec images on this memory), and it is exposed as "System RAM
(virtio_mem)" in /proc/iomem, so esp. kexec-tools can properly handle it.

Example /proc/iomem before this change:
[...]
140000000-333ffffff : virtio0
140000000-147ffffff : System RAM
334000000-533ffffff : virtio1
338000000-33fffffff : System RAM
340000000-347ffffff : System RAM
348000000-34fffffff : System RAM
[...]

Example /proc/iomem after this change:
[...]
140000000-333ffffff : virtio0
140000000-147ffffff : System RAM (virtio_mem)
334000000-533ffffff : virtio1
338000000-33fffffff : System RAM (virtio_mem)
340000000-347ffffff : System RAM (virtio_mem)
348000000-34fffffff : System RAM (virtio_mem)
[...]

Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
Cc: teawater <teawaterz@linux.alibaba.com>
Fixes: 5f1f79bbc9e26 ("virtio-mem: Paravirtualized memory hotplug")
Signed-off-by: David Hildenbrand <david@redhat.com>
Link: https://lore.kernel.org/r/20200611093518.5737-1-david@redhat.com
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Pankaj Gupta <pankaj.gupta.linux@gmail.com>

authored by

David Hildenbrand and committed by
Michael S. Tsirkin
b3562c60 1c3d69ab

+22 -3
+22 -3
drivers/virtio/virtio_mem.c
··· 101 101 102 102 /* The parent resource for all memory added via this device. */ 103 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; 104 109 105 110 /* Summary of all memory block states. */ 106 111 unsigned long nb_mb_state[VIRTIO_MEM_MB_STATE_COUNT]; ··· 419 414 if (nid == NUMA_NO_NODE) 420 415 nid = memory_add_physaddr_to_nid(addr); 421 416 417 + /* 418 + * When force-unloading the driver and we still have memory added to 419 + * Linux, the resource name has to stay. 420 + */ 421 + if (!vm->resource_name) { 422 + vm->resource_name = kstrdup_const("System RAM (virtio_mem)", 423 + GFP_KERNEL); 424 + if (!vm->resource_name) 425 + return -ENOMEM; 426 + } 427 + 422 428 dev_dbg(&vm->vdev->dev, "adding memory block: %lu\n", mb_id); 423 - return add_memory(nid, addr, memory_block_size_bytes()); 429 + return add_memory_driver_managed(nid, addr, memory_block_size_bytes(), 430 + vm->resource_name); 424 431 } 425 432 426 433 /* ··· 1907 1890 vm->nb_mb_state[VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL] || 1908 1891 vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE] || 1909 1892 vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL] || 1910 - vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE]) 1893 + vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE]) { 1911 1894 dev_warn(&vdev->dev, "device still has system memory added\n"); 1912 - else 1895 + } else { 1913 1896 virtio_mem_delete_resource(vm); 1897 + kfree_const(vm->resource_name); 1898 + } 1914 1899 1915 1900 /* remove all tracking data - no locking needed */ 1916 1901 vfree(vm->mb_state);