[PATCH 1/3] x86/mm: Correct the actual count of available global ASIDs

Hou Wenlong posted 3 patches 8 months, 3 weeks ago
[PATCH 1/3] x86/mm: Correct the actual count of available global ASIDs
Posted by Hou Wenlong 8 months, 3 weeks ago
The available ASID range for global ASID allocation is
[TLB_NR_DYN_ASIDS, MAX_ASID_AVAILABLE-1], which is a close interval. So
the actual count of available ASIDs for global ASID allocation should be
'(MAX_ASID_AVIALBE-1) - TLB_NR_DYN_ASIDS + 1'.

Fixes: d504d1247e36 ("x86/mm: Add global ASID allocation helper functions")
Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
---
 arch/x86/mm/tlb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
index e459d97ef397..cad4a8eae2d8 100644
--- a/arch/x86/mm/tlb.c
+++ b/arch/x86/mm/tlb.c
@@ -279,7 +279,7 @@ static DEFINE_RAW_SPINLOCK(global_asid_lock);
 static u16 last_global_asid = MAX_ASID_AVAILABLE;
 static DECLARE_BITMAP(global_asid_used, MAX_ASID_AVAILABLE);
 static DECLARE_BITMAP(global_asid_freed, MAX_ASID_AVAILABLE);
-static int global_asid_available = MAX_ASID_AVAILABLE - TLB_NR_DYN_ASIDS - 1;
+static int global_asid_available = MAX_ASID_AVAILABLE - TLB_NR_DYN_ASIDS;
 
 /*
  * When the search for a free ASID in the global ASID space reaches
-- 
2.31.1
Re: [PATCH 1/3] x86/mm: Correct the actual count of available global ASIDs
Posted by Rik van Riel 8 months, 3 weeks ago
On Sat, 2025-03-29 at 21:05 +0800, Hou Wenlong wrote:
> 
> +++ b/arch/x86/mm/tlb.c
> @@ -279,7 +279,7 @@ static DEFINE_RAW_SPINLOCK(global_asid_lock);
>  static u16 last_global_asid = MAX_ASID_AVAILABLE;
>  static DECLARE_BITMAP(global_asid_used, MAX_ASID_AVAILABLE);
>  static DECLARE_BITMAP(global_asid_freed, MAX_ASID_AVAILABLE);
> -static int global_asid_available = MAX_ASID_AVAILABLE -
> TLB_NR_DYN_ASIDS - 1;
> +static int global_asid_available = MAX_ASID_AVAILABLE -
> TLB_NR_DYN_ASIDS;

Unfortunately we are limited by the PCID space.

A process with ASID N will get PCID N+1.

The PCID space has the same size (and maximum value)
as the ASID space.

That means we cannot use the top ASID value.

Alternatively, I suppose we could have ASID and PCID
line up, and always exclude ASID 0 from being used.

That might (maybe) be prettier code, but it isn't what
we have today.


-- 
All Rights Reversed.
Re: [PATCH 1/3] x86/mm: Correct the actual count of available global ASIDs
Posted by Hou Wenlong 8 months, 3 weeks ago
On Sun, Mar 30, 2025 at 01:29:25AM +0800, Rik van Riel wrote:
> On Sat, 2025-03-29 at 21:05 +0800, Hou Wenlong wrote:
> > 
> > +++ b/arch/x86/mm/tlb.c
> > @@ -279,7 +279,7 @@ static DEFINE_RAW_SPINLOCK(global_asid_lock);
> >  static u16 last_global_asid = MAX_ASID_AVAILABLE;
> >  static DECLARE_BITMAP(global_asid_used, MAX_ASID_AVAILABLE);
> >  static DECLARE_BITMAP(global_asid_freed, MAX_ASID_AVAILABLE);
> > -static int global_asid_available = MAX_ASID_AVAILABLE -
> > TLB_NR_DYN_ASIDS - 1;
> > +static int global_asid_available = MAX_ASID_AVAILABLE -
> > TLB_NR_DYN_ASIDS;
> 
> Unfortunately we are limited by the PCID space.
> 
> A process with ASID N will get PCID N+1.
> 
> The PCID space has the same size (and maximum value)
> as the ASID space.
> 
> That means we cannot use the top ASID value.
>
Thank you for your quick reply!

Since the size of the bitmap is 'MAX_ASID_AVAILABLE', the maximum ASID
value that can be allocated from the bitmap is 'MAX_ASID_AVAILABLE-1'.
This matches the comments stating that the valid ASID range for global
ASID allocation is [TLB_NR_DYN_ASIDS, MAX_ASID_AVAILABLE-1]. It is a
close interval so the size should be '(MAX_ASID_AVAILABLE-1)-TLB_NR_DYN_ASIDS+1',
regardless of the value of 'MAX_ASID_AVAILABLE'. Moreover, 'MAX_ASID_AVAILABLE'
has already taken the reserved PCID 0 into account; 'MAX_ASID_AVAILABLE-1' is a
valid ASID, and the associated PCID 'MAX_ASID_AVAILABLE' is also a valid PCID.
Did I miss something?

> Alternatively, I suppose we could have ASID and PCID
> line up, and always exclude ASID 0 from being used.
> 
> That might (maybe) be prettier code, but it isn't what
> we have today.
>
Yes, I agree. Excluding ASID 0 and using PCID as ASID makes the code
clearer in global ASID allocation.

> 
> -- 
> All Rights Reversed.