Skip to contentSkip to conversation

rtc: fix a infinite loop in windows vm startup

PATCH In review Peng Hao 3 participants 11 messages Jul 24, 2017 – Jul 27, 2017

lore
Share & tools
lore

Peng Hao

author
#0 lore raw

PATCH v3 1/1 rtc: fix a infinite loop in windows vm startup

When a windows vm starts, periodic timer of rtc will stop several times. windows kernel will check whether REG_A_UIP is changed. REG_C's interrupt flags will not be cleared when periodic timer stops and the update timer will switch to alarm timer. So the expiration time of alarm timer is very long and REG_A_UIP will not vary.At last windows kernel will repeat to check REG_A_UIP all the time.

---
 hw/timer/mc146818rtc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/timer/mc146818rtc.c +3 -1
index 1b8d3d7..aa55fae 100644
@@ -457,6 +457,8 @@ static void rtc_update_timer(void *opaque)
     if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) {
         s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
         qemu_irq_raise(s->irq);
+    } else if ((s->cmos_data[RTC_REG_B] & REG_B_UIE) == 0) {
+        s->cmos_data[RTC_REG_C] &= ~REG_C_UF;
     }
     check_update_timer(s);
 }
@@ -559,7 +561,7 @@ static void cmos_ioport_write(void *opaque, hwaddr addr,
                 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
                 qemu_irq_raise(s->irq);
             } else {
-                s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF;
+                s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF);
                 qemu_irq_lower(s->irq);
             }
             s->cmos_data[RTC_REG_B] = data;
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 6 lines · Peng Hao

When a windows vm starts, periodic timer of rtc will stop several times. windows kernel will check whether REG_A_UIP is changed. REG_C's interrupt flags will not be cleared when periodic timer stops and the update timer will switch to alarm timer. So the expiration time of alarm timer is very long and REG_A_UIP will not vary.At last windows kernel will repeat to check REG_A_UIP all the time.

This should not happen. REG_A_UIP is set and cleared in register A every second, like this:

        case RTC_REG_A:
            if (update_in_progress(s)) {
                s->cmos_data[s->cmos_index] |= REG_A_UIP;
            } else {
                s->cmos_data[s->cmos_index] &= ~REG_A_UIP;
            }
            ret = s->cmos_data[s->cmos_index];
            break;

where update_in_progress does:

    guest_nsec = get_guest_rtc_ns(s);
    /* UIP bit will be set at last 244us of every second. */
    if ((guest_nsec % NANOSECONDS_PER_SECOND) >=
        (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH)) {
        return 1;
    }
    return 0;

This is done even if the timer is not pending.

How can the bug be reproduced?

Paolo

Quoted 29 lines
---
 hw/timer/mc146818rtc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/timer/mc146818rtc.c +3 -1
index 1b8d3d7..aa55fae 100644
@@ -457,6 +457,8 @@ static void rtc_update_timer(void *opaque)
     if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) {
         s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
         qemu_irq_raise(s->irq);
+    } else if ((s->cmos_data[RTC_REG_B] & REG_B_UIE) == 0) {
+        s->cmos_data[RTC_REG_C] &= ~REG_C_UF;
     }
     check_update_timer(s);
 }
@@ -559,7 +561,7 @@ static void cmos_ioport_write(void *opaque, hwaddr addr,
                 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
                 qemu_irq_raise(s->irq);
             } else {
-                s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF;
+                s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF);
                 qemu_irq_lower(s->irq);
             }
             s->cmos_data[RTC_REG_B] = data;
#2 lore raw
Quoted 15 lines · Peng Hao
Quoted 11 lines

When a windows vm starts, periodic timer of rtc will stop several times. windows kernel will check whether REG_A_UIP is changed. REG_C's interrupt flags will not be cleared when periodic timer stops and the update timer will switch to alarm timer. So the expiration time of alarm timer is very long and REG_A_UIP will not vary.At last windows kernel will repeat to check REG_A_UIP all the time.

