Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp4887987imm; Wed, 30 May 2018 14:10:08 -0700 (PDT) X-Google-Smtp-Source: ADUXVKISyHAjXltmvPK5npFEcuPQqeQtpebLa8cBNKRdo5yBdmROmhtEzaI77RJnQFfEYlKBlh6i X-Received: by 2002:a17:902:7883:: with SMTP id q3-v6mr4349491pll.71.1527714608308; Wed, 30 May 2018 14:10:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527714608; cv=none; d=google.com; s=arc-20160816; b=RfwQzwHh2b8EUuDaL0paoKHn4tPEhe6934O1fGaIFW10V5w4bmC10j1arFrKIJZPjt YuvGRAHz/VLzYqGU/lyMHmk6+uaFfHCBqh9HPUgtK+aEoBgCgXr5ojLa6su7Ea1knzzw c4Azdvbg3U/E1YcUVICgkTvqa5nbjTIetRK/4GCD+QhqNxsJIXH2Lfp/hPSKdfZT5E41 9iWBMx+zgpT275CgflvYJEvnLZbgFCRzUe2bK+0zbFcJl1ieN4qUQEYOkS3Ke76lQHtI ELSNdgVpS+uGGUMcvaES75EhswVOhPLScqEjyUjGINQM6m8uA9QBrqMIurS1bCC2S8V0 9ERA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=PAAHaVDPY8DS2u06pFzI1Qlaogc7xwVRxd8zF6QZfoQ=; b=izrbCEAymQwefs7hkPNN37T5s8iXV1p7odaGnonYa5x0CN8qbwQ4Smo1NSUKGUJKxW GD4pZbzOeuvtsfmtgWB+imL7c4kQQmc7kD7M39BN5+xQ5BQS0zVQJCvIWkx4AOaq3IMk TATS20moP1ZRIQTCa/OkDtFztYiKJzmYTFFEnwYNKv3yJYyZEX04UL+XOvbAdQTL5KBj +jv3uNmx7taguK16LXb8Q/Z1qdHr4RKNPl3uDKqJ5QpEnEta4j3i220SCC2XULxlp2u3 wofENFPVLzHlWG5BaZKShR6THePMQJYSLnCl1mQR+wm6bu/GjZ/Ejx4y1w0LDy2y3gGN lzOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Iq89ky72; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w5-v6si34481875plz.356.2018.05.30.14.09.54; Wed, 30 May 2018 14:10:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Iq89ky72; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932597AbeE3VI6 (ORCPT + 99 others); Wed, 30 May 2018 17:08:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:54636 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932365AbeE3VIx (ORCPT ); Wed, 30 May 2018 17:08:53 -0400 Received: from mail-qt0-f174.google.com (mail-qt0-f174.google.com [209.85.216.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A7F5720881; Wed, 30 May 2018 21:08:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527714532; bh=qnGb59BByw49le71zXa87bc9/0n212m250e0FySBq4E=; h=In-Reply-To:References:From:Date:Subject:To:Cc:From; b=Iq89ky72BmBUVZCZyjy3hli1uhiwMKkFKMspUVnBVzIZG8Vsg+AxaMmqZfN8AFSta /D/VSHhwjU7w40qlz9mnylFRbPgzLlRov4/kloD65cSyLyFcLlk+zvDFMef/95XY9B 4yEKg9zbhdnpvyDKut9bTG+pbgkkrjYcqxEwPwR0= Received: by mail-qt0-f174.google.com with SMTP id e8-v6so25212054qth.0; Wed, 30 May 2018 14:08:52 -0700 (PDT) X-Gm-Message-State: APt69E21gDHDoyppzLbCk/qR+h94aDHAVIXhVzziiSgXJ6q9tqSEr79+ y079wW1aDtw4xfrQDjFYPVG+YDVvdA6sAdx3IQ== X-Received: by 2002:a0c:f711:: with SMTP id w17-v6mr4200076qvn.25.1527714531850; Wed, 30 May 2018 14:08:51 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a0c:9b02:0:0:0:0:0 with HTTP; Wed, 30 May 2018 14:08:31 -0700 (PDT) In-Reply-To: <20180530052142.24761-1-jaewon31.kim@samsung.com> References: <20180530052142.24761-1-jaewon31.kim@samsung.com> From: Rob Herring Date: Wed, 30 May 2018 16:08:31 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] drivers: of: of_reserved_mem: detect count overflow or range overlap To: Jaewon Kim Cc: Marek Szyprowski , Mitchel Humpherys , Frank Rowand , devicetree@vger.kernel.org, linux-mm@kvack.org, "linux-kernel@vger.kernel.org" , Jaewon Kim Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 30, 2018 at 12:21 AM, Jaewon Kim wrote: > During development, number of reserved memory region could be increased > and a new region could be unwantedly overlapped. In that case the new > region may work well but one of exisiting region could be affected so > that it would not be defined properly. It may require time consuming > work to find reason that there is a newly added region. > > If a newly added region invoke kernel panic, it will be helpful. This > patch records if there is count overflow or range overlap, and invoke > panic if that case. > > These are test example based on v4.9. > > Case 1 - out of region count > <3>[ 0.000000] [0: swapper: 0] OF: reserved mem: not enough space all defined regions. > <0>[ 1.688695] [6: swapper/0: 1] Kernel panic - not syncing: overflow on reserved memory, check the latest change > <4>[ 1.688743] [6: swapper/0: 1] CPU: 6 PID: 1 Comm: swapper/0 Not tainted 4.9.65+ #10 > <4>[ 1.688836] [6: swapper/0: 1] Call trace: > <4>[ 1.688869] [6: swapper/0: 1] [] dump_backtrace+0x0/0x248 > <4>[ 1.688913] [6: swapper/0: 1] [] show_stack+0x18/0x28 > <4>[ 1.688958] [6: swapper/0: 1] [] dump_stack+0x98/0xc0 > <4>[ 1.689001] [6: swapper/0: 1] [] panic+0x1e0/0x404 > <4>[ 1.689046] [6: swapper/0: 1] [] check_reserved_mem+0x40/0x50 > <4>[ 1.689091] [6: swapper/0: 1] [] do_one_initcall+0x54/0x214 > <4>[ 1.689138] [6: swapper/0: 1] [] kernel_init_freeable+0x198/0x24c > <4>[ 1.689187] [6: swapper/0: 1] [] kernel_init+0x18/0x144 > <4>[ 1.689229] [6: swapper/0: 1] [] ret_from_fork+0x10/0x40 > > Case 2 - overlapped region > <3>[ 0.000000] [0: swapper: 0] OF: reserved mem: OVERLAP DETECTED! > <0>[ 2.309331] [2: swapper/0: 1] Kernel panic - not syncing: reserved memory overlap, check the latest change > <4>[ 2.309398] [2: swapper/0: 1] CPU: 2 PID: 1 Comm: swapper/0 Not tainted 4.9.65+ #14 > <4>[ 2.309508] [2: swapper/0: 1] Call trace: > <4>[ 2.309546] [2: swapper/0: 1] [] dump_backtrace+0x0/0x248 > <4>[ 2.309599] [2: swapper/0: 1] [] show_stack+0x18/0x28 > <4>[ 2.309652] [2: swapper/0: 1] [] dump_stack+0x98/0xc0 > <4>[ 2.309701] [2: swapper/0: 1] [] panic+0x1e0/0x404 > <4>[ 2.309751] [2: swapper/0: 1] [] check_reserved_mem+0x4c/0x50 > <4>[ 2.309802] [2: swapper/0: 1] [] do_one_initcall+0x54/0x214 > <4>[ 2.309856] [2: swapper/0: 1] [] kernel_init_freeable+0x198/0x24c > <4>[ 2.309913] [2: swapper/0: 1] [] kernel_init+0x18/0x144 > <4>[ 2.309961] [2: swapper/0: 1] [] ret_from_fork+0x10/0x40 > > Signed-off-by: Jaewon Kim > --- > drivers/of/of_reserved_mem.c | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c > index 9a4f4246231d..e97d5c5dcc9a 100644 > --- a/drivers/of/of_reserved_mem.c > +++ b/drivers/of/of_reserved_mem.c > @@ -65,6 +65,7 @@ int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, > } > #endif > > +static bool rmem_overflow; > /** > * res_mem_save_node() - save fdt node for second pass initialization > */ > @@ -75,6 +76,7 @@ void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname, > > if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) { > pr_err("not enough space all defined regions.\n"); > + rmem_overflow = true; > return; > } > > @@ -221,6 +223,7 @@ static int __init __rmem_cmp(const void *a, const void *b) > return 0; > } > > +static bool rmem_overlap; > static void __init __rmem_check_for_overlap(void) > { > int i; > @@ -245,6 +248,7 @@ static void __init __rmem_check_for_overlap(void) > pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n", > this->name, &this->base, &this_end, > next->name, &next->base, &next_end); > + rmem_overlap = true; > } > } > } > @@ -419,3 +423,13 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np) > return NULL; > } > EXPORT_SYMBOL_GPL(of_reserved_mem_lookup); > + > +static int check_reserved_mem(void) > +{ > + if (rmem_overflow) > + panic("overflow on reserved memory, check the latest change"); > + if (rmem_overlap) > + panic("overlap on reserved memory, check the latest change"); > + return 0; > +} > +late_initcall(check_reserved_mem); Just use WARN and avoid this dance you are doing to panic after your console is up. Rob