Skip to contentSkip to conversation

kvm: export host NUMA info to guest's scheduler

RFC In review Liu Ping Fan 3 participants 14 messages May 23, 2012

Subsystems (6)
x86 KVM Kbuild Scheduler +2 more subsystems
lore
Share & tools
lore
#0 lore raw

Currently, the guest can not know the NUMA info of the vcpu, which will result in performance drawback. For example:
Supposing vcpu-a on nodeA, vcpu-b on nodeB, when load balance,
the tasks' pull and push between these vcpus will cost more. But unfortunately, currently, the guest is just blind to this.

So here is the idea to solve it.
Export host numa info through guest's sched domain to its scheduler. So the guest's lb will consider the cost.

These patches include:
For guest:
0001-sched-add-virt-sched-domain-for-the-guest.patch 0002-sched-add-virt-domain-device-s-driver.patch
For host:
0001-kvm-collect-vcpus-numa-info-for-guest-s-scheduler.patch 0001-Qemu-add-virt-sched-domain-device.patch

Please give some comments and suggestion.

Thanks and regards,
pingfan

#1 lore raw

PATCH 1/2 sched: add virt sched domain for the guest

From: Liu Ping Fan <[email protected]>

The guest's scheduler can not see the numa info on the host and this will result to the following scene:
Supposing vcpu-a on nodeA, vcpu-b on nodeB, when load balance,
the tasks' pull and push between these vcpus will cost more. But unfortunately, currently, the guest is just blind to this.

This patch want to export the host numa info to the guest, and help guest to rebuild its sched domain based on host's info.

--todo:
vcpu's hotplug will be considered.

---
 kernel/cpuset.c      |    2 +-
 kernel/sched/core.c  |   65 ++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/sched.h |    5 ++++
 3 files changed, 71 insertions(+), 1 deletions(-)
kernel/cpuset.c +1 -1
index 14f7070..1246091 100644
@@ -778,7 +778,7 @@ static DECLARE_WORK(rebuild_sched_domains_work, do_rebuild_sched_domains);
  * to a separate workqueue thread, which ends up processing the
  * above do_rebuild_sched_domains() function.
  */
-static void async_rebuild_sched_domains(void)
+void async_rebuild_sched_domains(void)
 {
 	queue_work(cpuset_wq, &rebuild_sched_domains_work);
 }
kernel/sched/core.c +65 -0

86 lines not on this page · raw message

kernel/sched/sched.h +5 -0
index fb3acba..232482d 100644
@@ -8,6 +8,9 @@
 
 extern __read_mostly int scheduler_running;
 
+#ifdef CONFIG_VIRT_SCHED_DOMAIN
+extern s16 __vapicid_to_vnode[];
+#endif
 /*
  * Convert user-nice values [ -20 ... 0 ... 19 ]
  * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
@@ -198,6 +201,8 @@ struct cfs_bandwidth { };
 
 #endif	/* CONFIG_CGROUP_SCHED */
 
