From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6FC5CECAAD5 for ; Thu, 8 Sep 2022 07:46:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C67D76B0072; Thu, 8 Sep 2022 03:46:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C17A76B0073; Thu, 8 Sep 2022 03:46:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB8878D0002; Thu, 8 Sep 2022 03:46:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9A6A16B0072 for ; Thu, 8 Sep 2022 03:46:56 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2C5F1140C99 for ; Thu, 8 Sep 2022 07:46:56 +0000 (UTC) X-FDA: 79888136832.25.709ED0A Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf29.hostedemail.com (Postfix) with ESMTP id BF7B81200B5 for ; Thu, 8 Sep 2022 07:46:55 +0000 (UTC) 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 EABB961B91; Thu, 8 Sep 2022 07:46:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA39EC433D7; Thu, 8 Sep 2022 07:46:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662623214; bh=v1QudXHtVVOAARsA3M6UyAqD0An8z1adVccc10P8/gQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=FWngtO/uE682HqTHNbN3KFgqd5xKxn2B447PBrPJYFmWe5EGJtwZqPSh6J1SCmcCv 03I50d31V7Y6c0bKPXvJuFqm/uG8jQuFl1mGIP8XiVdmRuIh82ezF6WNLKtHrhHPv2 xya81c/641ta6kqbnlYnmsL0z3FX05jUXkCBW++tXcwDpcTI/UfJLyeLidwsIb4Ty2 Zel2EUJsdv6y583uXJSEECjJWAAtrACzrCvTCa1WXtsxzc2nzmrRA4ahgjO25rPARk PKgXmSZ7ga8Tw+njdKqpzqLJ1KFtx1gZf4ZbpPnW5yuYXvLcIqa/ERkhUb39oZY55N qtsC7PFbx0mvA== Date: Thu, 8 Sep 2022 10:46:48 +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: ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="FWngtO/u"; spf=pass (imf29.hostedemail.com: domain of jarkko@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=jarkko@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662623215; a=rsa-sha256; cv=none; b=xn2UXcAO1NqaQ/JtVfRwlTJWfKucOJ5uGKu0SYeZXqVuq6ZcNAG4YXBROCXdmZsJdRqZBj JFYPNp1/WunX1MryFWJtZsNwNwVcH9HffcCkhd6e6+5aEGV1tzTl6Ze9yzWr+uAuFQnCgl aB8DGR3F1FqFtC9T7PtnPUXm6e3X1sM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662623215; h=from:from:sender: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Z3WA5ch8qHzzrslKNamrR4HI5AY7p8r7SLrTJ8mQm7w=; b=nG9CAivbsnMZfVTdpiWan4PWcI/WG8q+spw4DCH9UCUeORa35ChIVLG/sUhEFHIUK3Ey0o XgXXH/LxU48utEW2VUb7gXWVJXwgEYxb2lNVxTe8cRC2XrEd6r4g06H1Axxmp3LzS3fQB5 mUF5VcA/4KGHvhpqyQTJcVZwFdVV3xI= X-Rspamd-Queue-Id: BF7B81200B5 X-Rspam-User: Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="FWngtO/u"; spf=pass (imf29.hostedemail.com: domain of jarkko@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=jarkko@kernel.org; dmarc=pass (policy=none) header.from=kernel.org X-Rspamd-Server: rspam04 X-Stat-Signature: 4i9w9st83nfpxz4mx1hct9rj7u4w5wi7 X-HE-Tag: 1662623215-579395 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. BR, Jarkko