Skip to content

Commit aa910c2

Browse files
s390x: virtio-mem support
Let's add our virtio-mem-ccw proxy device and wire it up. We should be supporting everything (e.g., device unplug, "dynamic-memslots") that we already support for the virtio-pci variant. With a Linux guest that supports virtio-mem (and has automatic memory onlining properly configured) the following example will work: 1. Start a VM with 4G initial memory and a virtio-mem device with a maximum capacity of 16GB: qemu/build/qemu-system-s390x \ --enable-kvm \ -m 4G,maxmem=20G \ -nographic \ -smp 8 \ -hda Fedora-Server-KVM-40-1.14.s390x.qcow2 \ -chardev socket,id=monitor,path=/var/tmp/monitor,server,nowait \ -mon chardev=monitor,mode=readline \ -object memory-backend-ram,id=mem0,size=16G,reserve=off \ -device virtio-mem-ccw,id=vmem0,memdev=mem0,dynamic-memslots=on 2. Query the current size of virtio-mem device: (qemu) info memory-devices Memory device [virtio-mem]: "vmem0" memaddr: 0x100000000 node: 0 requested-size: 0 size: 0 max-size: 17179869184 block-size: 1048576 memdev: /objects/mem0 3. Request to grow it to 8GB (hotplug 8GB): (qemu) qom-set vmem0 requested-size 8G (qemu) info memory-devices Memory device [virtio-mem]: "vmem0" memaddr: 0x100000000 node: 0 requested-size: 8589934592 size: 8589934592 max-size: 17179869184 block-size: 1048576 memdev: /objects/mem0 4. Request to grow to 16GB (hotplug another 8GB): (qemu) qom-set vmem0 requested-size 16G (qemu) info memory-devices Memory device [virtio-mem]: "vmem0" memaddr: 0x100000000 node: 0 requested-size: 17179869184 size: 17179869184 max-size: 17179869184 block-size: 1048576 memdev: /objects/mem0 5. Try to hotunplug all memory again, shrinking to 0GB: (qemu) qom-set vmem0 requested-size 0G (qemu) info memory-devices Memory device [virtio-mem]: "vmem0" memaddr: 0x100000000 node: 0 requested-size: 0 size: 0 max-size: 17179869184 block-size: 1048576 memdev: /objects/mem0 6. If it worked, unplug the device (qemu) device_del vmem0 (qemu) info memory-devices (qemu) object_del mem0 7. Hotplug a new device with a smaller capacity and directly size it to 1GB (qemu) object_add memory-backend-ram,id=mem0,size=8G,reserve=off (qemu) device_add virtio-mem-ccw,id=vmem0,memdev=mem0,\ dynamic-memslots=on,requested-size=1G (qemu) info memory-devices Memory device [virtio-mem]: "vmem0" memaddr: 0x100000000 node: 0 requested-size: 1073741824 size: 1073741824 max-size: 8589934592 block-size: 1048576 memdev: /objects/mem0 Trying to use a virtio-mem device backed by hugetlb into a !hugetlb VM correctly results in the error: ... Memory device uses a bigger page size than initial memory Note that the virtio-mem driver in Linux will supports 1 MiB (pageblock) granularity. Message-ID: <[email protected]> Acked-by: Michael S. Tsirkin <[email protected]> Signed-off-by: David Hildenbrand <[email protected]>
1 parent 88d86f6 commit aa910c2

File tree

6 files changed

+267
-1
lines changed

6 files changed

+267
-1
lines changed

MAINTAINERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,8 @@ W: https://virtio-mem.gitlab.io/
23902390
F: hw/virtio/virtio-mem.c
23912391
F: hw/virtio/virtio-mem-pci.h
23922392
F: hw/virtio/virtio-mem-pci.c
2393+
F: hw/s390x/virtio-ccw-mem.c
2394+
F: hw/s390x/virtio-ccw-mem.h
23932395
F: include/hw/virtio/virtio-mem.h
23942396

23952397
virtio-snd

hw/s390x/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ config S390_CCW_VIRTIO
1616
select SCLPCONSOLE
1717
select VIRTIO_CCW
1818
select MSI_NONBROKEN
19+
select VIRTIO_MEM_SUPPORTED

