From patchwork Fri Dec 11 22:10:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rob Herring (Arm)" X-Patchwork-Id: 11969659 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 732C2C4361B for ; Fri, 11 Dec 2020 23:04:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 38CB8221EA for ; Fri, 11 Dec 2020 23:04:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406587AbgLKWLa (ORCPT ); Fri, 11 Dec 2020 17:11:30 -0500 Received: from mail-ot1-f65.google.com ([209.85.210.65]:40487 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2394262AbgLKWKz (ORCPT ); Fri, 11 Dec 2020 17:10:55 -0500 Received: by mail-ot1-f65.google.com with SMTP id j12so9665184ota.7; Fri, 11 Dec 2020 14:10:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5DOUAwNBZTpsAGtj8/sa2DV++d3Am3UoJ46lgHI5NXw=; b=QoWSMnbppJSUUFb2H8QEY3uQskYL6C1V4t+yuxVo0PwvPZzl+WXljFchfg4U2l1bb1 QkZtqQnZxLWfrMGQP2mG6lvX/N32VNTzZMkx02Nwy3/yekUJN79e112wxf3+kCh8GXsC LWcZiGbeSvFYWeBrFa2WzzYpt/nJhZoRbD7auscNtM++BelSvjfrbLHKwEpDPD4KeWd+ RLz+yl6IpBam5aAsnQVh3zkyWT7l4yEaT0aCqsARcxfU0RYn+lYUAuGGe8ZFR8DYdy5M SScUsKBdY3TeHz4zHNYkErINx9KWQ3aJ9luNo4tcUI7WYKmSjfT4p9TPXDBqELs9Qq5a 9p8Q== X-Gm-Message-State: AOAM533MDVCvk6Z2gafqAviN/IhmzebBdYMMQGB6MsiAJ/Tg1oC722g7 3BrN3ksj4lHnDBG7gtFflQ== X-Google-Smtp-Source: ABdhPJxgyfaoDh+m5zmI8+VyVda4yZOf75Uysw6WxEnATVYka7mAtOv01ywSDzDwYlxDFX56wS+ecA== X-Received: by 2002:a9d:37c4:: with SMTP id x62mr10493712otb.87.1607724614033; Fri, 11 Dec 2020 14:10:14 -0800 (PST) Received: from xps15.herring.priv (24-155-109-49.dyn.grandenetworks.net. [24.155.109.49]) by smtp.googlemail.com with ESMTPSA id p3sm2137383otf.3.2020.12.11.14.10.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Dec 2020 14:10:11 -0800 (PST) From: Rob Herring To: Lakshmi Ramasubramanian , takahiro.akashi@linaro.org, will@kernel.org, catalin.marinas@arm.com, mpe@ellerman.id.au Cc: Thiago Jung Bauermann , zohar@linux.ibm.com, james.morse@arm.com, sashal@kernel.org, benh@kernel.crashing.org, paulus@samba.org, frowand.list@gmail.com, vincenzo.frascino@arm.com, mark.rutland@arm.com, dmitry.kasatkin@gmail.com, jmorris@namei.org, serge@hallyn.com, pasha.tatashin@soleen.com, allison@lohutok.net, masahiroy@kernel.org, bhsharma@redhat.com, mbrugger@suse.com, hsinyi@chromium.org, tao.li@vivo.com, christophe.leroy@c-s.fr, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, prsriva@linux.microsoft.com, balajib@linux.microsoft.com Subject: [RFC PATCH 1/4] powerpc: Rename kexec elfcorehdr_addr to elf_headers_mem Date: Fri, 11 Dec 2020 16:10:03 -0600 Message-Id: <20201211221006.1052453-2-robh@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201211221006.1052453-1-robh@kernel.org> References: <20201211221006.1052453-1-robh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org Align with arm64 name so common code can use it. Signed-off-by: Rob Herring Reviewed-by: Lakshmi Ramasubramanian Reviewed-by: Thiago Jung Bauermann --- arch/powerpc/include/asm/kexec.h | 2 +- arch/powerpc/kexec/file_load.c | 4 ++-- arch/powerpc/kexec/file_load_64.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h index 55d6ede30c19..dbf09d2f36d0 100644 --- a/arch/powerpc/include/asm/kexec.h +++ b/arch/powerpc/include/asm/kexec.h @@ -108,7 +108,7 @@ struct kimage_arch { unsigned long backup_start; void *backup_buf; - unsigned long elfcorehdr_addr; + unsigned long elf_headers_mem; unsigned long elf_headers_sz; void *elf_headers; diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c index 9a232bc36c8f..e452b11df631 100644 --- a/arch/powerpc/kexec/file_load.c +++ b/arch/powerpc/kexec/file_load.c @@ -45,7 +45,7 @@ char *setup_kdump_cmdline(struct kimage *image, char *cmdline, return NULL; elfcorehdr_strlen = sprintf(cmdline_ptr, "elfcorehdr=0x%lx ", - image->arch.elfcorehdr_addr); + image->arch.elf_headers_mem); if (elfcorehdr_strlen + cmdline_len > COMMAND_LINE_SIZE) { pr_err("Appending elfcorehdr= exceeds cmdline size\n"); @@ -263,7 +263,7 @@ int setup_new_fdt(const struct kimage *image, void *fdt, * Avoid elfcorehdr from being stomped on in kdump kernel by * setting up memory reserve map. */ - ret = fdt_add_mem_rsv(fdt, image->arch.elfcorehdr_addr, + ret = fdt_add_mem_rsv(fdt, image->arch.elf_headers_mem, image->arch.elf_headers_sz); if (ret) { pr_err("Error reserving elfcorehdr memory: %s\n", diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_load_64.c index c69bcf9b547a..a05c19b3cc60 100644 --- a/arch/powerpc/kexec/file_load_64.c +++ b/arch/powerpc/kexec/file_load_64.c @@ -815,7 +815,7 @@ static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf) goto out; } - image->arch.elfcorehdr_addr = kbuf->mem; + image->arch.elf_headers_mem = kbuf->mem; image->arch.elf_headers_sz = headers_sz; image->arch.elf_headers = headers; out: @@ -851,7 +851,7 @@ int load_crashdump_segments_ppc64(struct kimage *image, return ret; } pr_debug("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n", - image->arch.elfcorehdr_addr, kbuf->bufsz, kbuf->memsz); + image->arch.elf_headers_mem, kbuf->bufsz, kbuf->memsz); return 0; } From patchwork Fri Dec 11 22:10:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rob Herring (Arm)" X-Patchwork-Id: 11969661 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB7A6C433FE for ; Fri, 11 Dec 2020 23:04:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A116621707 for ; Fri, 11 Dec 2020 23:04:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406579AbgLKWLa (ORCPT ); Fri, 11 Dec 2020 17:11:30 -0500 Received: from mail-ot1-f65.google.com ([209.85.210.65]:42050 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406469AbgLKWK7 (ORCPT ); Fri, 11 Dec 2020 17:10:59 -0500 Received: by mail-ot1-f65.google.com with SMTP id 11so9663606oty.9; Fri, 11 Dec 2020 14:10:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hZ8Ccd5snkOKXftEHLmAvtNpaBoEgIct3KMYISqIfxE=; b=PqTgSimioWrReDOfzuNXnhtVOASK03wW2o0A5vKzmTownoTRK4ng2Shkd6IlFmujOM W6X3mGZRKhf/v058WxoWj6DqQ+/F18WcGZwsHlUrhLfT0ot7soLqtAxpyeXq6mQ3pgUr FrYmd5Pp4+7bbJ8jYpx9bD14Hi179fNP30Izyb8UMsNybJ1BZbCL7GycnMrGW4Eyd6fI KFkws1UhmBGy7xXL7hXNfOc9T/fsKjVsPqGfU7VArv44Vt6ESuykysL3uzpnDVy159Dc ACTWfMOzE4Iy61R/jTOvV/E8Px/6U+8gTgVUI/yjKwAeDtyiI2U4ucvZFgKZt2k/CN5l u9lw== X-Gm-Message-State: AOAM531XOfoAMczQqpZpl407+aINiwpZcAZh6PwXKlciFg4+sObNW6Cm VqjXuxGCAPTjhwqZDi1Jdw== X-Google-Smtp-Source: ABdhPJwJVPw/5LLfEpEuoG30HeiPU10Lb6XWXM9GlwC5aXrEoYtbW7QPoKUg1IdOmF8gEdRml2TS7Q== X-Received: by 2002:a9d:4c8d:: with SMTP id m13mr9281644otf.229.1607724617904; Fri, 11 Dec 2020 14:10:17 -0800 (PST) Received: from xps15.herring.priv (24-155-109-49.dyn.grandenetworks.net. [24.155.109.49]) by smtp.googlemail.com with ESMTPSA id p3sm2137383otf.3.2020.12.11.14.10.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Dec 2020 14:10:16 -0800 (PST) From: Rob Herring To: Lakshmi Ramasubramanian , takahiro.akashi@linaro.org, will@kernel.org, catalin.marinas@arm.com, mpe@ellerman.id.au Cc: Thiago Jung Bauermann , zohar@linux.ibm.com, james.morse@arm.com, sashal@kernel.org, benh@kernel.crashing.org, paulus@samba.org, frowand.list@gmail.com, vincenzo.frascino@arm.com, mark.rutland@arm.com, dmitry.kasatkin@gmail.com, jmorris@namei.org, serge@hallyn.com, pasha.tatashin@soleen.com, allison@lohutok.net, masahiroy@kernel.org, bhsharma@redhat.com, mbrugger@suse.com, hsinyi@chromium.org, tao.li@vivo.com, christophe.leroy@c-s.fr, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, prsriva@linux.microsoft.com, balajib@linux.microsoft.com Subject: [RFC PATCH 2/4] of: Add a common kexec FDT setup function Date: Fri, 11 Dec 2020 16:10:04 -0600 Message-Id: <20201211221006.1052453-3-robh@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201211221006.1052453-1-robh@kernel.org> References: <20201211221006.1052453-1-robh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org Both arm64 and powerpc do essentially the same FDT /chosen setup for kexec. We can simply combine everything each arch does. The differences are either omissions that arm64 should have or additional properties that will be ignored. The differences relative to the arm64 version: - If /chosen doesn't exist, it will be created (should never happen). - Any old dtb and initrd reserved memory will be released. - The new initrd and elfcorehdr are marked reserved. - "linux,booted-from-kexec" is set. The differences relative to the powerpc version: - "kaslr-seed" and "rng-seed" may be set. - "linux,elfcorehdr" is set. - Any existing "linux,usable-memory-range" is removed. Signed-off-by: Rob Herring Reviewed-by: Thiago Jung Bauermann --- This could be taken a step further and do the allocation of the new FDT. The difference is arm64 uses vmalloc and powerpc uses kmalloc. The arm64 version also retries with a bigger allocation. That seems unnecessary. --- drivers/of/Makefile | 1 + drivers/of/kexec.c | 228 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/of.h | 5 + 3 files changed, 234 insertions(+) create mode 100644 drivers/of/kexec.c diff --git a/drivers/of/Makefile b/drivers/of/Makefile index 6e1e5212f058..8ce11955afde 100644 --- a/drivers/of/Makefile +++ b/drivers/of/Makefile @@ -13,5 +13,6 @@ obj-$(CONFIG_OF_RESERVED_MEM) += of_reserved_mem.o obj-$(CONFIG_OF_RESOLVE) += resolver.o obj-$(CONFIG_OF_OVERLAY) += overlay.o obj-$(CONFIG_OF_NUMA) += of_numa.o +obj-$(CONFIG_KEXEC_FILE) += kexec.o obj-$(CONFIG_OF_UNITTEST) += unittest-data/ diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c new file mode 100644 index 000000000000..66787be081fe --- /dev/null +++ b/drivers/of/kexec.c @@ -0,0 +1,228 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2020 Arm Limited + * + * Based on arch/arm64/kernel/machine_kexec_file.c: + * Copyright (C) 2018 Linaro Limited + * + * And arch/powerpc/kexec/file_load.c: + * Copyright (C) 2016 IBM Corporation + */ + +#include +#include +#include +#include +#include +#include +#include + +/* relevant device tree properties */ +#define FDT_PROP_KEXEC_ELFHDR "linux,elfcorehdr" +#define FDT_PROP_MEM_RANGE "linux,usable-memory-range" +#define FDT_PROP_INITRD_START "linux,initrd-start" +#define FDT_PROP_INITRD_END "linux,initrd-end" +#define FDT_PROP_BOOTARGS "bootargs" +#define FDT_PROP_KASLR_SEED "kaslr-seed" +#define FDT_PROP_RNG_SEED "rng-seed" +#define RNG_SEED_SIZE 128 + +/** + * fdt_find_and_del_mem_rsv - delete memory reservation with given address and size + * + * Return: 0 on success, or negative errno on error. + */ +static int fdt_find_and_del_mem_rsv(void *fdt, unsigned long start, unsigned long size) +{ + int i, ret, num_rsvs = fdt_num_mem_rsv(fdt); + + for (i = 0; i < num_rsvs; i++) { + u64 rsv_start, rsv_size; + + ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size); + if (ret) { + pr_err("Malformed device tree.\n"); + return -EINVAL; + } + + if (rsv_start == start && rsv_size == size) { + ret = fdt_del_mem_rsv(fdt, i); + if (ret) { + pr_err("Error deleting device tree reservation.\n"); + return -EINVAL; + } + + return 0; + } + } + + return -ENOENT; +} + +/* + * of_kexec_setup_new_fdt - modify /chosen and memory reservation for the next kernel + * + * @image: kexec image being loaded. + * @fdt: Flattened device tree for the next kernel. + * @initrd_load_addr: Address where the next initrd will be loaded. + * @initrd_len: Size of the next initrd, or 0 if there will be none. + * @cmdline: Command line for the next kernel, or NULL if there will + * be none. + * + * Return: 0 on success, or negative errno on error. + */ +int of_kexec_setup_new_fdt(const struct kimage *image, void *fdt, + unsigned long initrd_load_addr, unsigned long initrd_len, + const char *cmdline) +{ + int ret, chosen_node; + const void *prop; + + /* Remove memory reservation for the current device tree. */ + ret = fdt_find_and_del_mem_rsv(fdt, __pa(initial_boot_params), + fdt_totalsize(initial_boot_params)); + if (ret == -EINVAL) + return ret; + + chosen_node = fdt_path_offset(fdt, "/chosen"); + if (chosen_node == -FDT_ERR_NOTFOUND) + chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), + "chosen"); + if (chosen_node < 0) { + ret = chosen_node; + goto out; + } + + ret = fdt_delprop(fdt, chosen_node, FDT_PROP_KEXEC_ELFHDR); + if (ret && ret != -FDT_ERR_NOTFOUND) + goto out; + ret = fdt_delprop(fdt, chosen_node, FDT_PROP_MEM_RANGE); + if (ret && ret != -FDT_ERR_NOTFOUND) + goto out; + + /* Did we boot using an initrd? */ + prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL); + if (prop) { + u64 tmp_start, tmp_end, tmp_size; + + tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop)); + + prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL); + if (!prop) + return -EINVAL; + + tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop)); + + /* + * kexec reserves exact initrd size, while firmware may + * reserve a multiple of PAGE_SIZE, so check for both. + */ + tmp_size = tmp_end - tmp_start; + ret = fdt_find_and_del_mem_rsv(fdt, tmp_start, tmp_size); + if (ret == -ENOENT) + ret = fdt_find_and_del_mem_rsv(fdt, tmp_start, + round_up(tmp_size, PAGE_SIZE)); + if (ret == -EINVAL) + return ret; + } + + /* add initrd-* */ + if (initrd_load_addr) { + ret = fdt_setprop_u64(fdt, chosen_node, FDT_PROP_INITRD_START, + initrd_load_addr); + if (ret) + goto out; + + ret = fdt_setprop_u64(fdt, chosen_node, FDT_PROP_INITRD_END, + initrd_load_addr + initrd_len); + if (ret) + goto out; + + ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len); + if (ret) + goto out; + + } else { + ret = fdt_delprop(fdt, chosen_node, FDT_PROP_INITRD_START); + if (ret && (ret != -FDT_ERR_NOTFOUND)) + goto out; + + ret = fdt_delprop(fdt, chosen_node, FDT_PROP_INITRD_END); + if (ret && (ret != -FDT_ERR_NOTFOUND)) + goto out; + } + + if (image->type == KEXEC_TYPE_CRASH) { + /* add linux,elfcorehdr */ + ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, + FDT_PROP_KEXEC_ELFHDR, + image->arch.elf_headers_mem, + image->arch.elf_headers_sz); + if (ret) + goto out; + + /* + * Avoid elfcorehdr from being stomped on in kdump kernel by + * setting up memory reserve map. + */ + ret = fdt_add_mem_rsv(fdt, image->arch.elf_headers_mem, + image->arch.elf_headers_sz); + + /* add linux,usable-memory-range */ + ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, + FDT_PROP_MEM_RANGE, + crashk_res.start, + crashk_res.end - crashk_res.start + 1); + if (ret) + goto out; + } + + /* add bootargs */ + if (cmdline) { + ret = fdt_setprop_string(fdt, chosen_node, FDT_PROP_BOOTARGS, cmdline); + if (ret) + goto out; + } else { + ret = fdt_delprop(fdt, chosen_node, FDT_PROP_BOOTARGS); + if (ret && (ret != -FDT_ERR_NOTFOUND)) + goto out; + } + + /* add kaslr-seed */ + ret = fdt_delprop(fdt, chosen_node, FDT_PROP_KASLR_SEED); + if (ret == -FDT_ERR_NOTFOUND) + ret = 0; + else if (ret) + goto out; + + if (rng_is_initialized()) { + u64 seed = get_random_u64(); + ret = fdt_setprop_u64(fdt, chosen_node, FDT_PROP_KASLR_SEED, seed); + if (ret) + goto out; + } else { + pr_notice("RNG is not initialised: omitting \"%s\" property\n", + FDT_PROP_KASLR_SEED); + } + + /* add rng-seed */ + if (rng_is_initialized()) { + void *rng_seed; + ret = fdt_setprop_placeholder(fdt, chosen_node, FDT_PROP_RNG_SEED, + RNG_SEED_SIZE, &rng_seed); + if (ret) + goto out; + get_random_bytes(rng_seed, RNG_SEED_SIZE); + } else { + pr_notice("RNG is not initialised: omitting \"%s\" property\n", + FDT_PROP_RNG_SEED); + } + + ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0); + +out: + if (ret) + return (ret == -FDT_ERR_NOSPACE) ? -ENOMEM : -EINVAL; + + return 0; +} diff --git a/include/linux/of.h b/include/linux/of.h index 5d51891cbf1a..3375f5295875 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -558,6 +558,11 @@ int of_map_id(struct device_node *np, u32 id, const char *map_name, const char *map_mask_name, struct device_node **target, u32 *id_out); +struct kimage; +int of_kexec_setup_new_fdt(const struct kimage *image, void *fdt, + unsigned long initrd_load_addr, unsigned long initrd_len, + const char *cmdline); + #else /* CONFIG_OF */ static inline void of_core_init(void) From patchwork Fri Dec 11 22:10:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rob Herring (Arm)" X-Patchwork-Id: 11969665 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA809C4361B for ; Fri, 11 Dec 2020 23:07:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7508821707 for ; Fri, 11 Dec 2020 23:07:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406599AbgLKWLg (ORCPT ); Fri, 11 Dec 2020 17:11:36 -0500 Received: from mail-oi1-f196.google.com ([209.85.167.196]:35010 "EHLO mail-oi1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406569AbgLKWLD (ORCPT ); Fri, 11 Dec 2020 17:11:03 -0500 Received: by mail-oi1-f196.google.com with SMTP id s2so11597683oij.2; Fri, 11 Dec 2020 14:10:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=atZrSis+DH0JxvPZXBDGMdQeM648/lZjeny5zjHLlh4=; b=NGYQ/rhwBf/ymts2BLTPGYpJmEUXcK2JFuKiwKl9scD818rgs4kUR8cdhWFqWHrdjP 4Tby73wMac5GSsfa1ccx28S+GXYVwVPnxb6H0YBeeMeYjeD9Pt1SrU+ahOfvlIil/yEv H5X7m7BteQhCX/s2O8v8UzOY7nICEP2ybxSRvjpysptUjVxor/GsYSAkR8boUlCuG7wY BwzlVgilFmCFA4GNdPiCbhyLYbZp+VpzkNPJrx2Bh32nfzxjsanpRtxG1l2cB6YuBDKI 6pA/A+g92rX2ett03JUri1+IIxIBDoe7EyOjBpEptFSj4lLzfZcEkcKchlDS54KVQd2L FAWQ== X-Gm-Message-State: AOAM531Sp17tELHgsJ7KQhIKeE5jAzu9I2porOsnp5K2HAQJClrkNu+D CzyXdqOtjbJDcfkMaEYD/w== X-Google-Smtp-Source: ABdhPJxQ/XLu6E05yQoPQukHMOuVz/UhY6MU1R6OfZWZeC6UcH7e+OhnToSo7cDuXpZE0DKJHbxnBQ== X-Received: by 2002:aca:58d7:: with SMTP id m206mr11170021oib.0.1607724621528; Fri, 11 Dec 2020 14:10:21 -0800 (PST) Received: from xps15.herring.priv (24-155-109-49.dyn.grandenetworks.net. [24.155.109.49]) by smtp.googlemail.com with ESMTPSA id p3sm2137383otf.3.2020.12.11.14.10.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Dec 2020 14:10:20 -0800 (PST) From: Rob Herring To: Lakshmi Ramasubramanian , takahiro.akashi@linaro.org, will@kernel.org, catalin.marinas@arm.com, mpe@ellerman.id.au Cc: Thiago Jung Bauermann , zohar@linux.ibm.com, james.morse@arm.com, sashal@kernel.org, benh@kernel.crashing.org, paulus@samba.org, frowand.list@gmail.com, vincenzo.frascino@arm.com, mark.rutland@arm.com, dmitry.kasatkin@gmail.com, jmorris@namei.org, serge@hallyn.com, pasha.tatashin@soleen.com, allison@lohutok.net, masahiroy@kernel.org, bhsharma@redhat.com, mbrugger@suse.com, hsinyi@chromium.org, tao.li@vivo.com, christophe.leroy@c-s.fr, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, prsriva@linux.microsoft.com, balajib@linux.microsoft.com Subject: [RFC PATCH 3/4] arm64: Use common of_kexec_setup_new_fdt() Date: Fri, 11 Dec 2020 16:10:05 -0600 Message-Id: <20201211221006.1052453-4-robh@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201211221006.1052453-1-robh@kernel.org> References: <20201211221006.1052453-1-robh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org Signed-off-by: Rob Herring Reviewed-by: Lakshmi Ramasubramanian Reviewed-by: Thiago Jung Bauermann Acked-by: Will Deacon --- arch/arm64/kernel/machine_kexec_file.c | 123 +------------------------ 1 file changed, 3 insertions(+), 120 deletions(-) diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c index 5b0e67b93cdc..7de9c47dee7c 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -15,23 +15,12 @@ #include #include #include +#include #include -#include #include #include #include #include -#include - -/* relevant device tree properties */ -#define FDT_PROP_KEXEC_ELFHDR "linux,elfcorehdr" -#define FDT_PROP_MEM_RANGE "linux,usable-memory-range" -#define FDT_PROP_INITRD_START "linux,initrd-start" -#define FDT_PROP_INITRD_END "linux,initrd-end" -#define FDT_PROP_BOOTARGS "bootargs" -#define FDT_PROP_KASLR_SEED "kaslr-seed" -#define FDT_PROP_RNG_SEED "rng-seed" -#define RNG_SEED_SIZE 128 const struct kexec_file_ops * const kexec_file_loaders[] = { &kexec_image_ops, @@ -50,112 +39,6 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image) return kexec_image_post_load_cleanup_default(image); } -static int setup_dtb(struct kimage *image, - unsigned long initrd_load_addr, unsigned long initrd_len, - char *cmdline, void *dtb) -{ - int off, ret; - - ret = fdt_path_offset(dtb, "/chosen"); - if (ret < 0) - goto out; - - off = ret; - - ret = fdt_delprop(dtb, off, FDT_PROP_KEXEC_ELFHDR); - if (ret && ret != -FDT_ERR_NOTFOUND) - goto out; - ret = fdt_delprop(dtb, off, FDT_PROP_MEM_RANGE); - if (ret && ret != -FDT_ERR_NOTFOUND) - goto out; - - if (image->type == KEXEC_TYPE_CRASH) { - /* add linux,elfcorehdr */ - ret = fdt_appendprop_addrrange(dtb, 0, off, - FDT_PROP_KEXEC_ELFHDR, - image->arch.elf_headers_mem, - image->arch.elf_headers_sz); - if (ret) - return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL); - - /* add linux,usable-memory-range */ - ret = fdt_appendprop_addrrange(dtb, 0, off, - FDT_PROP_MEM_RANGE, - crashk_res.start, - crashk_res.end - crashk_res.start + 1); - if (ret) - return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL); - } - - /* add bootargs */ - if (cmdline) { - ret = fdt_setprop_string(dtb, off, FDT_PROP_BOOTARGS, cmdline); - if (ret) - goto out; - } else { - ret = fdt_delprop(dtb, off, FDT_PROP_BOOTARGS); - if (ret && (ret != -FDT_ERR_NOTFOUND)) - goto out; - } - - /* add initrd-* */ - if (initrd_load_addr) { - ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_START, - initrd_load_addr); - if (ret) - goto out; - - ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_END, - initrd_load_addr + initrd_len); - if (ret) - goto out; - } else { - ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_START); - if (ret && (ret != -FDT_ERR_NOTFOUND)) - goto out; - - ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_END); - if (ret && (ret != -FDT_ERR_NOTFOUND)) - goto out; - } - - /* add kaslr-seed */ - ret = fdt_delprop(dtb, off, FDT_PROP_KASLR_SEED); - if (ret == -FDT_ERR_NOTFOUND) - ret = 0; - else if (ret) - goto out; - - if (rng_is_initialized()) { - u64 seed = get_random_u64(); - ret = fdt_setprop_u64(dtb, off, FDT_PROP_KASLR_SEED, seed); - if (ret) - goto out; - } else { - pr_notice("RNG is not initialised: omitting \"%s\" property\n", - FDT_PROP_KASLR_SEED); - } - - /* add rng-seed */ - if (rng_is_initialized()) { - void *rng_seed; - ret = fdt_setprop_placeholder(dtb, off, FDT_PROP_RNG_SEED, - RNG_SEED_SIZE, &rng_seed); - if (ret) - goto out; - get_random_bytes(rng_seed, RNG_SEED_SIZE); - } else { - pr_notice("RNG is not initialised: omitting \"%s\" property\n", - FDT_PROP_RNG_SEED); - } - -out: - if (ret) - return (ret == -FDT_ERR_NOSPACE) ? -ENOMEM : -EINVAL; - - return 0; -} - /* * More space needed so that we can add initrd, bootargs, kaslr-seed, * rng-seed, userable-memory-range and elfcorehdr. @@ -185,8 +68,8 @@ static int create_dtb(struct kimage *image, if (ret) return -EINVAL; - ret = setup_dtb(image, initrd_load_addr, initrd_len, - cmdline, buf); + ret = of_kexec_setup_new_fdt(image, buf, initrd_load_addr, + initrd_len, cmdline); if (ret) { vfree(buf); if (ret == -ENOMEM) { From patchwork Fri Dec 11 22:10:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rob Herring (Arm)" X-Patchwork-Id: 11969663 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E88EC4361B for ; Fri, 11 Dec 2020 23:04:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E81C821707 for ; Fri, 11 Dec 2020 23:04:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406604AbgLKWLg (ORCPT ); Fri, 11 Dec 2020 17:11:36 -0500 Received: from mail-oi1-f196.google.com ([209.85.167.196]:41179 "EHLO mail-oi1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406573AbgLKWLF (ORCPT ); Fri, 11 Dec 2020 17:11:05 -0500 Received: by mail-oi1-f196.google.com with SMTP id 15so11583703oix.8; Fri, 11 Dec 2020 14:10:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sZsbBtl86KBSh7SposuR5mWR/E5ADZV446RLtS+YWPM=; b=OGM6hCQT981eG6OOxJtcP0JGTGo+C1TyXNg7sL55GdS639UStuZbZ8kccriDvvI7Q4 XNOmXlopGXTZpJbMCkkysJvPUIhgGS0ClUBtKTXSb/RdVMox6eI61wpM6WM80SWqmW3p V1UGQU5IUlMc4HMwwC+T1wJIfNv5mw29JawBZb2FkEzS5FrK4dEFU62nxCq1A7DxP+3Q +FvHcwEvJWKGWzYqFAeNKs+QjA1v2oYngiIRqQeyyIh2boUJTJEhA22V56OydcbF3dzH NDBzcGHufQI5xZyYJvBSjNNuVUml2s9hUjZ8a4miakTEd2cB02c4WkuQ4kh13vEcI/Wi tC+w== X-Gm-Message-State: AOAM531R4xfQnnkfi8neprU3IblLR6JK4JUIOXgLOtC0pIQMsE1pY0ZO +U6rWFVaKR+yLTNCMveZuQ== X-Google-Smtp-Source: ABdhPJy5LRQclo0O7hCh4PpsWuQuDVh9fwWspX5Lxm/JR8O3OB2Pr/cajXyzNclFZ+tDC/nxzB8I+g== X-Received: by 2002:aca:ba42:: with SMTP id k63mr10639446oif.111.1607724624431; Fri, 11 Dec 2020 14:10:24 -0800 (PST) Received: from xps15.herring.priv (24-155-109-49.dyn.grandenetworks.net. [24.155.109.49]) by smtp.googlemail.com with ESMTPSA id p3sm2137383otf.3.2020.12.11.14.10.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Dec 2020 14:10:23 -0800 (PST) From: Rob Herring To: Lakshmi Ramasubramanian , takahiro.akashi@linaro.org, will@kernel.org, catalin.marinas@arm.com, mpe@ellerman.id.au Cc: Thiago Jung Bauermann , zohar@linux.ibm.com, james.morse@arm.com, sashal@kernel.org, benh@kernel.crashing.org, paulus@samba.org, frowand.list@gmail.com, vincenzo.frascino@arm.com, mark.rutland@arm.com, dmitry.kasatkin@gmail.com, jmorris@namei.org, serge@hallyn.com, pasha.tatashin@soleen.com, allison@lohutok.net, masahiroy@kernel.org, bhsharma@redhat.com, mbrugger@suse.com, hsinyi@chromium.org, tao.li@vivo.com, christophe.leroy@c-s.fr, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, prsriva@linux.microsoft.com, balajib@linux.microsoft.com Subject: [RFC PATCH 4/4] powerpc: Use common of_kexec_setup_new_fdt() Date: Fri, 11 Dec 2020 16:10:06 -0600 Message-Id: <20201211221006.1052453-5-robh@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201211221006.1052453-1-robh@kernel.org> References: <20201211221006.1052453-1-robh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org Signed-off-by: Rob Herring Reviewed-by: Lakshmi Ramasubramanian Reviewed-by: Thiago Jung Bauermann --- After the IMA changes, delete_fdt_mem_rsv() can also be removed. arch/powerpc/kexec/file_load.c | 125 ++------------------------------- 1 file changed, 6 insertions(+), 119 deletions(-) -- 2.25.1 diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c index e452b11df631..956bcb2d1ec2 100644 --- a/arch/powerpc/kexec/file_load.c +++ b/arch/powerpc/kexec/file_load.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -156,132 +157,18 @@ int setup_new_fdt(const struct kimage *image, void *fdt, unsigned long initrd_load_addr, unsigned long initrd_len, const char *cmdline) { - int ret, chosen_node; - const void *prop; - - /* Remove memory reservation for the current device tree. */ - ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params), - fdt_totalsize(initial_boot_params)); - if (ret == 0) - pr_debug("Removed old device tree reservation.\n"); - else if (ret != -ENOENT) - return ret; - - chosen_node = fdt_path_offset(fdt, "/chosen"); - if (chosen_node == -FDT_ERR_NOTFOUND) { - chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), - "chosen"); - if (chosen_node < 0) { - pr_err("Error creating /chosen.\n"); - return -EINVAL; - } - } else if (chosen_node < 0) { - pr_err("Malformed device tree: error reading /chosen.\n"); - return -EINVAL; - } - - /* Did we boot using an initrd? */ - prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL); - if (prop) { - uint64_t tmp_start, tmp_end, tmp_size; - - tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop)); - - prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL); - if (!prop) { - pr_err("Malformed device tree.\n"); - return -EINVAL; - } - tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop)); - - /* - * kexec reserves exact initrd size, while firmware may - * reserve a multiple of PAGE_SIZE, so check for both. - */ - tmp_size = tmp_end - tmp_start; - ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size); - if (ret == -ENOENT) - ret = delete_fdt_mem_rsv(fdt, tmp_start, - round_up(tmp_size, PAGE_SIZE)); - if (ret == 0) - pr_debug("Removed old initrd reservation.\n"); - else if (ret != -ENOENT) - return ret; - - /* If there's no new initrd, delete the old initrd's info. */ - if (initrd_len == 0) { - ret = fdt_delprop(fdt, chosen_node, - "linux,initrd-start"); - if (ret) { - pr_err("Error deleting linux,initrd-start.\n"); - return -EINVAL; - } - - ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end"); - if (ret) { - pr_err("Error deleting linux,initrd-end.\n"); - return -EINVAL; - } - } - } - - if (initrd_len) { - ret = fdt_setprop_u64(fdt, chosen_node, - "linux,initrd-start", - initrd_load_addr); - if (ret < 0) - goto err; - - /* initrd-end is the first address after the initrd image. */ - ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end", - initrd_load_addr + initrd_len); - if (ret < 0) - goto err; - - ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len); - if (ret) { - pr_err("Error reserving initrd memory: %s\n", - fdt_strerror(ret)); - return -EINVAL; - } - } - - if (cmdline != NULL) { - ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline); - if (ret < 0) - goto err; - } else { - ret = fdt_delprop(fdt, chosen_node, "bootargs"); - if (ret && ret != -FDT_ERR_NOTFOUND) { - pr_err("Error deleting bootargs.\n"); - return -EINVAL; - } - } + int ret; - if (image->type == KEXEC_TYPE_CRASH) { - /* - * Avoid elfcorehdr from being stomped on in kdump kernel by - * setting up memory reserve map. - */ - ret = fdt_add_mem_rsv(fdt, image->arch.elf_headers_mem, - image->arch.elf_headers_sz); - if (ret) { - pr_err("Error reserving elfcorehdr memory: %s\n", - fdt_strerror(ret)); - goto err; - } - } + ret = of_kexec_setup_new_fdt(image, fdt, initrd_load_addr, initrd_len, cmdline); + if (ret) + goto err; - ret = setup_ima_buffer(image, fdt, chosen_node); + ret = setup_ima_buffer(image, fdt, fdt_path_offset(fdt, "/chosen")); if (ret) { pr_err("Error setting up the new device tree.\n"); return ret; } - ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0); - if (ret) - goto err; - return 0; err: