[PATCH v3 2/3] cpu-common.c: export cpu_get_free_index to be reused later

Harsh Prateek Bora posted 3 patches 6 months ago
There is a newer version of this series
[PATCH v3 2/3] cpu-common.c: export cpu_get_free_index to be reused later
Posted by Harsh Prateek Bora 6 months ago
This helper provides an easy way to identify the next available free cpu
index which can be used for vcpu creation. Until now, this is being
called at a very later stage and there is a need to be able to call it
earlier (for now, with ppc64) hence the need to export.

Suggested-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Harsh Prateek Bora <harshpb@linux.ibm.com>
---
 include/exec/cpu-common.h | 2 ++
 cpu-common.c              | 7 ++++---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h
index 6d5318895a..0386f1ab29 100644
--- a/include/exec/cpu-common.h
+++ b/include/exec/cpu-common.h
@@ -29,6 +29,8 @@ void cpu_list_lock(void);
 void cpu_list_unlock(void);
 unsigned int cpu_list_generation_id_get(void);
 
+int cpu_get_free_index(void);
+
 void tcg_iommu_init_notifier_list(CPUState *cpu);
 void tcg_iommu_free_notifier_list(CPUState *cpu);
 
diff --git a/cpu-common.c b/cpu-common.c
index ce78273af5..82bd1b432d 100644
--- a/cpu-common.c
+++ b/cpu-common.c
@@ -57,14 +57,12 @@ void cpu_list_unlock(void)
     qemu_mutex_unlock(&qemu_cpu_list_lock);
 }
 
-static bool cpu_index_auto_assigned;
 
-static int cpu_get_free_index(void)
+int cpu_get_free_index(void)
 {
     CPUState *some_cpu;
     int max_cpu_index = 0;
 
-    cpu_index_auto_assigned = true;
     CPU_FOREACH(some_cpu) {
         if (some_cpu->cpu_index >= max_cpu_index) {
             max_cpu_index = some_cpu->cpu_index + 1;
@@ -83,8 +81,11 @@ unsigned int cpu_list_generation_id_get(void)
 
 void cpu_list_add(CPUState *cpu)
 {
+    static bool cpu_index_auto_assigned;
+
     QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
     if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) {
+        cpu_index_auto_assigned = true;
         cpu->cpu_index = cpu_get_free_index();
         assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
     } else {
-- 
2.39.3
Re: [PATCH v3 2/3] cpu-common.c: export cpu_get_free_index to be reused later
Posted by Nicholas Piggin 5 months, 4 weeks ago
On Thu May 23, 2024 at 5:26 PM AEST, Harsh Prateek Bora wrote:
> This helper provides an easy way to identify the next available free cpu
> index which can be used for vcpu creation. Until now, this is being
> called at a very later stage and there is a need to be able to call it
> earlier (for now, with ppc64) hence the need to export.
>

Reviewed-by: Nicholas Piggin <npiggin@gmail.com>

> Suggested-by: Nicholas Piggin <npiggin@gmail.com>
> Signed-off-by: Harsh Prateek Bora <harshpb@linux.ibm.com>
> ---
>  include/exec/cpu-common.h | 2 ++
>  cpu-common.c              | 7 ++++---
>  2 files changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h
> index 6d5318895a..0386f1ab29 100644
> --- a/include/exec/cpu-common.h
> +++ b/include/exec/cpu-common.h
> @@ -29,6 +29,8 @@ void cpu_list_lock(void);
>  void cpu_list_unlock(void);
>  unsigned int cpu_list_generation_id_get(void);
>  
> +int cpu_get_free_index(void);
> +
>  void tcg_iommu_init_notifier_list(CPUState *cpu);
>  void tcg_iommu_free_notifier_list(CPUState *cpu);
>  
> diff --git a/cpu-common.c b/cpu-common.c
> index ce78273af5..82bd1b432d 100644
> --- a/cpu-common.c
> +++ b/cpu-common.c
> @@ -57,14 +57,12 @@ void cpu_list_unlock(void)
>      qemu_mutex_unlock(&qemu_cpu_list_lock);
>  }
>  
> -static bool cpu_index_auto_assigned;
>  
> -static int cpu_get_free_index(void)
> +int cpu_get_free_index(void)
>  {
>      CPUState *some_cpu;
>      int max_cpu_index = 0;
>  
> -    cpu_index_auto_assigned = true;
>      CPU_FOREACH(some_cpu) {
>          if (some_cpu->cpu_index >= max_cpu_index) {
>              max_cpu_index = some_cpu->cpu_index + 1;
> @@ -83,8 +81,11 @@ unsigned int cpu_list_generation_id_get(void)
>  
>  void cpu_list_add(CPUState *cpu)
>  {
> +    static bool cpu_index_auto_assigned;
> +
>      QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
>      if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) {
> +        cpu_index_auto_assigned = true;
>          cpu->cpu_index = cpu_get_free_index();
>          assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
>      } else {