hw/s390x/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ virtio_ss.add(when: 'CONFIG_VHOST_SCSI', if_true: files('vhost-scsi-ccw.c'))
5151
virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-ccw.c'))
5252
virtio_ss.add(when: 'CONFIG_VHOST_USER_FS', if_true: files('vhost-user-fs-ccw.c'))
5353
virtio_ss.add(when: 'CONFIG_VIRTIO_MD', if_true: files('virtio-ccw-md.c'))
54+
virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-ccw-mem.c'))
5455
s390x_ss.add_all(when: 'CONFIG_VIRTIO_CCW', if_true: virtio_ss)
5556

5657
s390x_ss.add(when: 'CONFIG_VIRTIO_MD', if_false: files('virtio-ccw-md-stubs.c'))

hw/s390x/virtio-ccw-mem.c

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
* virtio-mem CCW implementation
3+
*
4+
* Copyright (C) 2024 Red Hat, Inc.
5+
*
6+
* Authors:
7+
* David Hildenbrand <[email protected]>
8+
*
9+
* This work is licensed under the terms of the GNU GPL, version 2.
10+
* See the COPYING file in the top-level directory.
11+
*/
12+
13+
#include "qemu/osdep.h"
14+
#include "hw/qdev-properties.h"
15+
#include "qapi/error.h"
16+
#include "qemu/module.h"
17+
#include "virtio-ccw-mem.h"
18+
#include "hw/mem/memory-device.h"
19+
#include "qapi/qapi-events-machine.h"
20+
#include "qapi/qapi-events-misc.h"
21+
22+
static void virtio_ccw_mem_realize(VirtioCcwDevice *ccw_dev, Error **errp)
23+
{
24+
VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(ccw_dev);
25+
DeviceState *vdev = DEVICE(&dev->vdev);
26+
27+
qdev_realize(vdev, BUS(&ccw_dev->bus), errp);
28+
}
29+
30+
static void virtio_ccw_mem_set_addr(MemoryDeviceState *md, uint64_t addr,
31+
Error **errp)
32+
{
33+
object_property_set_uint(OBJECT(md), VIRTIO_MEM_ADDR_PROP, addr, errp);
34+
}
35+
36+
static uint64_t virtio_ccw_mem_get_addr(const MemoryDeviceState *md)
37+
{
38+
return object_property_get_uint(OBJECT(md), VIRTIO_MEM_ADDR_PROP,
39+
&error_abort);
40+
}
41+
42+
static MemoryRegion *virtio_ccw_mem_get_memory_region(MemoryDeviceState *md,
43+
Error **errp)
44+
{
45+
VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(md);
46+
VirtIOMEM *vmem = &dev->vdev;
47+
VirtIOMEMClass *vmc = VIRTIO_MEM_GET_CLASS(vmem);
48+
49+
return vmc->get_memory_region(vmem, errp);
50+
}
51+
52+
static void virtio_ccw_mem_decide_memslots(MemoryDeviceState *md,
53+
unsigned int limit)
54+
{
55+
VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(md);
56+
VirtIOMEM *vmem = VIRTIO_MEM(&dev->vdev);
57+
VirtIOMEMClass *vmc = VIRTIO_MEM_GET_CLASS(vmem);
58+
59+
vmc->decide_memslots(vmem, limit);
60+
}
61+
62+
static unsigned int virtio_ccw_mem_get_memslots(MemoryDeviceState *md)
63+
{
64+
VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(md);
65+
VirtIOMEM *vmem = VIRTIO_MEM(&dev->vdev);
66+
VirtIOMEMClass *vmc = VIRTIO_MEM_GET_CLASS(vmem);
67+
68+
return vmc->get_memslots(vmem);
69+
}
70+
71+
static uint64_t virtio_ccw_mem_get_plugged_size(const MemoryDeviceState *md,
72+
Error **errp)
73+
{
74+
return object_property_get_uint(OBJECT(md), VIRTIO_MEM_SIZE_PROP,
75+
errp);
76+
}
77+
78+
static void virtio_ccw_mem_fill_device_info(const MemoryDeviceState *md,
79+
MemoryDeviceInfo *info)
80+
{
81+
VirtioMEMDeviceInfo *vi = g_new0(VirtioMEMDeviceInfo, 1);
82+
VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(md);
83+
VirtIOMEM *vmem = &dev->vdev;
84+
VirtIOMEMClass *vpc = VIRTIO_MEM_GET_CLASS(vmem);
85+
DeviceState *vdev = DEVICE(md);
86+
87+
if (vdev->id) {
88+
vi->id = g_strdup(vdev->id);
89+
}
90+
91+
/* let the real device handle everything else */
92+
vpc->fill_device_info(vmem, vi);
93+
94+
info->u.virtio_mem.data = vi;
95+
info->type = MEMORY_DEVICE_INFO_KIND_VIRTIO_MEM;
96+
}
97+
98+
static uint64_t virtio_ccw_mem_get_min_alignment(const MemoryDeviceState *md)
99+
{
100+
return object_property_get_uint(OBJECT(md), VIRTIO_MEM_BLOCK_SIZE_PROP,
101+
&error_abort);
102+
}
103+
104+
static void virtio_ccw_mem_size_change_notify(Notifier *notifier, void *data)
105+
{
106+
VirtIOMEMCcw *dev = container_of(notifier, VirtIOMEMCcw,
107+
size_change_notifier);
108+
DeviceState *vdev = DEVICE(dev);
109+
char *qom_path = object_get_canonical_path(OBJECT(dev));
110+
const uint64_t * const size_p = data;
111+
112+
qapi_event_send_memory_device_size_change(vdev->id, *size_p, qom_path);
113+
g_free(qom_path);
114+
}
115+
116+
static void virtio_ccw_mem_unplug_request_check(VirtIOMDCcw *vmd, Error **errp)
117+
{
118+
VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(vmd);
119+
VirtIOMEM *vmem = &dev->vdev;
120+
VirtIOMEMClass *vpc = VIRTIO_MEM_GET_CLASS(vmem);
121+
122+
vpc->unplug_request_check(vmem, errp);
123+
}
124+
125+
static void virtio_ccw_mem_get_requested_size(Object *obj, Visitor *v,
126+
const char *name, void *opaque,
127+
Error **errp)
128+
{
129+
VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(obj);
130+
131+
object_property_get(OBJECT(&dev->vdev), name, v, errp);
132+
}
133+
134+
static void virtio_ccw_mem_set_requested_size(Object *obj, Visitor *v,
135+
const char *name, void *opaque,
136+
Error **errp)
137+
{
138+
VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(obj);
139+
DeviceState *vdev = DEVICE(obj);
140+
141+
/*
142+
* If we passed virtio_ccw_mem_unplug_request_check(), making sure that
143+
* the requested size is 0, don't allow modifying the requested size
144+
* anymore, otherwise the VM might end up hotplugging memory before
145+
* handling the unplug request.
146+
*/
147+
if (vdev->pending_deleted_event) {
148+
error_setg(errp, "'%s' cannot be changed if the device is in the"
149+
" process of unplug", name);
150+
return;
151+
}
152+
153+
object_property_set(OBJECT(&dev->vdev), name, v, errp);
154+
}
155+
156+
static Property virtio_ccw_mem_properties[] = {
157+
DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
158+
VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
159+
DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
160+
VIRTIO_CCW_MAX_REV),
161+
DEFINE_PROP_END_OF_LIST(),
162+
};
163+
164+
static void virtio_ccw_mem_class_init(ObjectClass *klass, void *data)
165+
{
166+
DeviceClass *dc = DEVICE_CLASS(klass);
167+
VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
168+
MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(klass);
169+
VirtIOMDCcwClass *vmdc = VIRTIO_MD_CCW_CLASS(klass);
170+
171+
k->realize = virtio_ccw_mem_realize;
172+
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
173+
device_class_set_props(dc, virtio_ccw_mem_properties);
174+
175+
mdc->get_addr = virtio_ccw_mem_get_addr;
176+
mdc->set_addr = virtio_ccw_mem_set_addr;
177+
mdc->get_plugged_size = virtio_ccw_mem_get_plugged_size;
178+
mdc->get_memory_region = virtio_ccw_mem_get_memory_region;
179+
mdc->decide_memslots = virtio_ccw_mem_decide_memslots;
180+
mdc->get_memslots = virtio_ccw_mem_get_memslots;
181+
mdc->fill_device_info = virtio_ccw_mem_fill_device_info;
182+
mdc->get_min_alignment = virtio_ccw_mem_get_min_alignment;
183+
184+
vmdc->unplug_request_check = virtio_ccw_mem_unplug_request_check;
185+
}
186+
187+
static void virtio_ccw_mem_instance_init(Object *obj)
188+
{
189+
VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(obj);
190+
VirtIOMEMClass *vmc;
191+
VirtIOMEM *vmem;
192+
193+
virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
194+
TYPE_VIRTIO_MEM);
195+
196+
dev->size_change_notifier.notify = virtio_ccw_mem_size_change_notify;
197+
vmem = &dev->vdev;
198+
vmc = VIRTIO_MEM_GET_CLASS(vmem);
199+
/*
200+
* We never remove the notifier again, as we expect both devices to
201+
* disappear at the same time.
202+
*/
203+
vmc->add_size_change_notifier(vmem, &dev->size_change_notifier);
204+
205+
object_property_add_alias(obj, VIRTIO_MEM_BLOCK_SIZE_PROP,
206+
OBJECT(&dev->vdev), VIRTIO_MEM_BLOCK_SIZE_PROP);
207+
object_property_add_alias(obj, VIRTIO_MEM_SIZE_PROP, OBJECT(&dev->vdev),
208+
VIRTIO_MEM_SIZE_PROP);
209+
object_property_add(obj, VIRTIO_MEM_REQUESTED_SIZE_PROP, "size",
210+
virtio_ccw_mem_get_requested_size,
211+
virtio_ccw_mem_set_requested_size, NULL, NULL);
212+
}
213+
214+
static const TypeInfo virtio_ccw_mem = {
215+
.name = TYPE_VIRTIO_MEM_CCW,
216+
.parent = TYPE_VIRTIO_MD_CCW,
217+
.instance_size = sizeof(VirtIOMEMCcw),
218+
.instance_init = virtio_ccw_mem_instance_init,
219+
.class_init = virtio_ccw_mem_class_init,
220+
};
221+
222+
static void virtio_ccw_mem_register_types(void)
223+
{
224+
type_register_static(&virtio_ccw_mem);
225+
}
226+
type_init(virtio_ccw_mem_register_types)

