Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp275023rwn; Thu, 8 Sep 2022 01:06:56 -0700 (PDT) X-Google-Smtp-Source: AA6agR6wDB0cMu2hJYx9e3/JxjShDvNqyeRsOPTyzKamnw78O2MHmqdLbgazDaCraeP89Q/GFedy X-Received: by 2002:a05:6402:50cb:b0:440:87d4:3ad2 with SMTP id h11-20020a05640250cb00b0044087d43ad2mr6060844edb.219.1662624415868; Thu, 08 Sep 2022 01:06:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662624415; cv=none; d=google.com; s=arc-20160816; b=xBcsAEG8uVgoGOsdMWU+Xy11zLTi02KM38NTx72K/o32d/Ke2R/HKM8DSkNQpDEKbm yUF03yeIacrzuhpTZi+iUS0/NgayOwkhkGQiYgNyBtyrHriOKfB2ZxacEZ9+1ulms25G lQxXKC15uuQTRpslmRiR5ju4MxbK+uLaij2JjVlDUQxb7ygkOO9AcS4uc0E1OybNDpy1 U6h4nd0O0+CsHupmNo5d2r103chHaDvBlmpvo5LajQyF2wrdAMg6vBFqVXOpwA61c+/h f+7u4ISUICMfsnkhKbUjZbuzPgPgdovut3iMTO9FesZ+iiAVCY/0VnkoSK3P97mYqpuo q/Lg== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=n9znYBSY84FGsSauEOpgPAe00IaKmt06cOWmXOzfJ3g=; b=X0wH1PW8XgKesigPYBwu7p7/qNHVCKaIhW71SibpTjj1gAusD2xkyDfAd4o6vJ20qh /QUHQtZPArHG77XuJmrlroBHyyiztCjc1oIbT7C/momsfETMfJBoi4RYl0GSAij/dzXE ecU3mBCVu0QgET0IBhGeiVkokZVHCx+6M/nWhcRjyfb/HEXnFXFofZojZK2KW6YwYP1q Z6AlQgAiyAvJplU5FwO3rD210MYYGEXDKAAALAos1zTDd1f1q054pmibHGzsRaZy1N02 yzrbTK0OT/BjlVmbzxa+DV6sWyNMVoGrNZHYT8E3hnapYLjWYB6ocnzrGlJHIftjLmkS hJXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="rPrrL/kC"; 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 sg10-20020a170907a40a00b0073dc0a15a74si1747586ejc.48.2022.09.08.01.06.26; Thu, 08 Sep 2022 01:06:55 -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="rPrrL/kC"; 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 S230109AbiIHH5w (ORCPT + 99 others); Thu, 8 Sep 2022 03:57:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230061AbiIHH5t (ORCPT ); Thu, 8 Sep 2022 03:57:49 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4295A96742; Thu, 8 Sep 2022 00:57:47 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 793E461BB0; Thu, 8 Sep 2022 07:57:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5B4A3C433D6; Thu, 8 Sep 2022 07:57:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662623866; bh=14RGdwf+G17sAtxszOYpeygph7MCRQUU6OID7/uHLEs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=rPrrL/kCuuWnPdI/Y2fRRysK/5YEA9EgtLZ8+whFBuVwMBS4ai28EmGc5qFMLZwlY jCabc715nObJb6976nr2r1J/pmu5q5DxoqJZQYxA4TAVxthDV5qGuExz00aAIrHlph ciE1tjg1Pc4jdrjJ3w2jf17oRFtOk4fWI4ISa7EA3wyW6Woh443tnUdHZvWelReYNS qdsCSzN0Dhn3pjRLB5LrSRtL/EpiC83jxD6oe+05AxtQqbnhfdHOpXqnP3qjJNvKKK XQrIXGmmx2TQipDNrEpSMTyFFDoV0vSEsvg+OxXPvJaBuvxhzRZpKaeDDqU+PdTelg zh/9kRH44pQEg== Date: Thu, 8 Sep 2022 10:57:40 +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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit 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 Thu, Sep 08, 2022 at 10:46:51AM +0300, Jarkko Sakkinen wrote: > On Tue, Sep 06, 2022 at 06:44:23PM +0300, Jarkko Sakkinen wrote: > > 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). > > I created a small test program: > > $ cat test.c > #include > int main(void) > { > unsigned long arr[] = {0x8, 0x1000, 0x200000, 0x40000000, 0x8000000000}; > int i; > > for (i = 1; i < sizeof(arr)/sizeof(unsigned long); i++) { > printf("%048b\n", arr[i] - arr[i - 1]); > printf("%048b\n", (arr[i] - 1) ^ (arr[i - 1] - 1)); > } > } > > kultaheltta in linux on  host-snp-v7 [?] > $ gcc -o test test.c > > kultaheltta in linux on  host-snp-v7 [?] > $ ./test > 000000000000000000000000000000000000111111111000 > 000000000000000000000000000000000000111111111000 > 000000000000000000000000000111111111000000000000 > 000000000000000000000000000111111111000000000000 > 000000000000000000111111111000000000000000000000 > 000000000000000000111111111000000000000000000000 > 000000000000000011000000000000000000000000000000 > 000000000000000011000000000000000000000000000000 > > So the operation could be described as: > > pfn = PFN_DOWN(address & (~page_level_mask(level) ^ ~page_level_mask(level - 1))); > > Which IMHO already documents itself quite well: index > with the granularity of PGD by removing bits used for > PGD's below it. I mean: pfn = pte_pfn(*pte) | PFN_DOWN(address & (~page_level_mask(level) ^ ~page_level_mask(level - 1))); Note that PG_LEVEL_4K check is unnecessary as the result will be zero after PFN_DOWN(). BR, Jarkko