From patchwork Sat Nov 2 00:08:11 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: 13859832 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 4436379CD for ; Sat, 2 Nov 2024 00:08:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730506112; cv=none; b=oY+nZt/mRu3Lv4pLzsNqk452uQCi7CGqBxcAAS+4IVv17vHhp9ghYxmtVFjbIjIxwjQ9J42DZ5tfH+aGtyFiz1Ra6httD5WTWyDdjl7bcneFS+JvqCfdNElTB7Jdcm8NA0hsw3V1v0W7rhYL5mPREeiK4LiRM4K5JhiIFMqaVXM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730506112; c=relaxed/simple; bh=Rbij4r8cylGhkgGUg7WpUGdpkZboGKcsQk69ga17bxA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RMytTLlXda33BNvHH2NHMBgNmECUzDWTM27WCGtnur2SHDqFrHOe/slvIIYJBUjcvCwE2h/D+vfFnZZ07imARto/s2N+Ia/uSnexFWQwhU+r+Zizt8mCAzq1ZrqtmGClI35C9E39ODjXcXStKbND9Q+rwKtwBKhcwe6VjrQ2yUA= 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=ik1LXbrV; arc=none smtp.client-ip=209.85.210.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="ik1LXbrV" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-71e64cbb445so2473552b3a.1 for ; Fri, 01 Nov 2024 17:08:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730506110; x=1731110910; 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=9t5+hAx/Vx0CBMSZ/K4Xo8KI8HClXDiTkRfLYODx8EU=; b=ik1LXbrVNQnsgWF6rbrOsB8r1t9Gfuqn7z+m2PP7e4ivIj4Bob1HLqB21WmcEgy4jb kZvi3HBwzGIn/JZMD4iYTe3++hZbHqVhBJc42zq+88f/3ORi1SxS7lMWC1zTWtgu7HZw nMaat81114Pw545/9uP4UKOBuiiToVpWYiNWO1xH8P7wSSr5Xkr2E1zjh8SPy4ylTB+q legvHK+W2A4/BkfVRQyUH+vT78j/QK68AQK0F0YRf5dsJ1nULKirFzf37pRxKt7/AOPB QZC4iCYdy2nu/vfGBJYM8DNJvZkj0WFWXcwrJXQfF9lMivlXoweT5fQfIjejH0hTpayB B8+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506110; x=1731110910; 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=9t5+hAx/Vx0CBMSZ/K4Xo8KI8HClXDiTkRfLYODx8EU=; b=mpd/ZjSL0sqbRJTtaiJ4y+ldllcKEvsAtKuKCiX72+gxo49PtigQy+xk3VmA9eSdzA 3paO7cKD5Qj7GAzNGU34n0YzfGuD90pFLrJz8jLN0KOrsmCeAfPRttmM+YFMa98oguYq SKmzUfu/b0PLTpjtv7zw8Mm7XXidmlP+gGk6/AwnaUPUoyjwjfeDpkqusc7VHhDm8OWJ aIMlrHfVHYr/0qosMZucl/YtbqgmU8TR9thG7nrI//15gUoUGfDhuD5IrphJGd7Xip7H li6Qhf5XMK3958nhhCVP3STGatzZl/Co6TZ780vbbENeBVR7RGy4UEyCCNyR0Uu3t+h2 dBvw== X-Gm-Message-State: AOJu0YxyHxvDTxTw7e9VKs5Dg+F0ehsbmlejRPXsLluHSSgpO07u5Mc1 wXqe07Y9lmK1/IdBBXKxcbF/W1zR+Fa/FYzWtSpcLk0go3pG5sn6UY775CZqbZszbIRVCysBlU/ bsFeBt90PQqd3pDCrA8TY2PkAoPbG2sw7o4K8LZERu5PdynYxj79VXqoY8ZL+Zoagq23ymWpGCV +sx2Ie7h5KSrdc8IIhmP2rt+jhO+0PdUvAWa42TNSB+jWyb6jra7goXSmUIRu0SfvGvWI= X-Google-Smtp-Source: AGHT+IH0e3GALuKo0D69gpU9KNL0giGzOcfcFha1VASPxW64ZNHEOxXwwBULM49bCwY2pfhwLl60b9qTFoYifUuHZQ== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a05:6a00:600f:b0:71e:5879:689d with SMTP id d2e1a72fcca58-720b9e7aaa1mr26792b3a.6.1730506110334; Fri, 01 Nov 2024 17:08:30 -0700 (PDT) Date: Sat, 2 Nov 2024 00:08:11 +0000 In-Reply-To: <20241102000818.2512612-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241102000818.2512612-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241102000818.2512612-2-dionnaglaze@google.com> Subject: [PATCH v3 1/4] 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" , Michael Roth , Ashish Kalra , Brijesh Singh Cc: 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 , 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 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 714c517dd4b72..f6e96ec0a5caa 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 Sat Nov 2 00:08:12 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: 13859833 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 BF12F18035 for ; Sat, 2 Nov 2024 00:08:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730506116; cv=none; b=gX0olpm4+X3PYe9CCO+Qb7TzfUI3SwDPaxm2yhb3ooSQ2fzwYQ1N60dcrc+r2wa903pmu8QEx+a4qBX1W7uxVdAfe+/2wHpgV8YvxNEMe3vm/Em+YI6uwdETJZx2wBzPQUrhdm0k7GXCcRRwck5wCC42HWfVQRuBoiyLq1jKhEE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730506116; c=relaxed/simple; bh=D5TifaQ9HJSnoTRXPHyCsGQ6jNhcD5UTr+lHgNqoUEw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fKuqNzM4PoQRNErGSI//NWW+ouQ73hofm2myf2GmfZcSzEQuZWmbV/4PGnZSJUspccLCESqcb2R9gqc6SzMzBifsrHBIMuNraFENPSEvCIvt1H0LnENFPQnKyaCsRkMr0Sx+UKMOfBjIWdGAaOYz+YUesn9odRw2DKBVTFlH5QE= 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=mxPBksPH; arc=none smtp.client-ip=209.85.210.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="mxPBksPH" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-720738f8040so2501569b3a.2 for ; Fri, 01 Nov 2024 17:08:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730506114; x=1731110914; 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=ah3K57+pIWf1JIbhJyIJqLnkO2+WjvytEokHBlHWy4I=; b=mxPBksPHxxA1fO3//JAhu6IIlSGrdxrCWpqcHuY94Iyj3Hx/Fp90ntAYyKecbl3SMw 1Gys2Lyrif6fANE/l93jEvT5GSsx/4Sn6q+X2lIEeetK0TIwmszq2LobwlXwFGcN7R1x uo6ht+HbMuLucu+dI5OdOh/4EfI2TT46J9LSMg0lXpXULhCXTMrHHyObYW+lSq5CeC8s BpyByrO5HN9/dyFyDLnkGAolCHD+DYTXILUh4j+QKaakq7QMw2aVDBYGZrGTCCtfgeUj cUDZG5IhBkisiLinX8i3/JBiuqGMmnkK+11+ixacQZaFAqQJFLbQ+ceVZ3bhQDmbZe0g yuCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506114; x=1731110914; 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=ah3K57+pIWf1JIbhJyIJqLnkO2+WjvytEokHBlHWy4I=; b=vpu/r2sSXmAp8uyLC2Mwr6/SOlH9C5NgCo1AW2u7ZrilAroFAOZVGk4lw6ZPVwjjgM +Ef+PZBLTdL65c4jrzA2nxXO8q7rqveg/kcM1BRhCFpSwwNhIqod+lJctVg/tEo52EKz 3R0b9RgV0PBLtWgJ+xUIHSX2Gd5XSDHmjajr5WYZmBnL+D0c8clnXKyETaKsDESstCHB Zp0R/bWx/2/Ymy1qG+/dLvnhB+LUwufIJvbBdcS/Ez3ZnS7DI2F0B4yzABaig8POToVw 1QDqteLdF31au+ZOVPjDk1blTkOzZeY2ZAYTOivWrlCFG6N1TTkq8fRlMWh3+6mTMG0D wVfA== X-Gm-Message-State: AOJu0YwXVhy0QUHMgmclrF752Tb19gs46dnruTJn/LInMF9fxb1H4uEV k9UFKwIlf+EML7/nrsuxFag/ovu0DtIBTN2CbeGoeGtAqQ7G7QawHPyqtOnKBj8BkcGfa8ZWxqp faOeoTfd2AfpM7o9/Dhi7t+WCLLz2TSyxDJe+1duBY7l7CLrAUOzRW9vpEEgFXwe9ROpUvnI4D3 0yHoapJ/XuRpHoV207D6oa6EXeGhneUDw35I5sTi7RDGXpqBLErx428fd3W72/asVtAdg= X-Google-Smtp-Source: AGHT+IEsDXtDKFkROLd+6BH4rvOWuiRlmFEnY+ixeW1YDYF9nECO3enAXVuU4Q7y4wadXSfJW5YExyJzTxwvQFSZCA== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a05:6a00:a44d:b0:71e:4a06:8a5f with SMTP id d2e1a72fcca58-720ab690cc9mr24349b3a.6.1730506112466; Fri, 01 Nov 2024 17:08:32 -0700 (PDT) Date: Sat, 2 Nov 2024 00:08:12 +0000 In-Reply-To: <20241102000818.2512612-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241102000818.2512612-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241102000818.2512612-3-dionnaglaze@google.com> Subject: [PATCH v3 2/4] 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: 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 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 Sat Nov 2 00:08:13 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: 13859835 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 84BBB1E86E for ; Sat, 2 Nov 2024 00:08:36 +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=1730506118; cv=none; b=sUvx+VOU+DcsHwXZqHJdr+4mjD5fnTnAltrkB19RB2vcSbhW6SBOpCfyuj2SAkxP8pc8v7C+9pvGJ3UW/jcGXiq2kK3f3Ysus+tufsBjLH8E+suEGlquY2iinvwQhtJQBjkAUNKFxt2+mUU7tHjKoiUQqAEURXPuYmH8w/1c+ps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730506118; c=relaxed/simple; bh=n1fLU65LsY4cG54gUhI0pTlTY7OQFXC7ZwZ9Y8HeUTw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UtnowuQU1ccO6+Zp708ioosnX/P3f/9AWiP9VcA1z8UFreffo8IlqBbv8oosyqQbQCcAXAfQDrfo7hQSy6Z1YBPYEfWz/kQvMPZ9xkZ2YlzWY8/N0PijXH6JE1Cl8nPWJVVnEzSbOQ6zA7ojZoesqen++U+ufE5eUhJtfWjAo08= 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=EV2Nswd+; 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="EV2Nswd+" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71e7858820eso3037205b3a.2 for ; Fri, 01 Nov 2024 17:08:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730506116; x=1731110916; 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=2cAg2TIOmztZf4rIzo3Ks8a5SntMRhj56qb5+KV7CAo=; b=EV2Nswd+41Gatlz3nJlGHyCCL6mqhPT2JuMPFaKP1DhdL0GkRtyyRsyeZqLnTuFPOK 6xiBwFSjhj0UkIJU3cTzYnSmeSOoRv61qZdep6SAN2olB/Ktu9f+XZHCryokTV1K62Rb Qp43FJCnlF+pSHLtoH8Pe/WBeri98iUUFQC9nMH1VVrfknKote7Hj4XjYISs4//7ajiy TOFV/moAO9Eb746A58czIOYQATaMZLPon9D+A4CXijXRTXQ6TlrVpnUZHEaGqSYznYxG lAu989+0u7TcwLQuwF8qL0l2SU3ht14cNPL1NUC4UrDsklLH17ELIU+0YEnKFiWdFaFL biUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506116; x=1731110916; 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=2cAg2TIOmztZf4rIzo3Ks8a5SntMRhj56qb5+KV7CAo=; b=lbwXo0hCvqKlJxqcDZfn5hEAtl0PeplpHzD8PGrmK8RDWZbRSRLMZzAqB1940ngHgX 7gEvysp18Mcist3btTFw4Vpw/TUImNZ55mPuKqplPdAw54Lfx69t8VncSMe2YDK7kVoM 613Ote27Gy7QzhKQtX1W8Oz7c+hqqoq519rMe/hVt6daOVS/7T4fZ8lmx8Lee+1VBBEM G38kP5dmZYGXfEG75VTRN8x1TdzQC0de/75MgC3Ml2uL9cy/IyW84c7/+qLqnwpvdwb1 PdaYAqhnvVwCjz5YibDl1kGMY7yeMG13KM/0yWSthRs2zKhUCta1YTjJeK05kSfyLARR cSEA== X-Gm-Message-State: AOJu0Ywr5n5oGsFhKb196NZu2WFGzE5D8hxbtHexrymU7Y0eesvtlARn TJB05okJR87CWChRmeepfDiulRQbwyUBldHxFXyRtYFpjBfb2YJ1MNWVJrqPuLuDL5Qiph8zJQj pbEb/X7lzZVBuubwiX26n9aOEVSxAH2qvk/ifnaRBNEmFn6KRaTm1Kb/+FznNpvoWABVS1NTWEs qGhia1y9zLOdxzwvAzD8XuVchvKbahwd6953XTc2iqC3iyNpBZymW12Z/lWZiQ08AMh14= X-Google-Smtp-Source: AGHT+IGGQXmNHqS7TEy/27UvAT2AmC2OoEFgcpQ2zQzRiqM3LYhS4oJoa1GZzgf6sE26RnPq4c3JUYwQBUuINR8YoQ== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:aa7:9e92:0:b0:71e:4dce:75bf with SMTP id d2e1a72fcca58-720b9dc33camr26805b3a.3.1730506115129; Fri, 01 Nov 2024 17:08:35 -0700 (PDT) Date: Sat, 2 Nov 2024 00:08:13 +0000 In-Reply-To: <20241102000818.2512612-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241102000818.2512612-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241102000818.2512612-4-dionnaglaze@google.com> Subject: [PATCH v3 3/4] crypto: ccp: Add SNP firmware hotload 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: 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 uapi changes: The status values added are defined in AMD's SEV-SNP API documentation as new error modes for the DOWNLOAD_FIRMWARE_EX command. 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. snp_asid_to_gctx_page_map: 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_edx(0x8000001f)-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. Given that GCTX page creation and binding through the SNP_ACTIVATE command are separate, the creation operation also tracks pages that are yet to be bound to an asid. 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. According to SEV-SNP API section 3.3 Firmware Updates, the DOWNLOAD_FIRMWARE_EX command is usable when the SNP firmware is in either UNINIT or INIT states, but SEV-legacy firmware must be in the UNINIT state. A sev_platform_init call when only SEV-SNP VMs are expected should set supports_download_firmware_ex to delay legacy initialization actions. The firmware can only be hotloaded for SEV VMs when legacy initialization has not happened. 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 Tested-by: Ashish Kalra Signed-off-by: Dionna Glaze --- drivers/crypto/ccp/Kconfig | 2 + drivers/crypto/ccp/sev-dev.c | 399 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 14 ++ include/linux/psp-sev.h | 29 +++ include/uapi/linux/psp-sev.h | 5 + 5 files changed, 441 insertions(+), 8 deletions(-) diff --git a/drivers/crypto/ccp/Kconfig b/drivers/crypto/ccp/Kconfig index f394e45e11ab4..cd9b667640e7c 100644 --- a/drivers/crypto/ccp/Kconfig +++ b/drivers/crypto/ccp/Kconfig @@ -39,6 +39,8 @@ config CRYPTO_DEV_SP_PSP bool "Platform Security Processor (PSP) device" default y depends on CRYPTO_DEV_CCP_DD && X86_64 && AMD_IOMMU + select FW_LOADER + select FW_UPLOAD help Provide support for the AMD Platform Security Processor (PSP). The PSP is a dedicated processor that provides support for key diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 9810edbb272d2..1da183e8a9ebc 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -223,6 +223,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; } @@ -829,6 +830,7 @@ static int snp_reclaim_cmd_buf(int cmd, void *cmd_buf) return 0; } +static void snp_cmd_bookkeeping_locked(int cmd, struct sev_device *sev, void *data); static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) { struct cmd_buf_desc desc_list[CMD_BUF_DESC_MAX] = {0}; @@ -852,6 +854,15 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) if (WARN_ON_ONCE(!data != !buf_len)) return -EINVAL; + /* The firmware does not coordinate all GCTX page updates. Force an + * error to ensure that no new VM can be started and no active VM makes + * progress on guest requests until the firmware is rolled back. + */ + if (sev->synthetic_restore_required) { + *psp_ret = SEV_RET_RESTORE_REQUIRED; + return -EBUSY; + } + /* * 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 @@ -982,6 +993,9 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, buf_len, false); + if (!ret) + snp_cmd_bookkeeping_locked(cmd, sev, data); + return ret; } @@ -1082,6 +1096,7 @@ static int snp_filter_reserved_mem_regions(struct resource *rs, void *arg) return 0; } +static int sev_snp_platform_init_firmware_upload(struct sev_device *sev); static int __sev_snp_init_locked(int *error) { struct psp_device *psp = psp_master; @@ -1179,6 +1194,8 @@ static int __sev_snp_init_locked(int *error) sev_es_tmr_size = SNP_TMR_SIZE; + rc = sev_snp_platform_init_firmware_upload(sev); + return rc; } @@ -1326,6 +1343,13 @@ static int _sev_platform_init_locked(struct sev_platform_init_args *args) if (args->probe && !psp_init_on_probe) return 0; + /* + * Defer legacy SEV/SEV-ES support if legacy VMs are not being launched + * and module psp_init_on_probe parameter is false. + */ + if (args->supports_download_firmware_ex && !psp_init_on_probe) + return 0; + return __sev_platform_init_locked(&args->error); } @@ -1592,14 +1616,8 @@ 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); @@ -2224,6 +2242,367 @@ static int sev_misc_init(struct sev_device *sev) return 0; } +#ifdef CONFIG_FW_UPLOAD + +static enum fw_upload_err snp_dlfw_ex_prepare(struct fw_upload *fw_upload, + const u8 *data, u32 size) +{ + 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; +} + +/* + * This may be called asynchronously with an on-going update. All other + * functions are called sequentially in a single thread. To avoid contention on + * register accesses, only update the cancel_request flag. Other functions will + * check this flag and handle the cancel request synchronously. + */ +static void snp_dlfw_ex_cancel(struct fw_upload *fw_upload) +{ + struct sev_device *sev = psp_master->sev_data; + + mutex_lock(&sev->fw_lock); + sev->fw_cancel = true; + mutex_unlock(&sev->fw_lock); +} + +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\n"); + dev_warn(sev->dev, "Need to do manual firmware 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_dlfw_ex_write(struct fw_upload *fwl, const u8 *data, + u32 offset, u32 size, u32 *written) +{ + struct sev_data_download_firmware_ex *data_ex; + struct sev_data_snp_guest_status status_data; + struct sev_device *sev = fwl->dd_handle; + void *snp_guest_status, *fw_dest; + u8 api_major, api_minor, build; + int ret, error, order; + struct page *p; + u64 data_size; + bool cancel; + + if (!sev) + return FW_UPLOAD_ERR_HW_ERROR; + + mutex_lock(&sev->fw_lock); + cancel = sev->fw_cancel; + mutex_unlock(&sev->fw_lock); + + if (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; + + 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) { + ret = FW_UPLOAD_ERR_INVALID_SIZE; + goto fw_err; + } + + /* + * 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); + + data_ex->address = __psp_pa(fw_dest); + data_ex->len = size; + data_ex->cmdlen = sizeof(struct sev_data_download_firmware_ex); + + /* + * SNP_COMMIT should be issued explicitly to commit the updated + * firmware after guest context pages have been updated. + */ + + ret = sev_do_cmd(SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX, data_ex, &error); + + if (ret) { + ret = snp_dlfw_ex_err_translate(sev, error); + goto free_err; + } + + __free_pages(p, order); + + /* Need to do a DF_FLUSH after live firmware update */ + wbinvd_on_all_cpus(); + ret = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, &error); + if (ret) { + dev_dbg(sev->dev, "DF_FLUSH error %d\n", error); + ret = FW_UPLOAD_ERR_HW_ERROR; + goto fw_err; + } + + sev_get_api_version(); + + /* + * 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) { + ret = FW_UPLOAD_ERR_INVALID_SIZE; + goto fw_err; + } + + /* + * After the last bound asid-to-gctx page is snp_unbound_gctx_end-many + * unbound gctx pages that also need updating. + */ + for (int i = 1; i <= sev->last_snp_asid + sev->snp_unbound_gctx_end; i++) { + if (sev->snp_asid_to_gctx_pages_map[i]) { + status_data.gctx_paddr = sev->snp_asid_to_gctx_pages_map[i]; + status_data.address = __psp_pa(snp_guest_status); + ret = sev_do_cmd(SEV_CMD_SNP_GUEST_STATUS, &status_data, &error); + if (ret) { + /* + * 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; + sev->synthetic_restore_required = true; + dev_err(sev->dev, "SNP GCTX update error: %#x\n", error); + dev_err(sev->dev, "Roll back SNP firmware!\n"); + snp_free_firmware_page(snp_guest_status); + ret = FW_UPLOAD_ERR_RW_ERROR; + goto fw_err; + } + } + } + snp_free_firmware_page(snp_guest_status); + + 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 same as old %d.%d.%d\n", + api_major, api_minor, build); + } + + *written = size; + return FW_UPLOAD_ERR_NONE; + +free_err: + __free_pages(p, order); +fw_err: + return ret; +} + +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, +}; + +static void sev_snp_dev_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)); + + sev->fwl = fwl; + mutex_init(&sev->fw_lock); +} + +static void sev_snp_destroy_firmware_upload(struct sev_device *sev) +{ + firmware_upload_unregister(sev->fwl); +} + +/* + * After a gctx is created, it is used by snp_launch_start before getting + * bound to an asid. The launch protocol allocates an asid before creating a + * matching gctx page, so there should never be more unbound gctx pages than + * there are possible SNP asids. + * + * The unbound gctx pages must be updated after executing DOWNLOAD_FIRMWARE_EX + * and before committing the firmware. + */ +static void snp_gctx_create_track_locked(struct sev_device *sev, void *data) +{ + struct sev_data_snp_addr *gctx_create = data; + + /* This condition should never happen, but is needed for memory safety. */ + if (sev->snp_unbound_gctx_end >= sev->last_snp_asid) { + dev_warn(sev->dev, "Too many unbound SNP GCTX pages to track\n"); + return; + } + + sev->snp_unbound_gctx_pages[sev->snp_unbound_gctx_end] = gctx_create->address; + sev->snp_unbound_gctx_end++; +} + +/* + * PREREQUISITE: The snp_activate command was successful, meaning the asid + * is in the acceptable range 1..sev->last_snp_asid. + * + * The gctx_paddr must be in the unbound gctx buffer. + */ +static void snp_activate_track_locked(struct sev_device *sev, void *data) +{ + struct sev_data_snp_activate *data_activate = data; + + sev->snp_asid_to_gctx_pages_map[data_activate->asid] = data_activate->gctx_paddr; + + for (int i = 0; i < sev->snp_unbound_gctx_end; i++) { + if (sev->snp_unbound_gctx_pages[i] == data_activate->gctx_paddr) { + /* + * Swap the last unbound gctx page with the now-bound + * gctx page to shrink the buffer. + */ + sev->snp_unbound_gctx_end--; + sev->snp_unbound_gctx_pages[i] = + sev->snp_unbound_gctx_pages[sev->snp_unbound_gctx_end]; + sev->snp_unbound_gctx_pages[sev->snp_unbound_gctx_end] = 0; + break; + } + } +} + +static void snp_decommission_track_locked(struct sev_device *sev, void *data) +{ + struct sev_data_snp_addr *data_decommission = data; + + for (int i = 1; i <= sev->last_snp_asid; i++) { + if (sev->snp_asid_to_gctx_pages_map[i] == data_decommission->address) { + sev->snp_asid_to_gctx_pages_map[i] = 0; + break; + } + } +} + +static void snp_cmd_bookkeeping_locked(int cmd, struct sev_device *sev, void *data) +{ + if (!sev->snp_asid_to_gctx_pages_map) + return; + + switch (cmd) { + case SEV_CMD_SNP_GCTX_CREATE: + snp_gctx_create_track_locked(sev, data); + break; + case SEV_CMD_SNP_ACTIVATE: + snp_activate_track_locked(sev, data); + break; + case SEV_CMD_SNP_DECOMMISSION: + snp_decommission_track_locked(sev, data); + break; + default: + break; + } +} + +static int sev_snp_platform_init_firmware_upload(struct sev_device *sev) +{ + u32 max_snp_asid; + + /* + * cpuid_edx(0x8000001f) is the minimum SEV ASID, hence the exclusive + * maximum SEV-SNP ASID. Save the inclusive maximum to avoid confusing + * logic elsewhere. + */ + max_snp_asid = cpuid_edx(0x8000001f); + sev->last_snp_asid = max_snp_asid - 1; + if (sev->last_snp_asid) { + sev->snp_asid_to_gctx_pages_map = devm_kmalloc_array( + sev->dev, max_snp_asid * 2, sizeof(u64), GFP_KERNEL | __GFP_ZERO); + sev->snp_unbound_gctx_pages = &sev->snp_asid_to_gctx_pages_map[max_snp_asid]; + if (!sev->snp_asid_to_gctx_pages_map) { + kfree(snp_range_list); + dev_err(sev->dev, + "SEV-SNP: snp_asid_to_gctx_pages_map memory allocation failed\n"); + return -ENOMEM; + } + } + return 0; +} + +#else + +static int sev_snp_platform_init_firmware_upload(struct sev_device *sev) { return 0; } +static void sev_snp_dev_init_firmware_upload(struct sev_device *sev) { } +static void sev_snp_destroy_firmware_upload(struct sev_device *sev) { } +static void snp_cmd_bookkeeping_locked(int cmd, struct sev_device *sev, void *data) { } +#endif + int sev_dev_init(struct psp_device *psp) { struct device *dev = psp->dev; @@ -2265,6 +2644,8 @@ int sev_dev_init(struct psp_device *psp) if (ret) goto e_irq; + sev_snp_dev_init_firmware_upload(sev); + dev_notice(dev, "sev enabled\n"); return 0; @@ -2343,6 +2724,8 @@ void sev_dev_destroy(struct psp_device *psp) kref_put(&misc_dev->refcount, sev_exit); psp_clear_sev_irq_handler(psp); + + sev_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 3e4e5574e88a3..d6280688675d9 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -57,6 +57,20 @@ struct sev_device { bool cmd_buf_backup_active; bool snp_initialized; + +#ifdef CONFIG_FW_UPLOAD + /* Lock to protect fw_cancel */ + struct mutex fw_lock; + struct fw_upload *fwl; + bool fw_cancel; + + /* Firmware update management. */ + u32 last_snp_asid; + bool synthetic_restore_required; + u64 *snp_asid_to_gctx_pages_map; + u64 *snp_unbound_gctx_pages; + u32 snp_unbound_gctx_end; +#endif /* CONFIG_FW_UPLOAD */ }; int sev_dev_init(struct psp_device *psp); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 903ddfea85850..ff7f9e33f3335 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -16,6 +16,15 @@ #define SEV_FW_BLOB_MAX_SIZE 0x4000 /* 16KB */ +/* + * 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 + /** * SEV platform state */ @@ -185,6 +194,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 + * @address: physical address of firmware image + * @len: len of the firmware image + * @commit: automatically commit the newly installed image + */ +struct sev_data_download_firmware_ex { + u32 cmdlen; /* In */ + u32 reserved; /* in */ + u64 address; /* In */ + u32 len; /* In */ + u32 commit:1; /* In */ + u32 reserved2:31; /* In */ +} __packed; + /** * struct sev_data_get_id - GET_ID command parameters * @@ -797,10 +823,13 @@ struct sev_data_snp_shutdown_ex { * @probe: True if this is being called as part of CCP module probe, which * will defer SEV_INIT/SEV_INIT_EX firmware initialization until needed * unless psp_init_on_probe module param is set + * @supports_download_firmware_ex: True if legacy SEV/SEV-ES guests are not + * being launched via KVM */ struct sev_platform_init_args { int error; bool probe; + bool supports_download_firmware_ex; }; /** diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 832c15d9155bd..936464d4f282a 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -80,6 +80,11 @@ typedef enum { SEV_RET_INVALID_PAGE_OWNER, SEV_RET_INVALID_PAGE_AEAD_OFLOW, SEV_RET_RMP_INIT_REQUIRED, + SEV_RET_BAD_SVN, + SEV_RET_BAD_VERSION, + SEV_RET_SHUTDOWN_REQUIRED, + SEV_RET_UPDATE_FAILED, + SEV_RET_RESTORE_REQUIRED, SEV_RET_MAX, } sev_ret_code; From patchwork Sat Nov 2 00:08:14 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: 13859834 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 D4BCF25777 for ; Sat, 2 Nov 2024 00:08:38 +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=1730506120; cv=none; b=uIFKGNPkLb4yDwUCBb9o2kW2/+NViD4odCpQ7P3pZUp5xkSoMr2a/mrO8KMGa6llTj1me1wFgtFenvqK9Q6SJsUOO4J0B//nUIjHdyJz3prGzHK3Ab8dO/0qyjlFsNPZYyOXoGhIWZQVFA/8+pmzWG1Oj2XIS5QlkMlx4irGSRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730506120; c=relaxed/simple; bh=qPFYuz9BrK2CIqbnqSJdveevoaimtQftTkcT6cnXqi0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nPRoClL4PgSnXHYLbPNtvXidi9it8BjVadmMlTx11Vv5ZKodwfp2cZFIthFxslnGEUSjcnkYZOfGzxZvNypUc+ngnjBmibj9Eu0Ui6H5n5166QJwrfec0p3jsSUMyzxqYc48PNKTs91zbnSzX2SizkQAFMenboWi+bTRkQzALew= 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=xB/o17Cm; 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="xB/o17Cm" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e3497c8eb0so33017677b3.0 for ; Fri, 01 Nov 2024 17:08:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730506118; x=1731110918; 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=OMflO0iOxZawpN5ZUgjewO0l15Uk7dsPv9bQCJvRKFM=; b=xB/o17CmLkl9MlPv+u2Pt1Kv7k8cFsrSz7uvxSI/Ig46CyXfWtz2tZjIuZY+OFLSUn wZJX6MLdakmt48fMizgvjtftRxzSeMVNwvYwCGFs2zXcv/1t3Q+mW4HK1afSH/cGB2vO 0zwbPgqgLufAA1hF0j7J7yrlHt9k41oDAPv/FoggkVVo9SzKm0TuO51jbgqtBWi6Uxzt t4KCWycz/84QaN3P2J+rVSFdxDqfQrSUPz1xW2smntnqU5m34fnY3Pq/aZHS9Rc8BAkW k/+3HOs0mw4XTADKMnicVHRis3s15aRclXG8cr/yEh+TdFV+DbSdPfSBbPrCJ9+dF8MD 50cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730506118; x=1731110918; 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=OMflO0iOxZawpN5ZUgjewO0l15Uk7dsPv9bQCJvRKFM=; b=T63VyZ3qza2IDuY57mBCNYszX7HQFYv69Vbv4r1WZdRUr4iSEK+6vebB421IXacxTs 5SiDH1a4mLwcHeS/d6QflA2CvOOeDbFT7T7STSdmACxDb9mf99BCdGYv8XraqXHfSWLJ fI3Fo7iQVVg9zjd0WyDMl7neAhAGAnraMzrbcdueONI24vsQSEobNVUvE1OaaYWBhUeP ucu2zGVeD4yGoZzGvPfh42u/3PbuAq11bXMsITRN8DLnlg+pa9ipEpx0Xo/0MCeWkRFJ 7L0FG9iwWNwBzZgZF+MLlCcVODzuFghpzBqDHyUb9KrKHU6ff7VyJcS60747aJ1YX+U7 +wgg== X-Gm-Message-State: AOJu0Yxa/2AcxhRJ3xK4SCefQanWIA8J9LbBosexUBeNeKTCHNGCSpOo 4/B+N0+PqP28sTBC5gNz+VDrsuMKozCMCEPNjTp97qKkotJarm0Flc2TfrJVmDPBxkhgwsUNryN 8BYCCZ6PEidb/M7KARfhvAQ8xnl1g62HcXMVu0qydukuW7C4pwpu4GicGs072ymjbqx0e3zxw76 TpFjTzg6/M2fBl+JQcSkQws08UlK2ifAHFY2du71AUODYqjvB5NArke16q1TF+8i6Rors= X-Google-Smtp-Source: AGHT+IGk0DQ4lZzrHSF/msR4LceTfpO9UC2r93MoAUGhrBvpkJQaqubi60bBsN9z/YYJPlV+RQpFn5dca8i1rnQL/g== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:36:e7b8:ac13:c9e8]) (user=dionnaglaze job=sendgmr) by 2002:a05:690c:6711:b0:6d9:d865:46c7 with SMTP id 00721157ae682-6ea642ec3camr940447b3.2.1730506117249; Fri, 01 Nov 2024 17:08:37 -0700 (PDT) Date: Sat, 2 Nov 2024 00:08:14 +0000 In-Reply-To: <20241102000818.2512612-1-dionnaglaze@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241102000818.2512612-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241102000818.2512612-5-dionnaglaze@google.com> Subject: [PATCH v3 4/4] 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: 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 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f6e96ec0a5caa..8d365e2e3c1b1 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -445,6 +445,8 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp, goto e_no_asid; init_args.probe = false; + init_args.supports_download_firmware_ex = + vm_type != KVM_X86_SEV_VM && vm_type != KVM_X86_SEV_ES_VM; ret = sev_platform_init(&init_args); if (ret) goto e_free;