This should not happen. REG_A_UIP is set and cleared in register A every second, like this:

Quoted 9 lines
       case RTC_REG_A:

           if (update_in_progress(s)) {
               s->cmos_data[s->cmos_index] |= REG_A_UIP
           } else {
               s->cmos_data[s->cmos_index] &= ~REG_A_UIP
           }
           ret = s->cmos_data[s->cmos_index]
           break

when periodic timer stop, update timer is set to a long expire time (as alarm timer).

So update_in_progress always return 1 and REG_A_UIP is never cleared.

Quoted 10 lines

where update_in_progress does:

   guest_nsec = get_guest_rtc_ns(s)
   /* UIP bit will be set at last 244us of every second. */
   if ((guest_nsec % NANOSECONDS_PER_SECOND) >=
       (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH)) {
       return 1
   }
   return 0
Quoted 3 lines

This is done even if the timer is not pending.

How can the bug be reproduced?

many windows VMs reboot at the same time.some VM's vcpu thread go to a infinite loop

at the starting stage.

Quoted 31 lines

Paolo

---
 hw/timer/mc146818rtc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/timer/mc146818rtc.c +3 -1
index 1b8d3d7..aa55fae 100644
@@ -457,6 +457,8 @@ static void rtc_update_timer(void *opaque)
     if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) {
         s->cmos_data[RTC_REG_C] |= REG_C_IRQF
         qemu_irq_raise(s->irq)
+    } else if ((s->cmos_data[RTC_REG_B] & REG_B_UIE) == 0) {
+        s->cmos_data[RTC_REG_C] &= ~REG_C_UF
     }
     check_update_timer(s)
 }
@@ -559,7 +561,7 @@ static void cmos_ioport_write(void *opaque, hwaddr addr,
                 s->cmos_data[RTC_REG_C] |= REG_C_IRQF
                 qemu_irq_raise(s->irq)
             } else {
-                s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF
+                s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF)
                 qemu_irq_lower(s->irq)
             }
             s->cmos_data[RTC_REG_B] = data
#3 lore raw
Quoted 27 lines · On 25/07/2017 06:14, [email protected] wrote:
Quoted 15 lines · Peng Hao
Quoted 11 lines

When a windows vm starts, periodic timer of rtc will stop several times. windows kernel will check whether REG_A_UIP is changed. REG_C's interrupt flags will not be cleared when periodic timer stops and the update timer will switch to alarm timer. So the expiration time of alarm timer is very long and REG_A_UIP will not vary.At last windows kernel will repeat to check REG_A_UIP all the time.

This should not happen. REG_A_UIP is set and cleared in register A every second, like this:

Quoted 9 lines
       case RTC_REG_A:

           if (update_in_progress(s)) {
               s->cmos_data[s->cmos_index] |= REG_A_UIP
           } else {
               s->cmos_data[s->cmos_index] &= ~REG_A_UIP
           }
           ret = s->cmos_data[s->cmos_index]
           break
Quoted 1 line

when periodic timer stop, update timer is set to a long expire time (as alarm timer).

I think I see the bug now:

hw/timer/mc146818rtc.c +5 -3
index 1b8d3d7d4c..6184b4378e 100644
@@ -321,9 +321,11 @@ static void check_update_timer(RTCState *s)
     s->next_alarm_time = next_update_time +
                          (next_alarm_sec - 1) * NANOSECONDS_PER_SECOND;
 
