Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp683940pxb; Wed, 29 Sep 2021 07:38:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfHxGHwXYquvFGpzBDupr7w3fqjIXYefksXEVy/LWwGuUfBYUVTw1qJai4hcJfc0rU3E8o X-Received: by 2002:a17:902:6808:b0:13e:4fe8:8332 with SMTP id h8-20020a170902680800b0013e4fe88332mr11778plk.55.1632926290417; Wed, 29 Sep 2021 07:38:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632926290; cv=none; d=google.com; s=arc-20160816; b=vUeaVJUzDLs3spC8tmT4bs/ADwJI4xTYQw89kH5yphSN1ZYWo9wRfE0nZGs1j0qSmI DXdC51C59Kt98Lg2t7dG9cpHW7IBdBPCvn3KmvcE4+++s5bn6JmZNRCGZiIStw/KTtuA OKvBGLPW6VRYhB8G0S4cJ8B/Q0rMeinAToPPvhqqZeov0RGorZL/7srMHlJP+gvq/RN3 PYfgHNO5UXFZYY5XswKO1GjrM7GtvldcTWFZMTdb+QxakLOkunrSGXxNf2y86/7uD2n8 jD5L+Lxf7j9fll4X8+5AhTKPMAFqJ3BdFLLEFxdZD12QOnOVFLIlPEic/cx6cCOSGpWp Bn2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=QEjtvugqBhl7avNSjLLykeMqMn1pT0fAYHoaCtxwC5g=; b=wln8/rV0CFEp22WEkUPA4YqskhNtSsUWEDThX3SnrRXINZ7ZCpjYGXGV9cqYykd6EJ bOozD7YHGr1Fvro6JxKKN1QtQfX3DXRbzuZmUkMuh7VeITzs96sTgKjr4jx538cM+6QL +blw8xa2WwRS3CYHDbEN3cFmIfc7L0nvEyeFoYcGIQnNfEu3kIvEODwk3ctwRFk2i6r4 JOri37lhAjZrPRSln7cDk3/viBUBaNT7X5cnrPk2FxuoJFUXpj50Zvea55hNYHklSCV5 ef2sIZc5YfMJrlEupNrwNOkJAwOmdouYNquVqo+ZeB7hB6MvfsQVUcNykUdrK1Vs8tbX 2F/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=MJo8kK7E; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s9si3075014pfm.160.2021.09.29.07.37.48; Wed, 29 Sep 2021 07:38:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=MJo8kK7E; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245756AbhI2OgM (ORCPT + 99 others); Wed, 29 Sep 2021 10:36:12 -0400 Received: from mail.skyhub.de ([5.9.137.197]:52556 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245348AbhI2OgL (ORCPT ); Wed, 29 Sep 2021 10:36:11 -0400 Received: from zn.tnic (p200300ec2f0bd10038530ecc14d583ae.dip0.t-ipconnect.de [IPv6:2003:ec:2f0b:d100:3853:ecc:14d5:83ae]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 507141EC0814; Wed, 29 Sep 2021 16:34:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1632926069; h=from:from: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:in-reply-to:in-reply-to: references:references; bh=QEjtvugqBhl7avNSjLLykeMqMn1pT0fAYHoaCtxwC5g=; b=MJo8kK7EEisTu1gSO8tYOUFR16AEjm3UOf6KENNVp/6jifrPD0u/+Qt776B+Japu3N9iQD ZT6LtQ5jo1Mc2o/H3GZcZ6tCcc8PaER5x1Meb0gImR+qXsi1u2RomCSKiP3kydsb/BXhZR ZR/sGDuV/wVyW4a+UDkQ7NYR6prpmXk= Date: Wed, 29 Sep 2021 16:34:24 +0200 From: Borislav Petkov To: Brijesh Singh Cc: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , Dov Murik , Tobin Feldman-Fitzthum , Michael Roth , Vlastimil Babka , "Kirill A . Shutemov" , Andi Kleen , tony.luck@intel.com, marcorr@google.com, sathyanarayanan.kuppuswamy@linux.intel.com Subject: Re: [PATCH Part2 v5 06/45] x86/sev: Invalid pages from direct map when adding it to RMP table Message-ID: References: <20210820155918.7518-1-brijesh.singh@amd.com> <20210820155918.7518-7-brijesh.singh@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210820155918.7518-7-brijesh.singh@amd.com> Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Fri, Aug 20, 2021 at 10:58:39AM -0500, Brijesh Singh wrote: > Subject: Re: [PATCH Part2 v5 06/45] x86/sev: Invalid pages from direct map when adding it to RMP table That subject needs to have a verb. I think that verb should be "Invalidate". > The integrity guarantee of SEV-SNP is enforced through the RMP table. > The RMP is used with standard x86 and IOMMU page tables to enforce memory > restrictions and page access rights. The RMP check is enforced as soon as > SEV-SNP is enabled globally in the system. When hardware encounters an > RMP checks failure, it raises a page-fault exception. > > The rmp_make_private() and rmp_make_shared() helpers are used to add > or remove the pages from the RMP table. > Improve the rmp_make_private() to > invalid state so that pages cannot be used in the direct-map after its > added in the RMP table, and restore to its default valid permission after > the pages are removed from the RMP table. That sentence needs rewriting into proper english. The more important thing is, though, this doesn't talk about *why* you're doing this: you want to remove pages from the direct map when they're in the RMP table because something might modify the page and then the RMP check will fail? Also, set_direct_map_invalid_noflush() simply clears the Present and RW bit of a pte. So what's up? > Signed-off-by: Brijesh Singh > --- > arch/x86/kernel/sev.c | 61 ++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 60 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c > index 8627c49666c9..bad41deb8335 100644 > --- a/arch/x86/kernel/sev.c > +++ b/arch/x86/kernel/sev.c > @@ -2441,10 +2441,42 @@ int psmash(u64 pfn) > } > EXPORT_SYMBOL_GPL(psmash); > > +static int restore_direct_map(u64 pfn, int npages) restore_pages_in_direct_map() > +{ > + int i, ret = 0; > + > + for (i = 0; i < npages; i++) { > + ret = set_direct_map_default_noflush(pfn_to_page(pfn + i)); > + if (ret) > + goto cleanup; > + } So this is looping over a set of virtually contiguous pages, I presume, and if so, you should add a function called set_memory_p_rw() to arch/x86/mm/pat/set_memory.c which does return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT | _PAGE_RW), 0); so that you can do all pages in one go. > + > +cleanup: > + WARN(ret > 0, "Failed to restore direct map for pfn 0x%llx\n", pfn + i); > + return ret; > +} > + > +static int invalid_direct_map(unsigned long pfn, int npages) invalidate_pages_in_direct_map() or so. > +{ > + int i, ret = 0; > + > + for (i = 0; i < npages; i++) { > + ret = set_direct_map_invalid_noflush(pfn_to_page(pfn + i)); Same as above but that helper should do the reverse: set_memory_np_ro() { return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT | _PAGE_RW), 0); } Btw, please add those helpers in a separate patch. Thx. -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette