Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6128345iog; Thu, 23 Jun 2022 11:57:36 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uGCw74g8fe8oHrdjgtKhwX1ybXuO6GzWwMxk/27ZELdKN3vazVUdiA4ATwZfhN6FnECrc/ X-Received: by 2002:a17:907:60d2:b0:722:fd02:d71b with SMTP id hv18-20020a17090760d200b00722fd02d71bmr5950990ejc.249.1656010656486; Thu, 23 Jun 2022 11:57:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656010656; cv=none; d=google.com; s=arc-20160816; b=NuQykXA93NusrYcdD9tQ15zrICCdDTZFrJe859hkOEXDq4LXoprGDRCfs6LjXeaDDk jnF3xWdR5T1m0jxT577b11iz6QWEZb1s6GAJXuESsHnqC+kSKPd16zKucGeA8lD/Q1cG Qw6C8Zq+VVs+ujyHO+t2VgnzRshnQMH5Vf4DDAd4IZ933mHSHRccoKwgVpWhsZCm5doD BKC8Il9vEgUXTqESzn7kYpmP4P3ZhHuojf39wQJjmTaPb6/wZXljfeSTHCVJ5jOGw45x 1EttT9CQi8akuPezoZSwfl6OpNYwq2vEm4eTy0A/mbO+0xImxFmvgIV3BwI7QolQze8R pZ4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=mzzNy0adUcf8fr35H3z/fwarh0PFobY9AM8JpI1jWj4=; b=cB1l6g3x8LE77VRWWsOHUGXn8ABbsSFLJCafzsP8Xtvcq7ORxJy15TyxMnxIeKIPhi 8a2dSdlxtB4kU6cs4mdT5qJQaarbpySpeVfHz/hgStUym2URrm2dBA69+lnqcCTEvKFs +V0u7hUYBia6HyHMcyiQeOaAWAIM2ynXvXdNS7OLJHkkIGk6luJCk9oFUItwW7G8EEUR ORXVTaUseOiqlwY6xE3BY0vdsEYZ9+BjmEQ+lD6WyxJaNWliAiPhAaRfclx4D4PLzOTs 51YacDI8GtevnpCiZFc4GtwdO57R1tGz8K71evZLNZAFfxaQPCFtybghgN+irMAWWtHI k4Kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fvj2i8HH; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z6-20020a17090665c600b0070772bba1c4si22689180ejn.477.2022.06.23.11.57.11; Thu, 23 Jun 2022 11:57:36 -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=@intel.com header.s=Intel header.b=fvj2i8HH; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236438AbiFWSK6 (ORCPT + 99 others); Thu, 23 Jun 2022 14:10:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236825AbiFWSIg (ORCPT ); Thu, 23 Jun 2022 14:08:36 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7872BC241; Thu, 23 Jun 2022 10:19:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656004792; x=1687540792; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=2yZfCAUS0MYfDCPoO0523LT8rgoWa8z9eTj07PaU7AU=; b=fvj2i8HHUPSz29/3GnfE5v8ftSUsieZLIAUfb41hqDI3V5EYHGYjpA4S 4HtVIxu866sBu4oaIWkgV1dc4tPN0mDrTLqSU/TajVOdynwkjD+Cy3r/N LR/J74CGUCYDv5a5FFzur6Ca3hc4dOJoAXTrL06gURnuDiuOP0Jfy2nRK sAe5dkVGGZpYKBHYq3BZdbY61I1pPitW08SiETXZdbleZ+YXYWqkhSs2O Yl/RI0LTJ+Iq4BIPZwAmCF9Hh+cX3JTuckQmOgrcuN/71E3dyrbgUxLNu Vbe9YojRs7BoZnLc1r2epUa2LrwyFir8KZrrHUsaINRW9HDC3AnhrO5M7 A==; X-IronPort-AV: E=McAfee;i="6400,9594,10387"; a="263818203" X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="263818203" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jun 2022 10:19:42 -0700 X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="563530605" Received: from ckeane-mobl1.amr.corp.intel.com (HELO [10.209.81.98]) ([10.209.81.98]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jun 2022 10:19:41 -0700 Message-ID: Date: Thu, 23 Jun 2022 10:19:15 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: [PATCHv7 10/14] x86/mm: Avoid load_unaligned_zeropad() stepping into unaccepted memory Content-Language: en-US To: "Kirill A. Shutemov" , Borislav Petkov , Andy Lutomirski , Sean Christopherson , Andrew Morton , Joerg Roedel , Ard Biesheuvel Cc: Andi Kleen , Kuppuswamy Sathyanarayanan , David Rientjes , Vlastimil Babka , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Paolo Bonzini , Ingo Molnar , Varad Gautam , Dario Faggioli , Mike Rapoport , David Hildenbrand , marcelo.cerri@canonical.com, tim.gardner@canonical.com, khalid.elmously@canonical.com, philip.cox@canonical.com, x86@kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org References: <20220614120231.48165-1-kirill.shutemov@linux.intel.com> <20220614120231.48165-11-kirill.shutemov@linux.intel.com> From: Dave Hansen In-Reply-To: <20220614120231.48165-11-kirill.shutemov@linux.intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, SPF_HELO_NONE,SPF_NONE,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-kernel@vger.kernel.org On 6/14/22 05:02, Kirill A. Shutemov wrote: > load_unaligned_zeropad() can lead to unwanted loads across page boundaries. > The unwanted loads are typically harmless. But, they might be made to > totally unrelated or even unmapped memory. load_unaligned_zeropad() > relies on exception fixup (#PF, #GP and now #VE) to recover from these > unwanted loads. > > But, this approach does not work for unaccepted memory. For TDX, a load > from unaccepted memory will not lead to a recoverable exception within > the guest. The guest will exit to the VMM where the only recourse is to > terminate the guest. > > There are three parts to fix this issue and comprehensively avoid access > to unaccepted memory. Together these ensure that an extra “guard” page > is accepted in addition to the memory that needs to be used. > > 1. Implicitly extend the range_contains_unaccepted_memory(start, end) > checks up to end+2M if ‘end’ is aligned on a 2M boundary. > 2. Implicitly extend accept_memory(start, end) to end+2M if ‘end’ is > aligned on a 2M boundary. > 3. Set PageUnaccepted() on both memory that itself needs to be accepted > *and* memory where the next page needs to be accepted. Essentially, > make PageUnaccepted(page) a marker for whether work needs to be done > to make ‘page’ usable. That work might include accepting pages in > addition to ‘page’ itself. ... That all looks pretty good. > diff --git a/arch/x86/mm/unaccepted_memory.c b/arch/x86/mm/unaccepted_memory.c > index 1df918b21469..bcd56fe82b9e 100644 > --- a/arch/x86/mm/unaccepted_memory.c > +++ b/arch/x86/mm/unaccepted_memory.c > @@ -23,6 +23,38 @@ void accept_memory(phys_addr_t start, phys_addr_t end) > bitmap = __va(boot_params.unaccepted_memory); > range_start = start / PMD_SIZE; > > + /* > + * load_unaligned_zeropad() can lead to unwanted loads across page > + * boundaries. The unwanted loads are typically harmless. But, they > + * might be made to totally unrelated or even unmapped memory. > + * load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now > + * #VE) to recover from these unwanted loads. > + * > + * But, this approach does not work for unaccepted memory. For TDX, a > + * load from unaccepted memory will not lead to a recoverable exception > + * within the guest. The guest will exit to the VMM where the only > + * recourse is to terminate the guest. > + * > + * There are three parts to fix this issue and comprehensively avoid > + * access to unaccepted memory. Together these ensure that an extra > + * “guard” page is accepted in addition to the memory that needs to be > + * used: > + * > + * 1. Implicitly extend the range_contains_unaccepted_memory(start, end) > + * checks up to end+2M if ‘end’ is aligned on a 2M boundary. > + * > + * 2. Implicitly extend accept_memory(start, end) to end+2M if ‘end’ is > + * aligned on a 2M boundary. > + * > + * 3. Set PageUnaccepted() on both memory that itself needs to be > + * accepted *and* memory where the next page needs to be accepted. > + * Essentially, make PageUnaccepted(page) a marker for whether work > + * needs to be done to make ‘page’ usable. That work might include > + * accepting pages in addition to ‘page’ itself. > + */ One nit with this: I'd much rather add one sentence to these to help tie the code implementing it with this comment. Maybe something like: * 2. Implicitly extend accept_memory(start, end) to end+2M if ‘end’ is * aligned on a 2M boundary. (immediately following this comment) > + if (!(end % PMD_SIZE)) > + end += PMD_SIZE; > + > spin_lock_irqsave(&unaccepted_memory_lock, flags); > for_each_set_bitrange_from(range_start, range_end, bitmap, > DIV_ROUND_UP(end, PMD_SIZE)) { > @@ -46,6 +78,10 @@ bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end) > > bitmap = __va(boot_params.unaccepted_memory); > > + /* See comment on load_unaligned_zeropad() in accept_memory() */ > + if (!(end % PMD_SIZE)) > + end += PMD_SIZE; It's a wee bit hard to follow this back to the comment that it references, even with them sitting next to each other in this diff. How about adding: /* * Also consider the unaccepted state of the *next* page. See * fix #1 in the comment on load_unaligned_zeropad() in * accept_memory(). */ > spin_lock_irqsave(&unaccepted_memory_lock, flags); > while (start < end) { > if (test_bit(start / PMD_SIZE, bitmap)) { > diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c > index b91c89100b2d..bc1110509de4 100644 > --- a/drivers/firmware/efi/libstub/x86-stub.c > +++ b/drivers/firmware/efi/libstub/x86-stub.c > @@ -709,6 +709,13 @@ static efi_status_t allocate_unaccepted_memory(struct boot_params *params, > return EFI_SUCCESS; > } > > + /* > + * range_contains_unaccepted_memory() may need to check one 2M chunk > + * beyond the end of RAM to deal with load_unaligned_zeropad(). Make > + * sure that the bitmap is large enough handle it. > + */ > + max_addr += PMD_SIZE; I guess the alternative to this would have been to record 'max_addr', then special case 'max_addr'+2M in the bitmap checks. I agree this is probably nicer. Also, the changelog needs to at least *mention* this little tidbit. It was a bit of a surprise when I got here. With those fixed: Reviewed-by: Dave Hansen