Skip to contentSkip to conversation

XENPF_set_processor_pminfo XEN_PM_CX overflows states array

PATCH In review eric 4 participants 7 messages Mar 7, 2012 – Mar 8, 2012

lore
Share & tools
lore

eric

author
#0 lore raw

PATCH 1/1 XENPF_set_processor_pminfo XEN_PM_CX overflows states array

Calling XENPF_set_processor_pminfo with XEN_PM_CX could cause states array in "struct acpi_processor_power" to exceed its limit.

The array used to be reset (by function cpuidle_init_cpu()) for each hypercall. The patch puts it back that way and adds an assertion to make it clear in case that happens again.

xen/arch/x86/acpi/cpu_idle.c +14 -15
@@ -622,19 +622,19 @@ static int cpuidle_init_cpu(int cpu)
 
         for ( i = 0; i < ACPI_PROCESSOR_MAX_POWER; i++ )
             acpi_power->states[i].idx = i;
-
-        acpi_power->states[ACPI_STATE_C1].type = ACPI_STATE_C1;
-        acpi_power->states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_EM_HALT;
-
-        acpi_power->states[ACPI_STATE_C0].valid = 1;
-        acpi_power->states[ACPI_STATE_C1].valid = 1;
-
-        acpi_power->count = 2;
-        acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
-        acpi_power->cpu = cpu;
         processor_powers[cpu] = acpi_power;
     }
 
