Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp4581787rwb; Tue, 6 Sep 2022 09:26:00 -0700 (PDT) X-Google-Smtp-Source: AA6agR5V8xgkqsYbAu7g7e3OYOzdroMyw2ltKwBPrFiFgaUk8daUI035vrz7Q3gcmvk/jvnJBiq3 X-Received: by 2002:a05:6402:26c6:b0:448:658e:5b32 with SMTP id x6-20020a05640226c600b00448658e5b32mr37099191edd.250.1662481559863; Tue, 06 Sep 2022 09:25:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662481559; cv=none; d=google.com; s=arc-20160816; b=Zz6FAj6MqK1Cjj6RZbNudDNW1ycKyOqxbpyY9yoiGmh9NS8vj5fCDcAcQ26NCJEdFl POpScPsvY77Pg8EykeASMxQWgkEAO47UJNa6jr13ACxPiCaoRcQ3G4HekZLOWUbxKTCw PveVOwngCIR9gEioWVfOUuxJ4lObQVUanRXg2ZMDdaV9oycq19xJIowgm4pjp2VmtDn/ ky8kd+dMVw61K8oEFLa5rJZQ2E5ZYD9HP9zZ/I80D/mGxwvQtcixr5Hh7z6PqwYbR50v 4FcNsgpIUzHeiIAMBmafdb8z0yU/Ohp5v/GLmLr9yM+NKF3wIkpjinJIROwmFEEw2u8F YPTg== 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=zQccx06Vfk7ut5JI2oj7QuMqbu2wxYIOMiaqUGPW04k=; b=GCmI6MGK3qHUvOA09gCdTkOa0iFKZSYQiK3qOXfH7ZKsH2wRO+2arGlN+2fTXHok1+ kSDpotY/1zSPt1Tt5b4B/tfKnocQNOkw27WXti4l7/YteR0qXz1cs9FAqZIARQh+pKre XLy7rxpREkHpQbe7XnU6Qa6CsvpYW4/2zNH1ftYqMUR9M7yxIogKC6yar4tIy3IJyF0S oDmAPu5aNrylJ3b1YWSF+sBfcWta4SwoK8RKUishiXCOj6QhtsZMm540T9kmS8ae7CeC izgYv3kP76Kt5bsZqWiDFWhX+BAfpwU+ZjeEjmxnGik0XQ91IzL2UviL9NqVr6w9AxAY BqIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eiXfYf3T; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hr4-20020a1709073f8400b0073d9d374e81si12686336ejc.681.2022.09.06.09.25.25; Tue, 06 Sep 2022 09:25:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-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=@kernel.org header.s=k20201202 header.b=eiXfYf3T; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235392AbiIFQQv (ORCPT + 99 others); Tue, 6 Sep 2022 12:16:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235624AbiIFQQX (ORCPT ); Tue, 6 Sep 2022 12:16:23 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E1EDB488; Tue, 6 Sep 2022 08:44:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A59B5B818C2; Tue, 6 Sep 2022 15:44:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D0043C433C1; Tue, 6 Sep 2022 15:44:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662479069; bh=qFOk6Wp6NGa1Ej+qcHbPtJExBc58ed/Pmqdo5YRCPig=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eiXfYf3T/gGNUT6915A9D2Q7+AAUG7dPXv1inXwV2KRquc0Vpy4fQCmWfAWv6UIZy ul38C7L5QFDgOxcWixx9aHE1JYPngBP/l4hUN0nYK5LFncG4J7ZntKF5Q4nMN0bfvR 4T7yMDDVsh3onuYzC7zOwFIO2zOXeCkWo6afLSpKltK625RM14d5F5UJREXOnMEo/S 6gn768eCIJpQYeZJwrnIeha0S7hBYdm+NA5O2cVozNlrW4ugs06hKXsXLyu6VFwu3p b63o4MrKg0ZvzPEarvd5tTxFX1h/U36VOonPRQqiasczJtjjmLCqlt2XK4Dx5h90Aw 7zcaaSu+b5NiA== Date: Tue, 6 Sep 2022 18:44:23 +0300 From: Jarkko Sakkinen To: "Kalra, Ashish" Cc: Marc Orr , Borislav Petkov , x86 , LKML , kvm list , "linux-coco@lists.linux.dev" , Linux Memory Management List , 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 , "Roth, Michael" , Vlastimil Babka , "Kirill A . Shutemov" , Andi Kleen , Tony Luck , Sathyanarayanan Kuppuswamy , Alper Gun , "Dr . David Alan Gilbert" Subject: Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address Message-ID: References: <0ecb0a4781be933fcadeb56a85070818ef3566e7.1655761627.git.ashish.kalra@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-crypto@vger.kernel.org On Tue, Sep 06, 2022 at 02:17:15PM +0000, Kalra, Ashish wrote: > [AMD Official Use Only - General] > > >> On Tue, Aug 09, 2022 at 06:55:43PM +0200, Borislav Petkov wrote: > >> > On Mon, Jun 20, 2022 at 11:03:43PM +0000, Ashish Kalra wrote: > >> > > + pfn = pte_pfn(*pte); > >> > > + > >> > > + /* If its large page then calculte the fault pfn */ > >> > > + if (level > PG_LEVEL_4K) { > >> > > + unsigned long mask; > >> > > + > >> > > + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); > >> > > + pfn |= (address >> PAGE_SHIFT) & mask; > >> > > >> > Oh boy, this is unnecessarily complicated. Isn't this > >> > > >> > pfn |= pud_index(address); > >> > > >> > or > >> > pfn |= pmd_index(address); > >> > >> I played with this a bit and ended up with > >> > >> pfn = pte_pfn(*pte) | PFN_DOWN(address & page_level_mask(level > >> - 1)); > >> > >> Unless I got something terribly wrong, this should do the same (see > >> the attached patch) as the existing calculations. > > >Actually, I don't think they're the same. I think Jarkko's version is correct. Specifically: > >- For level = PG_LEVEL_2M they're the same. > >- For level = PG_LEVEL_1G: > >The current code calculates a garbage mask: > >mask = pages_per_hpage(level) - pages_per_hpage(level - 1); translates to: > >>> hex(262144 - 512) > >'0x3fe00' > > No actually this is not a garbage mask, as I explained in earlier responses we need to capture the address bits > to get to the correct 4K index into the RMP table. > Therefore, for level = PG_LEVEL_1G: > mask = pages_per_hpage(level) - pages_per_hpage(level - 1) => 0x3fe00 (which is the correct mask). > > >But I believe Jarkko's version calculates the correct mask (below), incorporating all 18 offset bits into the 1G page. > >>> hex(262144 -1) > >'0x3ffff' > > We can get this simply by doing (page_per_hpage(level)-1), but as I mentioned above this is not what we need. I think you're correct, so I'll retry: (address / PAGE_SIZE) & (pages_per_hpage(level) - pages_per_hpage(level - 1)) = (address / PAGE_SIZE) & ((page_level_size(level) / PAGE_SIZE) - (page_level_size(level - 1) / PAGE_SIZE)) = [ factor out 1 / PAGE_SIZE ] (address & (page_level_size(level) - page_level_size(level - 1))) / PAGE_SIZE = [ Substitute with PFN_DOWN() ] PFN_DOWN(address & (page_level_size(level) - page_level_size(level - 1))) So you can just: pfn = pte_pfn(*pte) | PFN_DOWN(address & (page_level_size(level) - page_level_size(level - 1))); Which is IMHO way better still what it is now because no branching and no ad-hoc helpers (the current is essentially just page_level_size wrapper). BR, Jarkko