Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6341741iog; Thu, 23 Jun 2022 17:18:11 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vW3jBObTmUo5yIMsxdnHCtK22b/E8pc141wLW3lF50LHqgu7YIHdtqJHeWMK3uzOTZM5hD X-Received: by 2002:aa7:cdd1:0:b0:435:750f:7c75 with SMTP id h17-20020aa7cdd1000000b00435750f7c75mr13941678edw.91.1656029890972; Thu, 23 Jun 2022 17:18:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656029890; cv=none; d=google.com; s=arc-20160816; b=No4BtzUW1n/G9UrbIFYeSIq23MYYgZi+vy/FUvHRDz1f6urFpkUAZANr8ggO74gCGw doN4s3r+puS733UlgYgZdV6yPo0yi2pNOtDhFhV4c1btWSwmhw/Oo2W+pcF9hbitbYF3 5VekmnlDsHE5DZ9iAsM8lj6yJAZBxF81dFzYUAKOp+pXMKSfGYRm6iw2Ow+55Wcnqyo0 seNvzp0jgUcfMY6dwCktsWh9FDwgqRZ7BPZebNChH6+M4XrlNen1+my0R+xR3WTQ6O2B TTV81CeUvn/NY/f0IZV34QhN/2vWgdjzyFq9uQwJE1dE79E/8v9ZLaP+RGAsBc4E+pu+ Hmzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=5ztn/U14y4+bLEVfHDiR279Ws4kSCGIatUDLg6ETdSA=; b=jzBcR8lawj4dhACnzfPJ2eby47iWSA5NYUIHWgwnNL+ljMd36hKtTB1+65OA2ubYJw N/BqYbGVcbR/utQUYzlvHHYz0riAuE/MD0wsLVLnHCYOICqP0pQ5sf6kZEwv6iAaFT4/ kyRew4gTFcBRPHfb888EZ9XARP3/q/xXVOjr0cJHDYQDm6NrmP5ireC+xxprV0K5GLEe nCyEPvKYGiHEPdabF7AcUWtv733uVUOE1M+9RUTmHPWDA1GzDbiMIjjcE8uNYUvyifI6 /+UO+FG3keJl5juYV0kLJAzAsv8b85dkMtkH8TdrnFONjVDRMj6VHPOEIoOX3yUzqMnk kwtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=sE1nWpmu; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hz3-20020a1709072ce300b00718cd33f793si801634ejc.158.2022.06.23.17.17.26; Thu, 23 Jun 2022 17:18:10 -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=@google.com header.s=20210112 header.b=sE1nWpmu; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230232AbiFXAGc (ORCPT + 99 others); Thu, 23 Jun 2022 20:06:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229475AbiFXAGa (ORCPT ); Thu, 23 Jun 2022 20:06:30 -0400 Received: from mail-oo1-xc34.google.com (mail-oo1-xc34.google.com [IPv6:2607:f8b0:4864:20::c34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A94C60F22 for ; Thu, 23 Jun 2022 17:06:28 -0700 (PDT) Received: by mail-oo1-xc34.google.com with SMTP id v33-20020a4a9764000000b0035f814bb06eso167428ooi.11 for ; Thu, 23 Jun 2022 17:06:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=5ztn/U14y4+bLEVfHDiR279Ws4kSCGIatUDLg6ETdSA=; b=sE1nWpmur5YxV9tq2Xcbc8zQ7qv68r4grAVVsiWE+Hb5IjzYTfE2kF71zHL669RRu1 8EOee7QUNQm5IwxycAlLVHbYxsj9/B6zjw6THdaB8U9bCQ04QFEyuYMUqXCJL/VwKgQw pERU0qG1+ubCmP0USVfbwqV9mQ7Ae/RYAmO3fT7BMk97N+H1MM15EKJoEsqjc0jqIM4J ZmZqKBi+d2WXbVUkR5ZsnAH9c+tnaqZuMUUSeMcuNV+zfEnu/lsMYmuSZtLU8S8UCtss yBdMMbxpNC6LgDmkRNH0oPPD4EO3hws1baml0AuV16zVe/kIfr0tuYlgKqK5lM8mR69W tMxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=5ztn/U14y4+bLEVfHDiR279Ws4kSCGIatUDLg6ETdSA=; b=okzruYhJpX5O1yKZzFygf0CCHIk6c+vGfKmVnkjXu7xAgz1RTaUdAi0NZfozltr0Le g9r5vpeLSr3KGI3ReAjM9oeXRwH2lFUoYPg/WuffqLyXiDZiY+l+mkHCTJPzfRdeJZQu YGjmUXURtNIGXQfKU+4Yg2DnYVW/SH7Z21QO7NMjKeGngfoh7HVQ8tQzd+toJBf2bBc5 LoxkIGeS6j5Uo0hFMxdRIS4OPptZkN1kLJ0XG+PYP1CuGnE407ddP29yaZd8od3aLNpz 6hKLvTmuPYnVF59lG5DoYuhwuYBsi8lCV2vQ7CBj33M0iUSzgUmgIDiKFav8dUaDY1ET mDTw== X-Gm-Message-State: AJIora905nZHVYXuzDgDTSUArZJvENolK1FYrv8ivtitVtmGQpzvJN0J /z/Jn9Jx42JNH3rMJA1k9VLBsx/vtsAzMKpPmNb5JQ== X-Received: by 2002:a4a:d842:0:b0:41b:c75d:f2dd with SMTP id g2-20020a4ad842000000b0041bc75df2ddmr4805823oov.20.1656029187423; Thu, 23 Jun 2022 17:06:27 -0700 (PDT) MIME-Version: 1.0 References: <243778c282cd55a554af9c11d2ecd3ff9ea6820f.1655761627.git.ashish.kalra@amd.com> In-Reply-To: <243778c282cd55a554af9c11d2ecd3ff9ea6820f.1655761627.git.ashish.kalra@amd.com> From: Marc Orr Date: Thu, 23 Jun 2022 17:06:16 -0700 Message-ID: Subject: Re: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table To: Ashish Kalra Cc: x86 , LKML , kvm list , linux-coco@lists.linux.dev, linux-mm@kvack.org, Linux Crypto Mailing List , Thomas Gleixner , Ingo Molnar , Joerg Roedel , "Lendacky, Thomas" , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , "Roth, Michael" , Vlastimil Babka , "Kirill A . Shutemov" , Andi Kleen , Tony Luck , Sathyanarayanan Kuppuswamy , Alper Gun , "Dr . David Alan Gilbert" , jarkko@kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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 On Mon, Jun 20, 2022 at 4:03 PM Ashish Kalra wrote: > > From: Brijesh Singh > > 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. nit: "RMP checks ..." -> "RMP-check ..." > > 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 nit: "invalid state ..." -> "invalidate state ..." nit: "... after its" -> "... after they're" (Here, and in the patch subject too.) > added in the RMP table, and restore to its default valid permission after nit: "... restore to its ..." -> "... restored to their ..." > the pages are removed from the RMP table. > > 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 f6c64a722e94..734cddd837f5 100644 > --- a/arch/x86/kernel/sev.c > +++ b/arch/x86/kernel/sev.c > @@ -2451,10 +2451,42 @@ int psmash(u64 pfn) > } > EXPORT_SYMBOL_GPL(psmash); > > +static int restore_direct_map(u64 pfn, int npages) > +{ > + 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; > + } > + > +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) I think we should rename this function to "invalidate_direct_map()". > +{ > + int i, ret = 0; > + > + for (i = 0; i < npages; i++) { > + ret = set_direct_map_invalid_noflush(pfn_to_page(pfn + i)); > + if (ret) > + goto cleanup; > + } > + > + return 0; > + > +cleanup: > + restore_direct_map(pfn, i); > + return ret; > +} > + > static int rmpupdate(u64 pfn, struct rmpupdate *val) > { > unsigned long paddr = pfn << PAGE_SHIFT; > - int ret; > + int ret, level, npages; > > if (!pfn_valid(pfn)) > return -EINVAL; > @@ -2462,11 +2494,38 @@ static int rmpupdate(u64 pfn, struct rmpupdate *val) > if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) > return -ENXIO; > > + level = RMP_TO_X86_PG_LEVEL(val->pagesize); > + npages = page_level_size(level) / PAGE_SIZE; > + > + /* > + * If page is getting assigned in the RMP table then unmap it from the > + * direct map. > + */ > + if (val->assigned) { > + if (invalid_direct_map(pfn, npages)) { > + pr_err("Failed to unmap pfn 0x%llx pages %d from direct_map\n", > + pfn, npages); > + return -EFAULT; > + } > + } > + > /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ > asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" > : "=a"(ret) > : "a"(paddr), "c"((unsigned long)val) > : "memory", "cc"); > + > + /* > + * Restore the direct map after the page is removed from the RMP table. > + */ > + if (!ret && !val->assigned) { > + if (restore_direct_map(pfn, npages)) { > + pr_err("Failed to map pfn 0x%llx pages %d in direct_map\n", > + pfn, npages); > + return -EFAULT; > + } > + } > + > return ret; > } > > -- > 2.25.1 > >