From patchwork Thu Feb 23 19:11:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 61060 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp484631wrd; Thu, 23 Feb 2023 11:13:56 -0800 (PST) X-Google-Smtp-Source: AK7set8cOKBglSP1NNLAhnVS8z1orNw5lfQlt8BN8rrvkrO8p0uUOINkJJhIRGhvoTHzf3y0MpLL X-Received: by 2002:a17:906:196:b0:88f:1255:59c with SMTP id 22-20020a170906019600b0088f1255059cmr18881031ejb.1.1677179636597; Thu, 23 Feb 2023 11:13:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677179636; cv=none; d=google.com; s=arc-20160816; b=Bp1gi2gAiIkpC0JYDOr3gFKopZ9j3/nBxRnjByUeYJtMHC00jgCgSiBHw0WrvwDf7s g2NJ7eWLSlQJ1KmEuFEP//O4D+mOyIs09IvO0vhfqE0uQ5+538lh7CV4mcz7CbRZSm5J d4xLa5mVdOsQ70WAcyOk0fp+jWXaPjBBYRIghTizJ9aFiz6gDQWiIR16jVSkADjGA+XV OG5w07fIev1P5Gjs94H7iBnTplFPizzYeU2vnt2Zu2QY0Fn4kH1h2hSgx88VEJ5n1G/p dmVLb+vQSScOLUW09mMeXf8nGzrOI2mSxAXbjkYr6T0W9q9cs1WkueQtOlvlXGNVhd2k pL5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+bBfIfpR8W2u8MvrmlDVru58AVxTNXaOUl1C+oqeDgw=; b=YjnY0D26vvVGxRVp6Wrx9cgdem7AgB8fVDKID4yvyqGpDNaoiH2i+WLHxeOWexEtX3 NPiWHhXZfIjfHjN5CbTACZgMLqrM4mHMr+gRwXjk8PMgC7FHQFq+x+uuGQG09jBpnJy+ YKlrYs3mEBF6+iWW91KkCiqGYh/a/VaOZFXn06+gMx8hUGBwFc3A3EMhar+QqCFGND5d t2RtedgjqOdWnCCB0grRW2eOeJQZ+ypn+oLdUjGh6AXDT0rBY+v6ujgmO9rXlN44BBpv yM2zilTeJ7RdTHdnTCz+pkC2if+FBC6yJQpe+TvyBATedC+60ZoLv6l5ADU9W9vI/DXJ yd1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=UE46E5BX; 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=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 28-20020a170906029c00b008b17e88ee87si28331093ejf.537.2023.02.23.11.13.33; Thu, 23 Feb 2023 11:13:56 -0800 (PST) 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=@bytedance.com header.s=google header.b=UE46E5BX; 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=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232070AbjBWTMm (ORCPT + 99 others); Thu, 23 Feb 2023 14:12:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231681AbjBWTMc (ORCPT ); Thu, 23 Feb 2023 14:12:32 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 698644ECF5 for ; Thu, 23 Feb 2023 11:12:09 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id c12so11561419wrw.1 for ; Thu, 23 Feb 2023 11:12:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+bBfIfpR8W2u8MvrmlDVru58AVxTNXaOUl1C+oqeDgw=; b=UE46E5BXTtS7vFvWGFT2AdaSl8TrYTYxGeVYdAYXiVQNr2k3cSU48ZjJ2fODdNa61H 7TJR/N6tZQylBTO7IpJ9cp5d/mIATGMWuRkIblEm8nQCyh2PETBEnHqzubZR/2DXj7/m wgeJczPN9rvrxjjHKhjwNltHZgWXORQUqCByNQajBrZPFxPZCUYxuJMVlAOY6TX6kEwd Qgvr6Gv/Mpm2BG81YG7apxdwuxhq8lkJJUzsz7TCCkIJabFCY+t0LoaQq3J5p8uBqlkQ H+jNwZ1Ca7vqi8kFhQGF8Y/R9rNXxqedDqvz2ym6wLDTh5wsxy8yYaMHmDQU0TcW3Kby QyUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+bBfIfpR8W2u8MvrmlDVru58AVxTNXaOUl1C+oqeDgw=; b=OdjXteqoVl6JZE9bcts/R3naLAj91BN81L/8C8o+s/fR6nrOuEiUv36OyxsHwzAxV/ lOv3kZyhXwByLRqG147eBHG3WTrq1Cr+ipTT2xMIqTBnXJZoLlLZYYj2TvcUH5AivlXd 64Pcd1AtEn0LivUW5pzEw8ABH2BfLEud8igL6lggfo/WUzAJCIyCG9NPxjRjbGSG6m2L vN9qU47qXS0o7mONseeIr37u9c8nyTBB3oo8hB0Z3pFzTJSoN75gtdn5bmIhi7NAQF0w 9JTm/IjBpJiE60/iYJSqYKLfkAd1JFv/XIIO1tWxvPHNf6ZwwHzE7hebjOk/5qSGzpYr kzKQ== X-Gm-Message-State: AO0yUKWRUsRdWUgT5M6Jx4sB3GR2MCJXpdOqNqcHGdL98+U7BNeqMF2y ByQNzTvzLh36aV9+szj94w9gIw== X-Received: by 2002:a5d:67d2:0:b0:2c5:a6c4:7fd3 with SMTP id n18-20020a5d67d2000000b002c5a6c47fd3mr10856571wrw.8.1677179505968; Thu, 23 Feb 2023 11:11:45 -0800 (PST) Received: from usaari01.cust.communityfibre.co.uk ([2a02:6b6a:b566:0:5ee0:5af0:64bd:6198]) by smtp.gmail.com with ESMTPSA id b15-20020a5d4b8f000000b002c561805a4csm12957286wrt.45.2023.02.23.11.11.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 11:11:45 -0800 (PST) From: Usama Arif To: dwmw2@infradead.org, tglx@linutronix.de, kim.phillips@amd.com, brgerst@gmail.com Cc: piotrgorski@cachyos.org, oleksandr@natalenko.name, arjan@linux.intel.com, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, x86@kernel.org, pbonzini@redhat.com, paulmck@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, rcu@vger.kernel.org, mimoja@mimoja.de, hewenliang4@huawei.com, thomas.lendacky@amd.com, seanjc@google.com, pmenzel@molgen.mpg.de, fam.zheng@bytedance.com, punit.agrawal@bytedance.com, simon.evans@bytedance.com, liangma@liangbit.com, David Woodhouse , Usama Arif Subject: [PATCH v11 03/12] cpu/hotplug: Add dynamic parallel bringup states before CPUHP_BRINGUP_CPU Date: Thu, 23 Feb 2023 19:11:31 +0000 Message-Id: <20230223191140.4155012-4-usama.arif@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230223191140.4155012-1-usama.arif@bytedance.com> References: <20230223191140.4155012-1-usama.arif@bytedance.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758650314469875526?= X-GMAIL-MSGID: =?utf-8?q?1758650314469875526?= From: David Woodhouse There is often significant latency in the early stages of CPU bringup, and time is wasted by waking each CPU (e.g. with SIPI/INIT/INIT on x86) and then waiting for it to respond before moving on to the next. Allow a platform to register a set of pre-bringup CPUHP states to which each CPU can be stepped in parallel, thus absorbing some of that latency. There is a subtlety here: even with an empty CPUHP_BP_PARALLEL_DYN step, this means that *all* CPUs are brought through the prepare states and to CPUHP_BP_PREPARE_DYN before any of them are taken to CPUHP_BRINGUP_CPU and then are allowed to run for themselves to CPUHP_ONLINE. So any combination of prepare/start calls which depend on A-B ordering for each CPU in turn, such as the X2APIC code which used to allocate a cluster mask 'just in case' and store it in a global variable in the prep stage, then potentially consume that preallocated structure from the AP and set the global pointer to NULL to be reallocated in CPUHP_X2APIC_PREPARE for the next CPU... would explode horribly. Any platform enabling the CPUHP_BP_PARALLEL_DYN steps must be reviewed and tested to ensure that such issues do not exist, and the existing behaviour of bringing CPUs to CPUHP_BP_PREPARE_DYN and then immediately to CPUHP_BRINGUP_CPU and CPUHP_ONLINE only one at a time does not change unless such a state is registered. Note that the new parallel stages do *not* yet bring each AP to the CPUHP_BRINGUP_CPU state at the same time, only to the new states which exist before it. The final loop in bringup_nonboot_cpus() is untouched, bringing each AP in turn from the final PARALLEL_DYN state (or all the way from CPUHP_OFFLINE) to CPUHP_BRINGUP_CPU and then waiting for that AP to do its own processing and reach CPUHP_ONLINE before releasing the next. Parallelising that part by bringing them all to CPUHP_BRINGUP_CPU and then waiting for them all is an exercise for the future. Signed-off-by: David Woodhouse Signed-off-by: Usama Arif Tested-by: Paul E. McKenney Tested-by: Kim Phillips Tested-by: Oleksandr Natalenko --- include/linux/cpuhotplug.h | 2 ++ kernel/cpu.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h index 6c6859bfc454..e5a73ae6ccc0 100644 --- a/include/linux/cpuhotplug.h +++ b/include/linux/cpuhotplug.h @@ -133,6 +133,8 @@ enum cpuhp_state { CPUHP_MIPS_SOC_PREPARE, CPUHP_BP_PREPARE_DYN, CPUHP_BP_PREPARE_DYN_END = CPUHP_BP_PREPARE_DYN + 20, + CPUHP_BP_PARALLEL_DYN, + CPUHP_BP_PARALLEL_DYN_END = CPUHP_BP_PARALLEL_DYN + 4, CPUHP_BRINGUP_CPU, /* diff --git a/kernel/cpu.c b/kernel/cpu.c index 6c0a92ca6bb5..fffb0da61ccc 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -1504,8 +1504,30 @@ int bringup_hibernate_cpu(unsigned int sleep_cpu) void bringup_nonboot_cpus(unsigned int setup_max_cpus) { + unsigned int n = setup_max_cpus - num_online_cpus(); unsigned int cpu; + /* + * An architecture may have registered parallel pre-bringup states to + * which each CPU may be brought in parallel. For each such state, + * bring N CPUs to it in turn before the final round of bringing them + * online. + */ + if (n > 0) { + enum cpuhp_state st = CPUHP_BP_PARALLEL_DYN; + + while (st <= CPUHP_BP_PARALLEL_DYN_END && cpuhp_hp_states[st].name) { + int i = n; + + for_each_present_cpu(cpu) { + cpu_up(cpu, st); + if (!--i) + break; + } + st++; + } + } + for_each_present_cpu(cpu) { if (num_online_cpus() >= setup_max_cpus) break; @@ -1882,6 +1904,10 @@ static int cpuhp_reserve_state(enum cpuhp_state state) step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN; end = CPUHP_BP_PREPARE_DYN_END; break; + case CPUHP_BP_PARALLEL_DYN: + step = cpuhp_hp_states + CPUHP_BP_PARALLEL_DYN; + end = CPUHP_BP_PARALLEL_DYN_END; + break; default: return -EINVAL; } @@ -1906,14 +1932,15 @@ static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name, /* * If name is NULL, then the state gets removed. * - * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on + * CPUHP_AP_ONLINE_DYN and CPUHP_BP_P*_DYN are handed out on * the first allocation from these dynamic ranges, so the removal * would trigger a new allocation and clear the wrong (already * empty) state, leaving the callbacks of the to be cleared state * dangling, which causes wreckage on the next hotplug operation. */ if (name && (state == CPUHP_AP_ONLINE_DYN || - state == CPUHP_BP_PREPARE_DYN)) { + state == CPUHP_BP_PREPARE_DYN || + state == CPUHP_BP_PARALLEL_DYN)) { ret = cpuhp_reserve_state(state); if (ret < 0) return ret;