linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] kernel/reboot: Fix powering off using a non-syscall code paths
@ 2022-06-06 16:56 Dmitry Osipenko
  2022-06-07 17:44 ` Rafael J. Wysocki
  0 siblings, 1 reply; 2+ messages in thread
From: Dmitry Osipenko @ 2022-06-06 16:56 UTC (permalink / raw)
  To: Rafael J . Wysocki, Michael Ellerman
  Cc: Geert Uytterhoeven, linux-kernel, linux-pm, linuxppc-dev

There are other methods of powering off machine than the reboot syscall.
Previously we missed to coved those methods and it created power-off
regression for some machines, like the PowerPC e500. Fix this problem
by moving the legacy sys-off handler registration to the latest phase
of power-off process and making the kernel_can_power_off() to check the
legacy pm_power_off presence.

Tested-by: Michael Ellerman <mpe@ellerman.id.au> # ppce500
Reported-by: Michael Ellerman <mpe@ellerman.id.au> # ppce500
Fixes: da007f171fc9 ("kernel/reboot: Change registration order of legacy power-off handler")
Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
---
 kernel/reboot.c | 46 ++++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/kernel/reboot.c b/kernel/reboot.c
index 3b19b123efec..b5a71d1ff603 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -320,6 +320,7 @@ static struct sys_off_handler platform_sys_off_handler;
 static struct sys_off_handler *alloc_sys_off_handler(int priority)
 {
 	struct sys_off_handler *handler;
+	gfp_t flags;
 
 	/*
 	 * Platforms like m68k can't allocate sys_off handler dynamically
@@ -330,7 +331,12 @@ static struct sys_off_handler *alloc_sys_off_handler(int priority)
 		if (handler->cb_data)
 			return ERR_PTR(-EBUSY);
 	} else {
-		handler = kzalloc(sizeof(*handler), GFP_KERNEL);
+		if (system_state > SYSTEM_RUNNING)
+			flags = GFP_ATOMIC;
+		else
+			flags = GFP_KERNEL;
+
+		handler = kzalloc(sizeof(*handler), flags);
 		if (!handler)
 			return ERR_PTR(-ENOMEM);
 	}
@@ -440,7 +446,7 @@ void unregister_sys_off_handler(struct sys_off_handler *handler)
 {
 	int err;
 
-	if (!handler)
+	if (IS_ERR_OR_NULL(handler))
 		return;
 
 	if (handler->blocking)
@@ -615,7 +621,23 @@ static void do_kernel_power_off_prepare(void)
  */
 void do_kernel_power_off(void)
 {
+	struct sys_off_handler *sys_off = NULL;
+
+	/*
+	 * Register sys-off handlers for legacy PM callback. This allows
+	 * legacy PM callbacks temporary co-exist with the new sys-off API.
+	 *
+	 * TODO: Remove legacy handlers once all legacy PM users will be
+	 *       switched to the sys-off based APIs.
+	 */
+	if (pm_power_off)
+		sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
+						   SYS_OFF_PRIO_DEFAULT,
+						   legacy_pm_power_off, NULL);
+
 	atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
+
+	unregister_sys_off_handler(sys_off);
 }
 
 /**
@@ -626,7 +648,8 @@ void do_kernel_power_off(void)
  */
 bool kernel_can_power_off(void)
 {
-	return !atomic_notifier_call_chain_is_empty(&power_off_handler_list);
+	return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) ||
+		pm_power_off;
 }
 EXPORT_SYMBOL_GPL(kernel_can_power_off);
 
@@ -661,7 +684,6 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 		void __user *, arg)
 {
 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
-	struct sys_off_handler *sys_off = NULL;
 	char buffer[256];
 	int ret = 0;
 
@@ -686,21 +708,6 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 	if (ret)
 		return ret;
 
-	/*
-	 * Register sys-off handlers for legacy PM callback. This allows
-	 * legacy PM callbacks temporary co-exist with the new sys-off API.
-	 *
-	 * TODO: Remove legacy handlers once all legacy PM users will be
-	 *       switched to the sys-off based APIs.
-	 */
-	if (pm_power_off) {
-		sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
-						   SYS_OFF_PRIO_DEFAULT,
-						   legacy_pm_power_off, NULL);
-		if (IS_ERR(sys_off))
-			return PTR_ERR(sys_off);
-	}
-
 	/* Instead of trying to make the power_off code look like
 	 * halt when pm_power_off is not set do it the easy way.
 	 */
@@ -758,7 +765,6 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 		break;
 	}
 	mutex_unlock(&system_transition_mutex);
-	unregister_sys_off_handler(sys_off);
 	return ret;
 }
 
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v1] kernel/reboot: Fix powering off using a non-syscall code paths
  2022-06-06 16:56 [PATCH v1] kernel/reboot: Fix powering off using a non-syscall code paths Dmitry Osipenko
@ 2022-06-07 17:44 ` Rafael J. Wysocki
  0 siblings, 0 replies; 2+ messages in thread
From: Rafael J. Wysocki @ 2022-06-07 17:44 UTC (permalink / raw)
  To: Dmitry Osipenko
  Cc: Rafael J . Wysocki, Michael Ellerman, Geert Uytterhoeven,
	Linux Kernel Mailing List, Linux PM, linuxppc-dev