-    if (s->cmos_data[RTC_REG_C] & REG_C_UF) {
-        /* UF is set, but AF is clear.  Program the timer to target
-         * the alarm time.  */
+    if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
+        !(s->cmos_data[RTC_REG_A] & REG_A_UIP)) {
+        /* If UIP was latched, we need to clear it at the next update.
+         * Otherwise, if UF is set we only need to program the timer to
+         * target the alarm time.  */
         next_update_time = s->next_alarm_time;
     }
     if (next_update_time != timer_expire_time_ns(s->update_timer)) {

but I would like to have a testcase for it in tests/rtc-test.c.

Can you check if the above works and try writing a testcase (that fails without the patch and succeeds with it)?

Thanks,

Paolo

#4 lore raw

Re: [Qemu-devel] [PATCH V3] rtc: fix a infinite loop in windows vmstartup

Quoted 57 lines · On 25/07/2017 06:14, [email protected] wrote:
Quoted 31 lines
Quoted 20 lines · Peng Hao
Quoted 11 lines

When a windows vm starts, periodic timer of rtc will stop several times. windows kernel will check whether REG_A_UIP is changed. REG_C's interrupt flags will not be cleared when periodic timer stops and the update timer will switch to alarm timer. So the expiration time of alarm timer is very long and REG_A_UIP will not vary.At last windows kernel will repeat to check REG_A_UIP all the time.

This should not happen. REG_A_UIP is set and cleared in register A every second, like this:

Quoted 8 lines
       case RTC_REG_A:
           if (update_in_progress(s)) {
               s->cmos_data[s->cmos_index] |= REG_A_UIP
           } else {
               s->cmos_data[s->cmos_index] &= ~REG_A_UIP
           }
           ret = s->cmos_data[s->cmos_index]
           break
Quoted 1 line

when periodic timer stop, update timer is set to a long expire time (as alarm timer).

I think I see the bug now:

hw/timer/mc146818rtc.c +5 -3
index 1b8d3d7d4c..6184b4378e 100644
@@ -321,9 +321,11 @@ static void check_update_timer(RTCState *s)
    s->next_alarm_time = next_update_time +
                         (next_alarm_sec - 1) * NANOSECONDS_PER_SECOND
 
-    if (s->cmos_data[RTC_REG_C] & REG_C_UF) {
-        /* UF is set, but AF is clear.  Program the timer to target
-         * the alarm time.  */
+    if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
+        !(s->cmos_data[RTC_REG_A] & REG_A_UIP)) {
+        /* If UIP was latched, we need to clear it at the next update.
+         * Otherwise, if UF is set we only need to program the timer to
+         * target the alarm time.  */
        next_update_time = s->next_alarm_time
    }
    if (next_update_time != timer_expire_time_ns(s->update_timer)) {

but I would like to have a testcase for it in tests/rtc-test.c.

Quoted 3 lines

Can you check if the above works and try writing a testcase (that fails without the patch and succeeds with it)?
Thanks,

I don't think it can works. REG_C_UF is totally cleared by periodic timer in original code.

after periodic timer stoped, the REG_C_UF is never cleared.

rtc_update_timer has cleared REG_A_UIP,I think the patch does nothing.

I reproduce the bug when many windows VMs reboot and it is about the method of windows kernel accessing rtc .

so i don't know how to write the testcase.

Quoted 1 line

Paolo

#5 lore raw

Re: [Qemu-devel] [PATCH V3] rtc: fix a infinite loop in windows vmstartup

Quoted 52 lines · On 25/07/2017 11:29, [email protected] wrote:
Quoted 52 lines · On 25/07/2017 06:14, [email protected] wrote:
Quoted 27 lines
Quoted 16 lines · Peng Hao
Quoted 11 lines

When a windows vm starts, periodic timer of rtc will stop several times. windows kernel will check whether REG_A_UIP is changed. REG_C's interrupt flags will not be cleared when periodic timer stops and the update timer will switch to alarm timer. So the expiration time of alarm timer is very long and REG_A_UIP will not vary.At last windows kernel will repeat to check REG_A_UIP all the time.

This should not happen. REG_A_UIP is set and cleared in register A every second, like this:

Quoted 8 lines
       case RTC_REG_A:
           if (update_in_progress(s)) {
               s->cmos_data[s->cmos_index] |= REG_A_UIP
           } else {
               s->cmos_data[s->cmos_index] &= ~REG_A_UIP
           }
           ret = s->cmos_data[s->cmos_index]
           break
Quoted 1 line

when periodic timer stop, update timer is set to a long expire time (as alarm timer).

I think I see the bug now:

hw/timer/mc146818rtc.c +5 -3
index 1b8d3d7d4c..6184b4378e 100644
@@ -321,9 +321,11 @@ static void check_update_timer(RTCState *s)
    s->next_alarm_time = next_update_time +
                         (next_alarm_sec - 1) * NANOSECONDS_PER_SECOND;
 
-    if (s->cmos_data[RTC_REG_C] & REG_C_UF) {
-        /* UF is set, but AF is clear.  Program the timer to target
-         * the alarm time.  */
+    if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
+        !(s->cmos_data[RTC_REG_A] & REG_A_UIP)) {
+        /* If UIP was latched, we need to clear it at the next update.
+         * Otherwise, if UF is set we only need to program the timer to
+         * target the alarm time.  */
        next_update_time = s->next_alarm_time;
    }
    if (next_update_time != timer_expire_time_ns(s->update_timer)) {
Quoted 6 lines
Quoted 4 lines

but I would like to have a testcase for it in tests/rtc-test.c. Can you check if the above works and try writing a testcase (that fails without the patch and succeeds with it)?
Thanks,

I don't think it can works.

Have you tested it?

Quoted 2 lines

REG_C_UF is totally cleared by periodic timer in original code. after periodic timer stoped, the REG_C_UF is never cleared.

Your patch cleared REG_C_UF, but you have not said why the actual hardware would clear REG_C_UF (it wouldn't).

Quoted 6 lines

rtc_update_timer has cleared REG_A_UIP,I think the patch does nothing.

I reproduce the bug when many windows VMs reboot and it is about the method of windows kernel accessing rtc .

so i don't know how to write the testcase.

First of all you have to describe the exact sequence of register accesses that causes the problem. I can help you writing the testcase, but your changes to the device model must reflect the actual behavior of the hardware.

Paolo

#6 lore raw

Re: [Qemu-devel] [PATCH V3] rtc: fix a infinite loop in windowsvmstartup

Quoted 71 lines · On 25/07/2017 11:29, [email protected] wrote:
Quoted 50 lines
Quoted 50 lines · On 25/07/2017 06:14, [email protected] wrote:
Quoted 25 lines
Quoted 14 lines · Peng Hao
Quoted 9 lines

When a windows vm starts, periodic timer of rtc will stop several times. windows kernel will check whether REG_A_UIP is changed. REG_C's interrupt flags will not be cleared when periodic timer stops and the update timer will switch to alarm timer. So the expiration time of alarm timer is very long and REG_A_UIP will not vary.At last windows kernel will repeat to check REG_A_UIP all the time.

This should not happen. REG_A_UIP is set and cleared in register A every second, like this:

Quoted 8 lines
       case RTC_REG_A:
           if (update_in_progress(s)) {
               s->cmos_data[s->cmos_index] |= REG_A_UIP
           } else {
               s->cmos_data[s->cmos_index] &= ~REG_A_UIP
           }
           ret = s->cmos_data[s->cmos_index]
           break
Quoted 1 line

when periodic timer stop, update timer is set to a long expire time (as alarm timer).

I think I see the bug now:

hw/timer/mc146818rtc.c +5 -3
index 1b8d3d7d4c..6184b4378e 100644
@@ -321,9 +321,11 @@ static void check_update_timer(RTCState *s)
    s->next_alarm_time = next_update_time +
                         (next_alarm_sec - 1) * NANOSECONDS_PER_SECOND
 
-    if (s->cmos_data[RTC_REG_C] & REG_C_UF) {
-        /* UF is set, but AF is clear.  Program the timer to target
-         * the alarm time.  */
+    if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
+        !(s->cmos_data[RTC_REG_A] & REG_A_UIP)) {
+        /* If UIP was latched, we need to clear it at the next update.
+         * Otherwise, if UF is set we only need to program the timer to
+         * target the alarm time.  */
        next_update_time = s->next_alarm_time
    }
    if (next_update_time != timer_expire_time_ns(s->update_timer)) {
Quoted 6 lines
Quoted 4 lines

but I would like to have a testcase for it in tests/rtc-test.c. Can you check if the above works and try writing a testcase (that fails without the patch and succeeds with it)?
Thanks,

I don't think it can works.

Have you tested it?

Quoted 2 lines

REG_C_UF is totally cleared by periodic timer in original code. after periodic timer stoped, the REG_C_UF is never cleared.

Your patch cleared REG_C_UF, but you have not said why the actual hardware would clear REG_C_UF (it wouldn't).

Quoted 5 lines

rtc_update_timer has cleared REG_A_UIP,I think the patch does nothing.

I reproduce the bug when many windows VMs reboot and it is about the method of windows kernel accessing rtc .

Quoted 6 lines
Quoted 1 line

so i don't know how to write the testcase.

First of all you have to describe the exact sequence of register accesses that causes the problem. I can help you writing the testcase, but your changes to the device model must reflect the actual behavior of the hardware.

when the problem happens , windows kernel is checking whether REG_A_UIP is

changing after periodic timer has stopped. windows kernel access REG_A according to

INB instrunction and it will spend several microseconds because of VM_EXIT.

update timer has changed to a long expire time (as alarm timer)on the one hand.

on the other hand 244 microseconds in one second is too short to hit the region.

windows kernel may check REG_A_UIP when considering RTC something wrong.

many windows VM reboot at the same time and rtc periodic timer may delay badly..

Quoted 1 line

Paolo

#7 lore raw

Re: [Qemu-devel] [PATCH V3] rtc: fix a infinite loop in windowsvmstartup

Quoted 6 lines · On 26/07/2017 03:28, [email protected] wrote:

when the problem happens , windows kernel is checking whether REG_A_UIP is changing after periodic timer has stopped. windows kernel access REG_A according to INB instrunction and it will spend several microseconds because of VM_EXIT.

A vmexit to the RTC timer should be around 10.000 clock cycles, which is less than a microsecond.

Quoted 9 lines

update timer has changed to a long expire time (as alarm timer)on the one hand.

on the other hand 244 microseconds in one second is too short to hit the region.

windows kernel may check REG_A_UIP when considering RTC something wrong. many windows VM reboot at the same time and rtc periodic timer may delay badly..

Does Windows do this test when Hyper-V englightenments are enabled (especially hv-relaxed)?

It seems to be a Windows issue to me. I'm not sure adding hacks to the device model is the right thing to do, especially because I don't understand why your fix worked.

Paolo

#8 lore raw

Re: [Qemu-devel] [PATCH V3] rtc: fix a infinite loop inwindowsvmstartup

Quoted 7 lines · On 26/07/2017 03:28, [email protected] wrote:
Quoted 6 lines
Quoted 7 lines
Quoted 4 lines

when the problem happens , windows kernel is checking whether REG_A_UIP is changing after periodic timer has stopped. windows kernel access REG_A according to INB instrunction and it will spend several microseconds because of VM_EXIT.

A vmexit to the RTC timer should be around 10.000 clock cycles, which is less than a microsecond.

I use perf tools getting data(windows VM)

IO Port Access Samples Samples% Time% Min Time Max Time Avg time

0x71: PIN 126 56.76% 32.62% 1us 2us 1.83us ( +- 0.62% )

0x70: POUT 63 28.38% 51.56% 4us 15us 5.78us ( +- 3.12% )

But I'm not sure it is accurate.

Quoted 6 lines
Quoted 6 lines

update timer has changed to a long expire time (as alarm timer)on the one hand.

on the other hand 244 microseconds in one second is too short to hit the region.

Quoted 6 lines
Quoted 3 lines

windows kernel may check REG_A_UIP when considering RTC something wrong. many windows VM reboot at the same time and rtc periodic timer may delay badly..

Does Windows do this test when Hyper-V englightenments are enabled (especially hv-relaxed)?

yes ,I enable hv-relaxed.in my windows VM.But I'm not sure the interconnection between them.

Quoted 4 lines

It seems to be a Windows issue to me. I'm not sure adding hacks to the

device model is the right thing to do, especially because I don't understand why your fix worked.

1. guest closes periodic timer,from then on rtc don't send interrupt anymore and no rtc handler can run to read

RTC_REG_C(this read set RTC_REG_C = 0, clear REG_C_UF)

2. update timer expires, rtc_update_timer clears REG_A_UIP, and set REG_C_UF

3. because REG_C_UF is setted, check_update_timer sets update_timer's expire time to next_alarm_time, which is

over ten hours

4. guest checks that REG_A_UIP changes from 0 to 1 in a infinte loop (we don't know why windows kernel does this) by read RTC_REG_A

5. update_in_progress called from cmos_ioport_read

6. beause update_timer is switched to alarm timer, which has a too big expire time to satisfes the fowllowing condition:

	if (qemu_clock_get_ns(rtc_clock) >=


            (next_update_time - UIP_HOLD_LENGTH)) {


            s->cmos_data[RTC_REG_A] |= REG_A_UIP


            return 1


        }

7. update_in_progress's second condition don't satisfes:

	if ((guest_nsec % NANOSECONDS_PER_SECOND) >=


        (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH)) {


        return 1


    }

because when many vms startup, inb instruction wastes much more time than normal situation and

guest_nsec % NANOSECONDS_PER_SECOND is nearly < NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH always.

8. finally, REG_A_UIP is always cleared, REG_C_UF is always set, update_timer is always alarm timer,

guest is always checking REG_A_UIP, and guest hung up.

#9 lore raw
In reply to Peng Hao
Quoted 10 lines · Peng Hao

When a windows vm starts, periodic timer of rtc will stop several times. windows kernel will check whether REG_A_UIP is changed. REG_C's interrupt flags will not be cleared when periodic timer stops and the update timer will switch to alarm timer. So the expiration time of alarm timer is very long and REG_A_UIP will not vary.At last windows kernel will repeat to check REG_A_UIP all the time.

---

When posting a v3, it's useful to tell us (after the --- separator) what changed from v2, to help us focus on why a v3 was needed.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#10 lore raw

Re: [Qemu-devel] [PATCH V3] rtc: fix a infinite loop in windows vmstartup

I'm sorry I copy and paste code to make a patch, but I make a mistake to send a uncompleted patch.

彭浩 penghao

IT开发工程师 IT Development
Engineer
操作系统产品部/中心研究院/系统产品 OS Product Department/Central R&D Institute/System Product

成都市天府大道中段800号中兴大厦A座
T: +86 028 18123274005
E: [email protected]
www.zte.com.cn

Original Mail

Sender: <[email protected]>
To: PengHao10096742 <[email protected]> <[email protected]>
CC: LiuYi10066463 <[email protected]>
Date: 2017/07/24 19:55
Subject: Re: [Qemu-devel] [PATCH V3] rtc: fix a infinite loop in windows vmstartup

Quoted 10 lines · Peng Hao

When a windows vm starts, periodic timer of rtc will stop several times. windows kernel will check whether REG_A_UIP is changed. REG_C's interrupt flags will not be cleared when periodic timer stops and the update timer will switch to alarm timer. So the expiration time of alarm timer is very long and REG_A_UIP will not vary.At last windows kernel will repeat to check REG_A_UIP all the time.

---

When posting a v3, it's useful to tell us (after the --- separator) what changed from v2, to help us focus on why a v3 was needed.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org