Skip to contentSkip to conversation

2.6.16 crashes when running numastat on p575

DISCUSSION Christoph Lameter 6 participants 13 messages Apr 3, 2006 – Apr 4, 2006 1 message not in the archive Driver core

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 1 line · Andrew Morton

I have a vague feeling that you guys worked on numastat?

This is mostly Andi's code although I added the zone_pcp() stuff. This is failing because zone_pcp() only returns valid information for online processors.

Initially all zone_pcps() point to the boot_cpuset (see zone_pcp_init) and therefore zone_pcp) is always valid and we do not see this bug. But if someone downs a processor or a processor dies then free_zone_pageset() is called which will set zone_pcp() = NULL.

Fix NULL pointer dereference in node_read_numastat()

zone_pcp() only returns valid values if the processor is online.

Change node_read_numastat() to only scan online processors.

drivers/base/node.c +1 -1
Index: linux-2.6.16/drivers/base/node.c
===================================================================
@@ -106,7 +106,7 @@ static ssize_t node_read_numastat(struct
 	other_node = 0;
 	for (i = 0; i < MAX_NR_ZONES; i++) {
 		struct zone *z = &pg->node_zones[i];
-		for (cpu = 0; cpu < NR_CPUS; cpu++) {
+		for_each_online_cpu(cpu) {
 			struct per_cpu_pageset *ps = zone_pcp(z,cpu);
 			numa_hit += ps->numa_hit;
 			numa_miss += ps->numa_miss;
#2 lore raw

- for (cpu = 0; cpu < NR_CPUS; cpu++) {
+ for_each_online_cpu(cpu) {

Idle curiosity -- what keeps a cpu from going offline during this scan, and leaving us with the same crash as before?

Signature · 3 lines
                  I won't rest till it's the best ...
                  Programmer, Linux Scalability
                  Paul Jackson <[email protected]> 1.925.600.0401
#3 lore raw
Quoted 5 lines · Paul Jackson
drivers/base/node.c +1 -1
-		for (cpu = 0; cpu < NR_CPUS; cpu++) {
+		for_each_online_cpu(cpu) {

Idle curiosity -- what keeps a cpu from going offline during this scan, and leaving us with the same crash as before?

Nothing keeps a processor from going offline. We could take the hotplug lock for every for_each_online_cpu() in the kernel. Could you take that up with the hotplug folks?

#4 lore raw
Quoted 1 line · Christoph

Could you take that up with the hotplug folks?

Hmmm ... I suddenly notice that I'm on vacation for the next two weeks. Sorry <grin>.

In any case, I'll have to leave that one for the hotplug folks to sort out. I have other priorities and limited talents.

Signature · 3 lines
                  I won't rest till it's the best ...
                  Programmer, Linux Scalability
                  Paul Jackson <[email protected]> 1.925.600.0401
#13 lore raw
In reply to Christoph Lameter
Quoted 27 lines · Christoph Lameter
Quoted 1 line · Andrew Morton

I have a vague feeling that you guys worked on numastat?

This is mostly Andi's code although I added the zone_pcp() stuff. This is failing because zone_pcp() only returns valid information for online processors.

Initially all zone_pcps() point to the boot_cpuset (see zone_pcp_init) and therefore zone_pcp) is always valid and we do not see this bug. But if someone downs a processor or a processor dies then free_zone_pageset() is called which will set zone_pcp() = NULL.

Fix NULL pointer dereference in node_read_numastat()

zone_pcp() only returns valid values if the processor is online.

Change node_read_numastat() to only scan online processors.

Index: linux-2.6.16/drivers/base/node.c
===================================================================

drivers/base/node.c static ssize_t node_read_numastat(struct · line 112

Quoted 8 lines
drivers/base/node.c +1 -1
@@ -106,7 +106,7 @@ static ssize_t node_read_numastat(struct
 	other_node = 0;
 	for (i = 0; i < MAX_NR_ZONES; i++) {
 		struct zone *z = &pg->node_zones[i];
-		for (cpu = 0; cpu < NR_CPUS; cpu++) {
+		for_each_online_cpu(cpu) {
 			struct per_cpu_pageset *ps = zone_pcp(z,cpu);
 			numa_hit += ps->numa_hit;
 			numa_miss += ps->numa_miss;

Works, thanks.

Sonny

#11 lore raw
In reply to Paul Jackson
Quoted 5 lines · Paul Jackson
drivers/base/node.c +1 -1
-		for (cpu = 0; cpu < NR_CPUS; cpu++) {
+		for_each_online_cpu(cpu) {

Idle curiosity -- what keeps a cpu from going offline during this scan, and leaving us with the same crash as before?

CPU hotdown uses RCU like techniques to avoid this. Only potential problem could be on a preemptive kernel, but I hope nobody tries cpu unplug on such a beast. The later could be probably fixed with a rcu_read_lock() or somesuch.

-Andi

#5 lore raw
In reply to Christoph Lameter
Quoted 10 lines · Christoph Lameter
Quoted 5 lines · Paul Jackson
drivers/base/node.c +1 -1
-		for (cpu = 0; cpu < NR_CPUS; cpu++) {
+		for_each_online_cpu(cpu) {

Idle curiosity -- what keeps a cpu from going offline during this scan, and leaving us with the same crash as before?

Nothing keeps a processor from going offline. We could take the hotplug lock for every for_each_online_cpu() in the kernel.

In this case, disabling preempt around the for_each_online_cpu loop would prevent any cpu from going down in the meantime. But since this function doesn't look like it's a hot path, and we're potentially traversing lots of zones and cpus, lock_cpu_hotplug might be preferable.

As Paul noted, the fix as it stands isn't adequate.

#12 lore raw
In reply to Andi Kleen
Quoted 11 lines · Andi Kleen
Quoted 5 lines · Paul Jackson
drivers/base/node.c +1 -1
-		for (cpu = 0; cpu < NR_CPUS; cpu++) {
+		for_each_online_cpu(cpu) {

Idle curiosity -- what keeps a cpu from going offline during this scan, and leaving us with the same crash as before?

CPU hotdown uses RCU like techniques to avoid this. Only potential problem could be on a preemptive kernel, but I hope nobody tries cpu unplug on such a beast.

I always turn on preempt when testing cpu hotplug (usually on ppc64). There were several preempt vs cpu hotplug issues until around 2.6.10 or so, iirc, but I think the situation has been stable for a while now.

#6 lore raw
In reply to Nathan Lynch
Quoted 6 lines · Nathan Lynch

In this case, disabling preempt around the for_each_online_cpu loop would prevent any cpu from going down in the meantime. But since this function doesn't look like it's a hot path, and we're potentially traversing lots of zones and cpus, lock_cpu_hotplug might be preferable.

As Paul noted, the fix as it stands isn't adequate.

There are many other for_each_*_cpu loops in the kernel that do not have any of the instrumentation you suggest. I suggest you come up with a general solution and then go through all of them and fix this. Please be aware that many of these loops are performance critical.

#7 lore raw
Quoted 8 lines · Christoph Lameter
Quoted 5 lines · Nathan Lynch

In this case, disabling preempt around the for_each_online_cpu loop would prevent any cpu from going down in the meantime. But since this function doesn't look like it's a hot path, and we're potentially traversing lots of zones and cpus, lock_cpu_hotplug might be preferable.

Quoted 6 lines
Quoted 1 line

As Paul noted, the fix as it stands isn't adequate.

There are many other for_each_*_cpu loops in the kernel that do not have any of the instrumentation you suggest. I suggest you come up with a general solution and then go through all of them and fix this. Please be aware that many of these loops are performance critical.

But this one isn't, right?

And I'm afraid there's a misunderstanding here -- only for_each_online_cpu (or accessing the cpu online map in general) has such restrictions -- for_each_possible_cpu doesn't require any locking or preempt tricks since cpu_possible_map must not change after boot.

#8 lore raw
Quoted 6 lines · Nathan Lynch
Quoted 4 lines

There are many other for_each_*_cpu loops in the kernel that do not have any of the instrumentation you suggest. I suggest you come up with a general solution and then go through all of them and fix this. Please be aware that many of these loops are performance critical.

But this one isn't, right?

Right. One could use more expensive processing here.

Quoted 4 lines

And I'm afraid there's a misunderstanding here -- only for_each_online_cpu (or accessing the cpu online map in general) has such restrictions -- for_each_possible_cpu doesn't require any locking or preempt tricks since cpu_possible_map must not change after boot.

Correct. We may want to audit the kernel and check that each for_each_possible_cpu or for_each_cpu is really correct. Developers frequently assume that all processors are up. There may be some complicated interactions with cpusets. Adding Paul to this.

However, note that I am not interested hotplug functionality. It is going to be a difficult task to make the kernel shutdown processors correctly. I can give you feedback but I am not going to do this work.

#9 lore raw
Quoted 11 lines · Christoph Lameter
Quoted 6 lines · Nathan Lynch
Quoted 4 lines

There are many other for_each_*_cpu loops in the kernel that do not have any of the instrumentation you suggest. I suggest you come up with a general solution and then go through all of them and fix this. Please be aware that many of these loops are performance critical.

But this one isn't, right?

Right. One could use more expensive processing here.

Hopefully none of the for_each_foo() loops are performance-critical - those things are expensive.

Quoted 4 lines
Quoted 4 lines

And I'm afraid there's a misunderstanding here -- only for_each_online_cpu (or accessing the cpu online map in general) has such restrictions -- for_each_possible_cpu doesn't require any locking or preempt tricks since cpu_possible_map must not change after boot.

for_each_present_cpu() presumably has the same problems.

Quoted 2 lines

Correct. We may want to audit the kernel and check that each for_each_possible_cpu or for_each_cpu is really correct.

A fair bit of that has been happening in recent weeks.

But yes, we should be protecting these things with rcu_read_lock() if possible, lock_cpu_hotplug() otherwise.

(rcu_read_lock() might not be the appropriate name for this operation - maybe it should be an open-coded preempt_disable(). Or some other suitably named alias; dunno).

#10 lore raw
In reply to Christoph Lameter
Quoted 1 line · Christoph

There may be some complicated interactions with cpusets.

Hopefully not, but it's possible. I did my best not to assume that cpus or memory nodes stayed online, and to build in fallbacks if one of them disappeared out from under me.

But the code has never been tested to these assumptions, nor even carefully reviewed to them.

So something will likely break somewhere, though quite possibly not in the cpuset code itself, but in the scheduler or allocator code that depends on selecting an online cpu or memory for some purpose.

Someone from the hotplug side will have to look at this, with an understanding of how the coming and going of cpus and memory nodes can affect the scheduler and allocators.

Look at the callers of cpuset_cpus_allowed(), cpuset_mems_allowed(), cpuset_update_task_memory_state(), and cpuset_zone_allowed() to see the likely hot spots for interaction between cpusets and hotplug.

The callers of these routines are important hooks into the scheduler and allocator, and the question needs to be asked what happens if a cpu or memory node goes offline after one of these cpuset_*() routines is called before the scheduler or allocator finishes doing what it is doing with what it thought was a usable cpu or node.

Also, there is currently no hotplug aware code that I know of in the kernel/cpuset.c code, so each of the references to the globals cpu_online_map and node_online_map therein are suspect. Presumably as anywhere else in the kernel these appear, or the for_each_online_*() macros appear, the code needs to be examined for perhaps needing some sort of guard.

So, in sum, the kernel needs to be inspected at the location of at least each of the following calls or references, for possible problems if the cpus or nodes online change unexpectedly:

any_online_node()
cpu_online_map
cpu_present()
cpu_present_map
cpuset_cpus_allowed()
cpuset_mems_allowed()
cpuset_update_task_memory_state()
cpuset_zone_allowed()
for_each_online_cpu()
for_each_online_node()
for_each_present_cpu()
node_online_map
num_online_cpus()
num_online_nodes()
num_present_cpus()

Signature · 3 lines
                  I won't rest till it's the best ...
                  Programmer, Linux Scalability
                  Paul Jackson <[email protected]> 1.925.600.0401