+    acpi_power->states[ACPI_STATE_C1].type = ACPI_STATE_C1;
+    acpi_power->states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_EM_HALT;
+
+    acpi_power->states[ACPI_STATE_C0].valid = 1;
+    acpi_power->states[ACPI_STATE_C1].valid = 1;
+
+    acpi_power->count = 2;
+    acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
+    acpi_power->cpu = cpu;
+
     if ( cpu == 0 )
     {
         if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
@@ -870,11 +870,10 @@ static void set_cx(
 
     if ( xen_cx->type == ACPI_STATE_C1 )
         cx = &acpi_power->states[1];
-    else
-        cx = &acpi_power->states[acpi_power->count];
-
-    if ( !cx->valid )
-        acpi_power->count++;
+    else {
+        ASSERT(acpi_power->count < ACPI_PROCESSOR_MAX_POWER);
+        cx = &acpi_power->states[acpi_power->count++];
+    }
 
     cx->valid    = 1;
     cx->type     = xen_cx->type;
Signature · 1 line
Eric Chanudet
#1 lore raw
Quoted 29 lines
Quoted 1 line
Quoted 1 line · eric

Calling XENPF_set_processor_pminfo with XEN_PM_CX could cause states array in "struct acpi_processor_power" to exceed its limit.

The array used to be reset (by function cpuidle_init_cpu()) for each hypercall. The patch puts it back that way and adds an assertion to make it clear in case that happens again.

xen/arch/x86/acpi/cpu_idle.c +0 -10
@@ -622,19 +622,19 @@ static int cpuidle_init_cpu(int cpu)
 
         for ( i = 0; i < ACPI_PROCESSOR_MAX_POWER; i++ )
             acpi_power->states[i].idx = i;
-
-        acpi_power->states[ACPI_STATE_C1].type = ACPI_STATE_C1;
-        acpi_power->states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_EM_HALT;
-
-        acpi_power->states[ACPI_STATE_C0].valid = 1;
-        acpi_power->states[ACPI_STATE_C1].valid = 1;
-
-        acpi_power->count = 2;
-        acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
-        acpi_power->cpu = cpu;
         processor_powers[cpu] = acpi_power;
     }

xen/arch/x86/acpi/cpu_idle.c static int cpuidle_init_cpu(int cpu) · line 636

Quoted 10 lines
xen/arch/x86/acpi/cpu_idle.c +9 -0
 
+    acpi_power->states[ACPI_STATE_C1].type = ACPI_STATE_C1;
+    acpi_power->states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_EM_HALT;
+
+    acpi_power->states[ACPI_STATE_C0].valid = 1;
+    acpi_power->states[ACPI_STATE_C1].valid = 1;
+
+    acpi_power->count = 2;
+    acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
+    acpi_power->cpu = cpu;

Is there any reason to move all of these assignments, rather than just the assignment of ->count and maybe ->states[ACPI_STATE_C1]? Everything else should not get modified anywhere else.

Quoted 5 lines
xen/arch/x86/acpi/cpu_idle.c +1 -0
+
     if ( cpu == 0 )
     {
         if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )

xen/arch/x86/acpi/cpu_idle.c static void set_cx( · line 873

Quoted 9 lines
xen/arch/x86/acpi/cpu_idle.c +1 -5
@@ -870,11 +870,10 @@ static void set_cx(
 
     if ( xen_cx->type == ACPI_STATE_C1 )
         cx = &acpi_power->states[1];
-    else
-        cx = &acpi_power->states[acpi_power->count];
-
-    if ( !cx->valid )
-        acpi_power->count++;
+    else {

Please put the brace on a new line.

xen/arch/x86/acpi/cpu_idle.c static void set_cx( · line 876

Quoted 3 lines
xen/arch/x86/acpi/cpu_idle.c +3 -0
+        ASSERT(acpi_power->count < ACPI_PROCESSOR_MAX_POWER);
+        cx = &acpi_power->states[acpi_power->count++];
+    }

To be on the safe side, you should probably also check for ACPI_STATE_C0 (and ignore its data), otherwise (other than before your patch) ->count would get incremented for this too. So I'd suggest converting the if() here to a switch().

Jan

Quoted 11 lines
xen/arch/x86/acpi/cpu_idle.c +0 -0
     cx->valid    = 1;
     cx->type     = xen_cx->type;
 
Signature · 6 lines
Eric Chanudet

_______________________________________________
Xen-devel mailing list
[email protected] 
http://lists.xen.org/xen-devel 
#2 lore raw
Quoted 3 lines · Jan Beulich

Is there any reason to move all of these assignments, rather than just the assignment of ->count and maybe ->states[ACPI_STATE_C1]? Everything else should not get modified anywhere else.

I see no reason indeed, this is how it was before everything went up in the if() statement.

Quoted 4 lines

To be on the safe side, you should probably also check for ACPI_STATE_C0 (and ignore its data), otherwise (other than before your patch) ->count would get incremented for this too. So I'd suggest converting the if() here to a switch().

Done.

Here is the refreshed patch.

xen/arch/x86/acpi/cpu_idle.c +15 -11
diff -r 8964c223836c xen/arch/x86/acpi/cpu_idle.c
@@ -645,12 +645,12 @@
      
         acpi_power->states[ACPI_STATE_C0].valid = 1;
         acpi_power->states[ACPI_STATE_C1].valid = 1;
+        acpi_power->cpu = cpu;
+        processor_powers[cpu] = acpi_power;
+    }
      
         acpi_power->count = 2;
         acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
-        acpi_power->cpu = cpu;
-        processor_powers[cpu] = acpi_power;
-    }
 
     if ( cpu == 0 )
     {
@@ -885,16 +885,20 @@
     if ( check_cx(acpi_power, xen_cx) != 0 )
         return;
 
-    if ( xen_cx->type == ACPI_STATE_C1 )
+    switch (xen_cx->type)
+    {
+    case ACPI_STATE_C0:
+        return;
+    case ACPI_STATE_C1:
         cx = &acpi_power->states[1];
-    else
-        cx = &acpi_power->states[acpi_power->count];
+        break;
+    default:
+        ASSERT(acpi_power->count < ACPI_PROCESSOR_MAX_POWER);
+        cx = &acpi_power->states[acpi_power->count++];
+        cx->valid = 1;
+        cx->type = xen_cx->type;
+    }
 
-    if ( !cx->valid )
-        acpi_power->count++;
-
-    cx->valid    = 1;
-    cx->type     = xen_cx->type;
     cx->address  = xen_cx->reg.address;
 
     switch ( xen_cx->reg.space_id )
Signature · 1 line
Eric Chanudet
#3 lore raw

xen/arch/x86/acpi/cpu_idle.c line 653

Quoted 13 lines
Quoted 1 line
Quoted 1 line · Eric Chanudet
xen/arch/x86/acpi/cpu_idle.c +3 -0
@@ -645,12 +645,12 @@
 
         acpi_power->states[ACPI_STATE_C0].valid = 1;
         acpi_power->states[ACPI_STATE_C1].valid = 1;
+        acpi_power->cpu = cpu;
+        processor_powers[cpu] = acpi_power;
+    }
 
         acpi_power->count = 2;
         acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];

Please fix the indentation here. Further, I'd really like to see the two C1-related lines (setting .type and .entry_method) moved out of the if() as well - if we're getting fresh data from Dom0, we shouldn't make assumptions on the validity of the old data.

xen/arch/x86/acpi/cpu_idle.c line 656

Quoted 6 lines
xen/arch/x86/acpi/cpu_idle.c +0 -3
-        acpi_power->cpu = cpu;
-        processor_powers[cpu] = acpi_power;
-    }
 
     if ( cpu == 0 )
     {

Further, as you're adjusting this anyway, I think this whole if() should move into the earlier if()'s body.

Quoted 6 lines
xen/arch/x86/acpi/cpu_idle.c +1 -1
@@ -885,16 +885,20 @@
     if ( check_cx(acpi_power, xen_cx) != 0 )
         return;
 
-    if ( xen_cx->type == ACPI_STATE_C1 )
+    switch (xen_cx->type)

xen/arch/x86/acpi/cpu_idle.c line 896

Quoted 10 lines
xen/arch/x86/acpi/cpu_idle.c +7 -2
+    {
+    case ACPI_STATE_C0:
+        return;
+    case ACPI_STATE_C1:
         cx = &acpi_power->states[1];
-    else
-        cx = &acpi_power->states[acpi_power->count];
+        break;
+    default:
+        ASSERT(acpi_power->count < ACPI_PROCESSOR_MAX_POWER);

After some more thinking about this, I don't see ASSERT() as a proper action here. Rather than crashing the debug hypervisor and silently corrupting data on a non-debug one, print a warning here and bail out.

xen/arch/x86/acpi/cpu_idle.c line 904

Quoted 13 lines
xen/arch/x86/acpi/cpu_idle.c +4 -5
+        cx = &acpi_power->states[acpi_power->count++];
+        cx->valid = 1;
+        cx->type = xen_cx->type;
+    }
 
-    if ( !cx->valid )
-        acpi_power->count++;
-
-    cx->valid    = 1;
-    cx->type     = xen_cx->type;
     cx->address  = xen_cx->reg.address;
 
     switch ( xen_cx->reg.space_id )

As the patch is sufficiently different from the previous version, I think you ought to re-submit with a full patch description and s-o-b line. Or maybe I should just take your patch as a basis and do the adjustments myself...

Jan

#4 lore raw
In reply to Eric Chanudet

Calling XENPF_set_processor_pminfo with XEN_PM_CX could cause states array in "struct acpi_processor_power" to exceed its limit.

The array used to be reset (by function cpuidle_init_cpu()) for each hypercall. The patch puts it back that way and adds an assertion to make it clear in case that happens again.

- convert assertion to printk() & bail
- eliminate struct acpi_processor_cx's valid member (not read anymore)
- further adjustments to one-time-only vs each-time operations in cpuidle_init_cpu()
- don't use ACPI_STATE_Cn as array index anymore

xen/arch/x86/acpi/cpu_idle.c +38 -40
@@ -73,10 +73,8 @@ static void lapic_timer_nop(void) { }
 static void (*lapic_timer_off)(void);
 static void (*lapic_timer_on)(void);
 
-static uint64_t (*get_tick)(void);
-static uint64_t (*ticks_elapsed)(uint64_t t1, uint64_t t2);
-static uint64_t (*tick_to_ns)(uint64_t ticks);
-static uint64_t (*ns_to_tick)(uint64_t ticks);
+static uint64_t (*__read_mostly tick_to_ns)(uint64_t) = acpi_pm_tick_to_ns;
+static uint64_t (*__read_mostly ns_to_tick)(uint64_t) = ns_to_acpi_pm_tick;
 
 static void (*pm_idle_save) (void) __read_mostly;
 unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER - 1;
@@ -234,6 +232,10 @@ static uint64_t acpi_pm_ticks_elapsed(ui
         return ((0xFFFFFFFF - t1) + t2 +1);
 }
 
+static uint64_t (*__read_mostly get_tick)(void) = get_acpi_pm_tick;
+static uint64_t (*__read_mostly ticks_elapsed)(uint64_t, uint64_t)
+    = acpi_pm_ticks_elapsed;
+
 #define MWAIT_ECX_INTERRUPT_BREAK   (0x1)
 
 /*
@@ -632,43 +634,31 @@ static int cpuidle_init_cpu(int cpu)
     acpi_power = processor_powers[cpu];
     if ( !acpi_power )
     {
-        int i;
+        unsigned int i;
+
+        if ( cpu == 0 && boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
+        {
+            get_tick = get_stime_tick;
+            ticks_elapsed = stime_ticks_elapsed;
+            tick_to_ns = stime_tick_to_ns;
+            ns_to_tick = ns_to_stime_tick;
+        }
+
         acpi_power = xzalloc(struct acpi_processor_power);
         if ( !acpi_power )
             return -ENOMEM;
 
         for ( i = 0; i < ACPI_PROCESSOR_MAX_POWER; i++ )
             acpi_power->states[i].idx = i;
-     
-        acpi_power->states[ACPI_STATE_C1].type = ACPI_STATE_C1;
-        acpi_power->states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_EM_HALT;
-     
-        acpi_power->states[ACPI_STATE_C0].valid = 1;
-        acpi_power->states[ACPI_STATE_C1].valid = 1;
-     
-        acpi_power->count = 2;
-        acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
+
         acpi_power->cpu = cpu;
         processor_powers[cpu] = acpi_power;
     }
 
-    if ( cpu == 0 )
-    {
-        if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) )
-        {
-            get_tick = get_stime_tick;
-            ticks_elapsed = stime_ticks_elapsed;
-            tick_to_ns = stime_tick_to_ns;
-            ns_to_tick = ns_to_stime_tick;
-        }
-        else
-        {
-            get_tick = get_acpi_pm_tick;
-            ticks_elapsed = acpi_pm_ticks_elapsed;
-            tick_to_ns = acpi_pm_tick_to_ns;
-            ns_to_tick = ns_to_acpi_pm_tick;
-        }
-    }
+    acpi_power->count = 2;
+    acpi_power->states[1].type = ACPI_STATE_C1;
+    acpi_power->states[1].entry_method = ACPI_CSTATE_EM_HALT;
+    acpi_power->safe_state = &acpi_power->states[1];
 
     return 0;
 }
@@ -885,17 +875,25 @@ static void set_cx(
     if ( check_cx(acpi_power, xen_cx) != 0 )
         return;
 
-    if ( xen_cx->type == ACPI_STATE_C1 )
+    switch ( xen_cx->type )
+    {
+    case ACPI_STATE_C1:
         cx = &acpi_power->states[1];
-    else
-        cx = &acpi_power->states[acpi_power->count];
-
-    if ( !cx->valid )
-        acpi_power->count++;
+        break;
+    default:
+        if ( acpi_power->count >= ACPI_PROCESSOR_MAX_POWER )
+        {
+    case ACPI_STATE_C0:
+            printk(XENLOG_WARNING "CPU%u: C%d data ignored\n",
+                   acpi_power->cpu, xen_cx->type);
+            return;
+        }
+        cx = &acpi_power->states[acpi_power->count++];
+        cx->type = xen_cx->type;
+        break;
+    }
 
-    cx->valid    = 1;
-    cx->type     = xen_cx->type;
-    cx->address  = xen_cx->reg.address;
+    cx->address = xen_cx->reg.address;
 
     switch ( xen_cx->reg.space_id )
     {
xen/include/xen/cpuidle.h +0 -1
@@ -40,7 +40,6 @@
 struct acpi_processor_cx
 {
     u8 idx;
-    u8 valid;
     u8 type;
     u32 address;
     u8 entry_method; /* ACPI_CSTATE_EM_xxx */
#5 lore raw
In reply to eric

Sorry, can you give more descriptions about this issue? I doesn't see any problem.

best regards
yang

Quoted 77 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of eric
Sent: Wednesday, March 07, 2012 10:07 PM
To: [email protected]
Cc: [email protected]
Subject: [Xen-devel] [PATCH] XENPF_set_processor_pminfo XEN_PM_CX overflows states array

Calling XENPF_set_processor_pminfo with XEN_PM_CX could cause states array in "struct acpi_processor_power" to exceed its limit.

The array used to be reset (by function cpuidle_init_cpu()) for each hypercall. The patch puts it back that way and adds an assertion to make it clear in case that happens again.

xen/arch/x86/acpi/cpu_idle.c +16 -16
@@ -622,19 +622,19 @@ static int cpuidle_init_cpu(int cpu)
 
         for ( i = 0; i < ACPI_PROCESSOR_MAX_POWER; i++ )
             acpi_power->states[i].idx = i;
-
-        acpi_power->states[ACPI_STATE_C1].type = ACPI_STATE_C1;
-        acpi_power->states[ACPI_STATE_C1].entry_method =
 ACPI_CSTATE_EM_HALT;
-
-        acpi_power->states[ACPI_STATE_C0].valid = 1;
-        acpi_power->states[ACPI_STATE_C1].valid = 1;
-
-        acpi_power->count = 2;
-        acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
-        acpi_power->cpu = cpu;
         processor_powers[cpu] = acpi_power;
     }
 
