Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp418989rdb; Mon, 29 Jan 2024 06:26:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IGopy3WceACYAzycja/W7drYBBEim6TvLirIY+wgdtBO6lJ0zUih5w4eLTI33652smbB0zR X-Received: by 2002:a05:6359:1981:b0:178:7701:2860 with SMTP id mh1-20020a056359198100b0017877012860mr979871rwb.14.1706538399395; Mon, 29 Jan 2024 06:26:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706538399; cv=pass; d=google.com; s=arc-20160816; b=Xbao/eJgWvpgx571GKPgHfCTe6OukXR6rSfdf7l1CwYY6/Tlgwmyaas7YXJv8Bb34n 0W5DGcFouph/j5ehOZyEJncAYOxRvVG2VMr7CTO0joRRfvoxZZf0C46DYQsE0fYh4/1V X5nD93gFNhWccXYmXPsXoJ3c6Yc/V2ZJGnFZq0Qubsf4WKo2v2BP6omXiSVQXxFXaUqt 2bcgwUscNpZ4T11yWvFJX0tzm2xEn16JegDz4166hXe1YjqqUKmtLVvtIVfblM5sJzGl XWmR7V+0YUTL3VAymRC9dyduR0HMe7IvSam/zXnm9I/juex4klurMMrXSuTsv9KLKFtJ DmVA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature :dkim-signature:dkim-signature:dkim-signature; bh=yVCErth3fJSXbSPYKO931WJVaP5CdJG9Rs6XjxdlFvY=; fh=3LZU+wU5dN6rylb9HFkRe5To/4m7O4dgyBOqxtX4NMU=; b=jg2Wsmbc+qAQHGVpvLRIsKWzetVPjQ5Qs1iVnNkxsS5b6hFYwBGrjFx+OaCHiVS3A2 R8c7j7wolI3EoKeEAMXA9fg/VRghAUJ0VtIiBz9fggVV7W1HAO64ZYhsy9U1aFfF8bn0 4Oz5v0yubPps1a92WB2Rfj0MoxZTQaYUrVMVrIxIl2TXXbUY2SYTzmueM/a088ByogW/ xilZ2xxZ/CZp0/cyGWvUnhm7XfmqeS36FyoHoT4a7OzD39dxulkcRPUTYXLOuBzNbxgr jd+8Pafpvkzn7EAymsPU4euOkgjKBrhOJIMTCAj97sHYt9RBFXUJUarEpCbn2fTXpUxA jXhg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=MoS1gs2H; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=gfO7Mc1E; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.cz dkim=pass dkdomain=suse.cz dkim=pass dkdomain=suse.cz); spf=pass (google.com: domain of linux-crypto+bounces-1736-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-1736-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id k186-20020a6384c3000000b005d7a12d668fsi5744564pgd.62.2024.01.29.06.26.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 06:26:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto+bounces-1736-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=MoS1gs2H; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=gfO7Mc1E; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.cz dkim=pass dkdomain=suse.cz dkim=pass dkdomain=suse.cz); spf=pass (google.com: domain of linux-crypto+bounces-1736-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-1736-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id B3C7E284224 for ; Mon, 29 Jan 2024 14:26:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B65E065BC8; Mon, 29 Jan 2024 14:26:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="MoS1gs2H"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="lWxaA4vI"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="gfO7Mc1E"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="sd1TWSyq" X-Original-To: linux-crypto@vger.kernel.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (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 B1DA865BAC; Mon, 29 Jan 2024 14:26:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706538395; cv=none; b=baj6qki1xC561MRX1J086jafdrkRFfE8nHxxfFShHsLLsD1anAgXcHukfc2+IpWEoFT5HSg6rn3otQTTtvy5DIbNU5o7xSbFtnFZPqH93MGVzKTaCkj7ETlk7/XUzeqDfGk7blp9yYQXYrTxZ7umGO0RGme6+p2X0BTHWoBBcgs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706538395; c=relaxed/simple; bh=1CWG8fGDTE0PFV2DRxwsn6+363/lO1dP/IeyyUt0PoY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=rsGd+iSf8nacC2mm6Ysrdp/iF6szX4yypq4LQGADPqYnzY2CoNEAsjwG5Of0myJrVS2pMy74xwsG54edAuCuyUY0CZju19mNxNdbvPt3hJhfTNDSCBNnK7Ym2GdMNvRxpgG5VgoVq8LiXgR/rySXq4p0qPVN73nRgFR8lLxSYOo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=MoS1gs2H; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=lWxaA4vI; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=gfO7Mc1E; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=sd1TWSyq; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id E012C21BF5; Mon, 29 Jan 2024 14:26:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1706538391; h=from:from:reply-to: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=yVCErth3fJSXbSPYKO931WJVaP5CdJG9Rs6XjxdlFvY=; b=MoS1gs2Hl4EgJ5NL6qalPr5In9E86rdnlvIznU6e6uzBepu+P+a0IA3lv84Ad2PzWvlMyQ hxoXkyxOG2VPmgjftKtmq1GlPP379m4z03COsx7ohuQr/BlvK5JSkoDNk7WLGBSbAbNLFw 5KJ009zaCwYLAYNjS9CHarYnvFpB7FI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1706538391; h=from:from:reply-to: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=yVCErth3fJSXbSPYKO931WJVaP5CdJG9Rs6XjxdlFvY=; b=lWxaA4vI45WlLR5SVl+jZF3+l5yuFu9OW9bU6bJGmfamA8bxRT4JWrN5/sgvElyJ5cHzV1 wUM/aMXPmstFo6CQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1706538389; h=from:from:reply-to: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=yVCErth3fJSXbSPYKO931WJVaP5CdJG9Rs6XjxdlFvY=; b=gfO7Mc1E1PiMODYu+5MLfCH3iGbrIB26IpBO4ToDdMgglzyXCPJCUV6j/pBJwIh28EHLOA vso3+m3dnRk3TNOJ/iAPbh9RFv8O4T1LKt1VC0+uiF+y+P1peMVG41CIi4juS3SKyDiOhf Hgrn5fDKGnsuNp7qAdgKdQBVKVSG3mQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1706538389; h=from:from:reply-to: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=yVCErth3fJSXbSPYKO931WJVaP5CdJG9Rs6XjxdlFvY=; b=sd1TWSyqZeSLloNag5rRyzE3DDd9JvMRR0bf2I1EfXwV4odzaQaVunxZ/10QQB7zUqffYs u7U2ysU9swvXdECA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 9B73012FF7; Mon, 29 Jan 2024 14:26:29 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id BmKbJZW1t2WqNQAAD6G6ig (envelope-from ); Mon, 29 Jan 2024 14:26:29 +0000 Message-ID: <1cc76023-ef3e-4639-9a02-644c5abe918d@suse.cz> Date: Mon, 29 Jan 2024 15:26:29 +0100 Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 15/25] x86/sev: Introduce snp leaked pages list Content-Language: en-US To: Michael Roth , x86@kernel.org Cc: kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, jroedel@suse.de, thomas.lendacky@amd.com, hpa@zytor.com, ardb@kernel.org, pbonzini@redhat.com, seanjc@google.com, vkuznets@redhat.com, jmattson@google.com, luto@kernel.org, dave.hansen@linux.intel.com, slp@redhat.com, pgonda@google.com, peterz@infradead.org, srinivas.pandruvada@linux.intel.com, rientjes@google.com, tobin@ibm.com, bp@alien8.de, kirill@shutemov.name, ak@linux.intel.com, tony.luck@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, alpergun@google.com, jarkko@kernel.org, ashish.kalra@amd.com, nikunj.dadhania@amd.com, pankaj.gupta@amd.com, liam.merwick@oracle.com References: <20240126041126.1927228-1-michael.roth@amd.com> <20240126041126.1927228-16-michael.roth@amd.com> From: Vlastimil Babka In-Reply-To: <20240126041126.1927228-16-michael.roth@amd.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Authentication-Results: smtp-out1.suse.de; none X-Spamd-Result: default: False [-3.09 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; XM_UA_NO_VERSION(0.01)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%]; MIME_GOOD(-0.10)[text/plain]; R_RATELIMIT(0.00)[to_ip_from(RL81e5qggtdx371s8ik49ru6xr)]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; RCPT_COUNT_TWELVE(0.00)[36]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; MID_RHS_MATCH_FROM(0.00)[] X-Spam-Level: X-Spam-Flag: NO X-Spam-Score: -3.09 On 1/26/24 05:11, Michael Roth wrote: > From: Ashish Kalra > > Pages are unsafe to be released back to the page-allocator, if they > have been transitioned to firmware/guest state and can't be reclaimed > or transitioned back to hypervisor/shared state. In this case add > them to an internal leaked pages list to ensure that they are not freed > or touched/accessed to cause fatal page faults. > > Suggested-by: Vlastimil Babka > Signed-off-by: Ashish Kalra > [mdr: relocate to arch/x86/virt/svm/sev.c] > Signed-off-by: Michael Roth Reviewed-by: Vlastimil Babka Some minor nitpicks: > --- > arch/x86/include/asm/sev.h | 2 ++ > arch/x86/virt/svm/sev.c | 34 ++++++++++++++++++++++++++++++++++ > 2 files changed, 36 insertions(+) > > diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h > index d3ccb7a0c7e9..435ba9bc4510 100644 > --- a/arch/x86/include/asm/sev.h > +++ b/arch/x86/include/asm/sev.h > @@ -264,6 +264,7 @@ void snp_dump_hva_rmpentry(unsigned long address); > int psmash(u64 pfn); > int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asid, bool immutable); > int rmp_make_shared(u64 pfn, enum pg_level level); > +void snp_leak_pages(u64 pfn, unsigned int npages); > #else > static inline bool snp_probe_rmptable_info(void) { return false; } > static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return -ENODEV; } > @@ -275,6 +276,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int as > return -ENODEV; > } > static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; } > +static inline void snp_leak_pages(u64 pfn, unsigned int npages) {} > #endif > > #endif > diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c > index 1a13eff78c9d..649ac1bb6b0e 100644 > --- a/arch/x86/virt/svm/sev.c > +++ b/arch/x86/virt/svm/sev.c > @@ -65,6 +65,11 @@ static u64 probed_rmp_base, probed_rmp_size; > static struct rmpentry *rmptable __ro_after_init; > static u64 rmptable_max_pfn __ro_after_init; > > +static LIST_HEAD(snp_leaked_pages_list); > +static DEFINE_SPINLOCK(snp_leaked_pages_list_lock); > + > +static unsigned long snp_nr_leaked_pages; > + > #undef pr_fmt > #define pr_fmt(fmt) "SEV-SNP: " fmt > > @@ -505,3 +510,32 @@ int rmp_make_shared(u64 pfn, enum pg_level level) > return rmpupdate(pfn, &state); > } > EXPORT_SYMBOL_GPL(rmp_make_shared); > + > +void snp_leak_pages(u64 pfn, unsigned int npages) > +{ > + struct page *page = pfn_to_page(pfn); > + > + pr_warn("Leaking PFN range 0x%llx-0x%llx\n", pfn, pfn + npages); > + > + spin_lock(&snp_leaked_pages_list_lock); > + while (npages--) { > + /* > + * Reuse the page's buddy list for chaining into the leaked > + * pages list. This page should not be on a free list currently > + * and is also unsafe to be added to a free list. > + */ > + if (likely(!PageCompound(page)) || > + (PageHead(page) && compound_nr(page) <= npages)) > + /* > + * Skip inserting tail pages of compound page as > + * page->buddy_list of tail pages is not usable. > + */ > + list_add_tail(&page->buddy_list, &snp_leaked_pages_list); Even though it's not necessary for correctness, with the comment I'd put the whole block into { } to make easier to follow. Or just move the comment above the if() itself? > + dump_rmpentry(pfn); > + snp_nr_leaked_pages++; > + pfn++; > + page++; > + } > + spin_unlock(&snp_leaked_pages_list_lock); > +} > +EXPORT_SYMBOL_GPL(snp_leak_pages);