Skip to contentSkip to conversation

xen/arm: Validate generic timer frequency

PATCH In review Michal Orzel 5 participants 9 messages Sep 28, 2023 – Oct 13, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: Validate generic timer frequency

Generic timer dt node property "clock-frequency" (refer Linux dt binding Documentation/devicetree/bindings/timer/arm,arch_timer.yaml) is used to override the incorrect value set by firmware in CNTFRQ_EL0. If the value of this property is 0 (i.e. by mistake), Xen would continue to work and use the value from the sysreg instead. The logic is thus incorrect and results in inconsistency when creating timer node for domUs:
- libxl domUs: clock_frequency member of struct xen_arch_domainconfig is set to 0 and libxl ignores setting the "clock-frequency" property,
- dom0less domUs: "clock-frequency" property is taken from dt_host and thus set to 0.

Property "clock-frequency" is used to override the value from sysreg, so if it is also invalid, there is nothing we can do and we shall panic to let user know about incorrect setting. Going even further, we operate under assumption that the frequency must be at least 1KHz (i.e. cpu_khz not 0) in order for Xen to boot. Therefore, introduce a new helper validate_timer_frequency() to verify this assumption and use it to check the frequency obtained either from dt property or from sysreg.

---
 xen/arch/arm/time.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
xen/arch/arm/time.c +15 -0
index 3535bd8ac7c7..04b07096b165 100644
@@ -101,6 +101,17 @@ static void __init preinit_acpi_xen_time(void)
 static void __init preinit_acpi_xen_time(void) { }
 #endif
 
+static void __init validate_timer_frequency(void)
+{
+    /*
+     * ARM ARM does not impose any strict limit on the range of allowable
+     * system counter frequencies. However, we operate under the assumption
+     * that cpu_khz must not be 0.
+     */
+    if ( !cpu_khz )
+        panic("Timer frequency is less than 1 KHz\n");
+}
+
 /* Set up the timer on the boot CPU (early init function) */
 static void __init preinit_dt_xen_time(void)
 {
@@ -122,6 +133,7 @@ static void __init preinit_dt_xen_time(void)
     if ( res )
     {
         cpu_khz = rate / 1000;
+        validate_timer_frequency();
         timer_dt_clock_frequency = rate;
     }
 }
@@ -137,7 +149,10 @@ void __init preinit_xen_time(void)
         preinit_acpi_xen_time();
 
     if ( !cpu_khz )
+    {
         cpu_khz = (READ_SYSREG(CNTFRQ_EL0) & CNTFRQ_MASK) / 1000;
+        validate_timer_frequency();
+    }
 
     res = platform_init_time();
     if ( res )
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 23 lines · Michal Orzel

Generic timer dt node property "clock-frequency" (refer Linux dt binding Documentation/devicetree/bindings/timer/arm,arch_timer.yaml) is used to override the incorrect value set by firmware in CNTFRQ_EL0. If the value of this property is 0 (i.e. by mistake), Xen would continue to work and use the value from the sysreg instead. The logic is thus incorrect and results in inconsistency when creating timer node for domUs:
- libxl domUs: clock_frequency member of struct xen_arch_domainconfig is set to 0 and libxl ignores setting the "clock-frequency" property,
- dom0less domUs: "clock-frequency" property is taken from dt_host and thus set to 0.

Property "clock-frequency" is used to override the value from sysreg, so if it is also invalid, there is nothing we can do and we shall panic to let user know about incorrect setting. Going even further, we operate under assumption that the frequency must be at least 1KHz (i.e. cpu_khz not 0) in order for Xen to boot. Therefore, introduce a new helper validate_timer_frequency() to verify this assumption and use it to check the frequency obtained either from dt property or from sysreg.

Quoted 2 lines

---

Reviewed-by on v1 1/1
#2 lore raw
In reply to Michal Orzel
Quoted 19 lines · Michal Orzel

Generic timer dt node property "clock-frequency" (refer Linux dt binding Documentation/devicetree/bindings/timer/arm,arch_timer.yaml) is used to override the incorrect value set by firmware in CNTFRQ_EL0. If the value of this property is 0 (i.e. by mistake), Xen would continue to work and use the value from the sysreg instead. The logic is thus incorrect and results in inconsistency when creating timer node for domUs:
- libxl domUs: clock_frequency member of struct xen_arch_domainconfig is set to 0 and libxl ignores setting the "clock-frequency" property,
- dom0less domUs: "clock-frequency" property is taken from dt_host and thus set to 0.

