Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4280008rwl; Mon, 3 Apr 2023 02:37:40 -0700 (PDT) X-Google-Smtp-Source: AKy350YHq+UFjge0Zy1ALiNInyqDSSXmD4GME2R0nNSZnw3ffLvYI6QJhgVf3oE7yz+8P9sXg8LU X-Received: by 2002:a17:907:3f97:b0:93f:505b:9742 with SMTP id hr23-20020a1709073f9700b0093f505b9742mr36882203ejc.65.1680514660364; Mon, 03 Apr 2023 02:37:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680514660; cv=none; d=google.com; s=arc-20160816; b=tZEGYkW9vc3Kr/6D3QjnpS08OgQ+Gu2WPlHj02bu9h+XGYUY+PEBQtInbQVI6lwnMX ZDGe3Q/ir6uLRq8fTGICJa8bBIWwqhB2oEWHhdL0VBwP8gHfoXK2JUpxOZhs6v7u/P1G VBqzHNCL2lLA40kWKw+6t+wkV3+v6zCRM1DB4HDSK1dVDrMirlZXXcEsiIPDpEmhOC2h 51cyJlpiccdTkvrxlXfoFEow67+QkHTjFMhJVno1nTMl2pURrxrX+0DcC2uKNHayXH9Q GkRgZwmz49FanPoRAWDZfv7UdYfwouEjJew5EhHs9e/0nCyBgW1yjw1OM2WYsn9EQ5og xHTg== 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:dkim-signature; bh=dD5mb90+GHm9wBE5Mlt3//7atUZgg8lKi994A27Sm5Q=; b=EQh2WIFNoJJUb6vBmX5CV5MvbzUcL8B5E70B1NsP6j5CSKQLqLI0Csucs+zxoMmnz2 yMugLO8HxcquPVNMkfBPf4Smp0DZwRO3CGo6l/fwA024Il7bN3ryq4pzS0a/Epsq/0iR Qy+7C9GjB3+01CJjXOTG4qQafvGU/htRkOgRqKGI73I07oUufLKje/COJ1x00kvTFJ0d Vm6jKsxWk4g1efz74fVtzY6+X9WsrWPpR/L6fVdhIRGCCZzIA0RXP4czsHYHS5ksV3so +PhPv0z2/USsoEoLpH62BOl9LRYlsRk086UAzxPVb/ElKEcNNqGyMTWi6EkGIbUrFsvm D7MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=2S4SKiU+; dkim=neutral (no key) header.i=@suse.cz header.b=qg+E357x; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s16-20020a056402015000b004fc995b3385si8111612edu.634.2023.04.03.02.37.16; Mon, 03 Apr 2023 02:37:40 -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=@suse.cz header.s=susede2_rsa header.b=2S4SKiU+; dkim=neutral (no key) header.i=@suse.cz header.b=qg+E357x; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231863AbjDCJ06 (ORCPT + 99 others); Mon, 3 Apr 2023 05:26:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230332AbjDCJ04 (ORCPT ); Mon, 3 Apr 2023 05:26:56 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8844813D; Mon, 3 Apr 2023 02:26:55 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 386091F8D9; Mon, 3 Apr 2023 09:26:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1680514014; h=from:from:reply-to: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; bh=dD5mb90+GHm9wBE5Mlt3//7atUZgg8lKi994A27Sm5Q=; b=2S4SKiU+vFQ91fvzKCZ6togVsV3AMf8gjnEsEmFgV5+OIrAYALI9m7J4sLWXI84YzHMAHt uD0vbXNmpAZekxEe2CM8bfdTotRG867XyeuglywITPGd4CBg7e0vH/7Vdnu1PTCbdQsXg+ 4TZI4gHOMi091dpGlfZIs2lIR7n47Co= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1680514014; h=from:from:reply-to: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; bh=dD5mb90+GHm9wBE5Mlt3//7atUZgg8lKi994A27Sm5Q=; b=qg+E357xtXrxgek5i3iRfTZh2Dk/r2o6MikDtk1VGmCFUTOYw0jOHyxSByKtZmlfUqHQbz 6ANQNJJ4CgBPjVCA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 80B1A13416; Mon, 3 Apr 2023 09:26:53 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id lfuoHt2bKmS9YQAAMHmgww (envelope-from ); Mon, 03 Apr 2023 09:26:53 +0000 Message-ID: <43234108-fa4f-7583-e3b4-2daa2de89fb0@suse.cz> Date: Mon, 3 Apr 2023 11:26:53 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCHv9 02/14] mm: Add support for 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 , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Paolo Bonzini , Ingo Molnar , Dario Faggioli , Dave Hansen , Mike Rapoport , David Hildenbrand , Mel Gorman , marcelo.cerri@canonical.com, tim.gardner@canonical.com, khalid.elmously@canonical.com, philip.cox@canonical.com, aarcange@redhat.com, peterx@redhat.com, x86@kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, Mike Rapoport References: <20230330114956.20342-1-kirill.shutemov@linux.intel.com> <20230330114956.20342-3-kirill.shutemov@linux.intel.com> From: Vlastimil Babka In-Reply-To: <20230330114956.20342-3-kirill.shutemov@linux.intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 3/30/23 13:49, Kirill A. Shutemov wrote: > UEFI Specification version 2.9 introduces the concept of memory > acceptance. Some Virtual Machine platforms, such as Intel TDX or AMD > SEV-SNP, require memory to be accepted before it can be used by the > guest. Accepting happens via a protocol specific to the Virtual Machine > platform. > > There are several ways kernel can deal with unaccepted memory: > > 1. Accept all the memory during the boot. It is easy to implement and > it doesn't have runtime cost once the system is booted. The downside > is very long boot time. > > Accept can be parallelized to multiple CPUs to keep it manageable > (i.e. via DEFERRED_STRUCT_PAGE_INIT), but it tends to saturate > memory bandwidth and does not scale beyond the point. > > 2. Accept a block of memory on the first use. It requires more > infrastructure and changes in page allocator to make it work, but > it provides good boot time. > > On-demand memory accept means latency spikes every time kernel steps > onto a new memory block. The spikes will go away once workload data > set size gets stabilized or all memory gets accepted. > > 3. Accept all memory in background. Introduce a thread (or multiple) > that gets memory accepted proactively. It will minimize time the > system experience latency spikes on memory allocation while keeping > low boot time. > > This approach cannot function on its own. It is an extension of #2: > background memory acceptance requires functional scheduler, but the > page allocator may need to tap into unaccepted memory before that. > > The downside of the approach is that these threads also steal CPU > cycles and memory bandwidth from the user's workload and may hurt > user experience. > > The patch implements #1 and #2 for now. #2 is the default. Some > workloads may want to use #1 with accept_memory=eager in kernel > command line. #3 can be implemented later based on user's demands. > > Support of unaccepted memory requires a few changes in core-mm code: > > - memblock has to accept memory on allocation; > > - page allocator has to accept memory on the first allocation of the > page; > > Memblock change is trivial. > > The page allocator is modified to accept pages. New memory gets accepted > before putting pages on free lists. It is done lazily: only accept new > pages when we run out of already accepted memory. The memory gets > accepted until the high watermark is reached. Great. > Architecture has to provide two helpers if it wants to support > unaccepted memory: > > - accept_memory() makes a range of physical addresses accepted. > > - range_contains_unaccepted_memory() checks anything within the range > of physical addresses requires acceptance. > > Signed-off-by: Kirill A. Shutemov > Acked-by: Mike Rapoport # memblock Reviewed-by: Vlastimil Babka Just a small suggestion below: > + > +static bool try_to_accept_memory(struct zone *zone, unsigned int order) > +{ > + long to_accept; > + int ret = false; > + > + if (!static_branch_unlikely(&zones_with_unaccepted_pages)) > + return false; This potentially (depends on what compiler decides) means we'll call this function just to skip the static branch. OTOH forcing it as inline would be wasteful too. So I'd split that away and make the callers do that static branch check inline. Just as deferred_pages_enabled() is used. > + /* How much to accept to get to high watermark? */ > + to_accept = high_wmark_pages(zone) - > + (zone_page_state(zone, NR_FREE_PAGES) - > + __zone_watermark_unusable_free(zone, order, 0)); > + > + /* Accept at least one page */ > + do { > + if (!try_to_accept_memory_one(zone)) > + break; > + ret = true; > + to_accept -= MAX_ORDER_NR_PAGES; > + } while (to_accept > 0); > + > + return ret; > +}