Skip to contentSkip to conversation

printk: use console_trylock() in console_cpu_notify()

PATCH In review Sergey Senozhatsky 3 participants 5 messages Jan 21, 2017 – Mar 23, 2017 Printk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 printk: use console_trylock() in console_cpu_notify()

There is no need to always call blocking console_lock() in console_cpu_notify(), it's quite possible that console_sem can be locked by other CPU on the system, either already printing or soon to begin printing the messages. console_lock() in this case can simply block CPU hotplug for unknown period of time (console_unlock() is time unbound). Not that hotplug is very fast, but still, with other CPUs being online and doing printk() console_cpu_notify() can stuck.

Use console_trylock() instead and opt-out if console_sem is already acquired from another CPU, since that CPU will do the printing for us.

---
 kernel/printk/printk.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel/printk/printk.c +6 -5
index 7180088cbb23..772eb16436ce 100644
@@ -2028,15 +2028,16 @@ void resume_console(void)
  * @cpu: unused
  *
  * If printk() is called from a CPU that is not online yet, the messages
- * will be spooled but will not show up on the console.  This function is
- * called when a new CPU comes online (or fails to come up), and ensures
- * that any such output gets printed.
+ * will be printed on the console only if there are CON_ANYTIME consoles.
+ * This function is called when a new CPU comes online (or fails to come
+ * up) or goes offline.
  */
 static int console_cpu_notify(unsigned int cpu)
 {
 	if (!cpuhp_tasks_frozen) {
-		console_lock();
-		console_unlock();
+		/* If trylock fails, someone else is doing the printing */
+		if (console_trylock())
+			console_unlock();
 	}
 	return 0;
 }
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 34 lines · Sergey Senozhatsky

There is no need to always call blocking console_lock() in console_cpu_notify(), it's quite possible that console_sem can be locked by other CPU on the system, either already printing or soon to begin printing the messages. console_lock() in this case can simply block CPU hotplug for unknown period of time (console_unlock() is time unbound). Not that hotplug is very fast, but still, with other CPUs being online and doing printk() console_cpu_notify() can stuck.

Use console_trylock() instead and opt-out if console_sem is already acquired from another CPU, since that CPU will do the printing for us.

---
 kernel/printk/printk.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel/printk/printk.c +3 -3
index 7180088cbb23..772eb16436ce 100644
@@ -2028,15 +2028,16 @@ void resume_console(void)
  * @cpu: unused
  *
  * If printk() is called from a CPU that is not online yet, the messages
- * will be spooled but will not show up on the console.  This function is
- * called when a new CPU comes online (or fails to come up), and ensures
- * that any such output gets printed.
+ * will be printed on the console only if there are CON_ANYTIME consoles.
+ * This function is called when a new CPU comes online (or fails to come
+ * up) or goes offline.
  */
 static int console_cpu_notify(unsigned int cpu)

kernel/printk/printk.c void resume_console(void) · line 2043

Quoted 10 lines
kernel/printk/printk.c +3 -2
 {
 	if (!cpuhp_tasks_frozen) {
-		console_lock();
-		console_unlock();
+		/* If trylock fails, someone else is doing the printing */
+		if (console_trylock())
+			console_unlock();
 	}
 	return 0;
 }

Rather theoretically, the other owner of console_sem might be on a CPU that is going online/offline and will refuse to print the messages.

It is not that big deal when it goes online because then we will call this notifier also for the other CPU and it will flush the messages.

