From nobody Wed Dec 17 14:31:21 2025 Received: from forwardcorp1a.mail.yandex.net (forwardcorp1a.mail.yandex.net [178.154.239.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 730B322156F for ; Mon, 10 Mar 2025 12:04:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741608280; cv=none; b=RSTqAt//cCLveaP9EqJDrJkVeMd6feUYEJgfmx7cXhHBanOWuI4hMuFcrYjUz2SSTbQFu0wPwlHDdZDcIUxdds5xslsXQHFm8hDnkHyManIvKgVgjO7nkf09mPbdevnz7l5RaY0TL5dG0jquWGFd0wyFs0o8FWFm5tVFngk5tnk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741608280; c=relaxed/simple; bh=5OH6zwiSw0oNmq1LgVVbHiG2za82nMq8SrllEkAQIHg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FL6+u3YesI+DvrFQN6C4w2zhCyvlxrwOKuRwTVSgto2CUQGyvQKGqlxdH7GLJhGXcXzxmpPDbWBid9acHhpzJztNLO1VJPJ/ZGAtkAcjhO0kTkbRpFdEV7nK5ytzVh0Gwoy8BK9/xdIYgxJifs/rqUlmEVx2sayGz7SADseQFH8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=yandex-team.com; spf=pass smtp.mailfrom=yandex-team.com; dkim=pass (1024-bit key) header.d=yandex-team.com header.i=@yandex-team.com header.b=N3tQSyH7; arc=none smtp.client-ip=178.154.239.72 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=yandex-team.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=yandex-team.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=yandex-team.com header.i=@yandex-team.com header.b="N3tQSyH7" Received: from mail-nwsmtp-smtp-corp-main-83.vla.yp-c.yandex.net (mail-nwsmtp-smtp-corp-main-83.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1f:600c:0:640:a431:0]) by forwardcorp1a.mail.yandex.net (Yandex) with ESMTPS id 3D4D160DE9; Mon, 10 Mar 2025 15:04:06 +0300 (MSK) Received: from dellarbn.yandex.net (unknown [10.214.35.248]) by mail-nwsmtp-smtp-corp-main-83.vla.yp-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id s3o0lL2FT0U0-U5ZTnMcq; Mon, 10 Mar 2025 15:04:05 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.com; s=default; t=1741608245; bh=QkhEET4Wo+gouJiK8Lgb4vMFbcgAXJpnZW6CjWUXYUI=; h=Message-ID:Date:In-Reply-To:Cc:Subject:References:To:From; b=N3tQSyH7q5Aw8vLpj6oIzXJUFol2N5amtQM0FyQ56H5V1ZTHW1L15Ttrhb4B+gc9s kwO45GOraHszy2v9J2VM/BFPgJC3K8JF8XkbIkUICHgn30PaHURZx5W6AKTRCMaz9h /YGOyUFNzrS71N7G5BQA84g6wuvpXWXemUAU3A/g= Authentication-Results: mail-nwsmtp-smtp-corp-main-83.vla.yp-c.yandex.net; dkim=pass header.i=@yandex-team.com From: Andrey Ryabinin To: linux-kernel@vger.kernel.org Cc: Alexander Graf , James Gowans , Mike Rapoport , Andrew Morton , linux-mm@kvack.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Eric Biederman , kexec@lists.infradead.org, Pratyush Yadav , Jason Gunthorpe , Pasha Tatashin , David Rientjes , Andrey Ryabinin Subject: [PATCH v2 2/7] kstate, kexec, x86: transfer kstate data across kexec Date: Mon, 10 Mar 2025 13:03:13 +0100 Message-ID: <20250310120318.2124-3-arbn@yandex-team.com> X-Mailer: git-send-email 2.45.3 In-Reply-To: <20250310120318.2124-1-arbn@yandex-team.com> References: <20250310120318.2124-1-arbn@yandex-team.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add kstate data to kexec segments so it got copied to the new kernel. Use cmdline to inform next kernel about kstate data location and size. Signed-off-by: Andrey Ryabinin --- I've used cmdline as it's the simplest way to transfer address to the new kernel. Perhaps passing it via dtb would be more elegant solution, but I don't have strong opinion here. --- arch/x86/Kconfig | 1 + arch/x86/kernel/kexec-bzimage64.c | 4 +++ arch/x86/kernel/setup.c | 2 ++ include/linux/kexec.h | 2 ++ include/linux/kstate.h | 5 ++++ kernel/kexec_file.c | 5 ++++ kernel/kstate.c | 49 ++++++++++++++++++++++++++++++- 7 files changed, 67 insertions(+), 1 deletion(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 0e27ebd7e36a..7358d9e15957 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -90,6 +90,7 @@ config X86 select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_KCOV if X86_64 select ARCH_HAS_KERNEL_FPU_SUPPORT + select ARCH_HAS_KSTATE if X86_64 select ARCH_HAS_MEM_ENCRYPT select ARCH_HAS_MEMBARRIER_SYNC_CORE select ARCH_HAS_NMI_SAFE_THIS_CPU_OPS diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzim= age64.c index 68530fad05f7..d3c98c8bda29 100644 --- a/arch/x86/kernel/kexec-bzimage64.c +++ b/arch/x86/kernel/kexec-bzimage64.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -77,6 +78,9 @@ static int setup_cmdline(struct kimage *image, struct boo= t_params *params, len =3D sprintf(cmdline_ptr, "elfcorehdr=3D0x%lx ", image->elf_load_addr); } + if (IS_ENABLED(CONFIG_KSTATE)) + len =3D sprintf(cmdline_ptr, "kstate_stream=3D0x0%lx@%ld ", + image->kstate_stream_addr, image->kstate_size); memcpy(cmdline_ptr + len, cmdline, cmdline_len); cmdline_len +=3D len; =20 diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index cebee310e200..b32c141ffcdd 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -992,6 +993,7 @@ void __init setup_arch(char **cmdline_p) =20 memblock_set_current_limit(ISA_END_ADDRESS); e820__memblock_setup(); + kstate_init(); =20 /* * Needs to run after memblock setup because it needs the physical diff --git a/include/linux/kexec.h b/include/linux/kexec.h index f0e9f8eda7a3..bd82f04888a1 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -299,6 +299,8 @@ struct kimage { unsigned long start; struct page *control_code_page; struct page *swap_page; + unsigned long kstate_stream_addr; + size_t kstate_size; void *vmcoreinfo_data_copy; /* locates in the crash memory */ =20 unsigned long nr_segments; diff --git a/include/linux/kstate.h b/include/linux/kstate.h index 4fc01e535bc0..ae583d090111 100644 --- a/include/linux/kstate.h +++ b/include/linux/kstate.h @@ -126,6 +126,8 @@ static inline unsigned long kstate_get_ulong(struct kst= ate_stream *stream) =20 #ifdef CONFIG_KSTATE =20 +void kstate_init(void); + int kstate_save_state(void); void free_kstate_stream(void); =20 @@ -137,14 +139,17 @@ int save_kstate(struct kstate_stream *stream, int id, void *obj); void restore_kstate(struct kstate_stream *stream, int id, const struct kstate_description *kstate, void *obj); +int kstate_load_migrate_buf(struct kimage *image); =20 #else =20 +static inline void kstate_init(void) { } #define kstate_register(state, obj) =20 static inline int kstate_save_state(void) { return 0; } static inline void free_kstate_stream(void) { } =20 +static inline int kstate_load_migrate_buf(struct kimage *image) { return 0= ; } #endif =20 =20 diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 3eedb8c226ad..a024ff379133 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -253,6 +254,10 @@ kimage_file_prepare_segments(struct kimage *image, int= kernel_fd, int initrd_fd, /* IMA needs to pass the measurement list to the next kernel. */ ima_add_kexec_buffer(image); =20 + ret =3D kstate_load_migrate_buf(image); + if (ret) + goto out; + /* Call image load handler */ ldata =3D kexec_image_load_default(image); =20 diff --git a/kernel/kstate.c b/kernel/kstate.c index a73a9a42e55b..d35996287b76 100644 --- a/kernel/kstate.c +++ b/kernel/kstate.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -182,6 +183,31 @@ int kstate_save_state(void) return 0; } =20 +int kstate_load_migrate_buf(struct kimage *image) +{ + int ret; + struct kexec_buf kbuf =3D { .image =3D image, .buf_min =3D 0, + .buf_max =3D ULONG_MAX, .top_down =3D true }; + + kbuf.bufsz =3D kstate_stream.size; + kbuf.buffer =3D kstate_stream.start; + + kbuf.memsz =3D kstate_stream.size; + + kbuf.buf_align =3D PAGE_SIZE; + kbuf.mem =3D KEXEC_BUF_MEM_UNKNOWN; + ret =3D kexec_add_buffer(&kbuf); + if (ret) + return ret; + image->kstate_stream_addr =3D kbuf.mem; + image->kstate_size =3D kstate_stream.size; + + pr_info("kstate: Loaded mig_stream at 0x%lx bufsz=3D0x%lx memsz=3D0x%lx\n= ", + kbuf.mem, kbuf.bufsz, kbuf.memsz); + + return ret; +} + void restore_kstate(struct kstate_stream *stream, int id, const struct kstate_description *kstate, void *obj) { @@ -258,6 +284,9 @@ static void restore_migrate_state(unsigned long kstate_= data, } } =20 +static unsigned long kstate_stream_addr =3D -1; +static unsigned long kstate_size; + static void __kstate_register(struct kstate_description *state, void *obj, struct state_entry *se) { @@ -265,7 +294,7 @@ static void __kstate_register(struct kstate_description= *state, void *obj, se->id =3D atomic_inc_return(&state->instance_id); se->obj =3D obj; list_add(&se->list, &states); - restore_migrate_state(0 /*migrate_stream_addr*/, se); + restore_migrate_state(kstate_stream_addr, se); } =20 int kstate_register(struct kstate_description *state, void *obj) @@ -280,3 +309,21 @@ int kstate_register(struct kstate_description *state, = void *obj) return 0; } =20 +static int __init setup_kstate(char *arg) +{ + char *end; + + if (!arg) + return -EINVAL; + kstate_stream_addr =3D memparse(arg, &end); + if (*end =3D=3D '@') + kstate_size =3D memparse(end + 1, &end); + + return end > arg ? 0 : -EINVAL; +} +early_param("kstate_stream", setup_kstate); + +void __init kstate_init(void) +{ + memblock_reserve(kstate_stream_addr, kstate_size); +} --=20 2.45.3