Skip to content

Commit ad789f8

Browse files
Waiman-LongPeter Zijlstra
authored andcommitted
sched/debug: Fix cgroup_path[] serialization
The handling of sysrq key can be activated by echoing the key to /proc/sysrq-trigger or via the magic key sequence typed into a terminal that is connected to the system in some way (serial, USB or other mean). In the former case, the handling is done in a user context. In the latter case, it is likely to be in an interrupt context. Currently in print_cpu() of kernel/sched/debug.c, sched_debug_lock is taken with interrupt disabled for the whole duration of the calls to print_*_stats() and print_rq() which could last for the quite some time if the information dump happens on the serial console. If the system has many cpus and the sched_debug_lock is somehow busy (e.g. parallel sysrq-t), the system may hit a hard lockup panic depending on the actually serial console implementation of the system. The purpose of sched_debug_lock is to serialize the use of the global cgroup_path[] buffer in print_cpu(). The rests of the printk calls don't need serialization from sched_debug_lock. Calling printk() with interrupt disabled can still be problematic if multiple instances are running. Allocating a stack buffer of PATH_MAX bytes is not feasible because of the limited size of the kernel stack. The solution implemented in this patch is to allow only one caller at a time to use the full size group_path[], while other simultaneous callers will have to use shorter stack buffers with the possibility of path name truncation. A "..." suffix will be printed if truncation may have happened. The cgroup path name is provided for informational purpose only, so occasional path name truncation should not be a big problem. Fixes: efe25c2 ("sched: Reinstate group names in /proc/sched_debug") Suggested-by: Peter Zijlstra <[email protected]> Signed-off-by: Waiman Long <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 9d10a13 commit ad789f8

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

kernel/sched/debug.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
*/
99
#include "sched.h"
1010

11-
static DEFINE_SPINLOCK(sched_debug_lock);
12-
1311
/*
1412
* This allows printing both to /proc/sched_debug and
1513
* to the console
@@ -476,16 +474,37 @@ static void print_cfs_group_stats(struct seq_file *m, int cpu, struct task_group
476474
#endif
477475

478476
#ifdef CONFIG_CGROUP_SCHED
477+
static DEFINE_SPINLOCK(sched_debug_lock);
479478
static char group_path[PATH_MAX];
480479

481-
static char *task_group_path(struct task_group *tg)
480+
static void task_group_path(struct task_group *tg, char *path, int plen)
482481
{
483-
if (autogroup_path(tg, group_path, PATH_MAX))
484-
return group_path;
482+
if (autogroup_path(tg, path, plen))
483+
return;
485484

486-
cgroup_path(tg->css.cgroup, group_path, PATH_MAX);
485+
cgroup_path(tg->css.cgroup, path, plen);
486+
}
487487

488-
return group_path;
488+
/*
489+
* Only 1 SEQ_printf_task_group_path() caller can use the full length
490+
* group_path[] for cgroup path. Other simultaneous callers will have
491+
* to use a shorter stack buffer. A "..." suffix is appended at the end
492+
* of the stack buffer so that it will show up in case the output length
493+
* matches the given buffer size to indicate possible path name truncation.
494+
*/
495+
#define SEQ_printf_task_group_path(m, tg, fmt...) \
496+
{ \
497+
if (spin_trylock(&sched_debug_lock)) { \
498+
task_group_path(tg, group_path, sizeof(group_path)); \
499+
SEQ_printf(m, fmt, group_path); \
500+
spin_unlock(&sched_debug_lock); \
501+
} else { \
502+
char buf[128]; \
503+
char *bufend = buf + sizeof(buf) - 3; \
504+
task_group_path(tg, buf, bufend - buf); \
505+
strcpy(bufend - 1, "..."); \
506+
SEQ_printf(m, fmt, buf); \
507+
} \
489508
}
490509
#endif
491510

@@ -512,7 +531,7 @@ print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
512531
SEQ_printf(m, " %d %d", task_node(p), task_numa_group_id(p));
513532
#endif
514533
#ifdef CONFIG_CGROUP_SCHED
515-
SEQ_printf(m, " %s", task_group_path(task_group(p)));
534+
SEQ_printf_task_group_path(m, task_group(p), " %s")
516535
#endif
517536

518537
SEQ_printf(m, "\n");
@@ -549,7 +568,7 @@ void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
549568

550569
#ifdef CONFIG_FAIR_GROUP_SCHED
551570
SEQ_printf(m, "\n");
552-
SEQ_printf(m, "cfs_rq[%d]:%s\n", cpu, task_group_path(cfs_rq->tg));
571+
SEQ_printf_task_group_path(m, cfs_rq->tg, "cfs_rq[%d]:%s\n", cpu);
553572
#else
554573
SEQ_printf(m, "\n");
555574
SEQ_printf(m, "cfs_rq[%d]:\n", cpu);
@@ -620,7 +639,7 @@ void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
620639
{
621640
#ifdef CONFIG_RT_GROUP_SCHED
622641
SEQ_printf(m, "\n");
623-
SEQ_printf(m, "rt_rq[%d]:%s\n", cpu, task_group_path(rt_rq->tg));
642+
SEQ_printf_task_group_path(m, rt_rq->tg, "rt_rq[%d]:%s\n", cpu);
624643
#else
625644
SEQ_printf(m, "\n");
626645
SEQ_printf(m, "rt_rq[%d]:\n", cpu);
@@ -672,7 +691,6 @@ void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq)
672691
static void print_cpu(struct seq_file *m, int cpu)
673692
{
674693
struct rq *rq = cpu_rq(cpu);
675-
unsigned long flags;
676694

677695
#ifdef CONFIG_X86
678696
{
@@ -723,13 +741,11 @@ do { \
723741
}
724742
#undef P
725743

726-
spin_lock_irqsave(&sched_debug_lock, flags);
727744
print_cfs_stats(m, cpu);
728745
print_rt_stats(m, cpu);
729746
print_dl_stats(m, cpu);
730747

731748
print_rq(m, rq, cpu);
732-
spin_unlock_irqrestore(&sched_debug_lock, flags);
733749
SEQ_printf(m, "\n");
734750
}
735751

0 commit comments

Comments
 (0)