hw/s390x/virtio-ccw-mem.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Virtio MEM CCW device
3+
*
4+
* Copyright (C) 2024 Red Hat, Inc.
5+
*
6+
* Authors:
7+
* David Hildenbrand <[email protected]>
8+
*
9+
* This work is licensed under the terms of the GNU GPL, version 2.
10+
* See the COPYING file in the top-level directory.
11+
*/
12+
13+
#ifndef HW_S390X_VIRTIO_CCW_MEM_H
14+
#define HW_S390X_VIRTIO_CCW_MEM_H
15+
16+
#include "virtio-ccw-md.h"
17+
#include "hw/virtio/virtio-mem.h"
18+
#include "qom/object.h"
19+
20+
typedef struct VirtIOMEMCcw VirtIOMEMCcw;
21+
22+
/*
23+
* virtio-mem-ccw: This extends VirtIOMDCcw
24+
*/
25+
#define TYPE_VIRTIO_MEM_CCW "virtio-mem-ccw"
26+
DECLARE_INSTANCE_CHECKER(VirtIOMEMCcw, VIRTIO_MEM_CCW, TYPE_VIRTIO_MEM_CCW)
27+
28+
struct VirtIOMEMCcw {
29+
VirtIOMDCcw parent_obj;
30+
VirtIOMEM vdev;
31+
Notifier size_change_notifier;
32+
};
33+
34+
#endif /* HW_S390X_VIRTIO_CCW_MEM_H */

hw/virtio/virtio-mem.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ static uint32_t virtio_mem_default_thp_size(void)
6161
} else if (qemu_real_host_page_size() == 64 * KiB) {
6262
default_thp_size = 512 * MiB;
6363
}
64+
#elif defined(__s390x__)
65+
default_thp_size = 1 * MiB;
6466
#endif
6567

6668
return default_thp_size;
@@ -168,7 +170,7 @@ static bool virtio_mem_has_shared_zeropage(RAMBlock *rb)
168170
* necessary (as the section size can change). But it's more likely that the
169171
* section size will rather get smaller and not bigger over time.
170172
*/
171-
#if defined(TARGET_X86_64) || defined(TARGET_I386)
173+
#if defined(TARGET_X86_64) || defined(TARGET_I386) || defined(TARGET_S390X)
172174
#define VIRTIO_MEM_USABLE_EXTENT (2 * (128 * MiB))
173175
#elif defined(TARGET_ARM)
174176
#define VIRTIO_MEM_USABLE_EXTENT (2 * (512 * MiB))

0 commit comments

Comments
 (0)