Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp906433pxt; Thu, 5 Aug 2021 14:56:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx7at4ExnYBPtZOxnliCCgesC4mroYJfGBdnrrKQ36uDaAyYJEGYu8ExZRXpKixQtNzb3mr X-Received: by 2002:a05:6402:4246:: with SMTP id g6mr9143641edb.95.1628200567119; Thu, 05 Aug 2021 14:56:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628200567; cv=none; d=google.com; s=arc-20160816; b=fWDahTwHIbRvwqCbty/NzPnD/3WnA5Gw5e81lbtNnfp14cHGSGbXePwThhJplacbiv zFiJV1lrg+8GHMEd6xNVyyGnXBlIbsXBjehdlWFD39q9clI6PkWtEeFEudWd9eppCXR8 c2PLTbu5R3chKLXxqePnnp0cMKtV3SZcWJJgl3Xo/L0VAK7E4w4VWRdoJsx6I2nPvrSg sRZz1tF484mVFZTZT3y1cQlQ79boZPUE0HhV//PCJDS8wehmNEPIUXXzg8leHpNVQUvb NFGZ3HCyb1tXQekaMgZ2TvFWUGJAZRytPLljpmuRJs5GPt4s4gMIfS9ekZbfZWVp6wJB 1ttg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature:dkim-filter; bh=BRDAbJ2+RsjCKon8JXNuQUOcN3jyb6WZEWzLpd8RSUc=; b=UDdZtm+K3hHnUlQh3bfLgEx32INztqspingts4peFu+Ycu1kRIbj3gi09mZcsfcD6E V2BzP83YPmGMh3Bz19luIk6j9/Lm/sIDygJaPmVqG2NxsJUujXu5F3KeF1IrT9y8yfyi 8MLmAKtJwoYo3sZbpbcYnMpB8gC+aXyoaOA/vjzUhMOeZjHxaXnR8xk73uaeOpJoPoQc OP4cBGy26vjdAY98z4JsOjVkiJhKggrdFVorAer+7JM98KgjN8woCmKdD4BSad27R/W1 i3KnyYSQeMH8aAE2bQZNfq9Qk6cGFcmjfX9Uu1EQDFH/MLdhcggwsuD2Lljr++kQPEeE aPDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=pbBjG4wL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j17si7128490edw.193.2021.08.05.14.55.44; Thu, 05 Aug 2021 14:56:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=pbBjG4wL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242380AbhHEVZD (ORCPT + 99 others); Thu, 5 Aug 2021 17:25:03 -0400 Received: from linux.microsoft.com ([13.77.154.182]:46546 "EHLO linux.microsoft.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230180AbhHEVYs (ORCPT ); Thu, 5 Aug 2021 17:24:48 -0400 Received: from linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net (linux.microsoft.com [13.77.154.182]) by linux.microsoft.com (Postfix) with ESMTPSA id 5E11C20B36EE; Thu, 5 Aug 2021 14:24:33 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5E11C20B36EE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1628198673; bh=BRDAbJ2+RsjCKon8JXNuQUOcN3jyb6WZEWzLpd8RSUc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pbBjG4wLtHGUTc1QPEhu+GPhaQumZ9Nms5vi33SDfhYY41IeC7pKYSxitSkNrJGMi 691azSSP3RTWbl/92JH/65mWlGNhboaxbPwiG6VQEov5HqfI2ZNyUfV6WBCGAPCmJU AdUcdsRmIZ7R7iKW2szlF0ILlI0+++U6D8aVpVUc= From: Nuno Das Neves To: linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Cc: virtualization@lists.linux-foundation.org, mikelley@microsoft.com, viremana@linux.microsoft.com, sunilmut@microsoft.com, wei.liu@kernel.org, vkuznets@redhat.com, ligrassi@microsoft.com, kys@microsoft.com, sthemmin@microsoft.com, anbelski@linux.microsoft.com Subject: [PATCH 06/19] drivers/hv: create, initialize, finalize, delete partition hypercalls Date: Thu, 5 Aug 2021 14:23:48 -0700 Message-Id: <1628198641-791-7-git-send-email-nunodasneves@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1628198641-791-1-git-send-email-nunodasneves@linux.microsoft.com> References: <1628198641-791-1-git-send-email-nunodasneves@linux.microsoft.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add hypercalls for fully setting up and mostly tearing down a guest partition. Export hv_call_deposit_memory and hv_call_create_vp. The teardown operation will generate an error as the deposited memory has not been withdrawn. This is fixed in the next patch. Co-developed-by: Lillian Grassin-Drake Signed-off-by: Lillian Grassin-Drake Signed-off-by: Nuno Das Neves Signed-off-by: Vineeth Pillai --- arch/x86/hyperv/hv_proc.c | 2 + drivers/hv/Makefile | 2 +- drivers/hv/hv_call.c | 129 ++++++++++++++++++++++++++++++ drivers/hv/mshv.h | 28 +++++++ drivers/hv/mshv_main.c | 34 +++++++- include/asm-generic/hyperv-tlfs.h | 49 ++++++++++++ 6 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 drivers/hv/hv_call.c create mode 100644 drivers/hv/mshv.h diff --git a/arch/x86/hyperv/hv_proc.c b/arch/x86/hyperv/hv_proc.c index e75c78a243e7..ae52dd5fab6d 100644 --- a/arch/x86/hyperv/hv_proc.c +++ b/arch/x86/hyperv/hv_proc.c @@ -146,6 +146,7 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) kfree(counts); return ret; } +EXPORT_SYMBOL_GPL(hv_call_deposit_pages); int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id) { @@ -243,4 +244,5 @@ int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags) return ret; } +EXPORT_SYMBOL_GPL(hv_call_create_vp); diff --git a/drivers/hv/Makefile b/drivers/hv/Makefile index c943fbeb70e7..649748c9c144 100644 --- a/drivers/hv/Makefile +++ b/drivers/hv/Makefile @@ -13,4 +13,4 @@ hv_vmbus-y := vmbus_drv.o \ hv_vmbus-$(CONFIG_HYPERV_TESTING) += hv_debugfs.o hv_utils-y := hv_util.o hv_kvp.o hv_snapshot.o hv_fcopy.o hv_utils_transport.o -mshv-y += mshv_main.o +mshv-y += mshv_main.o hv_call.o diff --git a/drivers/hv/hv_call.c b/drivers/hv/hv_call.c new file mode 100644 index 000000000000..a96809792d63 --- /dev/null +++ b/drivers/hv/hv_call.c @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2021, Microsoft Corporation. + * + * Authors: + * Nuno Das Neves + * Lillian Grassin-Drake + * Vineeth Pillai + */ + +#include +#include +#include + +#include "mshv.h" + +int hv_call_create_partition( + u64 flags, + struct hv_partition_creation_properties creation_properties, + u64 *partition_id) +{ + struct hv_create_partition_in *input; + struct hv_create_partition_out *output; + u64 status; + int ret; + unsigned long irq_flags; + int i; + + do { + local_irq_save(irq_flags); + input = (struct hv_create_partition_in *)(*this_cpu_ptr( + hyperv_pcpu_input_arg)); + output = (struct hv_create_partition_out *)(*this_cpu_ptr( + hyperv_pcpu_output_arg)); + + input->flags = flags; + input->proximity_domain_info.as_uint64 = 0; + input->compatibility_version = HV_COMPATIBILITY_20_H1; + for (i = 0; i < HV_PARTITION_PROCESSOR_FEATURE_BANKS; ++i) + input->partition_creation_properties + .disabled_processor_features.as_uint64[i] = 0; + input->partition_creation_properties + .disabled_processor_xsave_features.as_uint64 = 0; + input->isolation_properties.as_uint64 = 0; + + status = hv_do_hypercall(HVCALL_CREATE_PARTITION, + input, output); + + if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) { + if (hv_result_success(status)) + *partition_id = output->partition_id; + else + pr_err("%s: %s\n", + __func__, hv_status_to_string(status)); + local_irq_restore(irq_flags); + ret = hv_status_to_errno(status); + break; + } + local_irq_restore(irq_flags); + ret = hv_call_deposit_pages(NUMA_NO_NODE, + hv_current_partition_id, 1); + } while (!ret); + + return ret; +} + +int hv_call_initialize_partition(u64 partition_id) +{ + struct hv_initialize_partition input; + u64 status; + int ret; + + input.partition_id = partition_id; + + ret = hv_call_deposit_pages( + NUMA_NO_NODE, + partition_id, + HV_INIT_PARTITION_DEPOSIT_PAGES); + if (ret) + return ret; + + do { + status = hv_do_fast_hypercall8( + HVCALL_INITIALIZE_PARTITION, + *(u64 *)&input); + + if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) { + if (!hv_result_success(status)) + pr_err("%s: %s\n", + __func__, hv_status_to_string(status)); + ret = hv_status_to_errno(status); + break; + } + ret = hv_call_deposit_pages(NUMA_NO_NODE, partition_id, 1); + } while (!ret); + + return ret; +} + +int hv_call_finalize_partition(u64 partition_id) +{ + struct hv_finalize_partition input; + u64 status; + + input.partition_id = partition_id; + status = hv_do_fast_hypercall8( + HVCALL_FINALIZE_PARTITION, + *(u64 *)&input); + + if (!hv_result_success(status)) + pr_err("%s: %s\n", __func__, hv_status_to_string(status)); + + return hv_status_to_errno(status); +} + +int hv_call_delete_partition(u64 partition_id) +{ + struct hv_delete_partition input; + u64 status; + + input.partition_id = partition_id; + status = hv_do_fast_hypercall8(HVCALL_DELETE_PARTITION, *(u64 *)&input); + + if (!hv_result_success(status)) + pr_err("%s: %s\n", __func__, hv_status_to_string(status)); + + return hv_status_to_errno(status); +} + diff --git a/drivers/hv/mshv.h b/drivers/hv/mshv.h new file mode 100644 index 000000000000..46121bd30592 --- /dev/null +++ b/drivers/hv/mshv.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2021, Microsoft Corporation. + * + * Authors: + * Nuno Das Neves + * Lillian Grassin-Drake + * Vineeth Pillai + */ + +#ifndef _MSHV_H_ +#define _MSHV_H_ + +/* Determined empirically */ +#define HV_INIT_PARTITION_DEPOSIT_PAGES 208 + +/* + * Hyper-V hypercalls + */ +int hv_call_create_partition( + u64 flags, + struct hv_partition_creation_properties creation_properties, + u64 *partition_id); +int hv_call_initialize_partition(u64 partition_id); +int hv_call_finalize_partition(u64 partition_id); +int hv_call_delete_partition(u64 partition_id); + +#endif /* _MSHV_H */ diff --git a/drivers/hv/mshv_main.c b/drivers/hv/mshv_main.c index 98d8842980e4..d480b83316e1 100644 --- a/drivers/hv/mshv_main.c +++ b/drivers/hv/mshv_main.c @@ -15,6 +15,9 @@ #include #include #include +#include + +#include "mshv.h" MODULE_AUTHOR("Microsoft"); MODULE_LICENSE("GPL"); @@ -49,6 +52,7 @@ static struct miscdevice mshv_dev = { .mode = 0600, }; + static long mshv_partition_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { @@ -78,6 +82,17 @@ destroy_partition(struct mshv_partition *partition) spin_unlock_irqrestore(&mshv.partitions.lock, flags); + /* + * There are no remaining references to the partition, + * so the remaining cleanup can be lockless + */ + + /* Deallocates and unmaps everything including vcpus, GPA mappings etc */ + hv_call_finalize_partition(partition->id); + /* TODO: Withdraw and free all pages we deposited */ + + hv_call_delete_partition(partition->id); + kfree(partition); } @@ -138,6 +153,9 @@ mshv_ioctl_create_partition(void __user *user_arg) if (copy_from_user(&args, user_arg, sizeof(args))) return -EFAULT; + /* Only support EXO partitions */ + args.flags |= HV_PARTITION_CREATION_FLAG_EXO_PARTITION; + partition = kzalloc(sizeof(*partition), GFP_KERNEL); if (!partition) return -ENOMEM; @@ -148,11 +166,21 @@ mshv_ioctl_create_partition(void __user *user_arg) goto free_partition; } + ret = hv_call_create_partition(args.flags, + args.partition_creation_properties, + &partition->id); + if (ret) + goto put_fd; + + ret = hv_call_initialize_partition(partition->id); + if (ret) + goto delete_partition; + file = anon_inode_getfile("mshv_partition", &mshv_partition_fops, partition, O_RDWR); if (IS_ERR(file)) { ret = PTR_ERR(file); - goto put_fd; + goto finalize_partition; } refcount_set(&partition->ref_count, 1); @@ -166,6 +194,10 @@ mshv_ioctl_create_partition(void __user *user_arg) release_file: file->f_op->release(file->f_inode, file); +finalize_partition: + hv_call_finalize_partition(partition->id); +delete_partition: + hv_call_delete_partition(partition->id); put_fd: put_unused_fd(fd); free_partition: diff --git a/include/asm-generic/hyperv-tlfs.h b/include/asm-generic/hyperv-tlfs.h index 50dc6eafb6a6..49099b7e0f71 100644 --- a/include/asm-generic/hyperv-tlfs.h +++ b/include/asm-generic/hyperv-tlfs.h @@ -143,6 +143,10 @@ struct ms_hyperv_tsc_page { #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX 0x0013 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX 0x0014 #define HVCALL_SEND_IPI_EX 0x0015 +#define HVCALL_CREATE_PARTITION 0x0040 +#define HVCALL_INITIALIZE_PARTITION 0x0041 +#define HVCALL_FINALIZE_PARTITION 0x0042 +#define HVCALL_DELETE_PARTITION 0x0043 #define HVCALL_GET_PARTITION_ID 0x0046 #define HVCALL_DEPOSIT_MEMORY 0x0048 #define HVCALL_CREATE_VP 0x004e @@ -826,4 +830,49 @@ struct hv_memory_hint { union hv_gpa_page_range ranges[]; } __packed; +union hv_partition_isolation_properties { + u64 as_uint64; + struct { + u64 isolation_type: 5; + u64 rsvd_z: 7; + u64 shared_gpa_boundary_page_number: 52; + }; +} __packed; + +/* Non-userspace-visible partition creation flags */ +#define HV_PARTITION_CREATION_FLAG_EXO_PARTITION BIT(8) + +#define HV_MAKE_COMPATIBILITY_VERSION(major_, minor_) \ + ((u32)((major_) << 8 | (minor_))) + +#define HV_COMPATIBILITY_19_H1 HV_MAKE_COMPATIBILITY_VERSION(0X6, 0X5) +#define HV_COMPATIBILITY_20_H1 HV_MAKE_COMPATIBILITY_VERSION(0X6, 0X7) +#define HV_COMPATIBILITY_PRERELEASE HV_MAKE_COMPATIBILITY_VERSION(0XFE, 0X0) +#define HV_COMPATIBILITY_EXPERIMENT HV_MAKE_COMPATIBILITY_VERSION(0XFF, 0X0) + +struct hv_create_partition_in { + u64 flags; + union hv_proximity_domain_info proximity_domain_info; + u32 compatibility_version; + u32 padding; + struct hv_partition_creation_properties partition_creation_properties; + union hv_partition_isolation_properties isolation_properties; +} __packed; + +struct hv_create_partition_out { + u64 partition_id; +} __packed; + +struct hv_initialize_partition { + u64 partition_id; +} __packed; + +struct hv_finalize_partition { + u64 partition_id; +} __packed; + +struct hv_delete_partition { + u64 partition_id; +} __packed; + #endif -- 2.23.4