From patchwork Tue Jun 6 19:42:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 104098 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3636094vqr; Tue, 6 Jun 2023 12:59:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6rYREl+mLXtL7fIcQywFmXtJe1ymuojuycpekJyvfO0DA3/mEXDfj246XE+R/y8AH2C+tQ X-Received: by 2002:ac8:4e81:0:b0:3f1:381d:1067 with SMTP id 1-20020ac84e81000000b003f1381d1067mr1040066qtp.42.1686081589619; Tue, 06 Jun 2023 12:59:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686081589; cv=none; d=google.com; s=arc-20160816; b=fASgz5BCyqdQWDITCYG4g8fnM700jdqCeQ/nomey/zgXxn0y91ysZdv8tDDaA5jz24 90owzPX+Lob3axSbbDHR2MJFx4ewwUl/kd3boI2ALsuxWTfgKtFU9HZJJDUL+NmTQYYP 54+3WzGCRFdp0mQIISwYyHbiYZgLDs2P72vUeDVBLJYPm0E5ZvdMS4fhAWqOMsz2K57Y slVvJNCgfaQDhiqKZhWQuC9y8IKZLDMYM82J0B6p4FrqWaxMHTj5NWNtUzmizNtabpPw +PWab4/QE9+9dp0dZ0E8Fwx7XlYMm5mU5mR1cVDT+23eaDnBJxERuk7hRw8PhzduJli5 9wJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=T7/W+4ZHq/x1PNfpUKggwQg6LAins4+Gqe9NvzwsLNo=; b=t4v9r81Q1zTuGAQwBKlpU38uTD75a5YcOwnLz/0HQOWL4Xpzrh1gpW/fB1QfKwOM1J f7OH3oX+080cRdYixUqFuwvYoh6AbDLeIHM0SZjqvP6bOmJHJXk+s7ZuswZGss29lDdY yXzfxb+XgrA9LE+lhwl17iYHjN3SPpqp8CMvYZ4SaL6S7N1MCxBJn/XF0VEScpk3vTnl B5u+oyxdlyWmOtMsz0jVS+ehG8eU6GjNMXsRm20BRTXZA4/gFgqyIwhl7Ut4B0lTvPIc 1EQuDpryROlrApj2nivXAenqhZhdlthlROr2O/bDeJ4VGRMAfK06k0F7SbfsQpKHEYPL 0GwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Ayf3QJwV; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=xjedqBdc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v8-20020a05622a014800b003f51f102508si6908117qtw.32.2023.06.06.12.59.34; Tue, 06 Jun 2023 12:59:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Ayf3QJwV; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=xjedqBdc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239421AbjFFTnJ (ORCPT + 99 others); Tue, 6 Jun 2023 15:43:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239168AbjFFTml (ORCPT ); Tue, 6 Jun 2023 15:42:41 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19E4010FD; Tue, 6 Jun 2023 12:42:39 -0700 (PDT) Date: Tue, 06 Jun 2023 19:42:37 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686080557; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T7/W+4ZHq/x1PNfpUKggwQg6LAins4+Gqe9NvzwsLNo=; b=Ayf3QJwVRs8A+Wj2+xoW9tQwr2ksFYsWGibzKKpXLJS7Etd9aXhsedxVyo53Xhbtvuuzlq i8EnTVsiPKmw9CMwEO3lWcR7JvP0ONpKVKmjJQVXMWvLIOfOvvmDaUAkUOGSxwFSGydTvP VEsPqoBybx4aM7OQ7SqhDjruJ7nn7uHByfsDt4JRXD3gt3o0lC7MzH+XUMFl/OZPrKo8hj u1EwHirOnZQd4++achj99WfKtqy5/2xHhQNv38D0orWxZ9wcQwyvanFgFxz4b6+CGA5czO fXs0G//neT6jHN07Yvsruvpf3Y+MX2P/35l2r1FzGXdVfeukQKYkKahXQFNKBw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686080557; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T7/W+4ZHq/x1PNfpUKggwQg6LAins4+Gqe9NvzwsLNo=; b=xjedqBdcL6UffW8EaDJeBAqy1oi1itR5uo+4lGNQDazfwFUav1yZlv+6RpMo/BbAjUYmzV bAm2nER+Yubav5CA== From: "tip-bot2 for Tom Lendacky" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/cc] x86/sev: Put PSC struct on the stack in prep for unaccepted memory support Cc: Tom Lendacky , "Borislav Petkov (AMD)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: =?utf-8?q?=3C658c455c40e8950cb046dd885dd19dc1c52d060a=2E1659103?= =?utf-8?q?274=2Egit=2Ethomas=2Elendacky=40amd=2Ecom=3E?= References: =?utf-8?q?=3C658c455c40e8950cb046dd885dd19dc1c52d060a=2E16591032?= =?utf-8?q?74=2Egit=2Ethomas=2Elendacky=40amd=2Ecom=3E?= MIME-Version: 1.0 Message-ID: <168608055720.404.17339235955428446345.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767984689021609371?= X-GMAIL-MSGID: =?utf-8?q?1767984689021609371?= The following commit has been merged into the x86/cc branch of tip: Commit-ID: 69dcb1e3bbbe7fe092ea7af70e295e43ab0c7bc7 Gitweb: https://git.kernel.org/tip/69dcb1e3bbbe7fe092ea7af70e295e43ab0c7bc7 Author: Tom Lendacky AuthorDate: Tue, 06 Jun 2023 09:51:23 -05:00 Committer: Borislav Petkov (AMD) CommitterDate: Tue, 06 Jun 2023 18:28:25 +02:00 x86/sev: Put PSC struct on the stack in prep for unaccepted memory support In advance of providing support for unaccepted memory, switch from using kmalloc() for allocating the Page State Change (PSC) structure to using a local variable that lives on the stack. This is needed to avoid a possible recursive call into set_pages_state() if the kmalloc() call requires (more) memory to be accepted, which would result in a hang. The current size of the PSC struct is 2,032 bytes. To make the struct more stack friendly, reduce the number of PSC entries from 253 down to 64, resulting in a size of 520 bytes. This is a nice compromise on struct size and total PSC requests while still allowing parallel PSC operations across vCPUs. If the reduction in PSC entries results in any kind of performance issue (that is not seen at the moment), use of a larger static PSC struct, with fallback to the smaller stack version, can be investigated. For more background info on this decision, see the subthread in the Link: tag below. Signed-off-by: Tom Lendacky Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/lkml/658c455c40e8950cb046dd885dd19dc1c52d060a.1659103274.git.thomas.lendacky@amd.com --- arch/x86/include/asm/sev-common.h | 9 +++++++-- arch/x86/kernel/sev.c | 10 ++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 0759af9..b463fcb 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -106,8 +106,13 @@ enum psc_op { #define GHCB_HV_FT_SNP BIT_ULL(0) #define GHCB_HV_FT_SNP_AP_CREATION BIT_ULL(1) -/* SNP Page State Change NAE event */ -#define VMGEXIT_PSC_MAX_ENTRY 253 +/* + * SNP Page State Change NAE event + * The VMGEXIT_PSC_MAX_ENTRY determines the size of the PSC structure, which + * is a local stack variable in set_pages_state(). Do not increase this value + * without evaluating the impact to stack usage. + */ +#define VMGEXIT_PSC_MAX_ENTRY 64 struct psc_hdr { u16 cur_entry; diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 108bbae..7b0144a 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -882,11 +882,7 @@ static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, static void set_pages_state(unsigned long vaddr, unsigned long npages, int op) { unsigned long vaddr_end, next_vaddr; - struct snp_psc_desc *desc; - - desc = kmalloc(sizeof(*desc), GFP_KERNEL_ACCOUNT); - if (!desc) - panic("SNP: failed to allocate memory for PSC descriptor\n"); + struct snp_psc_desc desc; vaddr = vaddr & PAGE_MASK; vaddr_end = vaddr + (npages << PAGE_SHIFT); @@ -896,12 +892,10 @@ static void set_pages_state(unsigned long vaddr, unsigned long npages, int op) next_vaddr = min_t(unsigned long, vaddr_end, (VMGEXIT_PSC_MAX_ENTRY * PAGE_SIZE) + vaddr); - __set_pages_state(desc, vaddr, next_vaddr, op); + __set_pages_state(&desc, vaddr, next_vaddr, op); vaddr = next_vaddr; } - - kfree(desc); } void snp_set_memory_shared(unsigned long vaddr, unsigned long npages)