[RFC PATCH 15/15] drivers/acrn: add the support of offline SOS cpu

From: Zhao Yakui
Date: Thu Aug 15 2019 - 22:33:34 EST


The ACRN-hypervisor works in partition mode. In such case the guest OS
and domain0 kernel will run in the different CPUs. In course of booting
domain0 kernel, it can use all the available CPUs,which can accelerate
the booting. But after the booting is finished, it needs to offline the
other CPUs so that they can be allocated to the guest OS.

add sysfs with attr "offline_cpu", use
echo cpu_id > /sys/class/acrn/acrn_hsm/offline_cpu
to do the hypercall offline/destroy according vcpu.
before doing it, It will offline cpu by using the below cmd:
echo 0 > /sys/devices/system/cpu/cpuX/online

Currently this is mainly used in user-space device model before
booting other ACRN guest.

Co-developed-by: Jason Chen CJ <jason.cj.chen@xxxxxxxxx>
Signed-off-by: Jason Chen CJ <jason.cj.chen@xxxxxxxxx>
Signed-off-by: Zhao Yakui <yakui.zhao@xxxxxxxxx>
---
drivers/staging/acrn/acrn_dev.c | 45 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)

diff --git a/drivers/staging/acrn/acrn_dev.c b/drivers/staging/acrn/acrn_dev.c
index 0602125..6868003 100644
--- a/drivers/staging/acrn/acrn_dev.c
+++ b/drivers/staging/acrn/acrn_dev.c
@@ -588,6 +588,41 @@ static const struct file_operations fops = {
#define SUPPORT_HV_API_VERSION_MAJOR 1
#define SUPPORT_HV_API_VERSION_MINOR 0

+static ssize_t
+offline_cpu_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+#ifdef CONFIG_X86
+ u64 cpu, lapicid;
+
+ if (kstrtoull(buf, 0, &cpu) < 0)
+ return -EINVAL;
+
+ if (cpu_possible(cpu)) {
+ lapicid = cpu_data(cpu).apicid;
+ pr_info("acrn: try to offline cpu %lld with lapicid %lld\n",
+ cpu, lapicid);
+ if (hcall_sos_offline_cpu(lapicid) < 0) {
+ pr_err("acrn: failed to offline cpu from Hypervisor!\n");
+ return -EINVAL;
+ }
+ }
+#endif
+ return count;
+}
+
+static DEVICE_ATTR(offline_cpu, 00200, NULL, offline_cpu_store);
+
+static struct attribute *acrn_attrs[] = {
+ &dev_attr_offline_cpu.attr,
+ NULL
+};
+
+static struct attribute_group acrn_attr_group = {
+ .attrs = acrn_attrs,
+};
+
static int __init acrn_init(void)
{
unsigned long flag;
@@ -647,6 +682,15 @@ static int __init acrn_init(void)
return PTR_ERR(acrn_device);
}

+ if (sysfs_create_group(&acrn_device->kobj, &acrn_attr_group)) {
+ pr_warn("acrn: sysfs create failed\n");
+ device_destroy(acrn_class, MKDEV(major, 0));
+ class_unregister(acrn_class);
+ class_destroy(acrn_class);
+ unregister_chrdev(major, DEVICE_NAME);
+ return -EINVAL;
+ }
+
tasklet_init(&acrn_io_req_tasklet, io_req_tasklet, 0);
local_irq_save(flag);
acrn_setup_intr_irq(acrn_intr_handler);
@@ -664,6 +708,7 @@ static void __exit acrn_exit(void)

tasklet_kill(&acrn_io_req_tasklet);
acrn_remove_intr_irq();
+ sysfs_remove_group(&acrn_device->kobj, &acrn_attr_group);
device_destroy(acrn_class, MKDEV(major, 0));
class_unregister(acrn_class);
class_destroy(acrn_class);
--
2.7.4