Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6215519iob; Tue, 10 May 2022 13:03:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTw6IIFifBGG6ERMmMwbbH5T/8sGyy1tcVKTF/0Mj6ueph3QEoJnwR6FYvxghaiXqFtvMP X-Received: by 2002:a65:404b:0:b0:3c5:ffca:54a4 with SMTP id h11-20020a65404b000000b003c5ffca54a4mr18479321pgp.152.1652212992440; Tue, 10 May 2022 13:03:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652212992; cv=none; d=google.com; s=arc-20160816; b=m6Bu4DcRG9UpAQEZI2kY32DPMkWWOYVDw1Y9kHNhe5YhGa8RdJh3f2bA7anf7gpLSq /yv5y63hwuWXwn1qn6bYYoLx01G+/rJDdSJzFIk1b228hEtvv8m4SOp9aU7+bf2JtH1b yQXZeNtMG5sxOFdLktc2bB6LnQWOlQbskjCr2owUXCa+5201C54iOPRYXextpg8cqoK6 ySr751PAskWp7ETA3n96Gdvg6k5IR/q8gb4BM2+dCEioLjb0Aa6VFkluvkkKRPmBSfT2 V0SsH3uEwg8gyM8c7dwAyqZzLNa6dEPmmefFJMyVJ9bjbiU3duglveu2lS8gnm8VBxGH B9oA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=V5UfXzzIfIdpPL09x5scouxhG3kWOe545UMd9XA9Yig=; b=yFnu1/+24KBiSVBmcuaGNr8wS3hGXP9drp8A3x40/qJ+kqcz6bV6A/+o1P857xx8e3 JhOGruelIkMIseRMrEIlmscNXW++8w0Gltpu780iCrFZhYBwGGzG1/nsH5XOXIurwQXc 6i0D/LynX9CIoR2216O7vGIbryq9subGyhaeX9v2N/HN4nIbV4pJ4sljeqE244wuoa6s OCTNgouLxfyPuK9BpaQjq7ZUYDEjn49uM5p13Qw+1nGvkDS1LboB6cIrq0Qox7EU2FMx 2lF2IoimBkiaDuxa5N9u8FEipRJGlZBk1XdmB85MsR4v9n/UO8LXJcfT58G2Ia6DjK4X lsbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=h+mGm2Kv; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y190-20020a6364c7000000b003aafa3f12e8si224908pgb.492.2022.05.10.13.02.56; Tue, 10 May 2022 13:03:12 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=h+mGm2Kv; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245662AbiEJN6e (ORCPT + 99 others); Tue, 10 May 2022 09:58:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245202AbiEJNii (ORCPT ); Tue, 10 May 2022 09:38:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9F092980A; Tue, 10 May 2022 06:28:46 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 644EB60C1C; Tue, 10 May 2022 13:28:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 70378C385A6; Tue, 10 May 2022 13:28:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652189325; bh=iGVnxWxoOhBcJQqdzfpfv49ak3HYriGUEF0owT0UMQs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h+mGm2KvLp20rrWfzZ0I0nYZ5E/bv0/uLRjYcuyyFAuKcfFQdpxImTAAShFHkkAHN Ipik3pswvOdPvtZ2UIGjuxpMRSlgnKTTl8v/9xBwusTNK5Sa/d4nzK0B1vW+HY1Mx4 fuI/DgVrPXVJVY+a1rt/4e+EuYHBbrhkZKU3sr6k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nick Kossifidis , Conor Dooley , Palmer Dabbelt Subject: [PATCH 5.15 015/135] RISC-V: relocate DTB if its outside memory region Date: Tue, 10 May 2022 15:06:37 +0200 Message-Id: <20220510130740.836495192@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220510130740.392653815@linuxfoundation.org> References: <20220510130740.392653815@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 From: Nick Kossifidis commit c6fe81191bd74f7e6ae9ce96a4837df9485f3ab8 upstream. In case the DTB provided by the bootloader/BootROM is before the kernel image or outside /memory, we won't be able to access it through the linear mapping, and get a segfault on setup_arch(). Currently OpenSBI relocates DTB but that's not always the case (e.g. if FW_JUMP_FDT_ADDR is not specified), and it's also not the most portable approach since the default FW_JUMP_FDT_ADDR of the generic platform relocates the DTB at a specific offset that may not be available. To avoid this situation copy DTB so that it's visible through the linear mapping. Signed-off-by: Nick Kossifidis Link: https://lore.kernel.org/r/20220322132839.3653682-1-mick@ics.forth.gr Tested-by: Conor Dooley Fixes: f105aa940e78 ("riscv: add BUILTIN_DTB support for MMU-enabled targets") Cc: stable@vger.kernel.org Signed-off-by: Palmer Dabbelt Signed-off-by: Greg Kroah-Hartman --- arch/riscv/mm/init.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -218,8 +218,25 @@ static void __init setup_bootmem(void) * early_init_fdt_reserve_self() since __pa() does * not work for DTB pointers that are fixmap addresses */ - if (!IS_ENABLED(CONFIG_BUILTIN_DTB)) - memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va)); + if (!IS_ENABLED(CONFIG_BUILTIN_DTB)) { + /* + * In case the DTB is not located in a memory region we won't + * be able to locate it later on via the linear mapping and + * get a segfault when accessing it via __va(dtb_early_pa). + * To avoid this situation copy DTB to a memory region. + * Note that memblock_phys_alloc will also reserve DTB region. + */ + if (!memblock_is_memory(dtb_early_pa)) { + size_t fdt_size = fdt_totalsize(dtb_early_va); + phys_addr_t new_dtb_early_pa = memblock_phys_alloc(fdt_size, PAGE_SIZE); + void *new_dtb_early_va = early_memremap(new_dtb_early_pa, fdt_size); + + memcpy(new_dtb_early_va, dtb_early_va, fdt_size); + early_memunmap(new_dtb_early_va, fdt_size); + _dtb_early_pa = new_dtb_early_pa; + } else + memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va)); + } early_init_fdt_scan_reserved_mem(); dma_contiguous_reserve(dma32_phys_limit);