Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp12937678rwl; Wed, 4 Jan 2023 00:46:10 -0800 (PST) X-Google-Smtp-Source: AMrXdXu3mYkNyCcexZLsIiOmcGLbH26sXIG59ReRAZMhO+V7tDEKld0PGI2SGlGLrl6fYdSBgyoh X-Received: by 2002:a17:907:209c:b0:7c1:41bb:70ac with SMTP id pv28-20020a170907209c00b007c141bb70acmr40135931ejb.64.1672821970001; Wed, 04 Jan 2023 00:46:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672821969; cv=none; d=google.com; s=arc-20160816; b=V+6b+aB9XLwgfgTRse/TsCEgZ9E8PAxgvnI/GzmTOGXJ1CZmlahuWqA+y4Y8YUR0T9 yXxEYkMCa5qwEtWzYR1pcaUNEQcaTZtW9pNQ6I882G3i4mFyhmtubuubSch7LYdIRLsC cidwYkCmaDiYeP2QYXia3Py+JbQztq4r3lLgm2OiR8KSPZ1sB9odFawI1mMQ6cJNakYz c29LwbzwFa5/p58NGcs37TyR+ROP7weCwFzgZ51asmrJJRy5133ENi9zrTzH9mxjAYXQ S6mq7i25aWAcwV7zm3CayFERal2N7cqvDETGVoTQScnL8pRmTjFs3wqCgNSDHvzontiK pAoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=jOGkyHTPrK3vmgrPhBFfUfxbu3zppn14vaIHBbomZbE=; b=vMFt2XYdBgYHNZ1Gi/B8F861H1WU343JfePOitsG5gdNctOlGCN4612xHrdoa4rZLv wr+/NnZeJ7tVuCyzkEfFEBipdfLKFHF/5JQJyyieRroa+zKwxblvjFpe55Lp8yRRWhpK bv/+Nmexq/oeCCtHc6sMoHLxmolkedJRch4ob/9tCc/S7uPoCqiyieJtb2GrSvSKZmbs 2YNjl5VUAQA7PmUTgg7sRjBsDl7Ha4mNAzAWgUoOPf9pprJlXouvLrOck+3/SFC1gsQP 2TWPeCkYuJg7ckWWjiozjNJf1fNA3+ejmqPl9J6UIGgpz9o5P/7sIQjf4qKRMTrUUAaJ 0KsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=caYyn2Vz; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m2-20020a056402430200b0048ea4223b5asi6427502edc.449.2023.01.04.00.45.54; Wed, 04 Jan 2023 00:46:09 -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=@redhat.com header.s=mimecast20190719 header.b=caYyn2Vz; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234156AbjADIgM (ORCPT + 57 others); Wed, 4 Jan 2023 03:36:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234169AbjADIfl (ORCPT ); Wed, 4 Jan 2023 03:35:41 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FAD91A386 for ; Wed, 4 Jan 2023 00:34:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672821288; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=jOGkyHTPrK3vmgrPhBFfUfxbu3zppn14vaIHBbomZbE=; b=caYyn2VzHjCfByj3HRDqqsoXuLiT5sARtKMVvcOmhUBmDoCTsakDIhZDTVSdXKXj8Gte/5 gl0jSc2ewCprPzSGPkcMetzi4r770lf0aFiBIBrrXVBXcQ+FAYBmZAGFVFSkz5nPQOsJPP SB73yCJN8ynRy0/mLrh6LfWACq8W4dM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-19-N1CaYOJJONmlm46xYn6qpA-1; Wed, 04 Jan 2023 03:34:44 -0500 X-MC-Unique: N1CaYOJJONmlm46xYn6qpA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 81E36858F09; Wed, 4 Jan 2023 08:34:43 +0000 (UTC) Received: from localhost (ovpn-12-189.pek2.redhat.com [10.72.12.189]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D13B3C15BA0; Wed, 4 Jan 2023 08:34:41 +0000 (UTC) Date: Wed, 4 Jan 2023 16:34:38 +0800 From: Baoquan He To: Eric DeVolder Cc: linux-kernel@vger.kernel.org, x86@kernel.org, kexec@lists.infradead.org, ebiederm@xmission.com, dyoung@redhat.com, vgoyal@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, nramas@linux.microsoft.com, thomas.lendacky@amd.com, robh@kernel.org, efault@gmx.de, rppt@kernel.org, david@redhat.com, sourabhjain@linux.ibm.com, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com Subject: Re: [PATCH v15 3/7] crash: add generic infrastructure for crash hotplug support Message-ID: References: <20221209153656.3284-1-eric.devolder@oracle.com> <20221209153656.3284-4-eric.devolder@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221209153656.3284-4-eric.devolder@oracle.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 12/09/22 at 10:36am, Eric DeVolder wrote: > CPU and memory change notifications are received in order to > regenerate the elfcorehdr. > > To support cpu hotplug, a callback is registered to capture the > CPUHP_AP_ONLINE_DYN online and offline events via > cpuhp_setup_state_nocalls(). > > To support memory hotplug, a notifier is registered to capture the > MEM_ONLINE and MEM_OFFLINE events via register_memory_notifier(). > > The cpu callback and memory notifiers call handle_hotplug_event() > which performs needed tasks and then dispatches the event to the > architecture specific arch_crash_handle_hotplug_event(). During the > process, the kexec_lock is held. > > Signed-off-by: Eric DeVolder This looks good to me, thx. Acked-by: Baoquan He > --- > include/linux/crash_core.h | 8 +++ > include/linux/kexec.h | 12 ++++ > kernel/crash_core.c | 138 +++++++++++++++++++++++++++++++++++++ > 3 files changed, 158 insertions(+) > > diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h > index de62a722431e..a270f8660538 100644 > --- a/include/linux/crash_core.h > +++ b/include/linux/crash_core.h > @@ -84,4 +84,12 @@ int parse_crashkernel_high(char *cmdline, unsigned long long system_ram, > int parse_crashkernel_low(char *cmdline, unsigned long long system_ram, > unsigned long long *crash_size, unsigned long long *crash_base); > > +#define KEXEC_CRASH_HP_REMOVE_CPU 0 > +#define KEXEC_CRASH_HP_ADD_CPU 1 > +#define KEXEC_CRASH_HP_REMOVE_MEMORY 2 > +#define KEXEC_CRASH_HP_ADD_MEMORY 3 > +#define KEXEC_CRASH_HP_INVALID_CPU -1U > + > +struct kimage; > + > #endif /* LINUX_CRASH_CORE_H */ > diff --git a/include/linux/kexec.h b/include/linux/kexec.h > index ebf46c3b8f8b..389444cb03cc 100644 > --- a/include/linux/kexec.h > +++ b/include/linux/kexec.h > @@ -32,6 +32,7 @@ extern note_buf_t __percpu *crash_notes; > #include > #include > #include > +#include > #include > > /* Verify architecture specific macros are defined */ > @@ -374,6 +375,13 @@ struct kimage { > struct purgatory_info purgatory_info; > #endif > > +#ifdef CONFIG_CRASH_HOTPLUG > + bool hotplug_event; > + unsigned int offlinecpu; > + bool elfcorehdr_index_valid; > + int elfcorehdr_index; > +#endif > + > #ifdef CONFIG_IMA_KEXEC > /* Virtual address of IMA measurement buffer for kexec syscall */ > void *ima_buffer; > @@ -503,6 +511,10 @@ static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, g > static inline void arch_kexec_pre_free_pages(void *vaddr, unsigned int pages) { } > #endif > > +#ifndef arch_crash_handle_hotplug_event > +static inline void arch_crash_handle_hotplug_event(struct kimage *image) { } > +#endif > + > #else /* !CONFIG_KEXEC_CORE */ > struct pt_regs; > struct task_struct; > diff --git a/kernel/crash_core.c b/kernel/crash_core.c > index 8c648fd5897a..fcb642a03d7b 100644 > --- a/kernel/crash_core.c > +++ b/kernel/crash_core.c > @@ -11,6 +11,8 @@ > #include > #include > #include > +#include > +#include > > #include > #include > @@ -18,6 +20,7 @@ > #include > > #include "kallsyms_internal.h" > +#include "kexec_internal.h" > > /* vmcoreinfo stuff */ > unsigned char *vmcoreinfo_data; > @@ -612,3 +615,138 @@ static int __init crash_save_vmcoreinfo_init(void) > } > > subsys_initcall(crash_save_vmcoreinfo_init); > + > +#ifdef CONFIG_CRASH_HOTPLUG > +#undef pr_fmt > +#define pr_fmt(fmt) "crash hp: " fmt > +/* > + * To accurately reflect hot un/plug changes, the elfcorehdr (which > + * is passed to the crash kernel via the elfcorehdr= parameter) > + * must be updated with the new list of CPUs and memories. > + * > + * In order to make changes to elfcorehdr, two conditions are needed: > + * First, the segment containing the elfcorehdr must be large enough > + * to permit a growing number of resources; the elfcorehdr memory size > + * is based on NR_CPUS_DEFAULT and CRASH_MAX_MEMORY_RANGES. > + * Second, purgatory must explicitly exclude the elfcorehdr from the > + * list of segments it checks (since the elfcorehdr changes and thus > + * would require an update to purgatory itself to update the digest). > + */ > +static void handle_hotplug_event(unsigned int hp_action, unsigned int cpu) > +{ > + /* Obtain lock while changing crash information */ > + if (kexec_trylock()) { > + > + /* Check kdump is loaded */ > + if (kexec_crash_image) { > + struct kimage *image = kexec_crash_image; > + > + if (hp_action == KEXEC_CRASH_HP_ADD_CPU || > + hp_action == KEXEC_CRASH_HP_REMOVE_CPU) > + pr_debug("hp_action %u, cpu %u\n", hp_action, cpu); > + else > + pr_debug("hp_action %u\n", hp_action); > + > + /* > + * When the struct kimage is allocated, it is wiped to zero, so > + * the elfcorehdr_index_valid defaults to false. Find the > + * segment containing the elfcorehdr, if not already found. > + * This works for both the kexec_load and kexec_file_load paths. > + */ > + if (!image->elfcorehdr_index_valid) { > + unsigned long mem; > + unsigned char *ptr; > + unsigned int n; > + > + for (n = 0; n < image->nr_segments; n++) { > + mem = image->segment[n].mem; > + ptr = kmap_local_page(pfn_to_page(mem >> PAGE_SHIFT)); > + if (ptr) { > + /* The segment containing elfcorehdr */ > + if (memcmp(ptr, ELFMAG, SELFMAG) == 0) { > + image->elfcorehdr_index = (int)n; > + image->elfcorehdr_index_valid = true; > + } > + kunmap_local(ptr); > + } > + } > + } > + > + if (!image->elfcorehdr_index_valid) { > + pr_err("unable to locate elfcorehdr segment"); > + goto out; > + } > + > + /* Needed in order for the segments to be updated */ > + arch_kexec_unprotect_crashkres(); > + > + /* Flag to differentiate between normal load and hotplug */ > + image->hotplug_event = true; > + > + /* Now invoke arch-specific update handler */ > + arch_crash_handle_hotplug_event(image); > + > + /* No longer handling a hotplug event */ > + image->hotplug_event = false; > + > + /* Change back to read-only */ > + arch_kexec_protect_crashkres(); > + } > + > +out: > + /* Release lock now that update complete */ > + kexec_unlock(); > + } > +} > + > +static int crash_memhp_notifier(struct notifier_block *nb, unsigned long val, void *v) > +{ > + switch (val) { > + case MEM_ONLINE: > + handle_hotplug_event(KEXEC_CRASH_HP_ADD_MEMORY, > + KEXEC_CRASH_HP_INVALID_CPU); > + break; > + > + case MEM_OFFLINE: > + handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_MEMORY, > + KEXEC_CRASH_HP_INVALID_CPU); > + break; > + } > + return NOTIFY_OK; > +} > + > +static struct notifier_block crash_memhp_nb = { > + .notifier_call = crash_memhp_notifier, > + .priority = 0 > +}; > + > +static int crash_cpuhp_online(unsigned int cpu) > +{ > + handle_hotplug_event(KEXEC_CRASH_HP_ADD_CPU, cpu); > + return 0; > +} > + > +static int crash_cpuhp_offline(unsigned int cpu) > +{ > + handle_hotplug_event(KEXEC_CRASH_HP_REMOVE_CPU, cpu); > + return 0; > +} > + > +static int __init crash_hotplug_init(void) > +{ > + int result = 0; > + > + if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) > + register_memory_notifier(&crash_memhp_nb); > + > + if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) > + result = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, > + "crash/cpuhp", > + crash_cpuhp_online, > + crash_cpuhp_offline); > + > + return result; > +} > + > +subsys_initcall(crash_hotplug_init); > +#endif > -- > 2.31.1 >