Skip to content

Commit cb152ff

Browse files
Nathan Zimmeringomolnar
authored andcommitted
sched: Fix /proc/sched_stat failure on very very large systems
On systems with 4096 cores doing a cat /proc/sched_stat fails, because we are trying to push all the data into a single kmalloc buffer. The issue is on these very large machines all the data will not fit in 4mb. A better solution is to not use the single_open() mechanism but to provide our own seq_operations. The output should be identical to previous version and thus not need the version number. Reported-by: Dave Jones <davej@redhat.com> Signed-off-by: Nathan Zimmer <nzimmer@sgi.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Wu Fengguang <fengguang.wu@intel.com> [ Fix memleak] [ Fix spello in comment] [ Fix warnings] Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 1c3e826 commit cb152ff

1 file changed

Lines changed: 59 additions & 20 deletions

File tree

‎kernel/sched/stats.c‎

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ static int show_schedstat(struct seq_file *seq, void *v)
2121
if (mask_str == NULL)
2222
return -ENOMEM;
2323

24-
seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
25-
seq_printf(seq, "timestamp %lu\n", jiffies);
26-
for_each_online_cpu(cpu) {
27-
struct rq *rq = cpu_rq(cpu);
24+
if (v == (void *)1) {
25+
seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
26+
seq_printf(seq, "timestamp %lu\n", jiffies);
27+
} else {
28+
struct rq *rq;
2829
#ifdef CONFIG_SMP
2930
struct sched_domain *sd;
3031
int dcount = 0;
3132
#endif
33+
cpu = (unsigned long)(v - 2);
34+
rq = cpu_rq(cpu);
3235

3336
/* runqueue-specific stats */
3437
seq_printf(seq,
@@ -77,30 +80,66 @@ static int show_schedstat(struct seq_file *seq, void *v)
7780
return 0;
7881
}
7982

80-
static int schedstat_open(struct inode *inode, struct file *file)
83+
/*
84+
* This itererator needs some explanation.
85+
* It returns 1 for the header position.
86+
* This means 2 is cpu 0.
87+
* In a hotplugged system some cpus, including cpu 0, may be missing so we have
88+
* to use cpumask_* to iterate over the cpus.
89+
*/
90+
static void *schedstat_start(struct seq_file *file, loff_t *offset)
8191
{
82-
unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32);
83-
char *buf = kmalloc(size, GFP_KERNEL);
84-
struct seq_file *m;
85-
int res;
92+
unsigned long n = *offset;
8693

87-
if (!buf)
88-
return -ENOMEM;
89-
res = single_open(file, show_schedstat, NULL);
90-
if (!res) {
91-
m = file->private_data;
92-
m->buf = buf;
93-
m->size = size;
94-
} else
95-
kfree(buf);
96-
return res;
94+
if (n == 0)
95+
return (void *) 1;
96+
97+
n--;
98+
99+
if (n > 0)
100+
n = cpumask_next(n - 1, cpu_online_mask);
101+
else
102+
n = cpumask_first(cpu_online_mask);
103+
104+
*offset = n + 1;
105+
106+
if (n < nr_cpu_ids)
107+
return (void *)(unsigned long)(n + 2);
108+
return NULL;
109+
}
110+
111+
static void *schedstat_next(struct seq_file *file, void *data, loff_t *offset)
112+
{
113+
(*offset)++;
114+
return schedstat_start(file, offset);
115+
}
116+
117+
static void schedstat_stop(struct seq_file *file, void *data)
118+
{
119+
}
120+
121+
static const struct seq_operations schedstat_sops = {
122+
.start = schedstat_start,
123+
.next = schedstat_next,
124+
.stop = schedstat_stop,
125+
.show = show_schedstat,
126+
};
127+
128+
static int schedstat_open(struct inode *inode, struct file *file)
129+
{
130+
return seq_open(file, &schedstat_sops);
97131
}
98132

133+
static int schedstat_release(struct inode *inode, struct file *file)
134+
{
135+
return 0;
136+
};
137+
99138
static const struct file_operations proc_schedstat_operations = {
100139
.open = schedstat_open,
101140
.read = seq_read,
102141
.llseek = seq_lseek,
103-
.release = single_release,
142+
.release = schedstat_release,
104143
};
105144

106145
static int __init proc_schedstat_init(void)

0 commit comments

Comments
 (0)