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: 13873027 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.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 CFF2021C168 for ; Tue, 12 Nov 2024 23:23:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453784; cv=none; b=lqkGI/T5cXlUHI8mU+6zp1XoP0pBo7vdIYHccm86GsACzPJrw6QxXke3PLDyXKji4Z1XSo1MmywAE8eEOJEYT12e56k7EOrwz165d2cQTpq/70/a8oukI30deRS7EnvcZN/LHC55Id1xcMswqXbPRWMH8xYyvZU2g+SI66ojpa4= 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=rmASES+l; arc=none smtp.client-ip=209.85.128.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="rmASES+l" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6eae6aba72fso76317997b3.2 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=lists.linux.dev; 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=rmASES+lxC+WgCnFnz8/QmnH0ntUO2FnMS6CPY4h3TInVDjG7gbesqSaf/BwIDWe0P c8aRFWi9qMss9AB9OxTqxy/yqUVTLNiTJj/TLd3R+XpgtHq7Y/nXD9RJefRfryFi5iBo 8d0IrY1uz7ZbkQwjemq29oRZhyvgL11myBkjnBd3bI1SYp8pM8NA8CSDe2tE0K6BrlIv fNxiSrHKdMykqIspHFbBOBycCHyfpm+D2432dwqBx9gIt6kqgEiq4OWgMMEhoAZnBrXD yKqVt8EMolCW8SGTCthyIcQ7pQsXjKz3CJFxqFYafN5IN8pCoQx6zbMKzFjzJ2VM0GOm 2uhg== 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=ftqx0flqRQNbLaNGMVbqy+q1M0R4CDD+O17Lrf9eIRBFWA/ilm9xQc1WiyVB0O+eQi qforTOpdxpNLZyX9LbZEOEaKtYTtEtZPdywfDXY8BiOPOS/0CBvcL1qHng504uF/XQIh XCEHpdC9ebdkFapDhU71oYpQmEyc6g1YdRVBZyHxwjc72YARRClh+jYuM9C4b6ICrXvy sAWpnZyLbfSxaGJYQ+JXyvX4BX+3r+CHbPNrZzE89MNgJzrfQ6w/Hp5E7JEofN6N2qVQ yQiCWMtJSKv5ZnOA1sfLj6du1tiVEN8tCWwKHk87a4mv55uF8ECOhSu+OFwXykH0NTVw nXqQ== X-Gm-Message-State: AOJu0YxKtX+GoHfau+/gdqFV+wtj1IIchTS87Jjo5xCmcQR8xAYCVd5c LwAGDWWQS671qLRGM6PPQwamatsyYN1ahm/LLFeTmUdvvKn6Yafz+fFLQ6L7Jgp5AGq54lQ7JMe TRfHuCECd7PxD9VKTSaRp7w== 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: linux-coco@lists.linux.dev 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: 13873029 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 3D2DA21CF90 for ; Tue, 12 Nov 2024 23:23:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453786; cv=none; b=tB6tSl6hFqgTaZKMdFrjqOBChe7cTDDsB07Y8fi8Jn6zwWo8W58NwldGgBRUCZyetZ8dEAKTIZqNQbFVtLDcOryQNqnPsimOdI2M3snHFwTOlRUgVrErMGP7PScOZndoueOSsCcT7xxaILtdJFSxmpOWWqdZxGQ7scRi8H6Dc4c= 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=ru2zt3Dk; arc=none smtp.client-ip=209.85.216.74 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="ru2zt3Dk" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2e2ca4fb175so6496758a91.3 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=lists.linux.dev; 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=ru2zt3DkZCWDPEMGeBMIvKdRr/fy4NZE2hfk1ViwB+VUu4L6yH7uaHXqaFZjyFMGzJ NXD7vlA4776Rq+7NiVpaFUQHxwzvWjz+DN4nQNMiR/mBHiQficgbi8zbry2HeGsIvNt/ lPjSqePrwR58YOCp4V8+vKAAdZgxRH/ghz88Fi/BKK8Runsi6/5cEMV3S/Wu+2LqOBbI K2pHQq5o39aS1MLKjjUYv5UG5/smDxh/iimqRlk6VIISBG9BeoF3r51SqMQQ3vbE93x9 eHFEdqk+HdYXDB9E0FmhsoCxgFN0C6rgNz22nLFHKFexZLoOCPzILEcftd25p3jqbBv8 vbFg== 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=Ccpo+cQfLKgT03Dm9qWoyhYIeIM7stJl12Mraf/unmiUFKzslqhxh3bcus/XFG1ap9 nMVcJNp6P8yhe85dzEu18Tt8itzXaRbDHmo4eXQMjWpEpRFRLdV1iXEXioG6qNf1UWWZ 3TXZc+oFd1IdgRbljxXvkkoxL4FQP7IXoozCgZKbl/6sUwndHOlJJT5h6nnqo5Y7E05d Okze9h4z9gcfpkdur0k1VI8xHmkVyi/iB+/JSDqZvCCMm1651u18YUXSB9AwP7ObOWG6 m0gG1OGIVNgczTYYa0+H3D3M3PborFiNJCTUesFXLP5BMo+9yRD/qmhZ/T5AJzROj30x DgjA== X-Gm-Message-State: AOJu0YxVyRZWrE9SH20DhPXr08SgK7aV/xrYBwveGDBCgGkfqMSmLvnQ TeXE/TRC3N5lWKfaGS+ofhnnrGP2peQYqHes1knKegSqldvLZYSIoQ8mGpFu2Ol59AVralTqUNC TRZoI9jDVG9aQg/QPyPRNuQ== 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: linux-coco@lists.linux.dev 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:42 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: 13873030 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.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 2EEB521D211 for ; Tue, 12 Nov 2024 23:23:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453789; cv=none; b=EzmE/t/weafExFleAjawdDi4aB+g+Gq1cbXrR4NIitXCTFZ9qg99ykIqSuKjIKYMnwQjzvaxExwtX3IW3cgipGVIWx8FtXDUOiA1RXCgxiF+tM/yj5yb/v8b9/reEaCj0ivlrwWaZLzziyNfLvOYfSbO58lvAsG9oMTHPKCTQ3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453789; c=relaxed/simple; bh=fkSYYUFFU2dhT6w7OYZJxAhhsJkywG0c349xGH09+og=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DmrguSG0ubcc+fsFcuCk611bcHgVi5iozuMCQ66yePCnP/PY4XR4KTs6q+tQEYVspGekC7tiaIBabkb7TrgZy7F6/9ljty+2C2rBaOi5XVbw/01sqVKhrjDluPO9ceMuaXcbxeoF5vxtAgveAEN2Wl9nGejn6O+ZVCeDZ4exAtU= 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=yJmtVGD2; arc=none smtp.client-ip=209.85.128.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="yJmtVGD2" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6ea33aad097so2476827b3.0 for ; Tue, 12 Nov 2024 15:23:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731453787; x=1732058587; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=waco7iVOB4xSaLUY84IKzzTbmzK38z3CV8Wi2DuMfOk=; b=yJmtVGD2o5dJNzjdqwH7MZ3DifzXd9BO9QMNBk6VKI6DauTs84rAarQCKE1ioDJd4T 0jDzbdB/gPIGj5EJGonGCMw9gJ0zGqOqcd2PS7ahTItAO0fMOnkZY92/G0FqA+KOLEEj p47sQg4p1Wpx9eQvMEcCjueLx/aQcr/C0zHEnI+5MZZ26EAtkPuS/pW8ZsViRoyqC0WS /FaJljqb1FlwD+aF3b9kGEoJMCT5zph/BsGhyxkEOzAaawB7HH3/lAlMgs6TJX4Uu+T3 2u763f5Rr53LDuWHtWTvKnZfllIqCFQ3T7bglLSHNxnBpWwUR+LM73It9hrHPFN5Bagb wJJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731453787; x=1732058587; 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=waco7iVOB4xSaLUY84IKzzTbmzK38z3CV8Wi2DuMfOk=; b=Dl9/Ak8zGefEy2PwcM7tcqBRai40mFKel+J24Um31JdoouT953JKNXk6AkUvduAIbE F5VdaKR1p/XHYv5JPxWknexgiXtawa9/M4KnB1cpxg1d4Zoc2MrO+9guNnw9USp6MbQV M5qeamVUVDrceiqZ8t2fnA+CQmfy3SB+NXLjEJo3MuQrQWLujEVGzYF1vUPQ7mTSueVV h7Fir27CE5eyVcsG5B2GVzZRllpq9TgyGPy7yRg4jCPzA2hopSD4bP+Sd4s7uc4D2u0r OI5DyqgtNcZ4oy9Bhh9yhwjAMoYRLjI6Y5EQWNSoG83ZQ1+r/lPlp50eEDVtmVxq6e78 DsTA== X-Gm-Message-State: AOJu0Yy8PzKpiUa4xUlJ6Zl16Kg2wsAb9FuByx8lVSGYAIoh1tKtlz8A vFy4HrNQAl0S7go1Bz0jo1HL1PqapdLxZ6rYkVe7879UyZjPol54R1Pow4G/yLWFdw2QxhbwYA4 PXfD5VEkTs0v6QyT60cp4zA== X-Google-Smtp-Source: AGHT+IE0qPNehuCJmgZ3Y9jGmKLJIF83WkMtt6X+ytxPHivZrMnOcg1sXwjkCfj06PlKJC8uXCRQCyOSugbOqkdYyw== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a25:d04c:0:b0:e2e:317a:d599 with SMTP id 3f1490d57ef6-e337fdb51ccmr99338276.2.1731453787171; Tue, 12 Nov 2024 15:23:07 -0800 (PST) Date: Tue, 12 Nov 2024 23:22:42 +0000 In-Reply-To: <20241112232253.3379178-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev 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-4-dionnaglaze@google.com> Subject: [PATCH v6 3/8] firmware_loader: Move module refcounts to allow unloading From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang Cc: linux-coco@lists.linux.dev, Dionna Glaze , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Michael Roth , Alexey Kardashevskiy , Russ Weight If a kernel module registers a firmware upload API ops set, then it's unable to be moved due to effectively a cyclic reference that the module depends on the upload which depends on the module. Instead, only require the try_module_get when an upload is requested to disallow unloading a module only while the upload is in progress. Fixes: 97730bbb242c ("firmware_loader: Add firmware-upload support") 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 Tested-by: Ashish Kalra Reviewed-by: Russ Weight Signed-off-by: Dionna Glaze --- drivers/base/firmware_loader/sysfs_upload.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/drivers/base/firmware_loader/sysfs_upload.c b/drivers/base/firmware_loader/sysfs_upload.c index 829270067d163..7d9c6aef7720a 100644 --- a/drivers/base/firmware_loader/sysfs_upload.c +++ b/drivers/base/firmware_loader/sysfs_upload.c @@ -204,6 +204,7 @@ static void fw_upload_main(struct work_struct *work) fwlp->ops->cleanup(fwl); putdev_exit: + module_put(fwlp->module); put_device(fw_dev->parent); /* @@ -239,6 +240,9 @@ int fw_upload_start(struct fw_sysfs *fw_sysfs) } fwlp = fw_sysfs->fw_upload_priv; + if (!try_module_get(fwlp->module)) /* released in fw_upload_main */ + return -EFAULT; + mutex_lock(&fwlp->lock); /* Do not interfere with an on-going fw_upload */ @@ -310,13 +314,10 @@ firmware_upload_register(struct module *module, struct device *parent, return ERR_PTR(-EINVAL); } - if (!try_module_get(module)) - return ERR_PTR(-EFAULT); - fw_upload = kzalloc(sizeof(*fw_upload), GFP_KERNEL); if (!fw_upload) { ret = -ENOMEM; - goto exit_module_put; + goto exit_err; } fw_upload_priv = kzalloc(sizeof(*fw_upload_priv), GFP_KERNEL); @@ -358,7 +359,7 @@ firmware_upload_register(struct module *module, struct device *parent, if (ret) { dev_err(fw_dev, "%s: device_register failed\n", __func__); put_device(fw_dev); - goto exit_module_put; + goto exit_err; } return fw_upload; @@ -372,8 +373,7 @@ firmware_upload_register(struct module *module, struct device *parent, free_fw_upload: kfree(fw_upload); -exit_module_put: - module_put(module); +exit_err: return ERR_PTR(ret); } @@ -387,7 +387,6 @@ void firmware_upload_unregister(struct fw_upload *fw_upload) { struct fw_sysfs *fw_sysfs = fw_upload->priv; struct fw_upload_priv *fw_upload_priv = fw_sysfs->fw_upload_priv; - struct module *module = fw_upload_priv->module; mutex_lock(&fw_upload_priv->lock); if (fw_upload_priv->progress == FW_UPLOAD_PROG_IDLE) { @@ -403,6 +402,5 @@ void firmware_upload_unregister(struct fw_upload *fw_upload) unregister: device_unregister(&fw_sysfs->dev); - module_put(module); } EXPORT_SYMBOL_GPL(firmware_upload_unregister); From patchwork Tue Nov 12 23:22:43 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: 13873031 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 11FE421D22B for ; Tue, 12 Nov 2024 23:23:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453793; cv=none; b=cCFLPQORibo8YjVxuU60wyzYyPzIoQ/Ksgpb1lqdj/12VNzYoUqm+Ew8z8jIwa6Gx/W8+cY820NsT/rFFZWC/JINwDINSEp+qxpBM/CB7/zkoWX3Zmsku+ZTIrpczHBZuKpCHaH5/1jHxb8L4qGMRP0/BNhcIsuHkBB6Ls/uSTI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453793; c=relaxed/simple; bh=1hS42kMCXh2fd2/NsPf3DiCppJSEIF2GD7L2ejW+7Kw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FRq99uHG+96jSo3Wv8wsj9NKL1YHGn579fM8od4njylJbV8OZXhKPyWG2n9Y47Gnj1paAGOt9vsIBkaOyMshT8HPokEl0ugf5tjX8sjURgnPYyCTH7+pxhaJ0CVvKocqt5qU9i52EWmkq9XOhDLIrjKnKk3KH/gNnSHI3Pa3gT4= 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=mEBOls0f; arc=none smtp.client-ip=209.85.219.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="mEBOls0f" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e292dbfd834so10213625276.3 for ; Tue, 12 Nov 2024 15:23:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731453790; x=1732058590; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JK/GSTSvm9liRNiU4g1TG7hfg4UB9r2AkllQ23c8pWk=; b=mEBOls0fj+eLVjHVzi0GT9odtyT0R6hW36AJceakGsPunR8kWOrVWUyAdxJNcIJsWv 9FHYW+EVOgDxpkwSZrVNLZFSD+spwxooEdolXnmTyZFEvkPVQ44gUeWeya4/76q3Rk9S U+zN9Ifm+iGuuZUp8ibY+E7pDYuhqSaQ138BtAgXy4AOQJkqvrvmTIa5MxnPXTqn2jk5 rBpsQxKV0vSC8XVvboZeVOwAkUnIv2ONu89xp/ZzIOIYBPopuqIfon8YzJP8dL/C9E4q tPR67Ah5mXRiRlb984Sn6R0xQvsuGbnC/Y18xuSktzk8lCMJEuNDOM3qnAgNPg2rCKgm IJAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731453790; x=1732058590; 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=JK/GSTSvm9liRNiU4g1TG7hfg4UB9r2AkllQ23c8pWk=; b=KZdqOec9QRiQB7tVvT+BW0tEwRIgiv75iwjV7k5+vEqt+Wdx+mkfPPrn9WUo7roWL7 v9ESFxHz3oxcP2UEEs8SQZQ8sYRzBT7jHGoOXwSzyLcRu85WGkZp69WbzddR+A8C3Roi oVMDPWwbIL3A3yb93bhDKQqDXRwLxoMMmrodgBdjvxm6BKbGTG/mjBIiGwLJakWgEoHd 1yqwBXD8OFBmJyFr5HanAb7TZ0FhqRzxWlN9uSaSfWAFg7hWLnTdbFTPOIlSmoFQtdj2 8bhq+jqOQnrNula7+xyz0u5xCSMxXjXVc7e9Mlod9jF/nU6VdpWJKBegb/TIrTi6A+Ou C+SQ== X-Gm-Message-State: AOJu0YyaYDIaP34fkRlwuSpK7oJbca4QpDL3+sBQlXfyEpT85YTccE3H CGd2g0TMKfPBMkBhM+reZ7EyZRnoGFiDfWNKyXfM6N1NuGJs7+1C+leBsIAdYfDJA7ShAdQrqgV UeIb/RHUlX2KR/nZojqNrIg== X-Google-Smtp-Source: AGHT+IHGhhefKoRV5e5OubSGAwAM0UWk5yFk6qkA96dyKOOGAJu9z6Gb09Ycpw7kFzXXPso5tbzga53kt7B3No4ObA== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a25:a22a:0:b0:e30:b93a:b3e4 with SMTP id 3f1490d57ef6-e337f85f1c8mr43720276.4.1731453790070; Tue, 12 Nov 2024 15:23:10 -0800 (PST) Date: Tue, 12 Nov 2024 23:22:43 +0000 In-Reply-To: <20241112232253.3379178-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev 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-5-dionnaglaze@google.com> Subject: [PATCH v6 4/8] crypto: ccp: Fix uapi definitions of PSP errors From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Ashish Kalra , Tom Lendacky , Michael Roth , "Borislav Petkov (AMD)" , Brijesh Singh Cc: linux-coco@lists.linux.dev, Alexey Kardashevskiy , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Dave Hansen , John Allen , Herbert Xu , "David S. Miller" , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , stable@vger.kernel.org, Dionna Glaze , linux-crypto@vger.kernel.org From: Alexey Kardashevskiy Additions to the error enum after the explicit 0x27 setting for SEV_RET_INVALID_KEY leads to incorrect value assignments. Use explicit values to match the manufacturer specifications more clearly. Fixes: 3a45dc2b419e ("crypto: ccp: Define the SEV-SNP commands") 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: Alexey Kardashevskiy Signed-off-by: Dionna Glaze Reviewed-by: Tom Lendacky --- include/uapi/linux/psp-sev.h | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 832c15d9155bd..eeb20dfb1fdaa 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -73,13 +73,20 @@ typedef enum { SEV_RET_INVALID_PARAM, SEV_RET_RESOURCE_LIMIT, SEV_RET_SECURE_DATA_INVALID, - SEV_RET_INVALID_KEY = 0x27, - SEV_RET_INVALID_PAGE_SIZE, - SEV_RET_INVALID_PAGE_STATE, - SEV_RET_INVALID_MDATA_ENTRY, - SEV_RET_INVALID_PAGE_OWNER, - SEV_RET_INVALID_PAGE_AEAD_OFLOW, - SEV_RET_RMP_INIT_REQUIRED, + SEV_RET_INVALID_PAGE_SIZE = 0x0019, + SEV_RET_INVALID_PAGE_STATE = 0x001A, + SEV_RET_INVALID_MDATA_ENTRY = 0x001B, + SEV_RET_INVALID_PAGE_OWNER = 0x001C, + SEV_RET_AEAD_OFLOW = 0x001D, + SEV_RET_EXIT_RING_BUFFER = 0x001F, + SEV_RET_RMP_INIT_REQUIRED = 0x0020, + SEV_RET_BAD_SVN = 0x0021, + SEV_RET_BAD_VERSION = 0x0022, + SEV_RET_SHUTDOWN_REQUIRED = 0x0023, + SEV_RET_UPDATE_FAILED = 0x0024, + SEV_RET_RESTORE_REQUIRED = 0x0025, + SEV_RET_RMP_INITIALIZATION_FAILED = 0x0026, + SEV_RET_INVALID_KEY = 0x0027, SEV_RET_MAX, } sev_ret_code; From patchwork Tue Nov 12 23:22:44 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: 13873032 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 06CC721E110 for ; Tue, 12 Nov 2024 23:23:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453795; cv=none; b=hF+hM0aGaB60PI2ZKljXNemFfMxKpohPd58zdYvqKJH8SqcgWMFds4lB0VhuMQt2oksbypDQVdNFOck8K7ROXufmYKp9VuwvRN7joL1bhx8kbf57OZofdbTz1kgGIK3YdQJmt5UyRWy5QpuDD/NrkswDPyqsLEM3+NNi7niQJNw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453795; c=relaxed/simple; bh=ZYnveSnHe3FkDFRVZnrqkrTHNRmt3JMbLrbNeGvJt08=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hnEO4NwiCXL0BI1PkWBeMPmDgTpKZs0k1Lkf/fHi4ILPNqAvUHiHdMnkpoZRbBtJM3vMsqHQSdqWDKbJd9mBjsZLa4AkASEvAQETfZ/W/JZ1/9FyfIem35jhmsvlhbXjw+JNlHlRu0HIow8uleCfmIL3o2CMfJTaRY/eEHSdPbI= 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=qPY6ZTSe; arc=none smtp.client-ip=209.85.214.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="qPY6ZTSe" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-210d5ea3cf5so1241415ad.0 for ; Tue, 12 Nov 2024 15:23:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731453793; x=1732058593; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=USTo4YL/VPH3OMlRJlDOB1h2tpzzB/FL1xLG0AeYaEg=; b=qPY6ZTSevMWK4cLoxAvAf8iNCDC0yG2fRwV8TXv+rNckaYt2RiZJBENfQw94CIT7Q8 M2KajUESU6PsSv8tsmBPM4nZ/6nTMdt9xn9hc7f8UFvPTampVN6BiQqPpounvLUoqJNz X5tDb4/S/9ezx8eujmqCqOTCmjglc85rHVdp8hgFrSTEfBk6GjiuFKUWehJVzVGN3pW6 78GCBIdok6kehLWpc9DQih3XuPTsa0o/zMMETVBe89LJ9038X0DP0DAhnIFF0bhh4YlH I1m4t26u0I7TYnV2Q09Fd9zHDrJbDGQzAzoLlRRvZ7vP/ci8c0N0J9EL7dmHwe6fgAix 5xLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731453793; x=1732058593; 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=USTo4YL/VPH3OMlRJlDOB1h2tpzzB/FL1xLG0AeYaEg=; b=p92YiR+kkI803jlkjn/cEB3DiTUMUdCvAA+2P3nwiVBqi4mNKSLpn8Cyjm/rJ0E3hR LkjhUStXGCgH9AbXi6n5nm4vaDXs6cxEqzvxwip1HDEuLgTLMkCEswVhZPUze9mWFmi2 6UrY2GiGsuPN0OnjT8BDgDSQNxzaRv0lcYKkSjUpKSrU7S99+NRXXv/itwd9BBl3CuLA WtbHegXo88qYzdQ/diFKz6c4f8zPnmhk12EJakGakI/mm0eiNsGudATM0gJVUdUN8srY 2hNR8foT7KVIXbx4JbSYcUqF0oEoJXAry+k0GJpdH2U5DvLTNe8X45bQhPqrAcIDQb5u /5mw== X-Gm-Message-State: AOJu0Yw0kSKnRcfV5lAeOL2YHWvt5WYDBiTIQtH8FpLSSBZ4vebZMB68 GTQbp10ue0wL+pbIqK8kU0Az37lRXG/IEKPZ3nJiGKffNYvSAHlscN3gGo101h9bZR1l8kkM+wu cdDVb6Iotu3AABN0xb2W8bA== X-Google-Smtp-Source: AGHT+IEby86KOt7jOT5/DEApza0kwwIhq4p8JxRYX4qy2k+CNTUtIUvHeGv7sbHFOyCcpJ8TgiN/weldRpPBkDTfbw== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a17:902:e809:b0:20c:6764:6681 with SMTP id d9443c01a7336-211821273camr1743295ad.2.1731453793410; Tue, 12 Nov 2024 15:23:13 -0800 (PST) Date: Tue, 12 Nov 2024 23:22:44 +0000 In-Reply-To: <20241112232253.3379178-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev 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-6-dionnaglaze@google.com> Subject: [PATCH v6 5/8] crypto: ccp: Add GCTX API to track ASID assignment From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" Cc: linux-coco@lists.linux.dev, Dionna Glaze , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Michael Roth , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , linux-crypto@vger.kernel.org In preparation for SEV firmware hotloading support, introduce a new way to create, activate, and decommission GCTX pages such that ccp is has all GCTX pages available to update as needed. Compliance with SEV-SNP API section 3.3 Firmware Updates and 4.1.1 Live Update: before a firmware is committed, all active GCTX pages should be updated with SNP_GUEST_STATUS to ensure their data structure remains consistent for the new firmware version. There can only be CPUID 0x8000001f_EDX-1 many SEV-SNP asids in use at one time, so this map associates asid to gctx in order to track which addresses are active gctx pages that need updating. When an asid and gctx page are decommissioned, the page is removed from tracking for update-purposes. 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 --- drivers/crypto/ccp/sev-dev.c | 159 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 4 + include/linux/psp-sev.h | 55 ++++++++++++ 3 files changed, 217 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index af018afd9cd7f..d8c35b8478ff5 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -8,6 +8,7 @@ */ #include +#include #include #include #include @@ -109,6 +110,10 @@ static void *sev_init_ex_buffer; */ static struct sev_data_range_list *snp_range_list; +/* SEV ASID data tracks resources associated with an ASID to safely manage operations. */ +struct sev_asid_data *sev_asid_data; +u32 nr_asids, sev_min_asid, sev_es_max_asid; + static inline bool sev_version_greater_or_equal(u8 maj, u8 min) { struct sev_device *sev = psp_master->sev_data; @@ -1093,6 +1098,109 @@ static int snp_filter_reserved_mem_regions(struct resource *rs, void *arg) return 0; } +static bool sev_check_external_user(int fd); +void *sev_snp_create_context(int fd, int asid, int *psp_ret) +{ + struct sev_data_snp_addr data = {}; + void *context; + int rc, error; + + if (!sev_check_external_user(fd)) + return ERR_PTR(-EBADF); + + if (!sev_asid_data) + return ERR_PTR(-ENODEV); + + if (asid < 0 || asid >= nr_asids) + return ERR_PTR(-EINVAL); + + /* Can't create a context for a used ASID. */ + if (WARN_ON_ONCE(sev_asid_data[asid].snp_context)) + return ERR_PTR(-EBUSY); + + /* 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_do_cmd(SEV_CMD_SNP_GCTX_CREATE, &data, &error); + if (rc) { + pr_warn("Failed to create SEV-SNP context, rc=%d fw_error=0x%x", + rc, error); + if (psp_ret) + *psp_ret = error; + snp_free_firmware_page(context); + return ERR_PTR(-EIO); + } + + sev_asid_data[asid].snp_context = context; + + return context; +} +EXPORT_SYMBOL_GPL(sev_snp_create_context); + +int sev_snp_activate_asid(int fd, int asid, int *psp_ret) +{ + struct sev_data_snp_activate data = {0}; + void *context; + + if (!sev_check_external_user(fd)) + return -EBADF; + + if (!sev_asid_data) + return -ENODEV; + + if (asid < 0 || asid >= nr_asids) + return -EINVAL; + + context = sev_asid_data[asid].snp_context; + if (WARN_ON_ONCE(!context)) + return -EINVAL; + + data.gctx_paddr = __psp_pa(context); + data.asid = asid; + return sev_do_cmd(SEV_CMD_SNP_ACTIVATE, &data, psp_ret); +} +EXPORT_SYMBOL_GPL(sev_snp_activate_asid); + +int sev_snp_guest_decommission(int fd, int asid, int *psp_ret) +{ + struct sev_data_snp_addr addr = {}; + struct sev_asid_data *data; + int ret, error; + + if (!sev_check_external_user(fd)) + return -EBADF; + + if (!sev_asid_data) + return -ENODEV; + + if (asid < 0 || asid >= nr_asids) + return -EINVAL; + + data = &sev_asid_data[asid]; + /* If context is not created then do nothing */ + if (!data->snp_context) + return 0; + + /* Do the decommission, which will unbind the ASID from the SNP context */ + addr.address = __sme_pa(data->snp_context); + ret = sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, &addr, &error); + + if (WARN_ONCE(ret, "Failed to release guest context, rc=%d, fw_error=0x%x", ret, error)) { + if (psp_ret) + *psp_ret = error; + return ret; + } + + snp_free_firmware_page(data->snp_context); + data->snp_context = NULL; + + return 0; +} +EXPORT_SYMBOL_GPL(sev_snp_guest_decommission); + static int __sev_snp_init_locked(int *error) { struct psp_device *psp = psp_master; @@ -1306,6 +1414,27 @@ static int __sev_platform_init_locked(int *error) return 0; } +static int sev_asid_data_init(void) +{ + u32 eax, ebx, ecx; + + if (sev_asid_data) + return 0; + + cpuid(0x8000001f, &eax, &ebx, &ecx, &sev_min_asid); + if (!ecx) + return -ENODEV; + + nr_asids = ecx + 1; + sev_es_max_asid = sev_min_asid - 1; + + sev_asid_data = kcalloc(nr_asids, sizeof(*sev_asid_data), GFP_KERNEL); + if (!sev_asid_data) + return -ENOMEM; + + return 0; +} + static int _sev_platform_init_locked(struct sev_platform_init_args *args) { struct sev_device *sev; @@ -1319,6 +1448,10 @@ static int _sev_platform_init_locked(struct sev_platform_init_args *args) if (sev->state == SEV_STATE_INIT) return 0; + rc = sev_asid_data_init(); + if (rc) + return rc; + /* * Legacy guests cannot be running while SNP_INIT(_EX) is executing, * so perform SEV-SNP initialization at probe time. @@ -2329,6 +2462,9 @@ static void __sev_firmware_shutdown(struct sev_device *sev, bool panic) snp_range_list = NULL; } + kfree(sev_asid_data); + sev_asid_data = NULL; + __sev_snp_shutdown_locked(&error, panic); } @@ -2377,10 +2513,31 @@ static struct notifier_block snp_panic_notifier = { .notifier_call = snp_shutdown_on_panic, }; +static bool file_is_sev(struct file *filep) +{ + return filep && filep->f_op == &sev_fops; +} + +static bool sev_check_external_user(int fd) +{ + struct fd f; + bool ret = true; + + f = fdget(fd); + if (!fd_file(f)) + return false; + + if (!file_is_sev(fd_file(f))) + ret = false; + + fdput(f); + return ret; +} + int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, void *data, int *error) { - if (!filep || filep->f_op != &sev_fops) + if (!file_is_sev(filep)) return -EBADF; return sev_do_cmd(cmd, data, error); diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 3e4e5574e88a3..ccf3ba78d8332 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -65,4 +65,8 @@ void sev_dev_destroy(struct psp_device *psp); void sev_pci_init(void); void sev_pci_exit(void); +struct sev_asid_data { + void *snp_context; +}; + #endif /* __SEV_DEV_H */ diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 903ddfea85850..0b3b7707ccb21 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -942,6 +942,61 @@ int sev_guest_decommission(struct sev_data_decommission *data, int *error); */ int sev_do_cmd(int cmd, void *data, int *psp_ret); +/** + * sev_snp_create_context - allocates an SNP context firmware page + * + * Associates the created context with the ASID that an activation + * call after SNP_LAUNCH_START will commit. The association is needed + * to track active guest context pages to refresh during firmware hotload. + * + * @fd: A file descriptor for the SEV device + * @asid: The ASID allocated to the caller that will be used in a subsequent SNP_ACTIVATE. + * @psp_ret: sev command return code. + * + * Returns: + * A pointer to the SNP context page, or an ERR_PTR of + * -%ENODEV if the PSP device is not available + * -%ENOTSUPP if PSP device does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if PSP device returned a non-zero return code + */ +void *sev_snp_create_context(int fd, int asid, int *psp_ret); + +/** + * sev_snp_activate_asid - issues SNP_ACTIVATE for the ASID and associated guest context page. + * + * @fd: A file descriptor for the SEV device + * @asid: The ASID to activate. + * @psp_ret: sev command return code. + * + * Returns: + * 0 if the SEV device successfully processed the command + * -%ENODEV if the PSP device is not available + * -%ENOTSUPP if PSP device does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if PSP device returned a non-zero return code + */ +int sev_snp_activate_asid(int fd, int asid, int *psp_ret); + +/** + * sev_snp_guest_decommission - issues SNP_DECOMMISSION for an ASID's guest context page, and frees + * it. + * + * The caller must ensure mutual exclusion with any process that may deactivate ASIDs. + * + * @fd: A file descriptor for the SEV device + * @asid: The ASID to activate. + * @psp_ret: sev command return code. + * + * Returns: + * 0 if the SEV device successfully processed the command + * -%ENODEV if the PSP device is not available + * -%ENOTSUPP if PSP device does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if PSP device returned a non-zero return code + */ +int sev_snp_guest_decommission(int fd, int asid, int *psp_ret); + void *psp_copy_user_blob(u64 uaddr, u32 len); void *snp_alloc_firmware_page(gfp_t mask); void snp_free_firmware_page(void *addr); From patchwork Tue Nov 12 23:22:45 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: 13873035 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.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 4BE6121E12E for ; Tue, 12 Nov 2024 23:23:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453801; cv=none; b=hu8R52tLkgUokA9tc6yyK9IUss1C6DOye6zrkdSPAReIiY0QdtvHilp7YP63RPBhK16XAZX7yZVl0tUedGh48Po5T0mgJESBWRKCcKDEYEWsRg+pQ78n7djrvy/ShMtpp1UnffmOzlLttvxgPGNEwbQFvv+hGu2QNEbGPG0BISM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731453801; c=relaxed/simple; bh=ub/jeAuKxHgVeYJR7qfIWhbva40qGRoVqnXE5Bdp86E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OduEBNx9/de6KTu+q1VAchyUdO0Fvvz6HIyL0b1puJIIKBaAEQF+sABUQ8t3lIiqeF3TE7nX+o6niNohd2v4feBIrQATG9rQCBFdKIPTE9OpzoXJSS8WG4Et9x35fFEIPy4RqlvQ3Mb8Uzwogx2qMrW5168Sj6dUT3Aq7FP1BZE= 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=HFF/ECZ1; arc=none smtp.client-ip=209.85.128.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="HFF/ECZ1" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6ea86f1df79so115335277b3.1 for ; Tue, 12 Nov 2024 15:23:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731453797; x=1732058597; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ke0lLdlr1kk9jFT9Y4ArnxwOQ/foP1O5UmzGSVbFNLU=; b=HFF/ECZ1vGAxfw89iBv2NsNehUmNjWozpzdNMWLyp7yAXtd+60ynsu0GL42DkhGFdQ djvzXzhlrBXSl+vm07/g33OPTqtGkTTXppUI+h8gn2Vqht6dQ7pCv8NVXh74HA2KoYSp BihlKxs21aC94x2gtKcpGsRgo+zi5uNxIAOx5RcPfilyn3mpQyR6LOEXMRPNvKNf73jV +sKRfvW1+agbtXOxlQdmuC5ASyzxpkKWXWzMj9fHY7XSCGbd2cAYVPJxYBzAJYk+isAT vqTei6T7CGjtlqQPyjElB1IhJ7Wuf6W8zLFUnpCD5fXQmBQTlqwuNxSrpIF2odOgK6ta u37A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731453797; x=1732058597; 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=ke0lLdlr1kk9jFT9Y4ArnxwOQ/foP1O5UmzGSVbFNLU=; b=qfKYBcwwZQm9ZURa//XYWQTVpPBY0foXEDz/I578MIVAT/2zgLaI+8mOEobzc4xWUs 4kRICI/N+zhfu4oWqdms6ANKruhzeR4GNpvc9VnWUgHD37Xnkgt6tH2J21TPj1NMjbsO xYyqikNaCCoQPqT/2j5jGD2V7XNe9JSjfFhkC3eCkXv1qf+JvNmoG4GqV/NHmeWAoQK8 BZjWrF2VvCnsPO9l02gnHq4CqEYQ+vvgd8VpZCneQdsdxmx61pZK11fbfa9yqs3TN9Xl 2TEIeYnooGt3V8ke66jIiNRKtqiyvk540bceK5K/Xh9RY/OV3RB/zGLROm464/w4ZqTJ GEdA== X-Gm-Message-State: AOJu0YyCWzL7nkqtTT0HDkCF8w/wTzkuI1BNyyTHsQ/TalNcs3RUONam WIO4oEBOuS09vjJjfAcrPV70zv+Q/b/ekvZv7+tZ5M2TkKP6VIKaJyXSmDC9bySBAn/4KEFgYXr 5vuPA5r9rB6U8w1tbWR631w== X-Google-Smtp-Source: AGHT+IE9XrR6GjaQBHvQS5sK7gytB+L9sUsf5ZrG6EKKcAz5P+cTOu/VMoAAZDFGSEeppHcPeADdJdGGCa104awGmA== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a05:690c:1c:b0:6e3:b08:92c7 with SMTP id 00721157ae682-6eaddc1ed85mr3462377b3.0.1731453797357; Tue, 12 Nov 2024 15:23:17 -0800 (PST) Date: Tue, 12 Nov 2024 23:22:45 +0000 In-Reply-To: <20241112232253.3379178-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev 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-7-dionnaglaze@google.com> Subject: [PATCH v6 6/8] crypto: ccp: Add DOWNLOAD_FIRMWARE_EX support From: Dionna Glaze To: linux-kernel@vger.kernel.org, x86@kernel.org, Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Ashish Kalra Cc: linux-coco@lists.linux.dev, Dionna Glaze , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Michael Roth , Luis Chamberlain , Russ Weight , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Tianfei zhang , Alexey Kardashevskiy , linux-crypto@vger.kernel.org In order to support firmware hotloading, the DOWNLOAD_FIRMWARE_EX command must be available. The DOWNLOAD_FIRMWARE_EX command requires cache flushing and introduces new error codes that could be returned to user space. Access to the command is through the firmware_upload API rather than through the ioctl interface to prefer a common interface. On init, the ccp device will make /sys/class/firmware/amd/loading etc firmware upload API attributes available to late-load a SEV-SNP firmware binary. The firmware_upload API errors reported are actionable in the following ways: * FW_UPLOAD_ERR_HW_ERROR: the machine is in an unstable state and must be reset. * FW_UPLOAD_ERR_RW_ERROR: the firmware update went bad but can be recovered by hotloading the previous firmware version. Also used in the case that the kernel used the API wrong (bug). * FW_UPLOAD_ERR_FW_INVALID: user error with the data provided, but no instability is expected and no recovery actions are needed. * FW_UPLOAD_ERR_BUSY: upload attempted at a bad time either due to overload or the machine is in the wrong platform state. synthetic_restore_required: Instead of tracking the status of whether an individual GCTX is safe for use in a firmware command, force all following commands to fail with an error that is indicative of needing a firmware rollback. To test: 1. Build the kernel enabling SEV-SNP as normal and add CONFIG_FW_UPLOAD=y. 2. Add the following to your kernel_cmdline: ccp.psp_init_on_probe=0. 3.Get an AMD SEV-SNP firmware sbin appropriate to your Epyc chip model at https://www.amd.com/en/developer/sev.html and extract to get a .sbin file. 4. Run the following with your sbinfile in FW: echo 1 > /sys/class/firmware/snp_dlfw_ex/loading cat "${FW?}" > /sys/class/firmware/snp_dlfw_ex/data echo 0 > /sys/class/firmware/snp_dlfw_ex/loading 5. Verify the firmware update message in dmesg. 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 Signed-off-by: Dionna Glaze --- drivers/crypto/ccp/Kconfig | 10 ++ drivers/crypto/ccp/Makefile | 1 + drivers/crypto/ccp/sev-dev.c | 27 ++-- drivers/crypto/ccp/sev-dev.h | 31 ++++ drivers/crypto/ccp/sev-fw.c | 281 +++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 17 +++ 6 files changed, 357 insertions(+), 10 deletions(-) create mode 100644 drivers/crypto/ccp/sev-fw.c diff --git a/drivers/crypto/ccp/Kconfig b/drivers/crypto/ccp/Kconfig index f394e45e11ab4..40be991f15d28 100644 --- a/drivers/crypto/ccp/Kconfig +++ b/drivers/crypto/ccp/Kconfig @@ -46,6 +46,16 @@ config CRYPTO_DEV_SP_PSP along with software-based Trusted Execution Environment (TEE) to enable third-party trusted applications. +config CRYPTO_DEV_SP_PSP_FW_UPLOAD + bool "Platform Security Processor (PSP) device with firmware hotloading" + default y + depends on CRYPTO_DEV_SP_PSP && FW_LOADER && FW_UPLOAD + help + Provide support for AMD Platform Security Processor firmware. + The PSP firmware can be updated while no SEV or SEV-ES VMs are active. + Users of this feature should be aware of the error modes that indicate + required manual rollback or reset due to instablity. + config CRYPTO_DEV_CCP_DEBUGFS bool "Enable CCP Internals in DebugFS" default n diff --git a/drivers/crypto/ccp/Makefile b/drivers/crypto/ccp/Makefile index 394484929dae3..5ce69134ec48b 100644 --- a/drivers/crypto/ccp/Makefile +++ b/drivers/crypto/ccp/Makefile @@ -14,6 +14,7 @@ ccp-$(CONFIG_CRYPTO_DEV_SP_PSP) += psp-dev.o \ platform-access.o \ dbc.o \ hsti.o +ccp-$(CONFIG_CRYPTO_DEV_SP_PSP_FW_UPLOAD) += sev-fw.o obj-$(CONFIG_CRYPTO_DEV_CCP_CRYPTO) += ccp-crypto.o ccp-crypto-objs := ccp-crypto-main.o \ diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index d8c35b8478ff5..a8f5e35ab8a0a 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -228,6 +228,7 @@ static int sev_cmd_buffer_len(int cmd) case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request); case SEV_CMD_SNP_CONFIG: return sizeof(struct sev_user_data_snp_config); case SEV_CMD_SNP_COMMIT: return sizeof(struct sev_data_snp_commit); + case SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX: return sizeof(struct sev_data_download_firmware_ex); default: return 0; } @@ -489,7 +490,7 @@ void snp_free_firmware_page(void *addr) } EXPORT_SYMBOL_GPL(snp_free_firmware_page); -static void *sev_fw_alloc(unsigned long len) +void *sev_fw_alloc(unsigned long len) { struct page *page; @@ -857,6 +858,15 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) if (WARN_ON_ONCE(!data != !buf_len)) return -EINVAL; + /* Firmware hotloading can fail to update some guest context pages, in which case + * user space should roll back the firmware instead of committing it. This is already + * a firmware error code called RESTORE_REQUIRED, so report that error if VMs would + * be corrupted if user space were to commit the firmware. + */ + ret = sev_snp_synthetic_error(sev, psp_ret); + if (ret) + return ret; + /* * Copy the incoming data to driver's scratch buffer as __pa() will not * work for some memory, e.g. vmalloc'd addresses, and @data may not be @@ -1661,7 +1671,7 @@ void *psp_copy_user_blob(u64 uaddr, u32 len) } EXPORT_SYMBOL_GPL(psp_copy_user_blob); -static int sev_get_api_version(void) +int sev_get_api_version(void) { struct sev_device *sev = psp_master->sev_data; struct sev_user_data_status status; @@ -1736,14 +1746,7 @@ static int sev_update_firmware(struct device *dev) return -1; } - /* - * SEV FW expects the physical address given to it to be 32 - * byte aligned. Memory allocated has structure placed at the - * beginning followed by the firmware being passed to the SEV - * FW. Allocate enough memory for data structure + alignment - * padding + SEV FW. - */ - data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32); + data_size = ALIGN(sizeof(struct sev_data_download_firmware), SEV_FW_ALIGNMENT); order = get_order(firmware->size + data_size); p = alloc_pages(GFP_KERNEL, order); @@ -2407,6 +2410,8 @@ int sev_dev_init(struct psp_device *psp) if (ret) goto e_irq; + snp_init_firmware_upload(sev); + dev_notice(dev, "sev enabled\n"); return 0; @@ -2488,6 +2493,8 @@ void sev_dev_destroy(struct psp_device *psp) kref_put(&misc_dev->refcount, sev_exit); psp_clear_sev_irq_handler(psp); + + snp_destroy_firmware_upload(sev); } static int snp_shutdown_on_panic(struct notifier_block *nb, diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index ccf3ba78d8332..2417bcce97848 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -29,6 +29,15 @@ #define SEV_CMD_COMPLETE BIT(1) #define SEV_CMDRESP_IOC BIT(0) +/* + * SEV FW expects the physical address given to it to be 32 + * byte aligned. Memory allocated has structure placed at the + * beginning followed by the firmware being passed to the SEV + * FW. Allocate enough memory for data structure + alignment + * padding + SEV FW. + */ +#define SEV_FW_ALIGNMENT 32 + struct sev_misc_dev { struct kref refcount; struct miscdevice misc; @@ -57,6 +66,11 @@ struct sev_device { bool cmd_buf_backup_active; bool snp_initialized; + +#ifdef CONFIG_FW_UPLOAD + struct fw_upload *fwl; + bool fw_cancel; +#endif /* CONFIG_FW_UPLOAD */ }; int sev_dev_init(struct psp_device *psp); @@ -69,4 +83,21 @@ struct sev_asid_data { void *snp_context; }; +/* Extern to be shared with firmware_upload API implementation if configured. */ +extern struct sev_asid_data *sev_asid_data; +extern u32 nr_asids, sev_min_asid, sev_max_asid, sev_es_max_asid; + +void *sev_fw_alloc(unsigned long len); +int sev_get_api_version(void); + +#ifdef CONFIG_CRYPTO_DEV_SP_PSP_FW_UPLOAD +void snp_init_firmware_upload(struct sev_device *sev); +void snp_destroy_firmware_upload(struct sev_device *sev); +int sev_snp_synthetic_error(struct sev_device *sev, int *psp_ret); +#else +static inline void snp_init_firmware_upload(struct sev_device *sev) { } +static inline void snp_destroy_firmware_upload(struct sev_device *sev) { } +static inline int sev_snp_synthetic_error(struct sev_device *sev, int *psp_ret) { return 0; } +#endif /* CONFIG_CRYPTO_DEV_SP_PSP_FW_UPLOAD */ + #endif /* __SEV_DEV_H */ diff --git a/drivers/crypto/ccp/sev-fw.c b/drivers/crypto/ccp/sev-fw.c new file mode 100644 index 0000000000000..327feb846e5be --- /dev/null +++ b/drivers/crypto/ccp/sev-fw.c @@ -0,0 +1,281 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * AMD Secure Encrypted Virtualization (SEV) firmware upload API + */ + +#include +#include +#include + +#include + +#include "sev-dev.h" + +static bool synthetic_restore_required; + +int sev_snp_synthetic_error(struct sev_device *sev, int *psp_ret) +{ + if (synthetic_restore_required) { + *psp_ret = SEV_RET_RESTORE_REQUIRED; + return -EIO; + } + + return 0; +} + +static int sev_snp_download_firmware_ex(struct sev_device *sev, const u8 *data, u32 size, + int *error) +{ + struct sev_data_download_firmware_ex *data_ex; + int ret, order; + struct page *p; + u64 data_size; + void *fw_dest; + + data_size = ALIGN(sizeof(struct sev_data_download_firmware_ex), SEV_FW_ALIGNMENT); + + order = get_order(size + data_size); + p = alloc_pages(GFP_KERNEL, order); + if (!p) + return -ENOMEM; + + /* + * Copy firmware data to a kernel allocated contiguous + * memory region. + */ + data_ex = page_address(p); + fw_dest = page_address(p) + data_size; + memset(data_ex, 0, data_size); + memcpy(fw_dest, data, size); + + /* commit is purposefully unset for GCTX update failure to advise rollback */ + data_ex->fw_paddr = __psp_pa(fw_dest); + data_ex->fw_len = size; + data_ex->length = sizeof(struct sev_data_download_firmware_ex); + + ret = sev_do_cmd(SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX, data_ex, error); + + if (ret) + goto free_err; + + /* Need to do a DF_FLUSH after live firmware update */ + wbinvd_on_all_cpus(); + ret = sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, error); + if (ret) + dev_dbg(sev->dev, "DF_FLUSH error %d\n", *error); + +free_err: + __free_pages(p, order); + return ret; +} + +static enum fw_upload_err snp_dlfw_ex_prepare(struct fw_upload *fw_upload, + const u8 *data, u32 size) +{ + struct sev_device *sev = fw_upload->dd_handle; + + sev->fw_cancel = false; + return FW_UPLOAD_ERR_NONE; +} + +static enum fw_upload_err snp_dlfw_ex_poll_complete(struct fw_upload *fw_upload) +{ + return FW_UPLOAD_ERR_NONE; +} + +/* Cancel can be called asynchronously, but DOWNLOAD_FIRMWARE_EX is atomic and cannot + * be canceled. There is no need to synchronize updates to fw_cancel. + */ +static void snp_dlfw_ex_cancel(struct fw_upload *fw_upload) +{ + /* fw_upload not-NULL guaranteed by firmware_upload API */ + struct sev_device *sev = fw_upload->dd_handle; + + sev->fw_cancel = true; +} + +static enum fw_upload_err snp_dlfw_ex_err_translate(struct sev_device *sev, int psp_ret) +{ + dev_dbg(sev->dev, "Failed to update SEV firmware: %#x\n", psp_ret); + + /* + * Operation error: + * HW_ERROR: Critical error. Machine needs repairs now. + * RW_ERROR: Severe error. Roll back to the prior version to recover. + * User error: + * FW_INVALID: Bad input for this interface. + * BUSY: Wrong machine state to run download_firmware_ex. + */ + switch (psp_ret) { + case SEV_RET_RESTORE_REQUIRED: + dev_warn(sev->dev, "Firmware updated but unusable. Rollback!!!\n"); + return FW_UPLOAD_ERR_RW_ERROR; + case SEV_RET_SHUTDOWN_REQUIRED: + /* No state changes made. Not a hardware error. */ + dev_warn(sev->dev, "Firmware image cannot be live updated\n"); + return FW_UPLOAD_ERR_FW_INVALID; + case SEV_RET_BAD_VERSION: + /* No state changes made. Not a hardware error. */ + dev_warn(sev->dev, "Firmware image is not well formed\n"); + return FW_UPLOAD_ERR_FW_INVALID; + /* SEV-specific errors that can still happen. */ + case SEV_RET_BAD_SIGNATURE: + /* No state changes made. Not a hardware error. */ + dev_warn(sev->dev, "Firmware image signature is bad\n"); + return FW_UPLOAD_ERR_FW_INVALID; + case SEV_RET_INVALID_PLATFORM_STATE: + /* Calling at the wrong time. Not a hardware error. */ + dev_warn(sev->dev, "Firmware not updated as SEV in INIT state\n"); + return FW_UPLOAD_ERR_BUSY; + case SEV_RET_HWSEV_RET_UNSAFE: + dev_err(sev->dev, "Firmware is unstable. Reset your machine!!!\n"); + return FW_UPLOAD_ERR_HW_ERROR; + /* Kernel bug cases. */ + case SEV_RET_INVALID_PARAM: + dev_err(sev->dev, "Download-firmware-EX invalid parameter\n"); + return FW_UPLOAD_ERR_RW_ERROR; + case SEV_RET_INVALID_ADDRESS: + dev_err(sev->dev, "Download-firmware-EX invalid address\n"); + return FW_UPLOAD_ERR_RW_ERROR; + default: + dev_err(sev->dev, "Unhandled download_firmware_ex err %d\n", psp_ret); + return FW_UPLOAD_ERR_HW_ERROR; + } +} + +static enum fw_upload_err snp_update_guest_contexts(struct sev_device *sev) +{ + struct sev_data_snp_guest_status status_data; + void *snp_guest_status; + enum fw_upload_err ret = FW_UPLOAD_ERR_NONE; + int rc, error; + + /* + * Force an update of guest context pages after SEV firmware + * live update by issuing SNP_GUEST_STATUS on all guest + * context pages. + */ + snp_guest_status = sev_fw_alloc(PAGE_SIZE); + if (!snp_guest_status) + return FW_UPLOAD_ERR_INVALID_SIZE; + + for (int i = 1; i <= sev_es_max_asid; i++) { + if (!sev_asid_data[i].snp_context) + continue; + + status_data.gctx_paddr = __psp_pa(sev_asid_data[i].snp_context); + status_data.address = __psp_pa(snp_guest_status); + rc = sev_do_cmd(SEV_CMD_SNP_GUEST_STATUS, &status_data, &error); + if (!rc) + continue; + + /* + * Handle race with SNP VM being destroyed/decommissoned, + * if guest context page invalid error is returned, + * assume guest has been destroyed. + */ + if (error == SEV_RET_INVALID_GUEST) + continue; + + /* Guest context page update failure should force userspace to rollback, + * so make all non-DOWNLOAD_FIRMWARE_EX commands fail with RESTORE_REQUIRED. + * This emulates the behavior of the firmware on an older PSP bootloader version + * that couldn't auto-restore on DOWNLOAD_FIRMWARE_EX failure. However, the error + * is still relevant to this follow-up guest update failure. + */ + synthetic_restore_required = true; + dev_err(sev->dev, + "SNP guest context update error, rc=%d, fw_error=0x%x. Rollback!!!\n", + rc, error); + ret = FW_UPLOAD_ERR_RW_ERROR; + break; + } + + snp_free_firmware_page(snp_guest_status); + return ret; +} + +static enum fw_upload_err snp_dlfw_ex_write(struct fw_upload *fwl, const u8 *data, + u32 offset, u32 size, u32 *written) +{ + /* fwl not-NULL guaranteed by firmware_upload API, and sev is non-NULL by precondition to + * snp_init_firmware_upload. + */ + struct sev_device *sev = fwl->dd_handle; + u8 api_major, api_minor, build; + int ret, error; + + if (!sev) + return FW_UPLOAD_ERR_HW_ERROR; + + if (sev->fw_cancel) + return FW_UPLOAD_ERR_CANCELED; + + /* + * SEV firmware update is a one-shot update operation, the write() + * callback to be invoked multiple times for the same update is + * unexpected. + */ + if (offset) + return FW_UPLOAD_ERR_INVALID_SIZE; + + if (sev_get_api_version()) + return FW_UPLOAD_ERR_HW_ERROR; + + api_major = sev->api_major; + api_minor = sev->api_minor; + build = sev->build; + + ret = sev_snp_download_firmware_ex(sev, data, size, &error); + if (ret) + return snp_dlfw_ex_err_translate(sev, error); + + ret = snp_update_guest_contexts(sev); + if (ret) + return ret; + + sev_get_api_version(); + if (api_major != sev->api_major || api_minor != sev->api_minor || + build != sev->build) { + dev_info(sev->dev, "SEV firmware updated from %d.%d.%d to %d.%d.%d\n", + api_major, api_minor, build, + sev->api_major, sev->api_minor, sev->build); + } else { + dev_info(sev->dev, "SEV firmware not updated, same as current version %d.%d.%d\n", + api_major, api_minor, build); + } + + *written = size; + + return FW_UPLOAD_ERR_NONE; +} + +static const struct fw_upload_ops snp_dlfw_ex_ops = { + .prepare = snp_dlfw_ex_prepare, + .write = snp_dlfw_ex_write, + .poll_complete = snp_dlfw_ex_poll_complete, + .cancel = snp_dlfw_ex_cancel, +}; + +/* PREREQUISITE: sev is non-NULL */ +void snp_init_firmware_upload(struct sev_device *sev) +{ + struct fw_upload *fwl; + + fwl = firmware_upload_register(THIS_MODULE, sev->dev, "snp_dlfw_ex", &snp_dlfw_ex_ops, sev); + if (IS_ERR(fwl)) { + dev_err(sev->dev, "SEV firmware upload initialization error %ld\n", PTR_ERR(fwl)); + return; + } + + sev->fwl = fwl; +} + +/* PREREQUISITE: sev is non-NULL */ +void snp_destroy_firmware_upload(struct sev_device *sev) +{ + if (!sev->fwl) + return; + + firmware_upload_unregister(sev->fwl); +} diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 0b3b7707ccb21..9ad941e36bb63 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -185,6 +185,23 @@ struct sev_data_download_firmware { u32 len; /* In */ } __packed; +/** + * struct sev_data_download_firmware_ex - DOWNLOAD_FIRMWARE_EX command parameters + * + * @length: length of this command buffer + * @fw_paddr: physical address of firmware image + * @fw_len: len of the firmware image + * @commit: automatically commit the newly installed image + */ +struct sev_data_download_firmware_ex { + u32 length; /* In */ + u32 reserved; /* In */ + u64 fw_paddr; /* In */ + u32 fw_len; /* In */ + u32 commit:1; /* In */ + u32 reserved2:31; /* In */ +} __packed; + /** * struct sev_data_get_id - GET_ID command parameters * 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: 13873036 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 0841021E13A 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=txrCETwCShByGGXcBVxOu3b4bVcSgjE9o4TLyARpWYjXrb7WT5bSgvOVI5Otj12/tzGo3vptxMBcFNO5M+SoHvYyibjWx0QrMhJ9b6/Fmapq1ouiOsRssFvwCqNzGOqYzsJl5IQKw/4bygX/DnJclzO7OYHmNVkHrVE2nvQ5UO4= 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=BKHle7PT; 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="BKHle7PT" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e02fff66a83so9836509276.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=lists.linux.dev; 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=BKHle7PTlIC6vwj6r1hTwukOKj6mRt2rpIcWDLiSXX1qakXb5TvKel6ilTrkZzUy5F rJ+Y6ZoTwwchzzk3Riz1plY4hp4MoA5Jg27DHCrFayx4FHgMEhDyC6pEIxoB8KLn9upn LNKjymPVwv7SvDEyPGoq5FU3n4L6NPISb3CVxBsbsq5psRoGXT50SwB5SdBk37uPO0Zk x9cJBjjMZX9CicYWgyhmWJLN28cveQyd+VGYAkyK2GTO01NI2F6T2Zj1Uq4y2V5GIj3S NCENEesELJcgcPVP8IfnVgRiunlS7BDrYMh+5woqYq3gHSI7UOZBpU6uTVOHbjT4O+/5 IwBg== 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=D29okn9hvDNlSzdUuHNifQ4EerjTRzOtzgCwL5E0Jfjhgh4P//8AMfMsRQ/CZS6WJX ELjc+O5KFzAfFStcVKX+u/epfcRaF/bJ59SnHoZP1lcrWsiqA06reslqvW6Qc7tQbgOE pTNjubW8fs0A5zHiv9Gp/BIceDfPKRra8Z8zFowx5tdhIN5GWsdjT6VdoeU4Gytc/eC6 VYYV/PRT3K6wihD+LuOthWpN/ajuPJsXSTTtE8GV4y58e5k9H+lcI9DuJVpN81NWB4eb mjvkFPwmhH5HJ2QDcG9ioOrOxd7eJv6lhkNhxLsqTE4c4QWYZgLOgBlVCRqQkm94Z9V8 UeOg== X-Gm-Message-State: AOJu0YyykFDGV0dk0SUWw2wh3VAR1ij40/x26hxTiu/RnBTnuZmQg5Ej nhd8JWwJ1HTBWEMNguVZU+DUV0N3g5xGukQdrpSqf/Js4hoB6WVGIetDVkLhPzJClBXUXBRENVB 1YEd+lxcjtqaO6M2yFUDqIw== 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: linux-coco@lists.linux.dev 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: 13873037 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 48B7621EBAB 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=QQIaM6pepmK0tMaXtqLMC2gH9Q4eC+UaCQ19HJ5/psMOHuz6Hey/otkpQt2YAopkfQxd+nP6gYqEWXMesaGg1y1cBfdSCmsIV5XeH54qI2IK7TcqiSOzxXnoRTQKnky1Tpp4NhAtzjVvZYUis2017KHaobqrJDtMcUV+elPKqMk= 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=jHbhL/8V; 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="jHbhL/8V" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71e479829c8so7537206b3a.0 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=lists.linux.dev; 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=jHbhL/8V/UZZTTIqrB6qoJmF63OvxAxmXXP7bUtzOzOXjRBrLKZp8P8qEF4AOtf/0C 8PF+918plHnQMWZrIusXmUEzoNgM+mr993koB3fFB3w8gxHVAIclpFB7kqT351ocigKT quCzlLNewvjiHTcrWIK0DMO5YI942lRgrQe74WiGnL8iemQSuOQWQLwhWrqTfCiiSXVI BsgqS28fzWzWpvXCWv+2QyByB+b+ce540UB5qNkFEDSuuSbi4BZ2f5nEo2KZalaCoyVf YkYmvJV2mvTYWmLNfuHUFKU0xn9Cx2Jl4XpcQGhn0u2b+TTHMhcG4L2q5BEIIakVpeDe n8dA== 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=bPyHK1pcyVWo6RWH42ORqiJ6LSiuV0Wrne5aqWe+0fn9qV1tLUiQnXxC7ezzLWNBq3 Di8RtFUmNeDph7H7dWJjSMZFbEilFtqZpw/qeB4Rain/LBkd5GhCG1VUxkYLacrV+axP jVKpFKbRUBBhglqHRMUG1Dd87QKPg/QD4K3J06xHq8rR17L8UrkePA1PlFI9Nl3HEhMt e6U1tf+RUSwz+lWXWP0EF9TbuKQuAXvb6dR29bf+flwbNS8kxTV3AkSTuBTDvhfX8Xxy JjzKRBTULCpTIrb0YUzUtvTT1qL82rJxwOWMkbfIp4nzne/Gq63KCPjbdG8Qe6RkFyx1 j0Kw== X-Gm-Message-State: AOJu0YxAC2nWTpsHe1EbQvBXvE0Hs8dIV1sRVvdo3C5eUTDbNP6KmkQu Q3wSA6vxbvDjmCB5e2xs8utfYxTl0LFpa480Ku71duKJXjrZA2YKnD9tCsmFYR4gGEvGDdBojZx 7Mo+ETWEpj+K3ZilLw38vrg== 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: linux-coco@lists.linux.dev 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;