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 X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC2A4C432BE for ; Tue, 10 Aug 2021 17:50:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6F86261075 for ; Tue, 10 Aug 2021 17:50:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6F86261075 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id B45088D0003; Tue, 10 Aug 2021 13:50:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AF5738D0001; Tue, 10 Aug 2021 13:50:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9BC938D0003; Tue, 10 Aug 2021 13:50:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0003.hostedemail.com [216.40.44.3]) by kanga.kvack.org (Postfix) with ESMTP id 7A1CB8D0001 for ; Tue, 10 Aug 2021 13:50:39 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 31F7D182EDF4A for ; Tue, 10 Aug 2021 17:50:39 +0000 (UTC) X-FDA: 78459910998.11.2941446 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by imf19.hostedemail.com (Postfix) with ESMTP id 468B8B00244A for ; Tue, 10 Aug 2021 17:50:38 +0000 (UTC) X-IronPort-AV: E=McAfee;i="6200,9189,10072"; a="202125630" X-IronPort-AV: E=Sophos;i="5.84,310,1620716400"; d="scan'208";a="202125630" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Aug 2021 10:50:36 -0700 X-IronPort-AV: E=Sophos;i="5.84,310,1620716400"; d="scan'208";a="503213388" Received: from chdubay-mobl1.amr.corp.intel.com (HELO [10.212.234.193]) ([10.212.234.193]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Aug 2021 10:50:36 -0700 Subject: Re: [PATCH 2/5] efi/x86: Implement support for unaccepted memory To: "Kirill A. Shutemov" , Borislav Petkov , Andy Lutomirski , Sean Christopherson , Andrew Morton , Joerg Roedel Cc: Andi Kleen , Kuppuswamy Sathyanarayanan , David Rientjes , Vlastimil Babka , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Paolo Bonzini , Ingo Molnar , Varad Gautam , Dario Faggioli , x86@kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" References: <20210810062626.1012-1-kirill.shutemov@linux.intel.com> <20210810062626.1012-3-kirill.shutemov@linux.intel.com> From: Dave Hansen Autocrypt: addr=dave.hansen@intel.com; keydata= xsFNBE6HMP0BEADIMA3XYkQfF3dwHlj58Yjsc4E5y5G67cfbt8dvaUq2fx1lR0K9h1bOI6fC oAiUXvGAOxPDsB/P6UEOISPpLl5IuYsSwAeZGkdQ5g6m1xq7AlDJQZddhr/1DC/nMVa/2BoY 2UnKuZuSBu7lgOE193+7Uks3416N2hTkyKUSNkduyoZ9F5twiBhxPJwPtn/wnch6n5RsoXsb ygOEDxLEsSk/7eyFycjE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PPtaDzvKA4S5tfrWPnDr7n15AU5vsIZvgMjU42gkbemkjJwP0B1RkifIK60yQqAAlT YZ14P0dIPdIPIlfEPiAWcg5BtLQU4Wg3cNQdpWrCJ1E3m/RIlXy/2Y3YOVVohfSy+4kvvYU3 lXUdPb04UPw4VWwjcVZPg7cgR7Izion61bGHqVqURgSALt2yvHl7cr68NYoFkzbNsGsye9ft M9ozM23JSgMkRylPSXTeh5JIK9pz2+etco3AfLCKtaRVysjvpysukmWMTrx8QnI5Nn5MOlJj 1Ov4/50JY9pXzgIDVSrgy6LYSMc4vKZ3QfCY7ipLRORyalFDF3j5AGCMRENJjHPD6O7bl3Xo 4DzMID+8eucbXxKiNEbs21IqBZbbKdY1GkcEGTE7AnkA3Y6YB7I/j9mQ3hCgm5muJuhM/2Fr OPsw5tV/LmQ5GXH0JQ/TZXWygyRFyyI2FqNTx4WHqUn3yFj8rwTAU1tluRUYyeLy0ayUlKBH ybj0N71vWO936MqP6haFERzuPAIpxj2ezwu0xb1GjTk4ynna6h5GjnKgdfOWoRtoWndMZxbA z5cecg== Message-ID: <142bccc6-0e67-dfc1-9069-b773c2bad585@intel.com> Date: Tue, 10 Aug 2021 10:50:33 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20210810062626.1012-3-kirill.shutemov@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf19.hostedemail.com: domain of dave.hansen@intel.com has no SPF policy when checking 134.134.136.126) smtp.mailfrom=dave.hansen@intel.com X-Stat-Signature: 7qc74bd84a8bjo3gtdukh36s51xg9q48 X-Rspamd-Queue-Id: 468B8B00244A X-Rspamd-Server: rspam01 X-HE-Tag: 1628617838-891690 Content-Transfer-Encoding: quoted-printable 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: ... > +void mark_unaccepted(struct boot_params *params, u64 start, u64 num) > +{ Some of these interfaces like accept_memory() take a start/end physical address. Having this take a "num pages" is bound to cause confusion. Could you make these all consistently take start/end physical addresses? > + u64 end =3D start + num * PAGE_SIZE; > + unsigned int npages; Could you comment those, please? /* * The accepted memory bitmap only works at PMD_SIZE * granularity. If a request comes in to mark memory * as unaccepted which is not PMD_SIZE-aligned, simply * accept the memory now since it can not be *marked* as * unaccepted. */ Then go on to comment the three cases: /* Check for ranges which do not span a whole PMD_SIZE area: */ > + if ((start & PMD_MASK) =3D=3D (end & PMD_MASK)) { > + npages =3D (end - start) / PAGE_SIZE; > + __accept_memory(start, start + npages * PAGE_SIZE); > + return; > + } Hmm, is it possible to have this case hit, but neither of the two below cases? This seems to be looking for a case where the range is somehow entirely contained in one PMD_SIZE area, but where it doesn't consume a whole area. Wouldn't that mean that 'start' or 'end' must be unaligned? > + if (start & ~PMD_MASK) { > + npages =3D (round_up(start, PMD_SIZE) - start) / PAGE_SIZE; > + __accept_memory(start, start + npages * PAGE_SIZE); > + start =3D round_up(start, PMD_SIZE); > + } > + > + if (end & ~PMD_MASK) { > + npages =3D (end - round_down(end, PMD_SIZE)) / PAGE_SIZE; > + end =3D round_down(end, PMD_SIZE); > + __accept_memory(end, end + npages * PAGE_SIZE); > + } > + npages =3D (end - start) / PMD_SIZE; > + bitmap_set((unsigned long *)params->unaccepted_memory, > + start / PMD_SIZE, npages); > +} Even though it's changed right there, it's a bit cruel to change the units of 'npages' right in the middle of a function. It's just asking for bugs. It would only take a single extra variable declaration to make this unambiguous: u64 nr_unaccepted_bits; or something, then you can do: nr_unaccepted_bits =3D (end - start) / PMD_SIZE; bitmap_set((unsigned long *)params->unaccepted_memory, start / PMD_SIZE, nr_unaccepted_bits); ... > static efi_status_t allocate_e820(struct boot_params *params, > + struct efi_boot_memmap *map, > struct setup_data **e820ext, > u32 *e820ext_size) > { > - unsigned long map_size, desc_size, map_key; > efi_status_t status; > - __u32 nr_desc, desc_version; > - > - /* Only need the size of the mem map and size of each mem descriptor = */ > - map_size =3D 0; > - status =3D efi_bs_call(get_memory_map, &map_size, NULL, &map_key, > - &desc_size, &desc_version); > - if (status !=3D EFI_BUFFER_TOO_SMALL) > - return (status !=3D EFI_SUCCESS) ? status : EFI_UNSUPPORTED; I noticed that there's no reference to EFI_BUFFER_TOO_SMALL in the hunks you added back. That makes me a bit nervous that this is going to unintentionally change behavior. It might be worth having a preparatory reorganization patch for allocate_e820() before this new feature is added to make this more clear. > + __u32 nr_desc; > + bool unaccepted_memory_present =3D false; > + u64 max_addr =3D 0; > + int i; > =20 > - nr_desc =3D map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS; > + status =3D efi_get_memory_map(map); > + if (status !=3D EFI_SUCCESS) > + return status; > =20 > - if (nr_desc > ARRAY_SIZE(params->e820_table)) { > - u32 nr_e820ext =3D nr_desc - ARRAY_SIZE(params->e820_table); > + nr_desc =3D *map->map_size / *map->desc_size; > + if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOT= S) { > + u32 nr_e820ext =3D nr_desc - ARRAY_SIZE(params->e820_table) - > + EFI_MMAP_NR_SLACK_SLOTS; > =20 > status =3D alloc_e820ext(nr_e820ext, e820ext, e820ext_size); > if (status !=3D EFI_SUCCESS) > return status; > } > =20 > + if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) > + return EFI_SUCCESS; > + > + /* Check if there's any unaccepted memory and find the max address */ > + for (i =3D 0; i < nr_desc; i++) { > + efi_memory_desc_t *d; > + > + d =3D efi_early_memdesc_ptr(*map->map, *map->desc_size, i); > + if (d->type =3D=3D EFI_UNACCEPTED_MEMORY) > + unaccepted_memory_present =3D true; > + if (d->phys_addr + d->num_pages * PAGE_SIZE > max_addr) > + max_addr =3D d->phys_addr + d->num_pages * PAGE_SIZE; > + } This 'max_addr' variable looks a bit funky. It *seems* like it's related only to EFI_UNACCEPTED_MEMORY, but it's not underneath the EFI_UNACCEPTED_MEMORY check. Is this somehow assuming that once unaccepted memory as been found that *all* memory found in later descriptors at higher addresses is also going to be unaccepted? > + /* > + * If unaccepted memory present allocate a bitmap to track what memor= y > + * has to be accepted before access. > + * > + * One bit in the bitmap represents 2MiB in the address space: one 4k > + * page is enough to track 64GiB or physical address space. > + * > + * In the worst case scenario -- a huge hole in the middle of the > + * address space -- we would need 256MiB to handle 4PiB of the addres= s > + * space. > + * > + * TODO: handle situation if params->unaccepted_memory has already se= t. > + * It's required to deal with kexec. > + */ > + if (unaccepted_memory_present) { > + unsigned long *unaccepted_memory =3D NULL; > + u64 size =3D DIV_ROUND_UP(max_addr, PMD_SIZE * BITS_PER_BYTE); Oh, so the bitmap has to be present for *all* memory, not just unaccepted memory. So, we really do need to know the 'max_addr' so that we can allocate the bitmap for so that can be marked in the bitmap has having been accepted. > + status =3D efi_allocate_pages(size, > + (unsigned long *)&unaccepted_memory, > + ULONG_MAX); > + if (status !=3D EFI_SUCCESS) > + return status; > + memset(unaccepted_memory, 0, size); > + params->unaccepted_memory =3D (u64)unaccepted_memory; > + } It might be nice to refer to setup_e820() here to mention that it is the thing that actually fills out the bitmap.