The problem is when the CPU goes offline. But it is broken even now. It does not make much sense to call this notifier when cpu_online(raw_smp_processor_id() returns false, see can_use_console().

All in all. The change looks fine to me because it most likely improves the performance[1] and does not affect the reliability that much.

But we should revisit this notifier after adding the async printk patchset. It will allow to defer the console handling to another CPU and work even for going CPUs.

I add some people working on CPU hotplug into CC to get their opinion.

[1] https://lkml.kernel.org/r/[email protected]

Best Regards,
Petr

#2 lore raw

Hello,

thanks for taking a look, Petr.

On (01/25/17 16:02), Petr Mladek wrote:
[..]

Quoted 27 lines
Quoted 27 lines
---
 kernel/printk/printk.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel/printk/printk.c +5 -5
index 7180088cbb23..772eb16436ce 100644
@@ -2028,15 +2028,16 @@ void resume_console(void)
  * @cpu: unused
  *
  * If printk() is called from a CPU that is not online yet, the messages
- * will be spooled but will not show up on the console.  This function is
- * called when a new CPU comes online (or fails to come up), and ensures
- * that any such output gets printed.
+ * will be printed on the console only if there are CON_ANYTIME consoles.
+ * This function is called when a new CPU comes online (or fails to come
+ * up) or goes offline.
  */
 static int console_cpu_notify(unsigned int cpu)
 {
 	if (!cpuhp_tasks_frozen) {
-		console_lock();
-		console_unlock();
+		/* If trylock fails, someone else is doing the printing */
+		if (console_trylock())
Quoted 8 lines

kernel/printk/printk.c void resume_console(void) · line 2043

Quoted 4 lines
kernel/printk/printk.c +1 -0
+			console_unlock();
 	}
 	return 0;
 }

Rather theoretically, the other owner of console_sem might be on a CPU that is going online/offline and will refuse to print the messages.

hm, if console_sem is locked by CPU that is going offline then console_cpu_notify() will deadlock in console_lock(). if I understood your point correctly.

Quoted 3 lines

All in all. The change looks fine to me because it most likely improves the performance[1] and does not affect the reliability that much.

yeah, I think so.

Quoted 3 lines

But we should revisit this notifier after adding the async printk patchset. It will allow to defer the console handling to another CPU and work even for going CPUs.

ok, may be.

Quoted 2 lines

I add some people working on CPU hotplug into CC to get their opinion.

thanks.

Quoted 1 line

[1] https://lkml.kernel.org/r/[email protected]

	-ss
maintainer
#3 lore raw
Quoted 42 lines · Sergey Senozhatsky

Hello,

thanks for taking a look, Petr.

On (01/25/17 16:02), Petr Mladek wrote:
[..]

Quoted 27 lines
Quoted 27 lines
---
 kernel/printk/printk.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel/printk/printk.c +5 -5
index 7180088cbb23..772eb16436ce 100644
@@ -2028,15 +2028,16 @@ void resume_console(void)
  * @cpu: unused
  *
  * If printk() is called from a CPU that is not online yet, the messages
- * will be spooled but will not show up on the console.  This function is
- * called when a new CPU comes online (or fails to come up), and ensures
- * that any such output gets printed.
+ * will be printed on the console only if there are CON_ANYTIME consoles.
+ * This function is called when a new CPU comes online (or fails to come
+ * up) or goes offline.
  */
 static int console_cpu_notify(unsigned int cpu)
 {
 	if (!cpuhp_tasks_frozen) {
-		console_lock();
-		console_unlock();
+		/* If trylock fails, someone else is doing the printing */
+		if (console_trylock())
Quoted 8 lines

kernel/printk/printk.c void resume_console(void) · line 2043

Quoted 4 lines
kernel/printk/printk.c +1 -0
+			console_unlock();
 	}
 	return 0;
 }

Rather theoretically, the other owner of console_sem might be on a CPU that is going online/offline and will refuse to print the messages.

Quoted 3 lines

hm, if console_sem is locked by CPU that is going offline then console_cpu_notify() will deadlock in console_lock(). if I understood your point correctly.

No, it will not deadlock. I guess that it will just do nothing.

console_unlock() would probably return at the beginning because can_use_console() would return false because cpu_online() would return false.

I am not sure about it. It is not easy for me to find in which cpu_online() state the notifiers are called.

If the console really is not usable, we should rather defer the console work to another CPU.

Best Regards,
Petr

maintainer Acked-by
#4 lore raw
In reply to Petr Mladek
Quoted 50 lines · Petr Mladek
Quoted 34 lines · Sergey Senozhatsky

There is no need to always call blocking console_lock() in console_cpu_notify(), it's quite possible that console_sem can be locked by other CPU on the system, either already printing or soon to begin printing the messages. console_lock() in this case can simply block CPU hotplug for unknown period of time (console_unlock() is time unbound). Not that hotplug is very fast, but still, with other CPUs being online and doing printk() console_cpu_notify() can stuck.

Use console_trylock() instead and opt-out if console_sem is already acquired from another CPU, since that CPU will do the printing for us.

---
 kernel/printk/printk.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel/printk/printk.c +3 -3
index 7180088cbb23..772eb16436ce 100644
@@ -2028,15 +2028,16 @@ void resume_console(void)
  * @cpu: unused
  *
  * If printk() is called from a CPU that is not online yet, the messages
- * will be spooled but will not show up on the console.  This function is
- * called when a new CPU comes online (or fails to come up), and ensures
- * that any such output gets printed.
+ * will be printed on the console only if there are CON_ANYTIME consoles.
+ * This function is called when a new CPU comes online (or fails to come
+ * up) or goes offline.
  */
 static int console_cpu_notify(unsigned int cpu)

kernel/printk/printk.c void resume_console(void) · line 2043

Quoted 10 lines
kernel/printk/printk.c +3 -2
 {
 	if (!cpuhp_tasks_frozen) {
-		console_lock();
-		console_unlock();
+		/* If trylock fails, someone else is doing the printing */
+		if (console_trylock())
+			console_unlock();
 	}
 	return 0;
 }

All in all. The change looks fine to me because it most likely improves the performance[1] and does not affect the reliability that much.

Quoted 1 line

[1] https://lkml.kernel.org/r/[email protected]

I am going to add this patch into printk.git for 4.12.

Best Regards,
Petr