From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by mx.groups.io with SMTP id smtpd.web11.13247.1675895065479038715 for ; Wed, 08 Feb 2023 14:24:25 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@taylorbeebe.com header.s=google header.b=LGT8ixsj; spf=pass (domain: taylorbeebe.com, ip: 209.85.216.49, mailfrom: t@taylorbeebe.com) Received: by mail-pj1-f49.google.com with SMTP id s89-20020a17090a2f6200b0023125ebb4b1so343707pjd.3 for ; Wed, 08 Feb 2023 14:24:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=taylorbeebe.com; s=google; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=pw5LYwYbxpKhLpIfA4E6fuFk/kbAi6XNtadBcP5VsYY=; b=LGT8ixsjMmYFGZrddlfxYbH/RL5Dygy9BkSxBYqMBnblCuRH7WFbhOqPYRjywQtBTb rPEtbuZVAzV9O8VEQOormt7animUexhhIaz5clAvnTjcJlOst5mqh8OK1Ue/svTtedeB URBUnbPdUfpf4ludlEBOcX7gDrDc1add1OEZ5h/LC7No/3hVA0vPxs9Az6gNdraN64U/ iPcpiNWujjw9BPiWXWSJ9MugFlVk+umOLcrI4XIhGYai258ZirwCDTUZjW0gEp2vY/TC RLTI2IdF/ZTcgF1lxiyKjwlXLy6Bl5hUhwb7j49oHf5Nk4eDrloL3fCHZWWEk4p6o1sf 7WDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pw5LYwYbxpKhLpIfA4E6fuFk/kbAi6XNtadBcP5VsYY=; b=jnIyZY1P3FB3tD1CL1RNp+TFZls3mU8Hc/2udvPd9a+c0tXs72vmKKD+iFz2CCRhFX 3Hk/7hxc4CmszhjXLrJhuh3OfGqHZz7uJ7AVJT4yNm1GSmzI5R10FKD6KbdnYB+r07jX RYrnZiETxfipu/NVCoQGiunePSrDVwrOBbCfHaaL5PG8Zwhy7AdHk8Lt7P45MUbHBGWh j/i9fY4FwhABpVnj4debA+XjulFcy1ZQvIEwolrXHeFqEYWuwueJBm34yHSl7c6Nu2oJ A+oJ8NdZciimogBbRDXM1IsPJtU/4sCInayY1t+cU+DtP0YCqR+/0E3TIETD9GrNjzfi /4fw== X-Gm-Message-State: AO0yUKXnkbWg6L4ddAYTZGiPo4l4l1dYN3Ts+NQK/jJM697kZP+KM2JA c7xY2ipfDn2aNuNLBkNrqqBk/Q== X-Google-Smtp-Source: AK7set/NkpRlYZMujSO/au6L+obgDRb+mFYrzVsqDzUL10qgEkm/uCGauRzVtFM3Su6Sxs/pOpW0fA== X-Received: by 2002:a17:902:f809:b0:199:e58a:61c2 with SMTP id ix9-20020a170902f80900b00199e58a61c2mr685955plb.29.1675895064729; Wed, 08 Feb 2023 14:24:24 -0800 (PST) Return-Path: Received: from [192.168.50.162] ([50.46.230.135]) by smtp.gmail.com with ESMTPSA id 21-20020a170902ee5500b001991942dde7sm6547308plo.125.2023.02.08.14.24.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 08 Feb 2023 14:24:24 -0800 (PST) Message-ID: Date: Wed, 8 Feb 2023 14:24:23 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH 3/3] MdeModulePkg/DxeCore: Unconditionally set memory protections To: Ard Biesheuvel Cc: devel@edk2.groups.io, Michael Kinney , Liming Gao , Jiewen Yao , Michael Kubacki , Sean Brogan , Rebecca Cran , Leif Lindholm , Sami Mujawar , =?UTF-8?Q?Marvin_H=c3=a4user?= References: <20230208175812.700129-1-ardb@kernel.org> <20230208175812.700129-4-ardb@kernel.org> <32ae5b04-0a0c-c8c4-7b2d-cee3ac2a2517@taylorbeebe.com> From: "Taylor Beebe" In-Reply-To: Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2/8/2023 2:08 PM, Ard Biesheuvel wrote: > On Wed, 8 Feb 2023 at 20:12, Taylor Beebe wrote: >> >> I ran some tests and did some quick napkin math. Based on the time it >> takes to perform the SetMemoryAttributes() routine on QEMU, as long as >> <79% of the calls to ApplyMemoryProtectionPolicy() actually require a >> subsequent call to SetMemoryAttributes(), it is more efficient to walk >> the page/translation table to check if the attributes actually need to >> be updated. Even on a platform with varied NX policy settings, the >> number of times the attributes need to be updated is less than 0.0005% >> of all calls to ApplyMemoryProtectionPolicy() (likely due to most >> allocations being BootServicesData). >> >> Once the ARM and X64 implementations of the Memory Attribute Protocol >> are in, would you be open to updating this block to utilize the protocol >> to check the attributes of the region being updated? >> > > Yes, that was what I had in my initial prototype. > > However, I'm not sure how walking the page tables to retrieve all > existing attributes is fundamentally different from walking the page > tables to set them, given that everything is cached and we are running > uniprocessor at this point. > I was also skeptical, but running the experiment revealed that checking the page table is the clear winner. My guess is that branch prediction expects the table walk to reveal that the SetAttributes() call is unnecessary, and the cost of doing an instruction pipeline flush due to improper branch prediction is similar to the cost of doing a TLB flush/invalidation after the call to SetAttributes() making the upside outweigh the downside in almost all cases. > >> On 2/8/2023 10:25 AM, Ard Biesheuvel wrote: >>> On Wed, 8 Feb 2023 at 18:58, Ard Biesheuvel wrote: >>>> >>>> Instead of relying on a questionable heuristic that avoids calling into >>>> the SetMemoryAttributes () DXE service when the old memory type and the >>>> new one are subjected to the same NX memory protection policy, make this >>>> call unconditionally. This avoids corner cases where memory region >>>> attributes are out of sync with the policy, either due to the fact that >>>> we are in the middle of ramping up the protections, or due to explicit >>>> invocations of SetMemoryAttributes() by drivers. >>>> >>>> This requires the architecture page table code to be able to deal with >>>> this, in particular, it needs to be robust against potential recursion >>>> due to NX policies being applied to newly allocated page tables. >>>> >>>> Signed-off-by: Ard Biesheuvel >>>> --- >>>> MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c | 29 -------------------- >>>> 1 file changed, 29 deletions(-) >>>> >>>> diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c >>>> index 36987843f142..503feb72b5d0 100644 >>>> --- a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c >>>> +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c >>>> @@ -1263,9 +1263,7 @@ ApplyMemoryProtectionPolicy ( >>>> IN UINT64 Length >>>> ) >>>> { >>>> - UINT64 OldAttributes; >>>> UINT64 NewAttributes; >>>> - EFI_STATUS Status; >>>> >>>> // >>>> // The policy configured in PcdDxeNxMemoryProtectionPolicy >>>> @@ -1320,32 +1318,5 @@ ApplyMemoryProtectionPolicy ( >>>> // >>>> NewAttributes = GetPermissionAttributeForMemoryType (NewType); >>>> >>>> - if (OldType != EfiMaxMemoryType) { >>>> - OldAttributes = GetPermissionAttributeForMemoryType (OldType); >>>> - if (!mAfterDxeNxMemoryProtectionInit && >>>> - (OldAttributes == NewAttributes)) { >>>> - return EFI_SUCCESS; >>>> - } >>>> - >>> >>> This removes some code that does not actually exist - apologies. >>> >>> It comes down to just removing the conditional checks here, though, >>> and perform the tail call below unconditionally. >>> >>>> - // >>>> - // If available, use the EFI memory attribute protocol to obtain >>>> - // the current attributes of the region. If the entire region is >>>> - // covered and the attributes match, we don't have to do anything. >>>> - // >>>> - if (mMemoryAttribute != NULL) { >>>> - Status = mMemoryAttribute->GetMemoryAttributes (mMemoryAttribute, >>>> - Memory, >>>> - Length, >>>> - &OldAttributes >>>> - ); >>>> - if (!EFI_ERROR (Status) && (OldAttributes == NewAttributes)) { >>>> - return EFI_SUCCESS; >>>> - } >>>> - } >>>> - } else if (NewAttributes == 0) { >>>> - // newly added region of a type that does not require protection >>>> - return EFI_SUCCESS; >>>> - } >>>> - >>>> return gCpu->SetMemoryAttributes (gCpu, Memory, Length, NewAttributes); >>>> } >>>> -- >>>> 2.39.1 >>>> -- Taylor Beebe Software Engineer @ Microsoft