Property "clock-frequency" is used to override the value from sysreg, so if it is also invalid, there is nothing we can do and we shall panic to let user know about incorrect setting. Going even further, we operate under assumption that the frequency must be at least 1KHz (i.e. cpu_khz not 0) in order for Xen to boot. Therefore, introduce a new helper validate_timer_frequency() to verify this assumption and use it to check the frequency obtained either from dt property or from sysreg.

Quoted 1 line
Quoted 48 lines
---
 xen/arch/arm/time.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
xen/arch/arm/time.c +15 -0
index 3535bd8ac7c7..04b07096b165 100644
@@ -101,6 +101,17 @@ static void __init preinit_acpi_xen_time(void)
 static void __init preinit_acpi_xen_time(void) { }
 #endif
 
+static void __init validate_timer_frequency(void)
+{
+    /*
+     * ARM ARM does not impose any strict limit on the range of allowable
+     * system counter frequencies. However, we operate under the assumption
+     * that cpu_khz must not be 0.
+     */
+    if ( !cpu_khz )
+        panic("Timer frequency is less than 1 KHz\n");
+}
+
 /* Set up the timer on the boot CPU (early init function) */
 static void __init preinit_dt_xen_time(void)
 {
@@ -122,6 +133,7 @@ static void __init preinit_dt_xen_time(void)
     if ( res )
     {
         cpu_khz = rate / 1000;
+        validate_timer_frequency();
         timer_dt_clock_frequency = rate;
     }
 }
@@ -137,7 +149,10 @@ void __init preinit_xen_time(void)
         preinit_acpi_xen_time();
 
     if ( !cpu_khz )
+    {
         cpu_khz = (READ_SYSREG(CNTFRQ_EL0) & CNTFRQ_MASK) / 1000;
+        validate_timer_frequency();
+    }
 
     res = platform_init_time();
     if ( res )
Signature · 1 line
2.25.1
Acked-by on v1 1/1
#3 lore raw
In reply to Michal Orzel

Hi Michal,

Quoted 19 lines · Michal Orzel

Generic timer dt node property "clock-frequency" (refer Linux dt binding Documentation/devicetree/bindings/timer/arm,arch_timer.yaml) is used to override the incorrect value set by firmware in CNTFRQ_EL0. If the value of this property is 0 (i.e. by mistake), Xen would continue to work and use the value from the sysreg instead. The logic is thus incorrect and results in inconsistency when creating timer node for domUs:
- libxl domUs: clock_frequency member of struct xen_arch_domainconfig is set to 0 and libxl ignores setting the "clock-frequency" property,
- dom0less domUs: "clock-frequency" property is taken from dt_host and thus set to 0.

Property "clock-frequency" is used to override the value from sysreg, so if it is also invalid, there is nothing we can do and we shall panic to let user know about incorrect setting. Going even further, we operate under assumption that the frequency must be at least 1KHz (i.e. cpu_khz not 0) in order for Xen to boot. Therefore, introduce a new helper validate_timer_frequency() to verify this assumption and use it to check the frequency obtained either from dt property or from sysreg.

Quoted 1 line

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

(+ Henry)

Hi Michal,

Quoted 22 lines · Julien Grall

Hi Michal,

Quoted 19 lines · Michal Orzel

Generic timer dt node property "clock-frequency" (refer Linux dt binding Documentation/devicetree/bindings/timer/arm,arch_timer.yaml) is used to override the incorrect value set by firmware in CNTFRQ_EL0. If the value of this property is 0 (i.e. by mistake), Xen would continue to work and use the value from the sysreg instead. The logic is thus incorrect and results in inconsistency when creating timer node for domUs:
  - libxl domUs: clock_frequency member of struct xen_arch_domainconfig is set to 0 and libxl ignores setting the "clock-frequency" property,
  - dom0less domUs: "clock-frequency" property is taken from dt_host and thus set to 0.

Property "clock-frequency" is used to override the value from sysreg, so if it is also invalid, there is nothing we can do and we shall panic to let user know about incorrect setting. Going even further, we operate under assumption that the frequency must be at least 1KHz (i.e. cpu_khz not 0) in order for Xen to boot. Therefore, introduce a new helper validate_timer_frequency() to verify this assumption and use it to check the frequency obtained either from dt property or from sysreg.

Quoted 3 lines
Quoted 1 line

Going through the list of pending patches, I noticed Henry wasn't CCed. Is this patch intended for Xen 4.18? If so, can you provide some reasoning why would want it?

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw

Hi Julien, Henry,

Quoted 29 lines · Julien Grall

(+ Henry)

Hi Michal,

