From patchwork Tue Apr 23 22:47:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serge Semin X-Patchwork-Id: 10913801 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5C78314DB for ; Tue, 23 Apr 2019 22:50:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4D966287EA for ; Tue, 23 Apr 2019 22:50:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3E8472887A; Tue, 23 Apr 2019 22:50:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D276A2887A for ; Tue, 23 Apr 2019 22:50:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728852AbfDWWt5 (ORCPT ); Tue, 23 Apr 2019 18:49:57 -0400 Received: from mail-lf1-f66.google.com ([209.85.167.66]:46699 "EHLO mail-lf1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728647AbfDWWtm (ORCPT ); Tue, 23 Apr 2019 18:49:42 -0400 Received: by mail-lf1-f66.google.com with SMTP id k18so12985435lfj.13; Tue, 23 Apr 2019 15:49:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VJ3xwWJXog1KPx7zXkBt+0FT1onV4wm00T3cYGUdK8o=; b=dfuEeI/xqrZujHXykgX4UeKXjmUGMkHpprck0gZu6b9Bl+E4QQtmhiG0xc2Hno05dN 8NwdxoNghrzZa9uBXrKYxfALB8W0i2pkIt9A6r8VGoZIp4MJw/gte2dfoBfDvQaE9YoX 5qKAZfvywTQAZJ1LFEqWlqlbkL7uAqlkt/3kzxq16ZwCtG2CH8rNUjnacYPTjQHY4Q4T 5MBHpcQ6QrTZXh1oLEDtsPMmOGPJaHaSRO4wKCk389PNku/iR15uRauOVW2a/qWln0/y ScMdJNPbgjTei27drIJdmMnqPBQgtP3o4YsBPActjbdsufmqVipYWtTnNySptsptVU2V rvBg== 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=VJ3xwWJXog1KPx7zXkBt+0FT1onV4wm00T3cYGUdK8o=; b=bAGyEwtIF9JKMFfYJxnIqf/kbgITNZycpdv3ZWK2dDIkPK4u+d+sMvY6xyaTtQTBIf 33qCDv6PcskbPqRnxVIbKCob4WX6t/YOMhobcsLqmCdHLe124a5bh4ASFb2U/Of3uiTb pUoBplqTda/Jge84aJ0+YS5wcshNESiqp4dhlI8667HUr+EURMeOJbF/6sgelsmL4uEV IHqK8QDXnPZQZGgYN1IFZNtnmuSPYMw3Hd1IoRaJL7D0P2IU8Alcdm0jOknXJGaahJx8 fqUSCB/VwGx27Z8OYWs4aTAz/vcCskdPhyGApR8SuD9GrVWy8LfLzypjoRtsH13AxcV7 6ouA== X-Gm-Message-State: APjAAAWcYMTmqnJ9BDf+c7Z1hhaC/jK7xNoi/F9C0Hjnzo3sSMfQLUxD G4CLol9d6E/Xov9PP1MdkdM= X-Google-Smtp-Source: APXvYqyST+f6mO8FvKsw8ef7ytV8eNa4Hb8jVjd1N5jWDJW1Qzvmq1+c+eepJUlQYzYSmUbWjijskQ== X-Received: by 2002:a19:c216:: with SMTP id l22mr14887697lfc.112.1556059779541; Tue, 23 Apr 2019 15:49:39 -0700 (PDT) Received: from localhost.localdomain ([5.164.240.123]) by smtp.gmail.com with ESMTPSA id w2sm4904722lfa.63.2019.04.23.15.49.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 23 Apr 2019 15:49:38 -0700 (PDT) From: Serge Semin To: Ralf Baechle , Paul Burton , James Hogan , Matt Redfearn , Mike Rapoport , Andrew Morton , Michal Hocko , Greg Kroah-Hartman , Thomas Bogendoerfer , Huacai Chen , Stefan Agner , Stephen Rothwell , Alexandre Belloni , Juergen Gross Cc: linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, Serge Semin Subject: [PATCH 11/12] mips: Make sure dt memory regions are valid Date: Wed, 24 Apr 2019 01:47:47 +0300 Message-Id: <20190423224748.3765-12-fancer.lancer@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190423224748.3765-1-fancer.lancer@gmail.com> References: <20190423224748.3765-1-fancer.lancer@gmail.com> MIME-Version: 1.0 Sender: linux-mips-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mips@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP There are situations when memory regions coming from dts may be too big for the platform physical address space. It especially concerns XPA-capable systems. Bootleader may determine more than 4GB memory available and pass it to the kernel over dts memory node, while kernel is built without XPA support. In this case the region may either simply be truncated by add_memory_region() method or by u64->phys_addr_t type casting. But in worst case the method can even drop the memory region if it exceedes PHYS_ADDR_MAX size. So lets make sure the retrieved from dts memory regions are valid, and if some of them isn't just manually truncate it with a warning printed out. Signed-off-by: Serge Semin --- arch/mips/kernel/prom.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/arch/mips/kernel/prom.c b/arch/mips/kernel/prom.c index 437a174e3ef9..28bf01961bb2 100644 --- a/arch/mips/kernel/prom.c +++ b/arch/mips/kernel/prom.c @@ -41,7 +41,19 @@ char *mips_get_machine_name(void) #ifdef CONFIG_USE_OF void __init early_init_dt_add_memory_arch(u64 base, u64 size) { - return add_memory_region(base, size, BOOT_MEM_RAM); + if (base >= PHYS_ADDR_MAX) { + pr_warn("Trying to add an invalid memory region, skipped\n"); + return; + } + + /* Truncate the passed memory region instead of type casting */ + if (base + size - 1 >= PHYS_ADDR_MAX || base + size < base) { + pr_warn("Truncate memory region %llx @ %llx to size %llx\n", + size, base, PHYS_ADDR_MAX - base); + size = PHYS_ADDR_MAX - base; + } + + add_memory_region(base, size, BOOT_MEM_RAM); } int __init early_init_dt_reserve_memory_arch(phys_addr_t base,