From patchwork Fri Feb 23 00:42:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205161 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp308171dyb; Thu, 22 Feb 2024 16:46:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV6oSWXluf4dSku28lwrgwqECK3/TqxRPyIfltxA5/nwoucvuf3KHxU683mjjyj/02eE8ZcBTOOlapmjAihUx475+QcHw== X-Google-Smtp-Source: AGHT+IFWz5JVfTytPmKPCSbFNaHIoyAAesK5ktzrjnht1pGek7VJKnvoipFvjVtqfy0EIhURQhVn X-Received: by 2002:a05:6870:414b:b0:21f:bb0:506e with SMTP id r11-20020a056870414b00b0021f0bb0506emr578498oad.42.1708649180635; Thu, 22 Feb 2024 16:46:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649180; cv=pass; d=google.com; s=arc-20160816; b=nHH6bwjRfI7pJf+d2yJfFFE8m0lZGUQ2QaeC7toZ+WJj87UBrZOGZc9HAobSDZtSrH 39MOxRD1fwEjyeBhu2vBBouKeC8uhgXjxwbKdmUD2e8DKgNRsXnI5HO/d2Y5oF0mlCYK TmD0qnSzTn3xUdEWgj/HPkpShCADWkLkMDiZuWrGydn6ehiDSH6yzqqtTEf3IA7NVFXB K7V8e/yt1aW9R8/pDx1W6o+rgiTc1Ize+XuwuFQvMZJyIuxtXEK3atZ8FdrkC0fUEoI3 5jb6Bb3P2SJ7eHKp+8bmjsTp8hKjmSKV91seU5CsNqticm3OALRSXnua8UZtbIjpk60a bOxw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=lRpse7Hgpajlk57C2H5vth1Tg0pkPPWc04aEOHzQOwA=; fh=VrNDnL7yfHHULTK68DG6+UrhXoz3/xz2GIJjZHjZS8A=; b=az3AoPCCfUJlgEw3LdDMOnsHeRGSAk9NxeVpgr+9cKee/ajkwRqZ8k+p1lQcEvK7A1 qoMiYxQxvTI9ZvTcQFma/QaHQMlb/U82b6Nl1uLZOXTgcK29CTwBovNv1vemUtrAWS2C zZ0Fi28E+GFBXgNP3sYXTRKR2cVh04X0IWEdM7V9sEvQ839VNH+DN7JbrMIuCY0Z6jVa SCqk1e1iYvhsKtYfZBbeEtPOLEXD2Skf/ZEJJKqver3RHXjtOwaOAHYL1e0hLhqlAARQ UqEgNDye2w/GuYDBkX3kosy1o4XePDqsM5smOwiKT165PqnzaNq1R3fjoWFBm0X47FzN iR+g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Z1fqfPJU; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-77590-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77590-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id a17-20020a637051000000b005d8bea05154si10989393pgn.619.2024.02.22.16.46.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:46:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77590-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Z1fqfPJU; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-77590-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77590-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 562C7283C17 for ; Fri, 23 Feb 2024 00:46:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5A1DA1758D; Fri, 23 Feb 2024 00:43:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z1fqfPJU" Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D77012E4E for ; Fri, 23 Feb 2024 00:43:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708649001; cv=none; b=XeG3nW7AsOj1a0oBaUjEkECxCLEeYeSgpoP8atdxg3QLm3IX9mDVN9ijVmr3BJUi/X7Wf4Eu643cNlCDp0ztEJDGmPHhMNg3nDt0VKbFxr66Heayyoakxl/+HqOr91cluyxsAjsu/1IAQQWRZbZz295xlbhRX4w30g1NCZAv0zs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708649001; c=relaxed/simple; bh=tyZgfxNkQndxJJOO7ECuOTfeiV340CWZKe8/mOgdQK8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SC+vBkJjtM0BFYIdZllebxdfvw/4RwouuyZy1QjaIoOiKY2rrOP1H4KY08joquSsEfw9DpUdZaW520mf+kWCRxX7LAhJwe9YudMpr9ZdgXJkM9ewv66lQ7sgagKA+2S8N3BMNRHuktqwHBUDOHzG+kCB4zh1929ODJFnnk+Yr8A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Z1fqfPJU; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-299783294a6so299404a91.3 for ; Thu, 22 Feb 2024 16:43:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708648999; x=1709253799; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=lRpse7Hgpajlk57C2H5vth1Tg0pkPPWc04aEOHzQOwA=; b=Z1fqfPJUV+2UqYUSJtafJe8vLVB9XizU+aOn9sFXD6S0fm6oaLGS4E1DNI7snLbVs9 8GKrRjRlslYLGAXNMKHL0MeAmThcFnM9ysEI1ICg4MeROyVYJx9uFKIYODGBNq+KiHih S0SxywN7xIQd5JJ32+aNtGdPkWfoc5u9CwHloMZ9CEPiS+YRsXwhMbx2oeg4Es2wsQde yeY9piBAgqRMhK81CGmitNgJbvTiFYEX63ZnjFWPniPd2tCpUlh71hUou7+BUYFHpjc/ 5TMYy6hZY5gJSp5Tmcc9lKwELLwEQAU4yAKKrxJqxTlj+1oKNbxQGjAgtd62YkPOoJ7l PoQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708648999; x=1709253799; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lRpse7Hgpajlk57C2H5vth1Tg0pkPPWc04aEOHzQOwA=; b=OAjZCAh5w18SjFxuPZ291hb8+QLgr12OaxJ8MCu/R1IXNW+DmAMxxvTsI0SF2w8XMa GDXrBa/IwjGh3vDAXYida/05IUucpRcPojQcFHwLtodn8ySEbApff950bzq++x7SXoqR V7FP9KwR/EHg5YFjjLp+8D6+7r5nyLvT9A1EcZW4ztAHbOYtRCrapBS1NZguSQmGVeCl c40AtC77VDhwSddBKIN8qwbZSSdeFv/Zp5jRGpoCp66urwlF2uQP2FgzYenUlE7elSOy D48qZRJGE2JOqzU2co0zhOOs1GOX+KWY+8Lbh0Tih5tAj/Mq58lryrPK6O8ETg4rUsyJ en/Q== X-Forwarded-Encrypted: i=1; AJvYcCXqqjPpRbSuY7Io7FTMZb855KwcZaP2i9OPBHZ2A4JKi+KsiEnspwGEZRfHloUdnOmqDNUZXhunVCiSQ79f4sdSnON1PNVvB3sRefeN X-Gm-Message-State: AOJu0Yxya7146V3rl55XT8Byq6cXdRGo9mIZBuHruq2ZnYrgmfiS7Qt8 bR0E3GT+q+OKtVOXzOh1+87jlCj3kmJ3xhO2kKkVfHf7khZXA5mHcuDRZnC+8+Rk4zwn/uZ7tQq z3w== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:3d01:b0:299:942e:6634 with SMTP id pt1-20020a17090b3d0100b00299942e6634mr1218pjb.1.1708648999474; Thu, 22 Feb 2024 16:43:19 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:56 -0800 In-Reply-To: <20240223004258.3104051-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-10-seanjc@google.com> Subject: [PATCH v9 09/11] KVM: selftests: Use the SEV library APIs in the intra-host migration test From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648522767445612 X-GMAIL-MSGID: 1791648522767445612 Port the existing intra-host SEV(-ES) migration test to the recently added SEV library, which handles much of the boilerplate needed to create and configure SEV guests. Tested-by: Carlos Bilbao Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/sev_migrate_tests.c | 67 ++++++------------- 1 file changed, 21 insertions(+), 46 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c index a49828adf294..d6f7428e42c6 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c +++ b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c @@ -10,11 +10,9 @@ #include "test_util.h" #include "kvm_util.h" #include "processor.h" -#include "svm_util.h" +#include "sev.h" #include "kselftest.h" -#define SEV_POLICY_ES 0b100 - #define NR_MIGRATE_TEST_VCPUS 4 #define NR_MIGRATE_TEST_VMS 3 #define NR_LOCK_TESTING_THREADS 3 @@ -22,46 +20,24 @@ bool have_sev_es; -static int __sev_ioctl(int vm_fd, int cmd_id, void *data, __u32 *fw_error) -{ - struct kvm_sev_cmd cmd = { - .id = cmd_id, - .data = (uint64_t)data, - .sev_fd = open_sev_dev_path_or_exit(), - }; - int ret; - - ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd); - *fw_error = cmd.error; - return ret; -} - -static void sev_ioctl(int vm_fd, int cmd_id, void *data) -{ - int ret; - __u32 fw_error; - - ret = __sev_ioctl(vm_fd, cmd_id, data, &fw_error); - TEST_ASSERT(ret == 0 && fw_error == SEV_RET_SUCCESS, - "%d failed: return code: %d, errno: %d, fw error: %d", - cmd_id, ret, errno, fw_error); -} - static struct kvm_vm *sev_vm_create(bool es) { struct kvm_vm *vm; - struct kvm_sev_launch_start start = { 0 }; int i; vm = vm_create_barebones(); - sev_ioctl(vm->fd, es ? KVM_SEV_ES_INIT : KVM_SEV_INIT, NULL); + if (!es) + sev_vm_init(vm); + else + sev_es_vm_init(vm); + for (i = 0; i < NR_MIGRATE_TEST_VCPUS; ++i) __vm_vcpu_add(vm, i); + + sev_vm_launch(vm, es ? SEV_POLICY_ES : 0); + if (es) - start.policy |= SEV_POLICY_ES; - sev_ioctl(vm->fd, KVM_SEV_LAUNCH_START, &start); - if (es) - sev_ioctl(vm->fd, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL); + vm_sev_ioctl(vm, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL); return vm; } @@ -181,7 +157,7 @@ static void test_sev_migrate_parameters(void) sev_vm = sev_vm_create(/* es= */ false); sev_es_vm = sev_vm_create(/* es= */ true); sev_es_vm_no_vmsa = vm_create_barebones(); - sev_ioctl(sev_es_vm_no_vmsa->fd, KVM_SEV_ES_INIT, NULL); + sev_es_vm_init(sev_es_vm_no_vmsa); __vm_vcpu_add(sev_es_vm_no_vmsa, 1); ret = __sev_migrate_from(sev_vm, sev_es_vm); @@ -230,13 +206,13 @@ static void sev_mirror_create(struct kvm_vm *dst, struct kvm_vm *src) TEST_ASSERT(!ret, "Copying context failed, ret: %d, errno: %d", ret, errno); } -static void verify_mirror_allowed_cmds(int vm_fd) +static void verify_mirror_allowed_cmds(struct kvm_vm *vm) { struct kvm_sev_guest_status status; + int cmd_id; - for (int cmd_id = KVM_SEV_INIT; cmd_id < KVM_SEV_NR_MAX; ++cmd_id) { + for (cmd_id = KVM_SEV_INIT; cmd_id < KVM_SEV_NR_MAX; ++cmd_id) { int ret; - __u32 fw_error; /* * These commands are allowed for mirror VMs, all others are @@ -256,14 +232,13 @@ static void verify_mirror_allowed_cmds(int vm_fd) * These commands should be disallowed before the data * parameter is examined so NULL is OK here. */ - ret = __sev_ioctl(vm_fd, cmd_id, NULL, &fw_error); - TEST_ASSERT( - ret == -1 && errno == EINVAL, - "Should not be able call command: %d. ret: %d, errno: %d", - cmd_id, ret, errno); + ret = __vm_sev_ioctl(vm, cmd_id, NULL); + TEST_ASSERT(ret == -1 && errno == EINVAL, + "Should not be able call command: %d. ret: %d, errno: %d", + cmd_id, ret, errno); } - sev_ioctl(vm_fd, KVM_SEV_GUEST_STATUS, &status); + vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status); } static void test_sev_mirror(bool es) @@ -281,9 +256,9 @@ static void test_sev_mirror(bool es) __vm_vcpu_add(dst_vm, i); if (es) - sev_ioctl(dst_vm->fd, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL); + vm_sev_ioctl(dst_vm, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL); - verify_mirror_allowed_cmds(dst_vm->fd); + verify_mirror_allowed_cmds(dst_vm); kvm_vm_free(src_vm); kvm_vm_free(dst_vm);