[PATCH printk v2 23/38] tty: tty_io: document console_lock usage

John Ogness posted 38 patches 3 years, 5 months ago
There is a newer version of this series
[PATCH printk v2 23/38] tty: tty_io: document console_lock usage
Posted by John Ogness 3 years, 5 months ago
show_cons_active() uses the console_lock to gather information
on registered consoles. Since the console_lock is being used for
multiple reasons, explicitly document these reasons. This will
be useful when the console_lock is split into fine-grained
locking.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 drivers/tty/tty_io.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 2050e63963bb..333579bfa335 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -3526,6 +3526,14 @@ static ssize_t show_cons_active(struct device *dev,
 	struct console *c;
 	ssize_t count = 0;
 
+	/*
+	 * Hold the console_lock to guarantee that no consoles are
+	 * unregistered until all console processing is complete.
+	 * This also allows safe traversal of the console list.
+	 *
+	 * Stop console printing because the device() callback may
+	 * assume the console is not within its write() callback.
+	 */
 	console_lock();
 	for_each_console(c) {
 		if (!c->device)
-- 
2.30.2
Re: [PATCH printk v2 23/38] tty: tty_io: document console_lock usage
Posted by Petr Mladek 3 years, 5 months ago
On Wed 2022-10-19 17:01:45, John Ogness wrote:
> show_cons_active() uses the console_lock to gather information
> on registered consoles. Since the console_lock is being used for
> multiple reasons, explicitly document these reasons. This will
> be useful when the console_lock is split into fine-grained
> locking.
> 
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
> ---
>  drivers/tty/tty_io.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
> index 2050e63963bb..333579bfa335 100644
> --- a/drivers/tty/tty_io.c
> +++ b/drivers/tty/tty_io.c
> @@ -3526,6 +3526,14 @@ static ssize_t show_cons_active(struct device *dev,
>  	struct console *c;
>  	ssize_t count = 0;
>  
> +	/*
> +	 * Hold the console_lock to guarantee that no consoles are
> +	 * unregistered until all console processing is complete.
> +	 * This also allows safe traversal of the console list.

This is more or less clear. show_cons_active() reads a lot of
information from the registered consoles.

> +	 *
> +	 * Stop console printing because the device() callback may
> +	 * assume the console is not within its write() callback.

I wonder if this is based on some real example or if you just want
to stay on the safe side.

It is perfectly fine to stay on the safe side. But we should make
it clear if the dependency really exists or if it has to be
investigated later during the clean up.

> +	 */
>  	console_lock();
>  	for_each_console(c) {
>  		if (!c->device)

Anyway, thanks for adding the comment.

Best Regards,
Petr
Re: [PATCH printk v2 23/38] tty: tty_io: document console_lock usage
Posted by Greg Kroah-Hartman 3 years, 5 months ago
On Wed, Oct 19, 2022 at 05:01:45PM +0206, John Ogness wrote:
> show_cons_active() uses the console_lock to gather information
> on registered consoles. Since the console_lock is being used for
> multiple reasons, explicitly document these reasons. This will
> be useful when the console_lock is split into fine-grained
> locking.
> 
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
> ---
>  drivers/tty/tty_io.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
> index 2050e63963bb..333579bfa335 100644
> --- a/drivers/tty/tty_io.c
> +++ b/drivers/tty/tty_io.c
> @@ -3526,6 +3526,14 @@ static ssize_t show_cons_active(struct device *dev,
>  	struct console *c;
>  	ssize_t count = 0;
>  
> +	/*
> +	 * Hold the console_lock to guarantee that no consoles are
> +	 * unregistered until all console processing is complete.
> +	 * This also allows safe traversal of the console list.
> +	 *
> +	 * Stop console printing because the device() callback may
> +	 * assume the console is not within its write() callback.
> +	 */
>  	console_lock();
>  	for_each_console(c) {
>  		if (!c->device)
> -- 
> 2.30.2
> 

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>