Re: [PATCH 3/6] KVM: PPC: Book3S HV: Use IDA allocator for LPID allocator

2022-01-24 Thread Fabiano Rosas
Nicholas Piggin  writes:

> This removes the fixed-size lpid_inuse array.
>
> Signed-off-by: Nicholas Piggin 
> ---

Reviewed-by: Fabiano Rosas 

>  arch/powerpc/kvm/powerpc.c | 25 +
>  1 file changed, 13 insertions(+), 12 deletions(-)
>
> diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c
> index 102993462872..c527a5751b46 100644
> --- a/arch/powerpc/kvm/powerpc.c
> +++ b/arch/powerpc/kvm/powerpc.c
> @@ -2453,20 +2453,22 @@ long kvm_arch_vm_ioctl(struct file *filp,
>   return r;
>  }
>
> -static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
> +static DEFINE_IDA(lpid_inuse);
>  static unsigned long nr_lpids;
>
>  long kvmppc_alloc_lpid(void)
>  {
> - long lpid;
> + int lpid;
>
> - do {
> - lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
> - if (lpid >= nr_lpids) {
> + /* The host LPID must always be 0 (allocation starts at 1) */
> + lpid = ida_alloc_range(_inuse, 1, nr_lpids - 1, GFP_KERNEL);
> + if (lpid < 0) {
> + if (lpid == -ENOMEM)
> + pr_err("%s: Out of memory\n", __func__);
> + else
>   pr_err("%s: No LPIDs free\n", __func__);
> - return -ENOMEM;
> - }
> - } while (test_and_set_bit(lpid, lpid_inuse));
> + return -ENOMEM;
> + }
>
>   return lpid;
>  }
> @@ -2474,15 +2476,14 @@ EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
>
>  void kvmppc_free_lpid(long lpid)
>  {
> - clear_bit(lpid, lpid_inuse);
> + ida_free(_inuse, lpid);
>  }
>  EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
>
> +/* nr_lpids_param includes the host LPID */
>  void kvmppc_init_lpid(unsigned long nr_lpids_param)
>  {
> - nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
> - memset(lpid_inuse, 0, sizeof(lpid_inuse));
> - set_bit(0, lpid_inuse); /* The host LPID must always be 0 */
> + nr_lpids = nr_lpids_param;
>  }
>  EXPORT_SYMBOL_GPL(kvmppc_init_lpid);


[PATCH 3/6] KVM: PPC: Book3S HV: Use IDA allocator for LPID allocator

2022-01-23 Thread Nicholas Piggin
This removes the fixed-size lpid_inuse array.

Signed-off-by: Nicholas Piggin 
---
 arch/powerpc/kvm/powerpc.c | 25 +
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c
index 102993462872..c527a5751b46 100644
--- a/arch/powerpc/kvm/powerpc.c
+++ b/arch/powerpc/kvm/powerpc.c
@@ -2453,20 +2453,22 @@ long kvm_arch_vm_ioctl(struct file *filp,
return r;
 }
 
-static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
+static DEFINE_IDA(lpid_inuse);
 static unsigned long nr_lpids;
 
 long kvmppc_alloc_lpid(void)
 {
-   long lpid;
+   int lpid;
 
-   do {
-   lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
-   if (lpid >= nr_lpids) {
+   /* The host LPID must always be 0 (allocation starts at 1) */
+   lpid = ida_alloc_range(_inuse, 1, nr_lpids - 1, GFP_KERNEL);
+   if (lpid < 0) {
+   if (lpid == -ENOMEM)
+   pr_err("%s: Out of memory\n", __func__);
+   else
pr_err("%s: No LPIDs free\n", __func__);
-   return -ENOMEM;
-   }
-   } while (test_and_set_bit(lpid, lpid_inuse));
+   return -ENOMEM;
+   }
 
return lpid;
 }
@@ -2474,15 +2476,14 @@ EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
 
 void kvmppc_free_lpid(long lpid)
 {
-   clear_bit(lpid, lpid_inuse);
+   ida_free(_inuse, lpid);
 }
 EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
 
+/* nr_lpids_param includes the host LPID */
 void kvmppc_init_lpid(unsigned long nr_lpids_param)
 {
-   nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
-   memset(lpid_inuse, 0, sizeof(lpid_inuse));
-   set_bit(0, lpid_inuse); /* The host LPID must always be 0 */
+   nr_lpids = nr_lpids_param;
 }
 EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
 
-- 
2.23.0