Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp1085364rdg; Fri, 13 Oct 2023 09:45:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGrT0BJxDMfVmkwZuC7fhv4knUnuokrOhzjk6u4OFPRHCE7er+hc+klfNceTR0JNycANaIt X-Received: by 2002:a05:6e02:1c22:b0:351:526a:499 with SMTP id m2-20020a056e021c2200b00351526a0499mr34175266ilh.20.1697215502503; Fri, 13 Oct 2023 09:45:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697215502; cv=none; d=google.com; s=arc-20160816; b=npeAQwiykp8/fbTtUKvrQDdi6t7IQacmeR4l4ms/3JDmVafjTymOhiNMuOdNkVTFDD pr7BVU3/xBh90wkfFlIa6cECEhGNUPqhPOxXOHKy2Ql5+T/Vw7ooKuzWzPRkJwcJyRfY Uxds/RJmZ6JDdzItcCjOEabrICgGa6GH9DVJMMVl6+hEuHy8jWtcaF+35rN69YKB+e6a BHFm9HwaWzqVs9WU5bXqOBBOG3MTNCYtiLeMtm2mRuuZwZsT24SfKMTcP/QdUNWdUMQ0 aF57wnSgIHqs3PoAKqyPVOljPlJjEDIRTNAsoaPfTUt0y0JHcnRxJXiufxL/wgtYUl5T Dokg== 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 :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-signature; bh=JqX75Zi0TdK1SmGPAZ5miYmrbIOPlJaIeQlD1x+zdUg=; fh=4SWjg+dlXeuQWQPiidiBa78E7yZ8Dpnx2+NIg/CMZCI=; b=WiCgRXj46VyctQdOMB9IxBN6I8LAvcOZRznHj3qJoPylYx4zzkK1mxH33qEr8P8V+3 bNNAz9RH6Ivah+M65EZTunoZVwlTdAEOUSAQmtbcEZZXw7P0jL+o28c4IGAi4A+THTUG u49kLMKwmm++YQZqmlkmLc6/dsabDZVJI7YQDZnMz653YvvCaR61CX/H28zV2VJLOGSC DqVaQkeP4QVz3VfGcIwIs5jIL3C0jWrLJbJwry0XT2VoHUtm/OkHv+BfodiD5qXZ0y1H lEQ7rSGoSvrkqD+/Qy+5+sF6gfmtOC66rLOg9K50Tx7oywBT37EFh77qwBn0st3oC2Vf oTKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=UQRotTIH; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id z4-20020a056a001d8400b00690c19cb105si16767432pfw.250.2023.10.13.09.45.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 09:45:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=UQRotTIH; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 10E30801DB0E; Fri, 13 Oct 2023 09:45:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230213AbjJMQov (ORCPT + 99 others); Fri, 13 Oct 2023 12:44:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229518AbjJMQou (ORCPT ); Fri, 13 Oct 2023 12:44:50 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E16FDB7; Fri, 13 Oct 2023 09:44:47 -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-out1.suse.de (Postfix) with ESMTPS id 64602210DD; Fri, 13 Oct 2023 16:44:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1697215486; 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=JqX75Zi0TdK1SmGPAZ5miYmrbIOPlJaIeQlD1x+zdUg=; b=UQRotTIHMoU/AK0y+quvKMhR2J/LCldJHRe/w5Np0WFVyCYulv9ffRb6tz8F6DN4p+YB0r sw99P4r48hO5nV9Ao3oGF50Hq93oHTVwca2vF1bxWM953FWXLsAa2kxx8KCFNT7wxyAoUi xinBMa7CngkECPtSLczQKzwDH1l7rO0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1697215486; 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=JqX75Zi0TdK1SmGPAZ5miYmrbIOPlJaIeQlD1x+zdUg=; b=+WUoplQ8+AqVFXsmtjtdqC9+eroodPLlAmARY9dR3HwLQB3ll+qQ4e5W3MvsWBz/pHWlR6 gh4V2NzRf0SOlMDg== 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 0198E1358F; Fri, 13 Oct 2023 16:44:45 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id Ey0IO/1zKWVtcgAAMHmgww (envelope-from ); Fri, 13 Oct 2023 16:44:45 +0000 Message-ID: <34d94c58-f5f3-48eb-5833-0ef0c90cf868@suse.cz> Date: Fri, 13 Oct 2023 18:44:45 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCHv14 5/9] efi: Add unaccepted memory support To: "Kirill A. Shutemov" , Michael Roth Cc: Borislav Petkov , Andy Lutomirski , Dave Hansen , Sean Christopherson , Andrew Morton , Joerg Roedel , Ard Biesheuvel , Andi Kleen , Kuppuswamy Sathyanarayanan , David Rientjes , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Paolo Bonzini , Ingo Molnar , Dario Faggioli , 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 References: <20230606142637.5171-1-kirill.shutemov@linux.intel.com> <20230606142637.5171-6-kirill.shutemov@linux.intel.com> <20231010210518.jguawj7bscwgvszv@amd.com> <20231013123358.y4pcdp5fgtt4ax6g@box.shutemov.name> <20231013162210.bqepgz6wnh7uohqq@box> Content-Language: en-US From: Vlastimil Babka In-Reply-To: <20231013162210.bqepgz6wnh7uohqq@box> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Authentication-Results: smtp-out1.suse.de; none X-Spam-Level: X-Spam-Score: -11.10 X-Spamd-Result: default: False [-11.10 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; BAYES_HAM(-3.00)[100.00%]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; REPLY(-4.00)[]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; NEURAL_HAM_SHORT(-1.00)[-1.000]; RCPT_COUNT_TWELVE(0.00)[32]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; MID_RHS_MATCH_FROM(0.00)[] X-Spam-Status: No, score=-4.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,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 morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Fri, 13 Oct 2023 09:45:00 -0700 (PDT) On 10/13/23 18:22, Kirill A. Shutemov wrote: > On Fri, Oct 13, 2023 at 03:33:58PM +0300, Kirill A. Shutemov wrote: >> > While testing SNP guests running today's tip/master (ef19bc9dddc3) I ran >> > into what seems to be fairly significant lock contention due to the >> > unaccepted_memory_lock spinlock above, which results in a constant stream >> > of soft-lockups until the workload gets all its memory accepted/faulted >> > in if the guest has around 16+ vCPUs. >> > >> > I've included the guest dmesg traces I was seeing below. >> > >> > In this case I was running a 32 vCPU guest with 200GB of memory running on >> > a 256 thread EPYC (Milan) system, and can trigger the above situation fairly >> > reliably by running the following workload in a freshly-booted guests: >> > >> > stress --vm 32 --vm-bytes 5G --vm-keep >> > >> > Scaling up the number of stress threads and vCPUs should make it easier >> > to reproduce. >> > >> > Other than unresponsiveness/lockup messages until the memory is accepted, >> > the guest seems to continue running fine, but for large guests where >> > unaccepted memory is more likely to be useful, it seems like it could be >> > an issue, especially when consider 100+ vCPU guests. >> >> Okay, sorry for delay. It took time to reproduce it with TDX. >> >> I will look what can be done. > > Could you check if the patch below helps? > > diff --git a/drivers/firmware/efi/unaccepted_memory.c b/drivers/firmware/efi/unaccepted_memory.c > index 853f7dc3c21d..591da3f368fa 100644 > --- a/drivers/firmware/efi/unaccepted_memory.c > +++ b/drivers/firmware/efi/unaccepted_memory.c > @@ -8,6 +8,14 @@ > /* Protects unaccepted memory bitmap */ > static DEFINE_SPINLOCK(unaccepted_memory_lock); > > +struct accept_range { > + struct list_head list; > + unsigned long start; > + unsigned long end; > +}; > + > +static LIST_HEAD(accepting_list); > + > /* > * accept_memory() -- Consult bitmap and accept the memory if needed. > * > @@ -24,6 +32,7 @@ void accept_memory(phys_addr_t start, phys_addr_t end) > { > struct efi_unaccepted_memory *unaccepted; > unsigned long range_start, range_end; > + struct accept_range range, *entry; > unsigned long flags; > u64 unit_size; > > @@ -80,7 +89,25 @@ void accept_memory(phys_addr_t start, phys_addr_t end) > > range_start = start / unit_size; > > + range.start = start; > + range.end = end; > +retry: > spin_lock_irqsave(&unaccepted_memory_lock, flags); > + > + list_for_each_entry(entry, &accepting_list, list) { > + if (entry->end < start) > + continue; > + if (entry->start > end) > + continue; > + spin_unlock_irqrestore(&unaccepted_memory_lock, flags); > + > + /* Somebody else accepting the range */ > + cpu_relax(); Should this be rather cond_resched()? I think cpu_relax() isn't enough to prevent soft lockups. Although IIUC hitting this should be rare, as the contending tasks will pick different ranges via try_to_accept_memory_one(), right? > + goto retry; > + } > + > + list_add(&range.list, &accepting_list); > + > for_each_set_bitrange_from(range_start, range_end, unaccepted->bitmap, > DIV_ROUND_UP(end, unit_size)) { > unsigned long phys_start, phys_end; > @@ -89,9 +116,15 @@ void accept_memory(phys_addr_t start, phys_addr_t end) > phys_start = range_start * unit_size + unaccepted->phys_base; > phys_end = range_end * unit_size + unaccepted->phys_base; > > + spin_unlock_irqrestore(&unaccepted_memory_lock, flags); > + > arch_accept_memory(phys_start, phys_end); > + > + spin_lock_irqsave(&unaccepted_memory_lock, flags); > bitmap_clear(unaccepted->bitmap, range_start, len); > } > + > + list_del(&range.list); > spin_unlock_irqrestore(&unaccepted_memory_lock, flags); > } >