+    acpi_power->states[ACPI_STATE_C1].type = ACPI_STATE_C1;
+    acpi_power->states[ACPI_STATE_C1].entry_method =
+ ACPI_CSTATE_EM_HALT;
+
+    acpi_power->states[ACPI_STATE_C0].valid = 1;
+    acpi_power->states[ACPI_STATE_C1].valid = 1;
+
+    acpi_power->count = 2;
+    acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
+    acpi_power->cpu = cpu;
+
     if ( cpu == 0 )
     {
         if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) ) @@ -870,11
+870,10 @@ static void set_cx(
 
     if ( xen_cx->type == ACPI_STATE_C1 )
         cx = &acpi_power->states[1];
-    else
-        cx = &acpi_power->states[acpi_power->count];
-
-    if ( !cx->valid )
-        acpi_power->count++;
+    else {
+        ASSERT(acpi_power->count < ACPI_PROCESSOR_MAX_POWER);
+        cx = &acpi_power->states[acpi_power->count++];
+    }
 
     cx->valid    = 1;
     cx->type     = xen_cx->type;
 
--

Eric Chanudet

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#6 lore raw
Quoted 3 lines
Quoted 1 line
Quoted 1 line · Yang Z

Sorry, can you give more descriptions about this issue? I doesn't see any problem.

This is pretty obvious once you know what to look for: Just consider Dom0 issues the respective hypercall twice for each CPU. cpuidle_init_cpu() in its unpatched form won't reset acpi_power->count back to 2, yet set_cx() will continue to happily pick fresh array slots and increment the count further (unless the ->valid flag happens to be set in this uninitialized chunk of memory). Iiuc this should be easily reproducible unloading and reloading processor.ko on a XenoLinux kernel.

Jan

Quoted 84 lines
Quoted 9 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of eric
Sent: Wednesday, March 07, 2012 10:07 PM
To: [email protected]
Cc: [email protected]
Subject: [Xen-devel] [PATCH] XENPF_set_processor_pminfo XEN_PM_CX overflows states array

Quoted 4 lines

Calling XENPF_set_processor_pminfo with XEN_PM_CX could cause states array in "struct acpi_processor_power" to exceed its limit.

The array used to be reset (by function cpuidle_init_cpu()) for each

hypercall.

Quoted 1 line

The patch puts it back that way and adds an assertion to make it clear in

case that

Quoted 59 lines

happens again.

xen/arch/x86/acpi/cpu_idle.c +16 -16
@@ -622,19 +622,19 @@ static int cpuidle_init_cpu(int cpu)
 
         for ( i = 0; i < ACPI_PROCESSOR_MAX_POWER; i++ )
             acpi_power->states[i].idx = i;
-
-        acpi_power->states[ACPI_STATE_C1].type = ACPI_STATE_C1;
-        acpi_power->states[ACPI_STATE_C1].entry_method =
 ACPI_CSTATE_EM_HALT;
-
-        acpi_power->states[ACPI_STATE_C0].valid = 1;
-        acpi_power->states[ACPI_STATE_C1].valid = 1;
-
-        acpi_power->count = 2;
-        acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
-        acpi_power->cpu = cpu;
         processor_powers[cpu] = acpi_power;
     }
 
+    acpi_power->states[ACPI_STATE_C1].type = ACPI_STATE_C1;
+    acpi_power->states[ACPI_STATE_C1].entry_method =
+ ACPI_CSTATE_EM_HALT;
+
+    acpi_power->states[ACPI_STATE_C0].valid = 1;
+    acpi_power->states[ACPI_STATE_C1].valid = 1;
+
+    acpi_power->count = 2;
+    acpi_power->safe_state = &acpi_power->states[ACPI_STATE_C1];
+    acpi_power->cpu = cpu;
+
     if ( cpu == 0 )
     {
         if ( boot_cpu_has(X86_FEATURE_NONSTOP_TSC) ) @@ -870,11
+870,10 @@ static void set_cx(
 
     if ( xen_cx->type == ACPI_STATE_C1 )
         cx = &acpi_power->states[1];
-    else
-        cx = &acpi_power->states[acpi_power->count];
-
-    if ( !cx->valid )
-        acpi_power->count++;
+    else {
+        ASSERT(acpi_power->count < ACPI_PROCESSOR_MAX_POWER);
+        cx = &acpi_power->states[acpi_power->count++];
+    }
 
     cx->valid    = 1;
     cx->type     = xen_cx->type;
 
--

Eric Chanudet

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel