* [PATCH v2 01/14] smp: Create a new function to shutdown nonboot cpus [not found] <20191125112754.25223-1-qais.yousef@arm.com> @ 2019-11-25 11:27 ` Qais Yousef 2020-01-21 17:03 ` Russell King - ARM Linux admin 2019-11-25 11:27 ` [PATCH v2 02/14] ia64: Replace cpu_down with smp_shutdown_nonboot_cpus() Qais Yousef 1 sibling, 1 reply; 6+ messages in thread From: Qais Yousef @ 2019-11-25 11:27 UTC (permalink / raw) To: Thomas Gleixner, Greg Kroah-Hartman Cc: Qais Yousef, Josh Poimboeuf, Peter Zijlstra (Intel), Jiri Kosina, Nicholas Piggin, Daniel Lezcano, Ingo Molnar, Eiichi Tsukata, Zhenzhong Duan, Nadav Amit, Rafael J. Wysocki, Tony Luck, Fenghua Yu, Russell King, Catalin Marinas, Will Deacon, linux-arm-kernel, linux-ia64, linux-kernel This function will be used later in machine_shutdown() for some archs. Signed-off-by: Qais Yousef <qais.yousef@arm.com> CC: Thomas Gleixner <tglx@linutronix.de> CC: Josh Poimboeuf <jpoimboe@redhat.com> CC: "Peter Zijlstra (Intel)" <peterz@infradead.org> CC: Jiri Kosina <jkosina@suse.cz> CC: Nicholas Piggin <npiggin@gmail.com> CC: Daniel Lezcano <daniel.lezcano@linaro.org> CC: Ingo Molnar <mingo@kernel.org> CC: Eiichi Tsukata <devel@etsukata.com> CC: Zhenzhong Duan <zhenzhong.duan@oracle.com> CC: Nadav Amit <namit@vmware.com> CC: Greg Kroah-Hartman <gregkh@linuxfoundation.org> CC: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com> CC: Tony Luck <tony.luck@intel.com> CC: Fenghua Yu <fenghua.yu@intel.com> CC: Russell King <linux@armlinux.org.uk> CC: Catalin Marinas <catalin.marinas@arm.com> CC: Will Deacon <will@kernel.org> CC: linux-arm-kernel@lists.infradead.org CC: linux-ia64@vger.kernel.org CC: linux-kernel@vger.kernel.org --- include/linux/cpu.h | 2 ++ kernel/cpu.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/linux/cpu.h b/include/linux/cpu.h index bc6c879bd110..8229932fb053 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -118,6 +118,7 @@ extern void cpu_hotplug_disable(void); extern void cpu_hotplug_enable(void); void clear_tasks_mm_cpumask(int cpu); int cpu_down(unsigned int cpu); +extern void smp_shutdown_nonboot_cpus(unsigned int primary_cpu); #else /* CONFIG_HOTPLUG_CPU */ @@ -129,6 +130,7 @@ static inline int cpus_read_trylock(void) { return true; } static inline void lockdep_assert_cpus_held(void) { } static inline void cpu_hotplug_disable(void) { } static inline void cpu_hotplug_enable(void) { } +static inline void smp_shutdown_nonboot_cpus(unsigned int primary_cpu) { } #endif /* !CONFIG_HOTPLUG_CPU */ /* Wrappers which go away once all code is converted */ diff --git a/kernel/cpu.c b/kernel/cpu.c index e2cad3ee2ead..94055a0d989e 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -1058,6 +1058,23 @@ int cpu_down(unsigned int cpu) } EXPORT_SYMBOL(cpu_down); +void smp_shutdown_nonboot_cpus(unsigned int primary_cpu) +{ + unsigned int cpu; + + if (!cpu_online(primary_cpu)) { + pr_info("Attempting to shutdodwn nonboot cpus while boot cpu is offline!\n"); + cpu_online(primary_cpu); + } + + for_each_present_cpu(cpu) { + if (cpu = primary_cpu) + continue; + if (cpu_online(cpu)) + cpu_down(cpu); + } +} + #else #define takedown_cpu NULL #endif /*CONFIG_HOTPLUG_CPU*/ -- 2.17.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2 01/14] smp: Create a new function to shutdown nonboot cpus 2019-11-25 11:27 ` [PATCH v2 01/14] smp: Create a new function to shutdown nonboot cpus Qais Yousef @ 2020-01-21 17:03 ` Russell King - ARM Linux admin 2020-01-21 17:47 ` Qais Yousef 0 siblings, 1 reply; 6+ messages in thread From: Russell King - ARM Linux admin @ 2020-01-21 17:03 UTC (permalink / raw) To: Qais Yousef Cc: Thomas Gleixner, Greg Kroah-Hartman, Josh Poimboeuf, Peter Zijlstra (Intel), Jiri Kosina, Nicholas Piggin, Daniel Lezcano, Ingo Molnar, Eiichi Tsukata, Zhenzhong Duan, Nadav Amit, Rafael J. Wysocki, Tony Luck, Fenghua Yu, Catalin Marinas, Will Deacon, linux-arm-kernel, linux-ia64, linux-kernel On Mon, Nov 25, 2019 at 11:27:41AM +0000, Qais Yousef wrote: > This function will be used later in machine_shutdown() for some archs. > > Signed-off-by: Qais Yousef <qais.yousef@arm.com> > CC: Thomas Gleixner <tglx@linutronix.de> > CC: Josh Poimboeuf <jpoimboe@redhat.com> > CC: "Peter Zijlstra (Intel)" <peterz@infradead.org> > CC: Jiri Kosina <jkosina@suse.cz> > CC: Nicholas Piggin <npiggin@gmail.com> > CC: Daniel Lezcano <daniel.lezcano@linaro.org> > CC: Ingo Molnar <mingo@kernel.org> > CC: Eiichi Tsukata <devel@etsukata.com> > CC: Zhenzhong Duan <zhenzhong.duan@oracle.com> > CC: Nadav Amit <namit@vmware.com> > CC: Greg Kroah-Hartman <gregkh@linuxfoundation.org> > CC: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com> > CC: Tony Luck <tony.luck@intel.com> > CC: Fenghua Yu <fenghua.yu@intel.com> > CC: Russell King <linux@armlinux.org.uk> > CC: Catalin Marinas <catalin.marinas@arm.com> > CC: Will Deacon <will@kernel.org> > CC: linux-arm-kernel@lists.infradead.org > CC: linux-ia64@vger.kernel.org > CC: linux-kernel@vger.kernel.org > --- > include/linux/cpu.h | 2 ++ > kernel/cpu.c | 17 +++++++++++++++++ > 2 files changed, 19 insertions(+) > > diff --git a/include/linux/cpu.h b/include/linux/cpu.h > index bc6c879bd110..8229932fb053 100644 > --- a/include/linux/cpu.h > +++ b/include/linux/cpu.h > @@ -118,6 +118,7 @@ extern void cpu_hotplug_disable(void); > extern void cpu_hotplug_enable(void); > void clear_tasks_mm_cpumask(int cpu); > int cpu_down(unsigned int cpu); > +extern void smp_shutdown_nonboot_cpus(unsigned int primary_cpu); > > #else /* CONFIG_HOTPLUG_CPU */ > > @@ -129,6 +130,7 @@ static inline int cpus_read_trylock(void) { return true; } > static inline void lockdep_assert_cpus_held(void) { } > static inline void cpu_hotplug_disable(void) { } > static inline void cpu_hotplug_enable(void) { } > +static inline void smp_shutdown_nonboot_cpus(unsigned int primary_cpu) { } > #endif /* !CONFIG_HOTPLUG_CPU */ > > /* Wrappers which go away once all code is converted */ > diff --git a/kernel/cpu.c b/kernel/cpu.c > index e2cad3ee2ead..94055a0d989e 100644 > --- a/kernel/cpu.c > +++ b/kernel/cpu.c > @@ -1058,6 +1058,23 @@ int cpu_down(unsigned int cpu) > } > EXPORT_SYMBOL(cpu_down); > > +void smp_shutdown_nonboot_cpus(unsigned int primary_cpu) > +{ > + unsigned int cpu; > + > + if (!cpu_online(primary_cpu)) { > + pr_info("Attempting to shutdodwn nonboot cpus while boot cpu is offline!\n"); > + cpu_online(primary_cpu); > + } > + > + for_each_present_cpu(cpu) { > + if (cpu = primary_cpu) > + continue; > + if (cpu_online(cpu)) > + cpu_down(cpu); > + } How does this avoid racing with userspace attempting to restart CPUs that have already been taken down by this function? -- RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up According to speedtest.net: 11.9Mbps down 500kbps up ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 01/14] smp: Create a new function to shutdown nonboot cpus 2020-01-21 17:03 ` Russell King - ARM Linux admin @ 2020-01-21 17:47 ` Qais Yousef 2020-01-21 18:09 ` Russell King - ARM Linux admin 0 siblings, 1 reply; 6+ messages in thread From: Qais Yousef @ 2020-01-21 17:47 UTC (permalink / raw) To: Russell King - ARM Linux admin Cc: Thomas Gleixner, Greg Kroah-Hartman, Josh Poimboeuf, Peter Zijlstra (Intel), Jiri Kosina, Nicholas Piggin, Daniel Lezcano, Ingo Molnar, Eiichi Tsukata, Zhenzhong Duan, Nadav Amit, Rafael J. Wysocki, Tony Luck, Fenghua Yu, Catalin Marinas, Will Deacon, linux-arm-kernel, linux-ia64, linux-kernel On 01/21/20 17:03, Russell King - ARM Linux admin wrote: > On Mon, Nov 25, 2019 at 11:27:41AM +0000, Qais Yousef wrote: > > +void smp_shutdown_nonboot_cpus(unsigned int primary_cpu) > > +{ > > + unsigned int cpu; > > + > > + if (!cpu_online(primary_cpu)) { > > + pr_info("Attempting to shutdodwn nonboot cpus while boot cpu is offline!\n"); > > + cpu_online(primary_cpu); Eh, that should be cpu_up(primary_cpu)! Which I have to say I'm not if is the right thing to do. migrate_to_reboot_cpu() picks the first online cpu if reboot_cpu (assumed 0) is offline migrate_to_reboot_cpu(): 225 /* Make certain the cpu I'm about to reboot on is online */ 226 if (!cpu_online(cpu)) 227 cpu = cpumask_first(cpu_online_mask); > > + } > > + > > + for_each_present_cpu(cpu) { > > + if (cpu = primary_cpu) > > + continue; > > + if (cpu_online(cpu)) > > + cpu_down(cpu); > > + } > > How does this avoid racing with userspace attempting to restart CPUs > that have already been taken down by this function? This is meant to be called from machine_shutdown() only. But you've got a point. The previous logic that used disable_nonboot_cpus(), which in turn called freeze_secondary_cpus() didn't hold hotplug lock. So I assumed the higher level logic of machine_shutdown() ensures that hotplug lock is held to synchronize with potential other hotplug operations. But I can see now that it doesn't. With this series that migrates users to use device_{online,offline}, holding the lock_device_hotplug() should protect against such races. Worth noting that this an existing problem in the code and not something I introduced, of course it makes sense to fix it properly as part of this series. I'm not sure how the other archs deal with this TBH. Thanks for having a look! Cheers -- Qais Yousef ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 01/14] smp: Create a new function to shutdown nonboot cpus 2020-01-21 17:47 ` Qais Yousef @ 2020-01-21 18:09 ` Russell King - ARM Linux admin 2020-01-22 10:32 ` Qais Yousef 0 siblings, 1 reply; 6+ messages in thread From: Russell King - ARM Linux admin @ 2020-01-21 18:09 UTC (permalink / raw) To: Qais Yousef Cc: Thomas Gleixner, Greg Kroah-Hartman, Josh Poimboeuf, Peter Zijlstra (Intel), Jiri Kosina, Nicholas Piggin, Daniel Lezcano, Ingo Molnar, Eiichi Tsukata, Zhenzhong Duan, Nadav Amit, Rafael J. Wysocki, Tony Luck, Fenghua Yu, Catalin Marinas, Will Deacon, linux-arm-kernel, linux-ia64, linux-kernel On Tue, Jan 21, 2020 at 05:47:52PM +0000, Qais Yousef wrote: > On 01/21/20 17:03, Russell King - ARM Linux admin wrote: > > On Mon, Nov 25, 2019 at 11:27:41AM +0000, Qais Yousef wrote: > > > +void smp_shutdown_nonboot_cpus(unsigned int primary_cpu) > > > +{ > > > + unsigned int cpu; > > > + > > > + if (!cpu_online(primary_cpu)) { > > > + pr_info("Attempting to shutdodwn nonboot cpus while boot cpu is offline!\n"); > > > + cpu_online(primary_cpu); > > Eh, that should be cpu_up(primary_cpu)! > > Which I have to say I'm not if is the right thing to do. > migrate_to_reboot_cpu() picks the first online cpu if reboot_cpu (assumed 0) is > offline > > migrate_to_reboot_cpu(): > 225 /* Make certain the cpu I'm about to reboot on is online */ > 226 if (!cpu_online(cpu)) > 227 cpu = cpumask_first(cpu_online_mask); > > > > + } > > > + > > > + for_each_present_cpu(cpu) { > > > + if (cpu = primary_cpu) > > > + continue; > > > + if (cpu_online(cpu)) > > > + cpu_down(cpu); > > > + } > > > > How does this avoid racing with userspace attempting to restart CPUs > > that have already been taken down by this function? > > This is meant to be called from machine_shutdown() only. > > But you've got a point. > > The previous logic that used disable_nonboot_cpus(), which in turn called > freeze_secondary_cpus() didn't hold hotplug lock. So I assumed the higher level > logic of machine_shutdown() ensures that hotplug lock is held to synchronize > with potential other hotplug operations. freeze_secondary_cpus() takes the CPU maps lock while it takes CPUs down, and then disables cpu hotplug by incrementing cpu_hotplug_disabled. Incrementing that prevents cpu_up() and cpu_down() being used, thereby preventing userspace from changing the online state of any CPU in the system. > But I can see now that it doesn't. > > With this series that migrates users to use device_{online,offline}, holding > the lock_device_hotplug() should protect against such races. > > Worth noting that this an existing problem in the code and not something > I introduced, of course it makes sense to fix it properly as part of this > series. > > I'm not sure how the other archs deal with this TBH. > > Thanks for having a look! > > Cheers > > -- > Qais Yousef > -- RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up According to speedtest.net: 11.9Mbps down 500kbps up ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 01/14] smp: Create a new function to shutdown nonboot cpus 2020-01-21 18:09 ` Russell King - ARM Linux admin @ 2020-01-22 10:32 ` Qais Yousef 0 siblings, 0 replies; 6+ messages in thread From: Qais Yousef @ 2020-01-22 10:32 UTC (permalink / raw) To: Russell King - ARM Linux admin Cc: Thomas Gleixner, Greg Kroah-Hartman, Josh Poimboeuf, Peter Zijlstra (Intel), Jiri Kosina, Nicholas Piggin, Daniel Lezcano, Ingo Molnar, Eiichi Tsukata, Zhenzhong Duan, Nadav Amit, Rafael J. Wysocki, Tony Luck, Fenghua Yu, Catalin Marinas, Will Deacon, linux-arm-kernel, linux-ia64, linux-kernel On 01/21/20 18:09, Russell King - ARM Linux admin wrote: > On Tue, Jan 21, 2020 at 05:47:52PM +0000, Qais Yousef wrote: > > On 01/21/20 17:03, Russell King - ARM Linux admin wrote: > > > On Mon, Nov 25, 2019 at 11:27:41AM +0000, Qais Yousef wrote: > > > > +void smp_shutdown_nonboot_cpus(unsigned int primary_cpu) > > > > +{ > > > > + unsigned int cpu; > > > > + > > > > + if (!cpu_online(primary_cpu)) { > > > > + pr_info("Attempting to shutdodwn nonboot cpus while boot cpu is offline!\n"); > > > > + cpu_online(primary_cpu); > > > > Eh, that should be cpu_up(primary_cpu)! > > > > Which I have to say I'm not if is the right thing to do. > > migrate_to_reboot_cpu() picks the first online cpu if reboot_cpu (assumed 0) is > > offline > > > > migrate_to_reboot_cpu(): > > 225 /* Make certain the cpu I'm about to reboot on is online */ > > 226 if (!cpu_online(cpu)) > > 227 cpu = cpumask_first(cpu_online_mask); > > > > > > + } > > > > + > > > > + for_each_present_cpu(cpu) { > > > > + if (cpu = primary_cpu) > > > > + continue; > > > > + if (cpu_online(cpu)) > > > > + cpu_down(cpu); > > > > + } > > > > > > How does this avoid racing with userspace attempting to restart CPUs > > > that have already been taken down by this function? > > > > This is meant to be called from machine_shutdown() only. > > > > But you've got a point. > > > > The previous logic that used disable_nonboot_cpus(), which in turn called > > freeze_secondary_cpus() didn't hold hotplug lock. So I assumed the higher level > > logic of machine_shutdown() ensures that hotplug lock is held to synchronize > > with potential other hotplug operations. > > freeze_secondary_cpus() takes the CPU maps lock while it takes CPUs > down, and then disables cpu hotplug by incrementing > cpu_hotplug_disabled. Incrementing that prevents cpu_up() and > cpu_down() being used, thereby preventing userspace from changing the > online state of any CPU in the system. I see. Sorry I missed the CPU maps lock. Yes this makes sense and should work here too. Thanks for the help. Thomas, I'll wait for your comment on this and potentially other patches before sending v3. Thanks -- Qais Yousef ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 02/14] ia64: Replace cpu_down with smp_shutdown_nonboot_cpus() [not found] <20191125112754.25223-1-qais.yousef@arm.com> 2019-11-25 11:27 ` [PATCH v2 01/14] smp: Create a new function to shutdown nonboot cpus Qais Yousef @ 2019-11-25 11:27 ` Qais Yousef 1 sibling, 0 replies; 6+ messages in thread From: Qais Yousef @ 2019-11-25 11:27 UTC (permalink / raw) To: Thomas Gleixner, Greg Kroah-Hartman Cc: Qais Yousef, Tony Luck, Fenghua Yu, linux-ia64, linux-kernel Use the new smp_shutdown_nonboot_cpus() instead of open coding using cpu_down() directly. This also prepares to make cpu_up/down a private interface for anything but the cpu subsystem. Signed-off-by: Qais Yousef <qais.yousef@arm.com> CC: Tony Luck <tony.luck@intel.com> CC: Fenghua Yu <fenghua.yu@intel.com> CC: linux-ia64@vger.kernel.org CC: linux-kernel@vger.kernel.org --- arch/ia64/kernel/process.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/arch/ia64/kernel/process.c b/arch/ia64/kernel/process.c index 968b5f33e725..cc894d4900be 100644 --- a/arch/ia64/kernel/process.c +++ b/arch/ia64/kernel/process.c @@ -646,14 +646,8 @@ cpu_halt (void) void machine_shutdown(void) { -#ifdef CONFIG_HOTPLUG_CPU - int cpu; + smp_shutdown_nonboot_cpus(0); - for_each_online_cpu(cpu) { - if (cpu != smp_processor_id()) - cpu_down(cpu); - } -#endif #ifdef CONFIG_KEXEC kexec_disable_iosapic(); #endif -- 2.17.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2020-01-22 10:32 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20191125112754.25223-1-qais.yousef@arm.com>
2019-11-25 11:27 ` [PATCH v2 01/14] smp: Create a new function to shutdown nonboot cpus Qais Yousef
2020-01-21 17:03 ` Russell King - ARM Linux admin
2020-01-21 17:47 ` Qais Yousef
2020-01-21 18:09 ` Russell King - ARM Linux admin
2020-01-22 10:32 ` Qais Yousef
2019-11-25 11:27 ` [PATCH v2 02/14] ia64: Replace cpu_down with smp_shutdown_nonboot_cpus() Qais Yousef
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox