[RFC][PATCHv4 7/7] printk: add printk cpumask sysctl

From: Sergey Senozhatsky
Date: Fri Jun 02 2017 - 05:05:14 EST


Add `sysctl kernel.printk_cpumask' knob so user can enforce printk
kthread cpumask and control both the number of kthreads we wake up
during printk offloading and the CPUs those wake ups happen.

For example,

sysctl kernel.printk_cpumask = 0-3

will force printk to offload printing only to CPU0-CPU3.

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@xxxxxxxxx>
---
include/linux/printk.h | 4 ++++
kernel/printk/printk.c | 32 ++++++++++++++++++++++++++++++++
kernel/sysctl.c | 7 +++++++
3 files changed, 43 insertions(+)

diff --git a/include/linux/printk.h b/include/linux/printk.h
index e10f27468322..200499b9f23a 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -81,6 +81,7 @@ static inline void console_verbose(void)
#define DEVKMSG_STR_MAX_SIZE 10
extern char devkmsg_log_str[];
struct ctl_table;
+extern unsigned long *printk_cpumask_bits;

struct va_format {
const char *fmt;
@@ -196,6 +197,9 @@ devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void __user *buf,

extern void wake_up_klogd(void);

+extern int proc_printk_cpumask(struct ctl_table *table, int write,
+ void __user *buffer, size_t *lenp, loff_t *ppos);
+
char *log_buf_addr_get(void);
u32 log_buf_len_get(void);
void log_buf_vmcoreinfo_setup(void);
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 4e5233a265cb..46d82faf8613 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3049,6 +3049,38 @@ static int __init init_printk_kthreads(void)
}
late_initcall(init_printk_kthreads);

+#ifdef CONFIG_SYSCTL
+static DEFINE_MUTEX(printk_kthread_proc_mutex);
+unsigned long *printk_cpumask_bits = cpumask_bits(&printk_cpumask);
+
+/*
+ * Update printk kthread cpumask, so user can limit the number of printk
+ * kthreads we wake up during printing offloading.
+ */
+int proc_printk_cpumask(struct ctl_table *table, int write,
+ void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+ int err;
+
+ mutex_lock(&printk_kthread_proc_mutex);
+
+ if (printk_enforce_emergency)
+ goto out;
+
+ err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
+ if (!err && write) {
+ if (smpboot_update_cpumask_percpu_thread(&printk_kthreads,
+ &printk_cpumask)) {
+ printk_enforce_emergency = true;
+ pr_err("printk: can't update cpumask\n");
+ }
+ }
+out:
+ mutex_unlock(&printk_kthread_proc_mutex);
+ return err;
+}
+#endif
+
void wake_up_klogd(void)
{
preempt_disable();
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index df9f2a367882..d03dc7fa4f7f 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -853,6 +853,13 @@ static struct ctl_table kern_table[] = {
.extra1 = &zero,
.extra2 = &two,
},
+ {
+ .procname = "printk_cpumask",
+ .data = &printk_cpumask_bits,
+ .maxlen = NR_CPUS,
+ .mode = 0644,
+ .proc_handler = proc_printk_cpumask,
+ },
#endif
{
.procname = "ngroups_max",
--
2.13.0