Quoted 22 lines · Julien Grall

Hi Michal,

Quoted 19 lines · Michal Orzel

Generic timer dt node property "clock-frequency" (refer Linux dt binding Documentation/devicetree/bindings/timer/arm,arch_timer.yaml) is used to override the incorrect value set by firmware in CNTFRQ_EL0. If the value of this property is 0 (i.e. by mistake), Xen would continue to work and use the value from the sysreg instead. The logic is thus incorrect and results in inconsistency when creating timer node for domUs:
- libxl domUs: clock_frequency member of struct xen_arch_domainconfig is set to 0 and libxl ignores setting the "clock-frequency" property,
- dom0less domUs: "clock-frequency" property is taken from dt_host and thus set to 0.

Property "clock-frequency" is used to override the value from sysreg, so if it is also invalid, there is nothing we can do and we shall panic to let user know about incorrect setting. Going even further, we operate under assumption that the frequency must be at least 1KHz (i.e. cpu_khz not 0) in order for Xen to boot. Therefore, introduce a new helper validate_timer_frequency() to verify this assumption and use it to check the frequency obtained either from dt property or from sysreg.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Going through the list of pending patches, I noticed Henry wasn't CCed. Is this patch intended for Xen 4.18? If so, can you provide some reasoning why would want it?

Strictly speaking, lack of "for-4.18" prefix indicates that I did not particularly aim for 4.18. However, I find it useful, so I will leave it up to Henry to decide if we want that or not.

Benefits:
- fixes the invalid logic the consequence of which might result in inconsistency when booting the same OS as libxl domU and dom0less domU.
- prevents spreading out the error condition (i.e. rate < 1KHZ) that can lead to e.g. Xen inability to schedule, by panicing with proper msg
Risks:
- early init code, no critical path, in case of an error, panic returned with proper msg - no risks AFAICT

~Michal

#6 lore raw

Hi Michal, Julien,

Quoted 44 lines · Michal Orzel

Hi Julien, Henry,

Quoted 29 lines · Julien Grall

(+ Henry)

Hi Michal,

Quoted 22 lines · Julien Grall

Hi Michal,

Quoted 19 lines · Michal Orzel

Generic timer dt node property "clock-frequency" (refer Linux dt binding Documentation/devicetree/bindings/timer/arm,arch_timer.yaml) is used to override the incorrect value set by firmware in CNTFRQ_EL0. If the value of this property is 0 (i.e. by mistake), Xen would continue to work and use the value from the sysreg instead. The logic is thus incorrect and results in inconsistency when creating timer node for domUs:
- libxl domUs: clock_frequency member of struct xen_arch_domainconfig is set to 0 and libxl ignores setting the "clock-frequency" property,
- dom0less domUs: "clock-frequency" property is taken from dt_host and thus set to 0.

Property "clock-frequency" is used to override the value from sysreg, so if it is also invalid, there is nothing we can do and we shall panic to let user know about incorrect setting. Going even further, we operate under assumption that the frequency must be at least 1KHz (i.e. cpu_khz not 0) in order for Xen to boot. Therefore, introduce a new helper validate_timer_frequency() to verify this assumption and use it to check the frequency obtained either from dt property or from sysreg.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Going through the list of pending patches, I noticed Henry wasn't CCed. Is this patch intended for Xen 4.18? If so, can you provide some reasoning why would want it?

Strictly speaking, lack of "for-4.18" prefix indicates that I did not particularly aim for 4.18. However, I find it useful, so I will leave it up to Henry to decide if we want that or not.

Quoted 7 lines

Benefits:
- fixes the invalid logic the consequence of which might result in inconsistency when booting the same OS as libxl domU and dom0less domU.
- prevents spreading out the error condition (i.e. rate < 1KHZ) that can lead to e.g. Xen inability to schedule, by panicing with proper msg
Risks:
- early init code, no critical path, in case of an error, panic returned with proper msg - no risks AFAICT

Thanks for the explanation, this looks like an improvement for the current code to me so I am fine with including it in 4.18

Kind regards,
Henry

Quoted 2 lines

~Michal

#7 lore raw
Quoted 44 lines · Henry Wang

Hi Michal, Julien,

Quoted 42 lines · Michal Orzel

Hi Julien, Henry,

Quoted 29 lines · Julien Grall

(+ Henry)

Hi Michal,

Quoted 22 lines · Julien Grall

Hi Michal,

Quoted 19 lines · Michal Orzel

