From patchwork Tue Nov 12 23:22:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 13873023 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 C52C721B456 for ; Tue, 12 Nov 2024 23:23:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453784; cv=none; b=asJy25iBug73Nctmk+4Vwc565DfDR7uEnDrVjOBi2kRoqABN+uvWT9d9DdGg1eWQVlQJwGCkK3t2K0mup2C8qvJjypefwnHcxFpiRtFI1XXD66WBU/mUbLqTahTHDbsdNtqK/ye/UsdI31g9/z5ddymDzLLdXgaJo8g6GYEDUiU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453784; c=relaxed/simple; bh=Jb7bsAHuq1g+F+AyFt83enXaiVLr1zlWcNhrBiuDpbE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VqhYII684SaQayivVt+Kd/hxFXV+yONhgFGsm+mCGyzEQ/zyPWcomNjMbR11cDdt04Shg2KUK3VDDFnOHQhkC4KNckaSSgoEzBo6VqWUygGriCcWM/gZvYTyCbNQS/x6tL/XqO4N2CAkU9VS4D/WpMFuLrOyxICQXD1shzYxNxs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=COv6p2yz; arc=none smtp.client-ip=209.85.128.201 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--dionnaglaze.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="COv6p2yz" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e3705b2883so111860337b3.3 for ; Tue, 12 Nov 2024 15:23:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731453782; x=1732058582; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=te/cfPXpVlK1XxYIx1bZceEtuwPH7dTOHve13FO/vjw=; b=COv6p2yzzlQ/agU37mZSkJBosG1/nDrIxGPXdeoT1e+f7+nXggvri5hb2ZwVDaaGlm QoEuCCvoaicDqhytpFmmFuyey8qSaGcouhcOpZSOA4YC72CckecZ5BYi9UGy6MTk4iEO KU7399Vut3BQH99gDJUIsY/UY7J6DH9+ZZzdrZvSOhzRNa+/2iYtWh1GDs/dNHiIJ1R+ DTyIaX+C8JHuYWtkVtOK54QPERIAamyKT5JvIjAK6BkpzVTBwU3QR3qflGn8fKvmgdzA N/WJ1xcQWAFjw1Lt/9DmA4E4gjYJogErJ+7XIAjZZ9mKi3bzbtpUBz9vJloTeaNg9c34 xF7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731453782; x=1732058582; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=te/cfPXpVlK1XxYIx1bZceEtuwPH7dTOHve13FO/vjw=; b=vqIE3fz/UA2EL6PwOJk7HPsg8K24xPUMGTZIwUUwfBgfEEjkOmCTNEt6npxngod3Me H52uGjXNHVjUWDoc3kufW15WE2nK37k8G6PooxZbqcEjKGSa9emVyCICvUmVmTmjdOGk CzncCByAVm+CGEKDuTjRSUzRij0dqPoljHgIOcvpy+0GE1xVMTfO3JhqbrTUJTM6KEoj Kb+ubeIdDnVrwEeb/BGG9vscemtny9xvTNt08k8efQuJ4XQP88UGsn/2AZm9cAig2xwr nin/mLv7ylBl9SBZVphhbI/DjtIv+9rTc0XnJPxRRXOEIP9ctA6ya2Af5hJnWdr0XFmJ DHsg== X-Forwarded-Encrypted: i=1; AJvYcCX6oYv29p376QlAagGsFwHZrNwN92rUXrLCstlQsBBQOipNruXXAWk4Jx3l2Rey++tnfMM=@vger.kernel.org X-Gm-Message-State: AOJu0YyXMsGPv8tn3CL/g+PyFP9fShvKZ4MPOx3vOzM9MjUF/gsVrATe bXfX8brj2Sw58y1Yo3E3awX64YfZuvJRcZedpKbvld3SVsNUNs5R0FEtR2BgjgxuFcY8owKgBAH JxwI6eqHzSoscgMMYYHhtfw== X-Google-Smtp-Source: AGHT+IH0ZHDJ9RxOoA61xD1ipX9jxEyCm6JJVxkz4PucOgUVx/B+vOpDcYEieZFLPHqw48EsYNAyVJqKyr8eL5q2kw== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a05:690c:3688:b0:6ea:fa4:a365 with SMTP id 00721157ae682-6eaddfec9efmr1445227b3.8.1731453781898; Tue, 12 Nov 2024 15:23:01 -0800 (PST) Date: Tue, 12 Nov 2024 23:22:40 +0000 In-Reply-To: <20241112232253.3379178-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241112232253.3379178-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241112232253.3379178-2-dionnaglaze@google.com> Subject: [PATCH v6 1/8] KVM: SVM: Fix gctx page leak on invalid inputs From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Brijesh Singh , Michael Roth , Ashish Kalra Cc: linux-coco@lists.linux.dev, Dionna Glaze , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , stable@vger.kernel.org, kvm@vger.kernel.org Ensure that snp gctx page allocation is adequately deallocated on failure during snp_launch_start. Fixes: 136d8bc931c8 ("KVM: SEV: Add KVM_SEV_SNP_LAUNCH_START command") CC: Sean Christopherson CC: Paolo Bonzini CC: Thomas Gleixner CC: Ingo Molnar CC: Borislav Petkov CC: Dave Hansen CC: Ashish Kalra CC: Tom Lendacky CC: John Allen CC: Herbert Xu CC: "David S. Miller" CC: Michael Roth CC: Luis Chamberlain CC: Russ Weight CC: Danilo Krummrich CC: Greg Kroah-Hartman CC: "Rafael J. Wysocki" CC: Tianfei zhang CC: Alexey Kardashevskiy CC: stable@vger.kernel.org Signed-off-by: Dionna Glaze Acked-by: Sean Christopherson --- arch/x86/kvm/svm/sev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index c6c8524859001..357906375ec59 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2212,10 +2212,6 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) if (sev->snp_context) return -EINVAL; - sev->snp_context = snp_context_create(kvm, argp); - if (!sev->snp_context) - return -ENOTTY; - if (params.flags) return -EINVAL; @@ -2230,6 +2226,10 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) if (params.policy & SNP_POLICY_MASK_SINGLE_SOCKET) return -EINVAL; + sev->snp_context = snp_context_create(kvm, argp); + if (!sev->snp_context) + return -ENOTTY; + start.gctx_paddr = __psp_pa(sev->snp_context); start.policy = params.policy; memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw)); From patchwork Tue Nov 12 23:22:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 13873024 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 5DC7B21C167 for ; Tue, 12 Nov 2024 23:23:05 +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=1731453786; cv=none; b=tL0Tg0gfOC69oDxPFGdIlci2YnalbhFKjrpx4DII6aXLZoMvtBj2hdm1dRjTj49MzDeyTZTpSsJL7BYRi386t1w03EECAQ18UHIIGrotTaf91KQDFeE8vzTEsfCukyi7Ays23wkUlTT/b0PuW+IVS7rbcQyop3+Rh07J7mrjxvY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453786; c=relaxed/simple; bh=ayby78yQOyjOz6ls+yE5cxTfghhQvc3yyzZlL7gs+bU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BDtXmX326h82vlHRFk+en9FU9HqYq2Hwv/U7x99zaxTKRDVXOXwjrxDeXPsN+2Lnb849sIH2Tk2rJTyRaIH/AJq1gNRpGAlJvX3YkKNipkPjCuUoqyFc268HskQ0KC0yJPX3Xd8gI8bsVkiF7aj6sxSq03cnfXgYmIn6h5ydO2M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vhSGDvqb; 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--dionnaglaze.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vhSGDvqb" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2e2bb354e91so6498026a91.2 for ; Tue, 12 Nov 2024 15:23:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731453784; x=1732058584; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WmL++hD8fgHMSusvyHDFwqq57StgWysj/njcEHmccMk=; b=vhSGDvqb1dkZp23maJjOm0aVpI8AuAJy8zaOHGi1Ce2dLhI1Eboz1WTOohRMcP0xMS FpYWPLcJk8aZIKnmsjL3Y+Idv3Jwg4msGh++kQhDx1XRFcdbVsVDZA5oN+/zflE1UCiX wWJ74ulIO5b6b/NL/1E4UbVqHWAxkCRBllUGzNRfAjZQusRUKUCVAhsThbQJaG8Yo2C8 ebrQvVtea9tkmGTmNmFldRS+4bEjf3n8AHZZ5dVaM4W5zFfD1K52Q8CHa/uezxcLaUUR 0Q9S9jn3KpYAtPnc3H9qzU6X1eU2KMdSV1fsJXObLopapE/XjucGHhLY61A1yWwFFKiV g2TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731453784; x=1732058584; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WmL++hD8fgHMSusvyHDFwqq57StgWysj/njcEHmccMk=; b=ViJAk6bPjqjV4l1GEu6XYxWtccUP1NC12RTsJjsfeGbaROBS4Qi9gKssdM6DcZVUwl jAXJDPUDsiWJzT3RZd2ZXSHglE3EyHveRr0xCibvUeHM4zEuLv6VN6WQZ9l7dIM+daR7 KSnSW71Yej5rNTTONK2ifSJUFWH2X9SxRUS+DR7nhFGp0Gdp7zOjrO0a+opgZK+Yl/NJ d5776G2LhYTuz0Jngf9ht92dY1J8itQG1DqmMNBZqh6v58RsP7RPuKnNbIOMl3w1ytNV Y9/tWNCJ4wBhjHjoc8HDSW3j0bK2jkD+GB3mYPHvb8s7VFntCisLjzV6QSU5FOcLcFLn Pgyg== X-Forwarded-Encrypted: i=1; AJvYcCUPOBGhOjGfhaRTip2XHy6faKlNtJzhLnlRx3DHWB+Bwza0Y7vYkqWkwK6Ct7XiNvrU9XY=@vger.kernel.org X-Gm-Message-State: AOJu0YxkPnf2+kOh4ci6p5A4zQb+8IAAUKBcU8C8/4XAGh7tGdPBxplf +vSVhU6+MLEQFmZEXNBhZ8RMDEuYHoJwbUsVd2hxbkfRBMTO34hOsatyfSQxm+WxcG+YnoXp/RM n2jwdN06/SP23W0Ar4L3m6w== X-Google-Smtp-Source: AGHT+IEKIx/zzlZD9qfrCEEtWDHbsmZlYa32pBSY0nOxUmKrJ4QniZXwOwxKWzCDaGQhHEeyreWYnDeyUdJT3VYCwQ== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a17:90b:2ec3:b0:2e2:bb49:1052 with SMTP id 98e67ed59e1d1-2e9e4c7f1aamr70975a91.4.1731453784634; Tue, 12 Nov 2024 15:23:04 -0800 (PST) Date: Tue, 12 Nov 2024 23:22:41 +0000 In-Reply-To: <20241112232253.3379178-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241112232253.3379178-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241112232253.3379178-3-dionnaglaze@google.com> Subject: [PATCH v6 2/8] KVM: SVM: Fix snp_context_create error reporting From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Brijesh Singh , Michael Roth , Ashish Kalra Cc: linux-coco@lists.linux.dev, Dionna Glaze , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , stable@vger.kernel.org, kvm@vger.kernel.org Failure to allocate should not return -ENOTTY. Command failure has multiple possible error modes. Fixes: 136d8bc931c8 ("KVM: SEV: Add KVM_SEV_SNP_LAUNCH_START command") CC: Sean Christopherson CC: Paolo Bonzini CC: Thomas Gleixner CC: Ingo Molnar CC: Borislav Petkov CC: Dave Hansen CC: Ashish Kalra CC: Tom Lendacky CC: John Allen CC: Herbert Xu CC: "David S. Miller" CC: Michael Roth CC: Luis Chamberlain CC: Russ Weight CC: Danilo Krummrich CC: Greg Kroah-Hartman CC: "Rafael J. Wysocki" CC: Tianfei zhang CC: Alexey Kardashevskiy CC: stable@vger.kernel.org Signed-off-by: Dionna Glaze --- arch/x86/kvm/svm/sev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 357906375ec59..d0e0152aefb32 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2171,7 +2171,7 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) /* Allocate memory for context page */ context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); if (!context) - return NULL; + return ERR_PTR(-ENOMEM); data.address = __psp_pa(context); rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); @@ -2179,7 +2179,7 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) pr_warn("Failed to create SEV-SNP context, rc %d fw_error %d", rc, argp->error); snp_free_firmware_page(context); - return NULL; + return ERR_PTR(rc); } return context; @@ -2227,8 +2227,8 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return -EINVAL; sev->snp_context = snp_context_create(kvm, argp); - if (!sev->snp_context) - return -ENOTTY; + if (IS_ERR(sev->snp_context)) + return PTR_ERR(sev->snp_context); start.gctx_paddr = __psp_pa(sev->snp_context); start.policy = params.policy; From patchwork Tue Nov 12 23:22:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 13873026 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 0846F21E13B for ; Tue, 12 Nov 2024 23:23:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453802; cv=none; b=s9uHU4mIK8Bxms5WmMl/EBQ6pwc13PKllGcmUpqSIQTJn5vG0YeRhLQztndl7nOnsO3Od61j/pbM/y8zJYD1EjdUnqioZxJw6MDsMJaLKg4XHV4pl6eYPQGJ6xag7Lpn3r375wyz/j3dBX4h6tGVR0G2pZI0xlIY9fGr1D2T6Q4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453802; c=relaxed/simple; bh=nrQ932Y51OSd35PUGLLbau3Ik/xAjs0b/JpSV31X09E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qhULOHxoO4hbbFNvuiHdWTTOGVY3WOUC/faECXcB9JMnOG1KhhevQy4RMfOlsP70/2X8DPTW7fdrocuuedbNoss2nuvR8acG4wWOb+o8S4Im9OranX0vq4WnQFOW2snXMbO7MQ79+kjKPWO9OPVih9c836c3MurUqAset+pwiQ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YP0vFrgg; arc=none smtp.client-ip=209.85.219.202 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--dionnaglaze.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YP0vFrgg" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e02fff66a83so9836514276.0 for ; Tue, 12 Nov 2024 15:23:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731453800; x=1732058600; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Bt+/i8WOI79fmEsLhJLm2DSvIqfPWJS+0C7yUqoIIto=; b=YP0vFrgg2SAOHtUbdnkmodfrms4ov+Yd2zQ8Ngou9kTROtOCkOS6zZDf/G0bDbp/yy Izb2c3TGG1GqKu5ENaRlI5RRssUCwaRifKckUZPFI5ucxCrn8LqPt9iE4i/y2Jv4Yj3n /7T1SFucX1vjrXpGsOPt/+0Kr7WlaEyBUeA23VOsVtoYItLNWSudK00/IUpoDe+TuBR6 kC6sBtlFb8jS1xIaaG1pt6k0kxs8rDR5lipbBciE8Vp+AhVyPlKJ383MnHJ0InUToTCb tu2zQWS8HlARffU2I94s2tvRoWl3u0pKuQNNH7i+vBQ60q0Q5cnTsXlm++us8OpkVA8T tOEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731453800; x=1732058600; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Bt+/i8WOI79fmEsLhJLm2DSvIqfPWJS+0C7yUqoIIto=; b=tL1IrM3V+S1eRv6tVAUumKwSEvmVzwqk+QZ0pcD7DLuy2zs5uYGjSnWoIqhf25meSe zQ6PgIDhOG+hnE+s2c+lPKMnMDBhA/4ev6dLxSuL3HWGlpfMvImAP5/8UqFFU7LSadkX 4VVRdWxH1Y6Y7r6B0Q61fOtT5pZrzt9k6Z2sZUk1FlIaKDpBBpOpBYrWwsPvia05FfMj RHZlDE1KASb//yDfZ6fg3zYLbxqYJVh7on3XkY+8vhkwv58JB4uT/xXGvHaVjTBs+CKK 7/6YeWBWGNTHDZpKazvEKbGwj150MnHCQR7RZqxcylEZYC0LBXNL10R8dg2zc8/0Plt5 OCAA== X-Forwarded-Encrypted: i=1; AJvYcCUrSoY2PImqATwvSyA7FxT2PUX2zD4OzLXMEVVY493XOomND21bn8c21QC2Xh4tu1YrtpA=@vger.kernel.org X-Gm-Message-State: AOJu0YxAEAmbl/GscD0cyVcXWb+/QtAmaQBAB444PnCsTMwv74PXygbp fyq+kk1U+b2AACEqIi7pL/HE4J7oOjUFgI0LOqGlDb39VdC2ZzGRFF64PCIV57pvFg63a1iDfJj jeeUJDEplBJKuhWPBHrUYcw== X-Google-Smtp-Source: AGHT+IFdlks+cHVJJN0Pm1E8tmmOb+T06jth8P3+F/l88/NwWk3D7aE601eTKR+ZXuZ8P7m48FGAuSVd9gtK10Y6+A== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a05:6902:1342:b0:e2b:da82:f695 with SMTP id 3f1490d57ef6-e35ed2520d6mr631276.6.1731453799997; Tue, 12 Nov 2024 15:23:19 -0800 (PST) Date: Tue, 12 Nov 2024 23:22:46 +0000 In-Reply-To: <20241112232253.3379178-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241112232253.3379178-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241112232253.3379178-8-dionnaglaze@google.com> Subject: [PATCH v6 7/8] KVM: SVM: Use new ccp GCTX API From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Cc: linux-coco@lists.linux.dev, Dionna Glaze , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Michael Roth , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , kvm@vger.kernel.org Guest context pages should be near 1-to-1 with allocated ASIDs. With the GCTX API, the ccp driver is better able to associate guest context pages with the ASID that is/will be bound to it. This is important to the firmware hotloading implementation to not corrupt any running VM's guest context page before userspace commits a new firmware. CC: Sean Christopherson CC: Paolo Bonzini CC: Thomas Gleixner CC: Ingo Molnar CC: Borislav Petkov CC: Dave Hansen CC: Ashish Kalra CC: Tom Lendacky CC: John Allen CC: Herbert Xu CC: "David S. Miller" CC: Michael Roth CC: Luis Chamberlain CC: Russ Weight CC: Danilo Krummrich CC: Greg Kroah-Hartman CC: "Rafael J. Wysocki" CC: Tianfei zhang CC: Alexey Kardashevskiy Signed-off-by: Dionna Glaze --- arch/x86/kvm/svm/sev.c | 60 ++++++++---------------------------------- 1 file changed, 11 insertions(+), 49 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index d0e0152aefb32..5e6d1f1c14dfd 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2156,51 +2156,12 @@ int sev_dev_get_attr(u32 group, u64 attr, u64 *val) } } -/* - * The guest context contains all the information, keys and metadata - * associated with the guest that the firmware tracks to implement SEV - * and SNP features. The firmware stores the guest context in hypervisor - * provide page via the SNP_GCTX_CREATE command. - */ -static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) -{ - struct sev_data_snp_addr data = {}; - void *context; - int rc; - - /* Allocate memory for context page */ - context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); - if (!context) - return ERR_PTR(-ENOMEM); - - data.address = __psp_pa(context); - rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); - if (rc) { - pr_warn("Failed to create SEV-SNP context, rc %d fw_error %d", - rc, argp->error); - snp_free_firmware_page(context); - return ERR_PTR(rc); - } - - return context; -} - -static int snp_bind_asid(struct kvm *kvm, int *error) -{ - struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - struct sev_data_snp_activate data = {0}; - - data.gctx_paddr = __psp_pa(sev->snp_context); - data.asid = sev_get_asid(kvm); - return sev_issue_cmd(kvm, SEV_CMD_SNP_ACTIVATE, &data, error); -} - static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; struct sev_data_snp_launch_start start = {0}; struct kvm_sev_snp_launch_start params; - int rc; + int rc, asid; if (!sev_snp_guest(kvm)) return -ENOTTY; @@ -2226,7 +2187,8 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) if (params.policy & SNP_POLICY_MASK_SINGLE_SOCKET) return -EINVAL; - sev->snp_context = snp_context_create(kvm, argp); + asid = sev_get_asid(kvm); + sev->snp_context = sev_snp_create_context(argp->sev_fd, asid, &argp->error); if (IS_ERR(sev->snp_context)) return PTR_ERR(sev->snp_context); @@ -2241,7 +2203,7 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) } sev->fd = argp->sev_fd; - rc = snp_bind_asid(kvm, &argp->error); + rc = sev_snp_activate_asid(sev->fd, asid, &argp->error); if (rc) { pr_debug("%s: Failed to bind ASID to SEV-SNP context, rc %d\n", __func__, rc); @@ -2865,23 +2827,23 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd) static int snp_decommission_context(struct kvm *kvm) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - struct sev_data_snp_addr data = {}; - int ret; + int ret, error; /* If context is not created then do nothing */ if (!sev->snp_context) return 0; - /* Do the decommision, which will unbind the ASID from the SNP context */ - data.address = __sme_pa(sev->snp_context); + /* + * Do the decommision, which will unbind the ASID from the SNP context + * and free the context page. + */ down_write(&sev_deactivate_lock); - ret = sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, &data, NULL); + ret = sev_snp_guest_decommission(sev->fd, sev->asid, &error); up_write(&sev_deactivate_lock); - if (WARN_ONCE(ret, "Failed to release guest context, ret %d", ret)) + if (WARN_ONCE(ret, "Failed to release guest context, ret %d fw err %d", ret, error)) return ret; - snp_free_firmware_page(sev->snp_context); sev->snp_context = NULL; return 0; From patchwork Tue Nov 12 23:22:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dionna Amalie Glaze X-Patchwork-Id: 13873028 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 48BD321EBAC for ; Tue, 12 Nov 2024 23:23:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453806; cv=none; b=rwplYECBdXFTHs1TIpUi/sCHLzvxn0TilY6QwQyrT8dIO61J+4Sr4rOcI9N/QhoecKwbcFaFu2U3Hj3RfFvQ/o6EXbImpzjViX3i5X7uO3PjJOMVKQ7nl0sZ7+6hc6wcc0nihgGpvudzAhlCQHyw7+OS3AbGdG3dkVRmL+xO6hQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453806; c=relaxed/simple; bh=YSlU6X6JKLSeLbKMBuH/mDkE4l3HevChh3sRmgLUBXI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MuRe/rvSQ9bQczavjL5ahnoOckk5vL6D4JdNhpRSpWDbOf8w1Ood5y72qv0NnXX8o3XQjDPcbJPeBpiXIVdsjnoN+44OuDbNiJMSlwdAbKxpJ8YdyPSuZR2lsiTA2HwUSVrtTa8g7NndAGXoEahAL+LzTHEcVZhRu2RVQq4Tklg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dionnaglaze.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=csQXeJbp; arc=none smtp.client-ip=209.85.210.202 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--dionnaglaze.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="csQXeJbp" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7203cdc239dso7506353b3a.3 for ; Tue, 12 Nov 2024 15:23:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731453805; x=1732058605; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OuDWJG60RHXCqzpUFxAGPdqK8IqvXAJsitjiTXr/4nE=; b=csQXeJbpQqUsr1rFqQk+3XMj9E7wiBs3k93Fk+1wU6y4CertYGm0oMQkedpQbOc0um d+aeQz9Xz6QaN97XkR0eH8CmaMQDUJM1m2VmMrmwwraHu4XqydLhM3VhpNwirvNT698G 6e0f71+aVqIFQmK8qaU3olVH8k8hUFHjvgChHyJOE99HLQzZqWxqQaczXEiBH0CUmCHY cKY3IZOrGxSMVmqlAUr3i/2niWv5ahFNGwl7sWpvGzZ+3f6aoLz7cOSDfE6cFelaHCBO 19bAN10HAEuLgwX0WKzex7erSZhtPL1KPMxUtv2pIqzBWWAslkFTvyx0KZSz7dBHgVVC 96Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731453805; x=1732058605; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OuDWJG60RHXCqzpUFxAGPdqK8IqvXAJsitjiTXr/4nE=; b=kLfd8jzgspbQSnaVCMo/sHQy2OnVrgMpx//skseu29DYFP0ajmAnZj0DHkQ3YR3Avw QemAihf8Lp6WNq6Z4URnvrULdbUn4qS/WfG6dK1DG/qXf0M6dl9rKsHWo85c6R/azYdW Yk8s2v0gxHmBC7FYVhCN2irz+TZMi2uqyNH3f+HwRHQ6qvpmWXoUTkJMZncfimzPcmZ8 aFo0hYfqevpdupZGSm86rN4rY/RD8dhAlYOT4X5kxIJauMkjhYpCa49ytzSNXR3mXILU dBQV4QYacKkuMAf2KKEDcxixMP0vDi/KWvMQYuBhY3oJb+AwW2XODEQsqRJjPfIejafZ RVcA== X-Forwarded-Encrypted: i=1; AJvYcCUH66MDxtFh1anTnwdY07UIdWf9oGEp1sGZaVr+cRUG+0QZuCsIRopXvHn3CYh/MaJqJA0=@vger.kernel.org X-Gm-Message-State: AOJu0YyYkrNHF3eKFVQx5HDj/kw7bgBziwqrY5S7R0zTRnK/iR/spNGI nxpVmtF/1y489kAmux1fmIsz2WGuTKPN2giDTokNSAuZ75Maao8hvJOB93tjoIVlQj2e+DT758v 357hL9WkinjZ8EIuIfilt1Q== X-Google-Smtp-Source: AGHT+IE6zPnpQVIS7blTj/Vhu9TTjYHNmJoBXH4WGKbfY0DY+2UePrQxclpNCjF7vH3HqWjU/xgzhv/gANZuiZaWCA== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a62:ee0d:0:b0:720:2e1a:de with SMTP id d2e1a72fcca58-7244a4fcc54mr33020b3a.1.1731453804651; Tue, 12 Nov 2024 15:23:24 -0800 (PST) Date: Tue, 12 Nov 2024 23:22:47 +0000 In-Reply-To: <20241112232253.3379178-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241112232253.3379178-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Message-ID: <20241112232253.3379178-9-dionnaglaze@google.com> Subject: [PATCH v6 8/8] KVM: SVM: Delay legacy platform initialization on SNP From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Cc: linux-coco@lists.linux.dev, Dionna Glaze , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Michael Roth , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , kvm@vger.kernel.org When no SEV or SEV-ES guests are active, then the firmware can be updated while (SEV-SNP) VM guests are active. CC: Sean Christopherson CC: Paolo Bonzini CC: Thomas Gleixner CC: Ingo Molnar CC: Borislav Petkov CC: Dave Hansen CC: Ashish Kalra CC: Tom Lendacky CC: John Allen CC: Herbert Xu CC: "David S. Miller" CC: Michael Roth CC: Luis Chamberlain CC: Russ Weight CC: Danilo Krummrich CC: Greg Kroah-Hartman CC: "Rafael J. Wysocki" CC: Tianfei zhang CC: Alexey Kardashevskiy Co-developed-by: Ashish Kalra Signed-off-by: Ashish Kalra Reviewed-by: Ashish Kalra Signed-off-by: Dionna Glaze --- arch/x86/kvm/svm/sev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 5e6d1f1c14dfd..507ed87749f55 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -444,7 +444,11 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp, if (ret) goto e_no_asid; - init_args.probe = false; + /* + * Setting probe will skip SEV/SEV-ES platform initialization for an SEV-SNP guest in order + * for SNP firmware hotloading to be available when only SEV-SNP VMs are running. + */ + init_args.probe = vm_type != KVM_X86_SEV_VM && vm_type != KVM_X86_SEV_ES_VM; ret = sev_platform_init(&init_args); if (ret) goto e_free;