+extern void async_rebuild_sched_domains(void);
+
 /* CFS-related fields in a runqueue */
 struct cfs_rq {
 	struct load_weight load;
Signature · 1 line
1.7.4.4
maintainer
#2 lore raw

Re: [PATCH 1/2] sched: add virt sched domain for the guest

Quoted 10 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The guest's scheduler can not see the numa info on the host and this will result to the following scene:
Supposing vcpu-a on nodeA, vcpu-b on nodeB, when load balance,
the tasks' pull and push between these vcpus will cost more. But unfortunately, currently, the guest is just blind to this.

This patch want to export the host numa info to the guest, and help guest to rebuild its sched domain based on host's info.

Hell no, we're not going to export sched domains, if kvm/qemu wants this its all in sysfs.

The whole sched_domain stuff is a big enough pain as it is, exporting this and making it a sodding API is the worst thing ever.

Whatever brainfart made you think this is needed anyway? sysfs contains the host topology, qemu can already create whatever guest topology you want (see the -smp and -numa arguments), so what gives?

#3 lore raw

Re: [PATCH 1/2] sched: add virt sched domain for the guest

Quoted 15 lines · Peter Zijlstra
Quoted 10 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The guest's scheduler can not see the numa info on the host and this will result to the following scene:
  Supposing vcpu-a on nodeA, vcpu-b on nodeB, when load balance, the tasks' pull and push between these vcpus will cost more. But unfortunately, currently, the guest is just blind to this.

This patch want to export the host numa info to the guest, and help guest to rebuild its sched domain based on host's info.

Hell no, we're not going to export sched domains, if kvm/qemu wants this its all in sysfs.

Quoted 6 lines

The whole sched_domain stuff is a big enough pain as it is, exporting this and making it a sodding API is the worst thing ever.

Whatever brainfart made you think this is needed anyway? sysfs contains the host topology, qemu can already create whatever guest topology you want (see the -smp and -numa arguments), so what gives?

I think -numa option will be used to emulate the special virtual machine to customer, and do not necessary map to host topology. And even we map them exactly with -numa option, the movement of vcpu threads among host nodes will break the topology initialized by -numa option.
So give the guest a opportunity to adjust its topology?

Thanks and regards,
pingfan

maintainer
#4 lore raw

Re: [PATCH 1/2] sched: add virt sched domain for the guest

Quoted 3 lines · Liu ping fan

the movement of vcpu
threads among host nodes will break the topology initialized by -numa option.

You want to remap vcpu to nodes? Are you bloody insane? cpu:node maps are assumed static, you cannot make that a dynamic map and pray things keep working.

Also, have you any idea how expensive it is to rebuild the topology vs migrating the vcpu?

#5 lore raw

Re: [PATCH 1/2] sched: add virt sched domain for the guest

Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Liu ping fan

the movement of vcpu
threads among host nodes will break the topology initialized by -numa option.

Quoted 7 lines

You want to remap vcpu to nodes? Are you bloody insane? cpu:node maps are assumed static, you cannot make that a dynamic map and pray things keep working.

Also, have you any idea how expensive it is to rebuild the topology vs migrating the vcpu?

No, do not rebuild the topology too frequently. Supposing vcpus in node-A/B/C, now node-B is unplugged,
so we need to migrate some of vcpus from node-B to node-A, or to node-C.

Quoted 1 line
maintainer
#6 lore raw

Re: [PATCH 1/2] sched: add virt sched domain for the guest

Quoted 2 lines · Liu ping fan

so we need to migrate some of vcpus from node-B to node-A, or to node-C.

This is absolutely broken, you cannot do that.

A guest task might want to be node affine, it looks at the topology sets a cpu affinity mask and expects to stay on that node.

But then you come along, and flip one of those cpus to another node. The guest task will now run on another node and get remote memory accesses.

Similarly for the guest kernel, it assumes cpu:node maps are static, it will use this for all kinds of things, including the allocation of per-cpu memory to be node affine to that cpu.

If you go migrate cpus across nodes everything comes down.

Please go do something else, I'll do this.

#7 lore raw

Re: [PATCH 1/2] sched: add virt sched domain for the guest

Quoted 4 lines · Peter Zijlstra
Quoted 2 lines · Liu ping fan

so we need to migrate some of vcpus from node-B to node-A, or to node-C.

Quoted 8 lines

This is absolutely broken, you cannot do that.

A guest task might want to be node affine, it looks at the topology sets a cpu affinity mask and expects to stay on that node.

But then you come along, and flip one of those cpus to another node. The guest task will now run on another node and get remote memory accesses.

Oh, I had thought using -smp to handle such situation. The memory accesses cost problem can be partly handled by kvm,
while opening a gap for guest's scheduler to see the host numa info.

Quoted 8 lines

Similarly for the guest kernel, it assumes cpu:node maps are static, it will use this for all kinds of things, including the allocation of per-cpu memory to be node affine to that cpu.

If you go migrate cpus across nodes everything comes down.

Please go do something else, I'll do this.

OK, thanks.
pingfan

maintainer
#8 lore raw

Re: [PATCH 1/2] sched: add virt sched domain for the guest

Quoted 2 lines · Liu ping fan
Quoted 1 line

Please go do something else, I'll do this.

OK so that was to say never, as in dynamic cpu:node relations aren't going to happen. but tip/sched/numa contain the bits needed to make vnuma work.

#9 lore raw

Re: [PATCH 1/2] sched: add virt sched domain for the guest

In reply to Peter Zijlstra
Quoted 10 lines · Peter Zijlstra
Quoted 2 lines · Liu ping fan
Quoted 2 lines

so we need to migrate some of vcpus from node-B to node-A, or to node-C.

This is absolutely broken, you cannot do that.

A guest task might want to be node affine, it looks at the topology sets a cpu affinity mask and expects to stay on that node.

But then you come along, and flip one of those cpus to another node. The guest task will now run on another node and get remote memory accesses.

Insane, sure. But, if the node has physically gone away, what do we do?
I think we've got to either kill the guest, or let it run somewhere
suboptimal. Sounds like you're advocating killing it. ;)

maintainer
#10 lore raw

Re: [PATCH 1/2] sched: add virt sched domain for the guest

Quoted 9 lines · Dave Hansen
Quoted 8 lines · Peter Zijlstra
Quoted 2 lines · Liu ping fan
Quoted 2 lines

so we need to migrate some of vcpus from node-B to node-A, or to node-C.

This is absolutely broken, you cannot do that.

A guest task might want to be node affine, it looks at the topology sets a cpu affinity mask and expects to stay on that node.

Quoted 6 lines
Quoted 2 lines

But then you come along, and flip one of those cpus to another node. The guest task will now run on another node and get remote memory accesses.

Insane, sure. But, if the node has physically gone away, what do we do?
I think we've got to either kill the guest, or let it run somewhere
suboptimal. Sounds like you're advocating killing it. ;)

You all seem terribly confused. If you want a guest that 100% mirrors the host topology you need hard-binding of all vcpu threads and clearly you're in trouble if you unplug a host cpu while there's still a vcpu expecting to run there.

That's an administrator error and you get to keep the pieces, I don't care.

In case you want simple virt-numa where a number of vcpus constitute a vnode and have their memory all on the same node the vcpus are ran on, what does it matter if you unplug something in the host? Just migrate everything -- including memory.

But what Liu was proposing is completely insane and broken. You cannot simply remap cpu:node relations. Wanting to do that shows a profound lack of understanding.

Our kernel assumes that a cpu remains on the same node. All userspace that does anything with NUMA assumes the same. You cannot change this.

#11 lore raw

PATCH 2/2 sched: add virt domain device's driver

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

A driver plays with Qemu's emulated "virt domain device". They aims to export the host numa info to the guest.

--todo:
A more proper place to archive this driver?

---
 drivers/virtio/Kconfig  |    4 ++
 drivers/virtio/Makefile |    1 +
 drivers/virtio/vsd.c    |  124 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 129 insertions(+), 0 deletions(-)
 create mode 100644 drivers/virtio/vsd.c
drivers/virtio/Kconfig +4 -0
index 1a61939..2ab6faa 100644
@@ -45,5 +45,9 @@ config VIRTIO_BALLOON
 	 platform device driver.
 
  	 If unsure, say N.
+config VIRT_SCHED_DOMAIN
+	tristate "virt sched domain driver (EXPERIMENTAL)"
+	---help---
+	This driver make guest scheduler know the NUMA info on host
 
 endmenu
drivers/virtio/Makefile +1 -0
index 5a4c63c..20a565d 100644
@@ -1,4 +1,5 @@
 obj-$(CONFIG_VIRTIO) += virtio.o
+obj-$(CONFIG_VIRT_SCHED_DOMAIN) += vsd.o
 obj-$(CONFIG_VIRTIO_RING) += virtio_ring.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
 obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
drivers/virtio/vsd.c new file +124 -0

124 lines not on this page · raw message

Signature · 1 line
1.7.4.4
#12 lore raw

PATCH 1/1 kvm: collect vcpus' numa info for guest's scheduler

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The guest's scheduler can not see the numa info on the host and this will result to the following scene:
Supposing vcpu-a on nodeA, vcpu-b on nodeB, when load balance, the tasks' pull and push between these vcpus will cost more. But unfortunately, currently, the guest is just blind to this.

This patch want to collect vm's vcpus' numa info.

--todo:
consider about vcpu's initial and hotplug event

---
 arch/x86/kvm/x86.c       |   33 +++++++++++++++++++++++++++++++++
 include/linux/kvm.h      |    6 ++++++
 include/linux/kvm_host.h |    4 ++++
 virt/kvm/kvm_main.c      |   10 ++++++++++
 4 files changed, 53 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +33 -0
index 185a2b8..d907504 100644
@@ -4918,6 +4918,39 @@ void kvm_arch_exit(void)
 	kvm_mmu_module_exit();
 }
 