On Mon, Jun 6, 2022 at 6:57 PM Dmitry Osipenko
<dmitry.osipenko@collabora.com> wrote:
>
> There are other methods of powering off machine than the reboot syscall.
> Previously we missed to coved those methods and it created power-off
> regression for some machines, like the PowerPC e500. Fix this problem
> by moving the legacy sys-off handler registration to the latest phase
> of power-off process and making the kernel_can_power_off() to check the
> legacy pm_power_off presence.
>
> Tested-by: Michael Ellerman <mpe@ellerman.id.au> # ppce500
> Reported-by: Michael Ellerman <mpe@ellerman.id.au> # ppce500
> Fixes: da007f171fc9 ("kernel/reboot: Change registration order of legacy power-off handler")
> Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
> ---
>  kernel/reboot.c | 46 ++++++++++++++++++++++++++--------------------
>  1 file changed, 26 insertions(+), 20 deletions(-)
>
> diff --git a/kernel/reboot.c b/kernel/reboot.c
> index 3b19b123efec..b5a71d1ff603 100644
> --- a/kernel/reboot.c
> +++ b/kernel/reboot.c
> @@ -320,6 +320,7 @@ static struct sys_off_handler platform_sys_off_handler;
>  static struct sys_off_handler *alloc_sys_off_handler(int priority)
>  {
>         struct sys_off_handler *handler;
> +       gfp_t flags;
>
>         /*
>          * Platforms like m68k can't allocate sys_off handler dynamically
> @@ -330,7 +331,12 @@ static struct sys_off_handler *alloc_sys_off_handler(int priority)
>                 if (handler->cb_data)
>                         return ERR_PTR(-EBUSY);
>         } else {
> -               handler = kzalloc(sizeof(*handler), GFP_KERNEL);
> +               if (system_state > SYSTEM_RUNNING)
> +                       flags = GFP_ATOMIC;
> +               else
> +                       flags = GFP_KERNEL;
> +
> +               handler = kzalloc(sizeof(*handler), flags);
>                 if (!handler)
>                         return ERR_PTR(-ENOMEM);
>         }
> @@ -440,7 +446,7 @@ void unregister_sys_off_handler(struct sys_off_handler *handler)
>  {
>         int err;
>
> -       if (!handler)
> +       if (IS_ERR_OR_NULL(handler))
>                 return;
>
>         if (handler->blocking)
> @@ -615,7 +621,23 @@ static void do_kernel_power_off_prepare(void)
>   */
>  void do_kernel_power_off(void)
>  {
> +       struct sys_off_handler *sys_off = NULL;
> +
> +       /*
> +        * Register sys-off handlers for legacy PM callback. This allows
> +        * legacy PM callbacks temporary co-exist with the new sys-off API.
> +        *
> +        * TODO: Remove legacy handlers once all legacy PM users will be
> +        *       switched to the sys-off based APIs.
> +        */
> +       if (pm_power_off)
> +               sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
> +                                                  SYS_OFF_PRIO_DEFAULT,
> +                                                  legacy_pm_power_off, NULL);
> +
>         atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
> +
> +       unregister_sys_off_handler(sys_off);
>  }
>
>  /**
> @@ -626,7 +648,8 @@ void do_kernel_power_off(void)
>   */
>  bool kernel_can_power_off(void)
>  {
> -       return !atomic_notifier_call_chain_is_empty(&power_off_handler_list);
> +       return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) ||
> +               pm_power_off;
>  }
>  EXPORT_SYMBOL_GPL(kernel_can_power_off);
>
> @@ -661,7 +684,6 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
>                 void __user *, arg)
>  {
>         struct pid_namespace *pid_ns = task_active_pid_ns(current);
> -       struct sys_off_handler *sys_off = NULL;
>         char buffer[256];
>         int ret = 0;
>
> @@ -686,21 +708,6 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
>         if (ret)
>                 return ret;
>
> -       /*
> -        * Register sys-off handlers for legacy PM callback. This allows
> -        * legacy PM callbacks temporary co-exist with the new sys-off API.
> -        *
> -        * TODO: Remove legacy handlers once all legacy PM users will be
> -        *       switched to the sys-off based APIs.
> -        */
> -       if (pm_power_off) {
> -               sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
> -                                                  SYS_OFF_PRIO_DEFAULT,
> -                                                  legacy_pm_power_off, NULL);
> -               if (IS_ERR(sys_off))
> -                       return PTR_ERR(sys_off);
> -       }
> -
>         /* Instead of trying to make the power_off code look like
>          * halt when pm_power_off is not set do it the easy way.
>          */
> @@ -758,7 +765,6 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
>                 break;
>         }
>         mutex_unlock(&system_transition_mutex);
> -       unregister_sys_off_handler(sys_off);
>         return ret;
>  }
>
> --

Applied (with a couple of edits in the changelog), thanks!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-07 18:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-06 16:56 [PATCH v1] kernel/reboot: Fix powering off using a non-syscall code paths Dmitry Osipenko
2022-06-07 17:44 ` Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).