Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1373537 > unrolled thread

[PATCH v11 3/3] printk: make printk.synchronous param rw

Started bySergey Senozhatsky <sergey.senozhatsky@gmail.com>
First post2016-04-07 18:40 +0200
Last post2016-04-08 07:50 +0200
Articles 4 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH v11 3/3] printk: make printk.synchronous param rw Sergey Senozhatsky <sergey.senozhatsky@gmail.com> - 2016-04-07 18:40 +0200
    Re: [PATCH v11 3/3] printk: make printk.synchronous param rw Pan Xinhui <xinhui@linux.vnet.ibm.com> - 2016-04-08 06:10 +0200
      Re: [PATCH v11 3/3] printk: make printk.synchronous param rw Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> - 2016-04-08 07:30 +0200
        Re: [PATCH v11 3/3] printk: make printk.synchronous param rw Pan Xinhui <xinhui@linux.vnet.ibm.com> - 2016-04-08 07:50 +0200

#1373537 — [PATCH v11 3/3] printk: make printk.synchronous param rw

FromSergey Senozhatsky <sergey.senozhatsky@gmail.com>
Date2016-04-07 18:40 +0200
Subject[PATCH v11 3/3] printk: make printk.synchronous param rw
Message-ID<rll8m-6JT-17@gated-at.bofh.it>
Change `synchronous' printk param to be RW, so user space
can change printk mode back and forth to/from sync mode
(which is considered to be more reliable).

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
 kernel/printk/printk.c | 63 ++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 53 insertions(+), 10 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 89f5441..5ae6f73 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -288,9 +288,6 @@ static u32 log_buf_len = __LOG_BUF_LEN;
 
 /* Control whether printing to console must be synchronous. */
 static bool __read_mostly printk_sync = true;
-module_param_named(synchronous, printk_sync, bool, S_IRUGO);
-MODULE_PARM_DESC(synchronous, "make printing to console synchronous");
-
 /* Printing kthread for async printk */
 static struct task_struct *printk_kthread;
 /* When `true' printing thread has messages to print */