+#ifdef CONFIG_VIRT_SD_SUPPORTD
+int kvm_arch_guest_numa_update(struct kvm *kvm, void __user *to, int n)
+{
+	struct kvm_vcpu *vcpup;
+	s16 *apci_ids;
+	int idx, node;
+	int ret = 0;
+	unsigned int cpu;
+	struct pid *pid;
+	struct task_struct *tsk;
+	apci_ids = kmalloc(n, GFP_KERNEL);
+	if (apci_ids == NULL)
+		return -ENOMEM;
+	kvm_for_each_vcpu(idx, vcpup, kvm) {
+		rcu_read_lock();
+		pid = rcu_dereference(vcpup->pid);
+		tsk = get_pid_task(pid, PIDTYPE_PID);
+		rcu_read_unlock();
+		if (tsk) {
+			cpu = task_cpu(tsk);
+			put_task_struct(tsk);
+			node = cpu_to_node(cpu);
+		} else
+			node = NUMA_NO_NODE;
+		apci_ids[vcpup->vcpu_id] = node;
+	}
+	if (copy_to_user(to, apci_ids, n))
+		ret = -EFAULT;
+	kfree(apci_ids);
+	return ret;
+}
+#endif
+
 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
 {
 	++vcpu->stat.halt_exits;
include/linux/kvm.h +6 -0
index 6c322a9..da4c0bc 100644
@@ -732,6 +732,7 @@ struct kvm_one_reg {
 					struct kvm_userspace_memory_region)
 #define KVM_SET_TSS_ADDR          _IO(KVMIO,   0x47)
 #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO,  0x48, __u64)
+#define KVM_SET_GUEST_NUMA             _IOW(KVMIO,  0x49, struct kvm_virt_sd)
 
 /* enable ucontrol for s390 */
 struct kvm_s390_ucas_mapping {
@@ -909,5 +910,10 @@ struct kvm_assigned_msix_entry {
 	__u16 entry; /* The index of entry in the MSI-X table */
 	__u16 padding[3];
 };
+#define VIRT_SD_SUPPORTD
+struct kvm_virt_sd {
+	__u64 *vapic_map;
+	__u64 sz;
+};
 
 #endif /* __LINUX_KVM_H */
include/linux/kvm_host.h +4 -0
index 72cbf08..328aa0c 100644
@@ -526,6 +526,10 @@ void kvm_arch_destroy_vm(struct kvm *kvm);
 void kvm_free_all_assigned_devices(struct kvm *kvm);
 void kvm_arch_sync_events(struct kvm *kvm);
 
+#ifdef CONFIG_VIRT_SD_SUPPORTD
+int kvm_arch_guest_numa_update(struct kvm *kvm, void __user *to, int n);
+#endif
+
 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
 void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
 
virt/kvm/kvm_main.c +10 -0
index 9739b53..46292bd 100644
@@ -2029,6 +2029,16 @@ static long kvm_vm_ioctl(struct file *filp,
 		r = kvm_ioeventfd(kvm, &data);
 		break;
 	}
+#ifdef CONFIG_VIRT_SD_SUPPORTD
+	case KVM_SET_GUEST_NUMA: {
+		struct kvm_virt_sd sd;
+		r = -EFAULT;
+		if (copy_from_user(&sd, argp, sizeof sd))
+			goto out;
+		r = kvm_arch_guest_numa_update(kvm, sd.vapic_map, sd.sz);
+		break;
+	}
+#endif
 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
 	case KVM_SET_BOOT_CPU_ID:
 		r = 0;
Signature · 1 line
1.7.4.4
#13 lore raw

PATCH 1/1 Qemu: add virt sched domain device

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

The device will demand the collection of vcpus' numa info, and trigger the guest to rebuild the sched domain.

---
 Makefile.target           |    1 +
 hmp-commands.hx           |   16 +++++
 hw/qdev.h                 |    1 +
 hw/virt_sd.c              |  155 +++++++++++++++++++++++++++++++++++++++++++++
 linux-headers/linux/kvm.h |    8 ++-
 5 files changed, 180 insertions(+), 1 deletions(-)
 create mode 100644 hw/virt_sd.c
Makefile.target +1 -0
index 4fbbabf..fded330 100644
@@ -265,6 +265,7 @@ obj-i386-y += pci-hotplug.o smbios.o wdt_ib700.o
 obj-i386-y += debugcon.o multiboot.o
 obj-i386-y += pc_piix.o
 obj-i386-y += pc_sysfw.o
+obj-i386-y += virt_sd.o
 obj-i386-$(CONFIG_KVM) += kvm/clock.o kvm/apic.o kvm/i8259.o kvm/ioapic.o kvm/i8254.o
 obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
 
hmp-commands.hx +16 -0
index 461fa59..47b826c 100644
@@ -1254,6 +1254,22 @@ Change I/O throttle limits for a block drive to @var{bps} @var{bps_rd} @var{bps_
 ETEXI
 
     {
+        .name       = "guest_numa_notify",
+        .args_type  = "",
+        .params     = "",
+        .help       = "force guest to update numa info based on host",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_guest_numa_notify,
+    },
+
+STEXI
+@item device_add @var{config}
+@findex device_add
+
+Add device.
+ETEXI
+
+    {
         .name       = "block_set_io_throttle",
         .args_type  = "device:B,bps:l,bps_rd:l,bps_wr:l,iops:l,iops_rd:l,iops_wr:l",
         .params     = "device bps bps_rd bps_wr iops iops_rd iops_wr",
hw/qdev.h +1 -0
index 4e90119..6902474 100644
@@ -203,6 +203,7 @@ void do_info_qtree(Monitor *mon);
 void do_info_qdm(Monitor *mon);
 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
+int do_guest_numa_notify(Monitor *mon, const QDict *qdict, QObject **ret_data);
 
 /*** qdev-properties.c ***/
 
hw/virt_sd.c new file +155 -0

155 lines not on this page · raw message

linux-headers/linux/kvm.h +7 -1
index ee7bd9c..aa5aec3 100644
@@ -448,7 +448,6 @@ struct kvm_ppc_pvinfo {
 	__u32 hcall[4];
 	__u8  pad[108];
 };
-
 #define KVMIO 0xAE
 
 /* machine type bits, to be used as argument to KVM_CREATE_VM */
@@ -478,6 +477,7 @@ struct kvm_ppc_pvinfo {
 #define KVM_TRACE_PAUSE           __KVM_DEPRECATED_MAIN_0x07
 #define KVM_TRACE_DISABLE         __KVM_DEPRECATED_MAIN_0x08
 
+
 /*
  * Extension capability list.
  */
@@ -733,6 +733,7 @@ struct kvm_one_reg {
 					struct kvm_userspace_memory_region)
 #define KVM_SET_TSS_ADDR          _IO(KVMIO,   0x47)
 #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO,  0x48, __u64)
+#define KVM_SET_GUEST_NUMA             _IOW(KVMIO,  0x49, struct kvm_virt_sd)
 
 /* enable ucontrol for s390 */
 struct kvm_s390_ucas_mapping {
@@ -913,4 +914,9 @@ struct kvm_assigned_msix_entry {
 	__u16 padding[3];
 };
 
+struct kvm_virt_sd {
+	__u64 *vapic_map;
+	__u64 sz;
+};
+
 #endif /* __LINUX_KVM_H */
Signature · 1 line
1.7.4.4