From patchwork Mon Jun 18 11:40:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sudeep Holla X-Patchwork-Id: 138893 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp3846129lji; Mon, 18 Jun 2018 04:40:16 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLKS2x6Ka++4yV3iiyMQ2y0mnWcsA+M3Zc9mXD8Jyz6FsOO2il3+4Rmr06K6KiMatfqwd1c X-Received: by 2002:a17:902:8a4:: with SMTP id 33-v6mr13496008pll.343.1529322016706; Mon, 18 Jun 2018 04:40:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529322016; cv=none; d=google.com; s=arc-20160816; b=roJFZuI5OsaLR1RAKgbp2qAZ3ReGnpe2+kdpIzsN8LhqFzVnojihZwCIXayvKDpnW4 UzAjsbbu6EUpwaGgXK+rjBXZ67UquLBKFFmwnbRd6vyG3Kog1Bj8ZcGuftaHU4DBbZTx 9dDY1viLGXdE69CP6MvDhW64NIDxBGsPDBijXsvoNP7I8ou6v0eM5fgGWPbSqkgY5MRf BDQn3MxI9cdD42VCvkjVzGiI4UrbbpOhVY0RysLCL6wXrM7F8W7esWzAzg5twf8mKYib kzvTsvWm3815UEiba4vspvSJwoDmEEmhMKN47RFn8QqASh2PMnWMe726iy3pd5Vo2/GM Wifw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=1tdeYeayPR0rewhPCro7h0nRvSP5vYsKkF1W1OEkGaQ=; b=GVVO80A7IsmifDLKtACEKEbUrRAkW/YKl9jGFEzQOJ8K7amIURcKO01NMJTNBSCJfV 9Pk+XdEnfBzFWpBGpOQc3Dmg0Qjj2jw6+01amaIKPISgmKAN+5mBbzynccpxGHWcuuBv JYVjLozptFSdP7xJ1oQ05Kk6zPYQdSYVCqYp1TogBHCovjD6+P7tRR8fzclkr0yuwabe qai2YEdG0/9ZrrD6pUGdB050HFgAPH37qnRyXQrKR2oAvw4U3WG44n21HPB63QztHocY xBE6RANvIccoKDXKdQmY7lT5dPokoViLiGJBIw3oa61bZMB0TwGVZH5Ob2swt9nBmaUj KkeQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u190-v6si14538221pfb.325.2018.06.18.04.40.16; Mon, 18 Jun 2018 04:40:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933501AbeFRLkP (ORCPT + 10 others); Mon, 18 Jun 2018 07:40:15 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:60606 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933381AbeFRLkP (ORCPT ); Mon, 18 Jun 2018 07:40:15 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C868F80D; Mon, 18 Jun 2018 04:40:14 -0700 (PDT) Received: from usa.arm.com (e107155-lin.cambridge.arm.com [10.1.210.28]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 710A93F25D; Mon, 18 Jun 2018 04:40:13 -0700 (PDT) From: Sudeep Holla To: Sebastian Reichel , linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Sudeep Holla , Liviu Dudau , Lorenzo Pieralisi , Sebastian Reichel Subject: [PATCH] power: vexpress: fix corruption in notifier registration Date: Mon, 18 Jun 2018 12:40:07 +0100 Message-Id: <1529322007-4637-1-git-send-email-sudeep.holla@arm.com> X-Mailer: git-send-email 2.7.4 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Vexpress platforms provide two different restart handlers: SYS_REBOOT that restart the entire system, while DB_RESET only restarts the daughter board containing the CPU. DB_RESET is overridden by SYS_REBOOT if it exists. notifier_chain_register used in register_restart_handler by design allows notifier to be registered once only, however vexpress restart notifier can get registered twice. When this happen it corrupts list of notifiers, as result some notifiers can be not called on proper event, traverse on list can be cycled forever, and second unregister can access already freed memory. So far, since this was the only restart handler in the system, no issue was observed even if the same notifier was registered twice. However commit 6c5c0d48b686 ("watchdog: sp805: add restart handler") added support for SP805 restart handlers and since the system under test contains two vexpress restart and two SP805 watchdog instances, it was observed that during the boot traversing the restart handler list looped forever as there's a cycle in that list resulting in boot hang. This patch fixes the issues by ensuring that the notifier is installed only once. Cc: Sebastian Reichel Signed-off-by: Sudeep Holla --- drivers/power/reset/vexpress-poweroff.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) -- 2.7.4 diff --git a/drivers/power/reset/vexpress-poweroff.c b/drivers/power/reset/vexpress-poweroff.c index 102f95a09460..cdc68eb06a91 100644 --- a/drivers/power/reset/vexpress-poweroff.c +++ b/drivers/power/reset/vexpress-poweroff.c @@ -35,6 +35,7 @@ static void vexpress_reset_do(struct device *dev, const char *what) } static struct device *vexpress_power_off_device; +static atomic_t vexpress_restart_nb_refcnt = ATOMIC_INIT(0); static void vexpress_power_off(void) { @@ -96,13 +97,16 @@ static const struct of_device_id vexpress_reset_of_match[] = { static int _vexpress_register_restart_handler(struct device *dev) { - int err; + int err = 0; vexpress_restart_device = dev; - err = register_restart_handler(&vexpress_restart_nb); - if (err) { - dev_err(dev, "cannot register restart handler (err=%d)\n", err); - return err; + if (atomic_inc_return(&vexpress_restart_nb_refcnt) == 1) { + err = register_restart_handler(&vexpress_restart_nb); + if (err) { + dev_err(dev, "cannot register restart handler (err=%d)\n", err); + atomic_dec(&vexpress_restart_nb_refcnt); + return err; + } } device_create_file(dev, &dev_attr_active);