Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2896845pxb; Tue, 24 Aug 2021 10:06:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz1AY6/8gCNUmqmjH2MZYcpCk53cxg28kErw+ZMI5bPhLDDdN/Or8k5Yax7lvFEWXeD/Bq9 X-Received: by 2002:a17:906:8257:: with SMTP id f23mr10511091ejx.509.1629824766169; Tue, 24 Aug 2021 10:06:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629824766; cv=none; d=google.com; s=arc-20160816; b=bKF7iUPbJsjBq0diAQzQWCgB+aW5UYb9O1jDrS0oIzW0yoq+qGekZeVKVsmtRkQiBB 6lecT5LJ8ud/hg0Tf+HUGeC77k7OepDMmNk4TMkxUwSuZY4EmS3N07b74TIbWVuM7rkO xzd3j1QINCDBEyBeJ5NRUc13sAzOv538bBoLr6qfUt8lD2lyCz5JoD5DCMsS2ubGU3/x j6Ka/2+lnQjl9wBxw2Opp9ruymuqVQhcRdRkm7oPq6/D2cg/pRqZEQnvZwiskpQIuylq px2BMEEi7VXBdqix/Isx8YeCTDyx1mn7rnSkI6IkMSWd7p6V+kJuNRS699zPiUs6BIiU gmxA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=yac76T8Uswt4y1I6rQ4BPSt1N1hthxTFbvTZKNO9Vdo=; b=K4/d2AIQo0kWr3n1aEPW1h3UlBLg/PeBE3ENf07Gmt7QohXxHQ/tTezo/jqllDlxVa Vp6ZG7il/MukuP1skpdbB4LTmzwH7gpwmOfciMctWB/st0vST1uRAyCjqcV+/zuE8KiG k3N9Fh/j86wlQifKpVTA0iLUPJLtgIm7DN2RwDxk+fuY6sJ8quQpgJyTY7RwWrJBKe9e 5pHJaIYwhJcbIUhlMQeggp67koWU4jWInXAgPK7m6rlZkuoTwOYsdd5OWA/3AkQfzxL3 zrkPf1H90uiHWnac8T2n0a4Z5kG0epeXPxv7McUCZaOlO7z5Vv+AE+YZfR2QM8dWTzfq mZSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Xd/k0KFZ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id dz8si17599992edb.137.2021.08.24.10.05.41; Tue, 24 Aug 2021 10:06:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Xd/k0KFZ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S239722AbhHXREs (ORCPT + 99 others); Tue, 24 Aug 2021 13:04:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:39206 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239084AbhHXRBi (ORCPT ); Tue, 24 Aug 2021 13:01:38 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B34446140B; Tue, 24 Aug 2021 16:58:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629824284; bh=pP6bmhH3tT/9yS0Lw3n8AP5dHpZMt8HdRW2hU46QoyE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xd/k0KFZ9eC+xre8R5sNpWudrj68D+wuBuEzvZFPl9R92Y9++LlYdVmJ+gQjxWmfs OTQdUk5I+Z6FiZAlH2MPlbS7xCYVJMFinnowGQ6ZIGPZ5jls/BHP9cAhRf+MLxC6VX KoQoWqIv4fqKbvzyW6pP8fWBF/zasybK722DhcmoOfeLqhFafu9aQzPbvsqwbS6R2J 4cCaYVqloExCp8D+eS2RHudnqLSgmbof/DMTTPmwjCXpIjdE0CyuUJPkWQqp97FRHj 60zJsvkzS1tb6hrYjTTGdMM5huyA019MXRn+WiTtM2x5mbUlMoV/k7kmtyQzA4ALdi 87eUJJmfl3B8g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Petr Pavlu , Sunil V L , Nick Kossifidis , Palmer Dabbelt , Sasha Levin Subject: [PATCH 5.13 119/127] riscv: Fix a number of free'd resources in init_resources() Date: Tue, 24 Aug 2021 12:55:59 -0400 Message-Id: <20210824165607.709387-120-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210824165607.709387-1-sashal@kernel.org> References: <20210824165607.709387-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.13.13-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.13.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.13.13-rc1 X-KernelTest-Deadline: 2021-08-26T16:55+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Petr Pavlu [ Upstream commit aa3e1ba32e553e611a58145c2eb349802feaa6eb ] Function init_resources() allocates a boot memory block to hold an array of resources which it adds to iomem_resource. The array is filled in from its end and the function then attempts to free any unused memory at the beginning. The problem is that size of the unused memory is incorrectly calculated and this can result in releasing memory which is in use by active resources. Their data then gets corrupted later when the memory is reused by a different part of the system. Fix the size of the released memory to correctly match the number of unused resource entries. Fixes: ffe0e5261268 ("RISC-V: Improve init_resources()") Signed-off-by: Petr Pavlu Reviewed-by: Sunil V L Acked-by: Nick Kossifidis Tested-by: Sunil V L Cc: stable@vger.kernel.org Signed-off-by: Palmer Dabbelt Signed-off-by: Sasha Levin --- arch/riscv/kernel/setup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c index 9a1b7a0603b2..f2a9cd4284b0 100644 --- a/arch/riscv/kernel/setup.c +++ b/arch/riscv/kernel/setup.c @@ -230,8 +230,8 @@ static void __init init_resources(void) } /* Clean-up any unused pre-allocated resources */ - mem_res_sz = (num_resources - res_idx + 1) * sizeof(*mem_res); - memblock_free(__pa(mem_res), mem_res_sz); + if (res_idx >= 0) + memblock_free(__pa(mem_res), (res_idx + 1) * sizeof(*mem_res)); return; error: -- 2.30.2