From patchwork Wed Jul 9 03:37:57 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 4511311 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id E8D7C9F86C for ; Wed, 9 Jul 2014 03:41:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1C08B2034F for ; Wed, 9 Jul 2014 03:41:00 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id CE5EC201EC for ; Wed, 9 Jul 2014 03:40:58 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1X4iiN-0007nw-H7; Wed, 09 Jul 2014 03:38:39 +0000 Received: from mail-pd0-x22d.google.com ([2607:f8b0:400e:c02::22d]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1X4iiI-0007f5-Tu for linux-arm-kernel@lists.infradead.org; Wed, 09 Jul 2014 03:38:35 +0000 Received: by mail-pd0-f173.google.com with SMTP id r10so8213344pdi.4 for ; Tue, 08 Jul 2014 20:38:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=moQ3jhT/Gg41cV6RmOhRDZcN3JDDlSzUZjvqod55cNQ=; b=NKzadHOMCLTtG/KA7GPUB+wg9iEErD+42FuNLAnn7x9f33uurNULmRJ5YGz/2VpjVX QvdAJ5Dx++ge9cHT8ck3ajiCbPMGwkF5c0ptukuw8xCEF5xIsMhjL5h7JroSIf5TvkUj kKWW92kHq7+rf697gkSzLd5R/LIzJViEsqrIsc+CPkgXS411h8ITnf2AQvfuzc890L1n lE4zNq2rB2M3+SO3Rw0aji6KTYF3QnADBxFpM1DN1gyIM0AjOKNLegDfZ2Z9WFZVBSfh TcteKp3cN0MVHLiVRB4QegY9R/VHTSQ2aL1Q8zBXN+OoJO43w8llrNAY/79hgXSqf8cq 8SpA== X-Received: by 10.70.131.5 with SMTP id oi5mr8878811pdb.69.1404877092588; Tue, 08 Jul 2014 20:38:12 -0700 (PDT) Received: from localhost (108-223-40-66.lightspeed.sntcca.sbcglobal.net. [108.223.40.66]) by mx.google.com with ESMTPSA id fm8sm210397883pab.28.2014.07.08.20.38.10 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 08 Jul 2014 20:38:12 -0700 (PDT) From: Guenter Roeck To: linux-watchdog@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 1/7] kernel: Add support for restart notifier call chain Date: Tue, 8 Jul 2014 20:37:57 -0700 Message-Id: <1404877083-6552-2-git-send-email-linux@roeck-us.net> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1404877083-6552-1-git-send-email-linux@roeck-us.net> References: <1404877083-6552-1-git-send-email-linux@roeck-us.net> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140708_203834_991844_F62DE453 X-CRM114-Status: GOOD ( 22.29 ) X-Spam-Score: -0.4 (/) Cc: Ingo Molnar , Russell King , Heiko Stuebner , Arnd Bergmann , linux-doc@vger.kernel.org, Dmitry Eremin-Solenikov , Catalin Marinas , Tomasz Figa , Randy Dunlap , Will Deacon , linux-kernel@vger.kernel.org, Steven Rostedt , Jonas Jensen , Wim Van Sebroeck , Maxime Ripard , David Woodhouse , Andrew Morton , Guenter Roeck X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Various drivers implement architecture and/or device specific means to restart (reset) the system. Various mechanisms have been implemented to support those schemes. The best known mechanism is arm_pm_restart, which is a function pointer to be set either from platform specific code or from drivers. Another mechanism is to use hardware watchdogs to issue a reset; this mechanism is used if there is no other method available to reset a board or system. Two examples are alim7101_wdt, which currently uses the reboot notifier to trigger a reset, and moxart_wdt, which registers the arm_pm_restart function. The existing mechanisms have a number of drawbacks. Typically only one scheme to restart the system is supported (at least if arm_pm_restart is used). At least in theory there can be mutliple means to restart the system, some of which may be less desirable (for example one mechanism may only reset the CPU, while another may reset the entire system). Using arm_pm_restart can also be racy if the function pointer is set from a driver, as the driver may be in the process of being unloaded when arm_pm_restart is called. Using the reboot notifier is always racy, as it is unknown if and when other functions using the reboot notifier have completed execution by the time the watchdog fires. To solve the problem, introduce a system restart notifier. This notifier is expected to be called from the architecture specific machine_restart() function. Drivers providing system restart functionality (such as the watchdog drivers mentioned above) are expected to register with this notifier. Signed-off-by: Guenter Roeck --- v3: Add kernel_restart_notify wrapper function to execute notifier. Improve documentation. Move restart_notifier_list into kernel/reboot.c and make it static. v2: No change. include/linux/reboot.h | 3 +++ kernel/reboot.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/include/linux/reboot.h b/include/linux/reboot.h index 48bf152..120db73 100644 --- a/include/linux/reboot.h +++ b/include/linux/reboot.h @@ -38,6 +38,9 @@ extern int reboot_force; extern int register_reboot_notifier(struct notifier_block *); extern int unregister_reboot_notifier(struct notifier_block *); +extern int register_restart_notifier(struct notifier_block *); +extern int unregister_restart_notifier(struct notifier_block *); +extern void kernel_restart_notify(char *cmd); /* * Architecture-specific implementations of sys_reboot commands. diff --git a/kernel/reboot.c b/kernel/reboot.c index a3a9e24..7bd6575 100644 --- a/kernel/reboot.c +++ b/kernel/reboot.c @@ -104,6 +104,77 @@ int unregister_reboot_notifier(struct notifier_block *nb) } EXPORT_SYMBOL(unregister_reboot_notifier); +/* + * Notifier list for kernel code which wants to be called + * to restart the system. + */ +static BLOCKING_NOTIFIER_HEAD(restart_notifier_list); + +/** + * register_restart_notifier - Register function to be called to reset + * the system + * @nb: Info about notifier function to be called + * + * Registers a function with the list of functions + * to be called to restart the system. + * + * Registered functions will be called from machine_restart as last + * step of the restart sequence (if the architecture specific + * machine_restart function calls kernel_restart_notify - see below + * for details). + * Registered functions are expected to restart the system immediately. + * If more than one function is registered, the notifier priority + * selects which function will be called first. + * + * Restart notifiers are expected to be registered from non-architecture + * code, typically from drivers. A typical use case would be a system + * where restart functionality is provided through a watchdog. Multiple + * restart handlers may exist; for example, one restart handler might + * restart the entire system, while another only restarts the CPU. + * In such cases, the restart handler which only restarts part of the + * hardware is expected to register with low priority to ensure that + * it only runs if no other means to restart the system is available. + * + * Currently always returns zero, as blocking_notifier_chain_register() + * always returns zero. + */ +int register_restart_notifier(struct notifier_block *nb) +{ + return blocking_notifier_chain_register(&restart_notifier_list, nb); +} +EXPORT_SYMBOL(register_restart_notifier); + +/** + * unregister_restart_notifier - Unregister previously registered + * restart notifier + * @nb: Hook to be unregistered + * + * Unregisters a previously registered restart notifier function. + * + * Returns zero on success, or %-ENOENT on failure. + */ +int unregister_restart_notifier(struct notifier_block *nb) +{ + return blocking_notifier_chain_unregister(&restart_notifier_list, nb); +} +EXPORT_SYMBOL(unregister_restart_notifier); + +/** + * kernel_restart_notify - Execute kernel restart handler call chain + * + * Calls functions registered with register_restart_notifier. + * + * Expected to be called from machine_restart as last step of the restart + * sequence. + * + * Restarts the system immediately if a restart notifier function has been + * registered. Otherwise does nothing. + */ +void kernel_restart_notify(char *cmd) +{ + blocking_notifier_call_chain(&restart_notifier_list, reboot_mode, cmd); +} + void migrate_to_reboot_cpu(void) { /* The boot cpu is always logical cpu 0 */