@@ -1785,7 +1782,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 		 * operate in sync mode once panic() occurred.
 		 */
 		if (console_loglevel != CONSOLE_LOGLEVEL_MOTORMOUTH &&
-				printk_kthread) {
+				!printk_sync && printk_kthread) {
 			/* Offload printing to a schedulable context. */
 			printk_kthread_need_flush_console = true;
 			wake_up_process(printk_kthread);
@@ -2757,6 +2754,16 @@ static int __init printk_late_init(void)
 late_initcall(printk_late_init);
 
 #if defined CONFIG_PRINTK
+/*
+ * kernel_param_ops.set is called from two places:
+ * - from parse_args()->printk_sync_set(), when we can't kthread_run(), so
+ *   we just set the param value. The actual initalization happens later,
+ *   from late_initcall().
+ *
+ * - from user space via sysfs knob; we can kthread_run() there (if needed).
+ */
+static bool printk_initcall_done;
+
 static int printk_kthread_func(void *data)
 {
 	while (1) {
@@ -2780,11 +2787,7 @@ static int printk_kthread_func(void *data)
 	return 0;
 }
 
-/*
- * Init async printk via late_initcall, after core/arch/device/etc.
- * initialization.
- */
-static int __init init_printk_kthread(void)
+static int __init_printk_kthread(void)
 {
 	struct task_struct *thread;
 	struct sched_param param = {
@@ -2794,6 +2797,9 @@ static int __init init_printk_kthread(void)
 	if (printk_sync)
 		return 0;
 
+	if (printk_kthread)
+		return 0;
+
 	thread = kthread_run(printk_kthread_func, NULL, "printk");
 	if (IS_ERR(thread)) {
 		pr_err("printk: unable to create printing thread\n");
@@ -2805,6 +2811,43 @@ static int __init init_printk_kthread(void)
 	printk_kthread = thread;
 	return 0;
 }
+
+static int printk_sync_set(const char *val, const struct kernel_param *kp)
+{
+	static DEFINE_MUTEX(printk_sync_lock);
+	int ret;
+
+	mutex_lock(&printk_sync_lock);
+	ret = param_set_bool(val, kp);
+	if (ret) {
+		mutex_unlock(&printk_sync_lock);
+		return ret;
+	}
+
+	if (printk_initcall_done)
+		ret = __init_printk_kthread();
+	mutex_unlock(&printk_sync_lock);
+	return ret;
+}
+
+static const struct kernel_param_ops param_ops_printk_sync = {
+	.set = printk_sync_set,
+	.get = param_get_bool,
+};
+
+module_param_cb(synchronous, &param_ops_printk_sync, &printk_sync,
+		S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(synchronous, "make printing to console synchronous");
+
+/*
+ * Init async printk via late_initcall, after core/arch/device/etc.
+ * initialization.
+ */
+static __init int init_printk_kthread(void)
+{
+	printk_initcall_done = true;
+	return __init_printk_kthread();
+}
 late_initcall(init_printk_kthread);
 
 /*
@@ -2820,7 +2863,7 @@ static void wake_up_klogd_work_func(struct irq_work *irq_work)
 	int pending = __this_cpu_xchg(printk_pending, 0);
 
 	if (pending & PRINTK_PENDING_OUTPUT) {
-		if (printk_kthread) {
+		if (!printk_sync && printk_kthread) {
 			wake_up_process(printk_kthread);
 		} else {
 			/*
-- 
2.8.0

[toc] | [next] | [standalone]


#1373952

FromPan Xinhui <xinhui@linux.vnet.ibm.com>
Date2016-04-08 06:10 +0200
Message-ID<rlvU6-6xI-9@gated-at.bofh.it>
In reply to#1373537

On 2016年04月08日 01:31, Sergey Senozhatsky wrote:
> Change `synchronous' printk param to be RW, so user space
> can change printk mode back and forth to/from sync mode
> (which is considered to be more reliable).
> 
> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
> ---
>  kernel/printk/printk.c | 63 ++++++++++++++++++++++++++++++++++++++++++--------
>  1 file changed, 53 insertions(+), 10 deletions(-)
> 
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index 89f5441..5ae6f73 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -288,9 +288,6 @@ static u32 log_buf_len = __LOG_BUF_LEN;
> 
>  /* Control whether printing to console must be synchronous. */
>  static bool __read_mostly printk_sync = true;
> -module_param_named(synchronous, printk_sync, bool, S_IRUGO);
> -MODULE_PARM_DESC(synchronous, "make printing to console synchronous");
> -
>  /* Printing kthread for async printk */
>  static struct task_struct *printk_kthread;
>  /* When `true' printing thread has messages to print */
> @@ -1785,7 +1782,7 @@ asmlinkage int vprintk_emit(int facility, int level,
>  		 * operate in sync mode once panic() occurred.
>  		 */
>  		if (console_loglevel != CONSOLE_LOGLEVEL_MOTORMOUTH &&
> -				printk_kthread) {
> +				!printk_sync && printk_kthread) {
>  			/* Offload printing to a schedulable context. */
>  			printk_kthread_need_flush_console = true;
>  			wake_up_process(printk_kthread);
> @@ -2757,6 +2754,16 @@ static int __init printk_late_init(void)
>  late_initcall(printk_late_init);
> 
>  #if defined CONFIG_PRINTK
> +/*
> + * kernel_param_ops.set is called from two places:
> + * - from parse_args()->printk_sync_set(), when we can't kthread_run(), so
> + *   we just set the param value. The actual initalization happens later,
> + *   from late_initcall().
> + *
> + * - from user space via sysfs knob; we can kthread_run() there (if needed).
> + */
> +static bool printk_initcall_done;
> +
>  static int printk_kthread_func(void *data)
>  {
>  	while (1) {
> @@ -2780,11 +2787,7 @@ static int printk_kthread_func(void *data)
>  	return 0;
>  }
> 
> -/*
> - * Init async printk via late_initcall, after core/arch/device/etc.
> - * initialization.
> - */
> -static int __init init_printk_kthread(void)
> +static int __init_printk_kthread(void)
>  {
>  	struct task_struct *thread;
>  	struct sched_param param = {
> @@ -2794,6 +2797,9 @@ static int __init init_printk_kthread(void)
>  	if (printk_sync)
>  		return 0;
> 
> +	if (printk_kthread)
> +		return 0;
> +
>  	thread = kthread_run(printk_kthread_func, NULL, "printk");
>  	if (IS_ERR(thread)) {
>  		pr_err("printk: unable to create printing thread\n");
> @@ -2805,6 +2811,43 @@ static int __init init_printk_kthread(void)
>  	printk_kthread = thread;
>  	return 0;
>  }
> +
> +static int printk_sync_set(const char *val, const struct kernel_param *kp)
> +{
> +	static DEFINE_MUTEX(printk_sync_lock);
> +	int ret;
> +
> +	mutex_lock(&printk_sync_lock);
> +	ret = param_set_bool(val, kp);
> +	if (ret) {
> +		mutex_unlock(&printk_sync_lock);
> +		return ret;
> +	}
> +
> +	if (printk_initcall_done)
> +		ret = __init_printk_kthread();
> +	mutex_unlock(&printk_sync_lock);
> +	return ret;
> +}
> +
> +static const struct kernel_param_ops param_ops_printk_sync = {
> +	.set = printk_sync_set,
> +	.get = param_get_bool,
> +};
> +
> +module_param_cb(synchronous, &param_ops_printk_sync, &printk_sync,
> +		S_IRUGO | S_IWUSR);
> +MODULE_PARM_DESC(synchronous, "make printing to console synchronous");
> +
> +/*
> + * Init async printk via late_initcall, after core/arch/device/etc.
> + * initialization.
> + */
> +static __init int init_printk_kthread(void)
> +{
> +	printk_initcall_done = true;
> +	return __init_printk_kthread();
hello, 

One confusion, Why not use a lock to protect __init_printk_kthread from parallel call? Otherwise I think there is a race.
But for simplicity, maybe you could write codes as below.

+	int ret  = __init_printk_kthread();
+	printk_initcall_done = true;
+	return ret;

In my opinion, using a lock is better.

thanks
xinhui
> +}
>  late_initcall(init_printk_kthread);
> 
>  /*
> @@ -2820,7 +2863,7 @@ static void wake_up_klogd_work_func(struct irq_work *irq_work)
>  	int pending = __this_cpu_xchg(printk_pending, 0);
> 
>  	if (pending & PRINTK_PENDING_OUTPUT) {
> -		if (printk_kthread) {
> +		if (!printk_sync && printk_kthread) {
>  			wake_up_process(printk_kthread);
>  		} else {
>  			/*
> 

[toc] | [prev] | [next] | [standalone]


#1373990

FromSergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
Date2016-04-08 07:30 +0200
Message-ID<rlx9w-7sb-13@gated-at.bofh.it>
In reply to#1373952
On (04/08/16 12:04), Pan Xinhui wrote:
[..]
> > +/*
> > + * Init async printk via late_initcall, after core/arch/device/etc.
> > + * initialization.
> > + */
> > +static __init int init_printk_kthread(void)
> > +{
> > +	printk_initcall_done = true;
> > +	return __init_printk_kthread();
> hello, 
> 
> One confusion, Why not use a lock to protect __init_printk_kthread from parallel call? Otherwise I think there is a race.
> But for simplicity, maybe you could write codes as below.
> 
> +	int ret  = __init_printk_kthread();
> +	printk_initcall_done = true;
> +	return ret;
> 
> In my opinion, using a lock is better.

Hello,

I though about this, but isn't late_initcall() happening before kernel
starts /sbin/init? who can race with

	late_initcall() -> init_printk_kthread() -> __init_printk_kthread()?

looking at

static int __ref kernel_init(void *unused)
{
        int ret;

        kernel_init_freeable();
        /* need to finish all async __init code before freeing the memory */
        async_synchronize_full();
        free_initmem();
..

        if (!try_to_run_init_process("/sbin/init") ||
            !try_to_run_init_process("/etc/init") ||
            !try_to_run_init_process("/bin/init") ||
            !try_to_run_init_process("/bin/sh"))
                return 0;

__init (and init_printk_kthread is __init) is finished and freed by the
time kernel try_to_run_init_process. isn't it?


sysfs knob -> __init_printk_kthread() is protected by printk_sync_lock
mutex, obviously there can be parallel calls from user space.

	-ss

[toc] | [prev] | [next] | [standalone]


#1373995

FromPan Xinhui <xinhui@linux.vnet.ibm.com>
Date2016-04-08 07:50 +0200
Message-ID<rlxsS-7C5-11@gated-at.bofh.it>
In reply to#1373990

On 2016年04月08日 13:29, Sergey Senozhatsky wrote:
> On (04/08/16 12:04), Pan Xinhui wrote:
> [..]
>>> +/*
>>> + * Init async printk via late_initcall, after core/arch/device/etc.
>>> + * initialization.
>>> + */
>>> +static __init int init_printk_kthread(void)
>>> +{
>>> +	printk_initcall_done = true;
>>> +	return __init_printk_kthread();
>> hello, 
>>
>> One confusion, Why not use a lock to protect __init_printk_kthread from parallel call? Otherwise I think there is a race.
>> But for simplicity, maybe you could write codes as below.
>>
>> +	int ret  = __init_printk_kthread();
>> +	printk_initcall_done = true;
>> +	return ret;
>>
>> In my opinion, using a lock is better.
> 
> Hello,
> 
> I though about this, but isn't late_initcall() happening before kernel
> starts /sbin/init? who can race with
> 
> 	late_initcall() -> init_printk_kthread() -> __init_printk_kthread()?
> 
yep, you are right. I made a mistake.

> looking at
> 
> static int __ref kernel_init(void *unused)
> {
>         int ret;
> 
>         kernel_init_freeable();
>         /* need to finish all async __init code before freeing the memory */
>         async_synchronize_full();
>         free_initmem();
> ..
> 
>         if (!try_to_run_init_process("/sbin/init") ||
>             !try_to_run_init_process("/etc/init") ||
>             !try_to_run_init_process("/bin/init") ||
>             !try_to_run_init_process("/bin/sh"))
>                 return 0;
> 
> __init (and init_printk_kthread is __init) is finished and freed by the
> time kernel try_to_run_init_process. isn't it?
> 
> 
good explanation. thanks

> sysfs knob -> __init_printk_kthread() is protected by printk_sync_lock
> mutex, obviously there can be parallel calls from user space.
> 
> 	-ss
> 

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web