[PATCH v2 1/3] x86/match-cpu: Improvements to x86_match_cpu()

Andrew Cooper posted 3 patches 3 months, 2 weeks ago
[PATCH v2 1/3] x86/match-cpu: Improvements to x86_match_cpu()
Posted by Andrew Cooper 3 months, 2 weeks ago
Xen's use of struct x86_cpu_id is a bit different to Linux's, so we can
simplify the loop termination condition.  Leave a comment explaining Xen's
assumptions.

Switch to Xen style, as we've properly devated from Linux, and switch to the
new vendor/family/model names.

No practical change.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Roger Pau Monné <roger.pau@citrix.com>

v2:
 * New

There is a marginal code generation improvement, because of not needing to
hold as many registers live over the loop termination check.
---
 xen/arch/x86/cpu/common.c | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/xen/arch/x86/cpu/common.c b/xen/arch/x86/cpu/common.c
index f221b9497c61..a659ea7bf604 100644
--- a/xen/arch/x86/cpu/common.c
+++ b/xen/arch/x86/cpu/common.c
@@ -1007,19 +1007,26 @@ void cpu_uninit(unsigned int cpu)
 
 const struct x86_cpu_id *x86_match_cpu(const struct x86_cpu_id table[])
 {
-	const struct x86_cpu_id *m;
-	const struct cpuinfo_x86 *c = &boot_cpu_data;
+    const struct x86_cpu_id *m;
+    const struct cpuinfo_x86 *c = &boot_cpu_data;
 
-	for (m = table; m->vendor | m->family | m->model | m->feature; m++) {
-		if (c->x86_vendor != m->vendor)
-			continue;
-		if (c->x86 != m->family)
-			continue;
-		if (c->x86_model != m->model)
-			continue;
-		if (!cpu_has(c, m->feature))
-			continue;
-		return m;
-	}
-	return NULL;
+    /*
+     * Although derived from Linux originally, Xen has no valid rows where
+     * ->vendor is nonzero, so used this in place of checking all metadata.
+     */
+    for ( m = table; m->vendor; m++ )
+    {
+        if ( c->vendor != m->vendor )
+            continue;
+        if ( c->family != m->family )
+            continue;
+        if ( c->model != m->model )
+            continue;
+        if ( !cpu_has(c, m->feature) )
+            continue;
+
+        return m;
+    }
+
+    return NULL;
 }
-- 
2.39.5


Re: [PATCH v2 1/3] x86/match-cpu: Improvements to x86_match_cpu()
Posted by Jan Beulich 3 months, 1 week ago
On 18.07.2025 16:20, Andrew Cooper wrote:
> --- a/xen/arch/x86/cpu/common.c
> +++ b/xen/arch/x86/cpu/common.c
> @@ -1007,19 +1007,26 @@ void cpu_uninit(unsigned int cpu)
>  
>  const struct x86_cpu_id *x86_match_cpu(const struct x86_cpu_id table[])
>  {
> -	const struct x86_cpu_id *m;
> -	const struct cpuinfo_x86 *c = &boot_cpu_data;
> +    const struct x86_cpu_id *m;
> +    const struct cpuinfo_x86 *c = &boot_cpu_data;
>  
> -	for (m = table; m->vendor | m->family | m->model | m->feature; m++) {
> -		if (c->x86_vendor != m->vendor)
> -			continue;
> -		if (c->x86 != m->family)
> -			continue;
> -		if (c->x86_model != m->model)
> -			continue;
> -		if (!cpu_has(c, m->feature))
> -			continue;
> -		return m;
> -	}
> -	return NULL;
> +    /*
> +     * Although derived from Linux originally, Xen has no valid rows where
> +     * ->vendor is nonzero, so used this in place of checking all metadata.
> +     */

s/nonzero/zero/, I expect? Then
Reviewed-by: Jan Beulich <jbeulich@suse.com>

> +    for ( m = table; m->vendor; m++ )
> +    {
> +        if ( c->vendor != m->vendor )
> +            continue;
> +        if ( c->family != m->family )
> +            continue;
> +        if ( c->model != m->model )
> +            continue;
> +        if ( !cpu_has(c, m->feature) )
> +            continue;
> +
> +        return m;
> +    }
> +
> +    return NULL;
>  }