Generic timer dt node property "clock-frequency" (refer Linux dt binding Documentation/devicetree/bindings/timer/arm,arch_timer.yaml) is used to override the incorrect value set by firmware in CNTFRQ_EL0. If the value of this property is 0 (i.e. by mistake), Xen would continue to work and use the value from the sysreg instead. The logic is thus incorrect and results in inconsistency when creating timer node for domUs:
- libxl domUs: clock_frequency member of struct xen_arch_domainconfig is set to 0 and libxl ignores setting the "clock-frequency" property,
- dom0less domUs: "clock-frequency" property is taken from dt_host and thus set to 0.

Property "clock-frequency" is used to override the value from sysreg, so if it is also invalid, there is nothing we can do and we shall panic to let user know about incorrect setting. Going even further, we operate under assumption that the frequency must be at least 1KHz (i.e. cpu_khz not 0) in order for Xen to boot. Therefore, introduce a new helper validate_timer_frequency() to verify this assumption and use it to check the frequency obtained either from dt property or from sysreg.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Going through the list of pending patches, I noticed Henry wasn't CCed. Is this patch intended for Xen 4.18? If so, can you provide some reasoning why would want it?

Strictly speaking, lack of "for-4.18" prefix indicates that I did not particularly aim for 4.18.

I have seen a mix. Hence why I asked in case this was expected. To avoid confusion during the freeze period, I tend to also tag e-mail for the next release (e.g. for-next/for-4.19). So the intention is clear.

Quoted 8 lines
Quoted 7 lines

Benefits:
- fixes the invalid logic the consequence of which might result in inconsistency when booting the same OS as libxl domU and dom0less domU.
- prevents spreading out the error condition (i.e. rate < 1KHZ) that can lead to e.g. Xen inability to schedule, by panicing with proper msg
Risks:
- early init code, no critical path, in case of an error, panic returned with proper msg - no risks AFAICT

Quoted 4 lines

Thanks for the explanation, this looks like an improvement for the current code to me so I am fine with including it in 4.18

Thanks. I will commit the patch later today.

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw

Hi,

Quoted 61 lines · Julien Grall
Quoted 44 lines · Henry Wang

Hi Michal, Julien,

Quoted 42 lines · Michal Orzel

Hi Julien, Henry,

Quoted 29 lines · Julien Grall

(+ Henry)

Hi Michal,

Quoted 22 lines · Julien Grall

Hi Michal,

Quoted 19 lines · Michal Orzel

Generic timer dt node property "clock-frequency" (refer Linux dt binding Documentation/devicetree/bindings/timer/arm,arch_timer.yaml) is used to override the incorrect value set by firmware in CNTFRQ_EL0. If the value of this property is 0 (i.e. by mistake), Xen would continue to work and use the value from the sysreg instead. The logic is thus incorrect and results in inconsistency when creating timer node for domUs:
  - libxl domUs: clock_frequency member of struct xen_arch_domainconfig is set to 0 and libxl ignores setting the "clock-frequency" property,
  - dom0less domUs: "clock-frequency" property is taken from dt_host and thus set to 0.

Property "clock-frequency" is used to override the value from sysreg, so if it is also invalid, there is nothing we can do and we shall panic to let user know about incorrect setting. Going even further, we operate under assumption that the frequency must be at least 1KHz (i.e. cpu_khz not 0) in order for Xen to boot. Therefore, introduce a new helper validate_timer_frequency() to verify this assumption and use it to check the frequency obtained either from dt property or from sysreg.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Going through the list of pending patches, I noticed Henry wasn't CCed. Is this patch intended for Xen 4.18? If so, can you provide some reasoning why would want it?

Strictly speaking, lack of "for-4.18" prefix indicates that I did not particularly aim for 4.18.

I have seen a mix. Hence why I asked in case this was expected. To avoid confusion during the freeze period, I tend to also tag e-mail for the next release (e.g. for-next/for-4.19). So the intention is clear.

Quoted 11 lines
Quoted 10 lines

Benefits:
- fixes the invalid logic the consequence of which might result in inconsistency when booting
  the same OS as libxl domU and dom0less domU.
- prevents spreading out the error condition (i.e. rate < 1KHZ) that can lead to e.g. Xen inability to schedule,
  by panicing with proper msg
Risks:
- early init code, no critical path, in case of an error, panic returned with proper msg - no risks AFAICT

Quoted 6 lines
Quoted 4 lines

Thanks for the explanation, this looks like an improvement for the current code to me so I am fine with including it in 4.18

Thanks. I will commit the patch later today.

Now committed. Sorry for the delay.

Cheers,

Signature · 1 line
Julien Grall