Skip to contentSkip to conversation

xen/dt: Remove loop in dt_read_number()

PATCH In review Alejandro Vallejo 2 participants 2 messages Jun 19, 2025 – Jun 21, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 xen/dt: Remove loop in dt_read_number()

The DT spec declares only two number types for a property: u32 and u64, as per Table 2.3 in Section 2.2.4. Remove unbounded loop and replace with a switch statement. Default to a size of 1 cell in the nonsensical size case, with a warning printed on the Xen console.

---
If there's any #address-size > 2 out there at least we'll know in debug builds. It's not like this function can cope with them either way.

I haven't tested things past CI.

pipeline: https://gitlab.com/xen-project/people/agvallejo/xen/-/pipelines/1876848415

v3:
* s/WARNING/ERR
* //-style comment to /**/-style comment
* break after ASSERT_UNREACHABLE() to please MISRA

---
 xen/include/xen/device_tree.h | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
xen/include/xen/device_tree.h +15 -3
index 75017e4266..d2de7c3a13 100644
@@ -261,10 +261,22 @@ void intc_dt_preinit(void);
 /* Helper to read a big number; size is in cells (not bytes) */
 static inline u64 dt_read_number(const __be32 *cell, int size)
 {
-    u64 r = 0;
+    u64 r = be32_to_cpu(*cell);
+
+    switch ( size )
+    {
+    case 1:
+        break;
+    case 2:
+        r = (r << 32) | be32_to_cpu(cell[1]);
+        break;
+    default:
+        /* Nonsensical size. default to 1 */
+        printk(XENLOG_ERR "dt_read_number(,%d) bad size\n", size);
+        ASSERT_UNREACHABLE();
+        break;
+    };
 
-    while ( size-- )
-        r = (r << 32) | be32_to_cpu(*(cell++));
     return r;
 }
 

base-commit: 22650d6054625be10172fe0c78b9cadd1a39bd63

Signature · 1 line
2.43.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 7 lines · Alejandro Vallejo

The DT spec declares only two number types for a property: u32 and u64, as per Table 2.3 in Section 2.2.4. Remove unbounded loop and replace with a switch statement. Default to a size of 1 cell in the nonsensical size case, with a warning printed on the Xen console.

Quoted 51 lines

---
If there's any #address-size > 2 out there at least we'll know in debug builds. It's not like this function can cope with them either way.

I haven't tested things past CI.

pipeline: https://gitlab.com/xen-project/people/agvallejo/xen/-/pipelines/1876848415

v3:
* s/WARNING/ERR
* //-style comment to /**/-style comment
* break after ASSERT_UNREACHABLE() to please MISRA

---
 xen/include/xen/device_tree.h | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
xen/include/xen/device_tree.h +15 -3
index 75017e4266..d2de7c3a13 100644
@@ -261,10 +261,22 @@ void intc_dt_preinit(void);
 /* Helper to read a big number; size is in cells (not bytes) */
 static inline u64 dt_read_number(const __be32 *cell, int size)
 {
-    u64 r = 0;
+    u64 r = be32_to_cpu(*cell);
+
+    switch ( size )
+    {
+    case 1:
+        break;
+    case 2:
+        r = (r << 32) | be32_to_cpu(cell[1]);
+        break;
+    default:
+        /* Nonsensical size. default to 1 */
+        printk(XENLOG_ERR "dt_read_number(,%d) bad size\n", size);
+        ASSERT_UNREACHABLE();
+        break;
+    };
 
-    while ( size-- )
-        r = (r << 32) | be32_to_cpu(*(cell++));
     return r;
 }
 

base-commit: 22650d6054625be10172fe0c78b9cadd1a39bd63

Signature · 1 line
2.43.0