[PATCH v7 09/24] x86/resctrl: Use __set_bit()/__clear_bit() instead of open coding

James Morse posted 24 patches 2 years, 1 month ago
There is a newer version of this series
[PATCH v7 09/24] x86/resctrl: Use __set_bit()/__clear_bit() instead of open coding
Posted by James Morse 2 years, 1 month ago
The resctrl CLOSID allocator uses a single 32bit word to track which
CLOSID are free. The setting and clearing of bits is open coded.

A subsequent patch adds closid_allocated(), which adds more open
coded bitmaps operations. These will eventually need changing to use
the bitops helpers so that a CLOSID bitmap of the correct size can be
allocated dynamically.

Convert the existing open coded bit manipulations of closid_free_map
to use __set_bit() and friends. These don't need to be atomic as this
list is protected by the mutex.

Tested-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
Tested-by: Peter Newman <peternewman@google.com>
Reviewed-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
Signed-off-by: James Morse <james.morse@arm.com>

---
Changes since v6:
 * Use the __ inatomic helpers and add lockdep_assert_held() annotations to
   document how this is safe.
 * Fixed a resctrl_closid_is_free()/closid_allocated() rename in the commit
   message.
 * Use RESCTRL_RESERVED_CLOSID to improve readability.
---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
index 9864cb49d58c..f6051a3e7262 100644
--- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
+++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
@@ -111,7 +111,7 @@ void rdt_staged_configs_clear(void)
  * - Our choices on how to configure each resource become progressively more
  *   limited as the number of resources grows.
  */
-static int closid_free_map;
+static unsigned long closid_free_map;
 static int closid_free_map_len;
 
 int closids_supported(void)
@@ -131,7 +131,7 @@ static void closid_init(void)
 	closid_free_map = BIT_MASK(rdt_min_closid) - 1;
 
 	/* CLOSID 0 is always reserved for the default group */
-	closid_free_map &= ~1;
+	__clear_bit(RESCTRL_RESERVED_CLOSID, &closid_free_map);
 	closid_free_map_len = rdt_min_closid;
 }
 
@@ -139,17 +139,21 @@ static int closid_alloc(void)
 {
 	u32 closid = ffs(closid_free_map);
 
+	lockdep_assert_held(&rdtgroup_mutex);
+
 	if (closid == 0)
 		return -ENOSPC;
 	closid--;
-	closid_free_map &= ~(1 << closid);
+	__clear_bit(closid, &closid_free_map);
 
 	return closid;
 }
 
 void closid_free(int closid)
 {
-	closid_free_map |= 1 << closid;
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	__set_bit(closid, &closid_free_map);
 }
 
 /**
@@ -161,7 +165,9 @@ void closid_free(int closid)
  */
 static bool closid_allocated(unsigned int closid)
 {
-	return (closid_free_map & (1 << closid)) == 0;
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	return !test_bit(closid, &closid_free_map);
 }
 
 /**
-- 
2.39.2
Re: [PATCH v7 09/24] x86/resctrl: Use __set_bit()/__clear_bit() instead of open coding
Posted by Moger, Babu 2 years, 1 month ago
Hi James,

On 10/25/23 13:03, James Morse wrote:
> The resctrl CLOSID allocator uses a single 32bit word to track which
> CLOSID are free. The setting and clearing of bits is open coded.
> 
> A subsequent patch adds closid_allocated(), which adds more open
> coded bitmaps operations. These will eventually need changing to use
> the bitops helpers so that a CLOSID bitmap of the correct size can be
> allocated dynamically.
> 
> Convert the existing open coded bit manipulations of closid_free_map
> to use __set_bit() and friends. These don't need to be atomic as this
> list is protected by the mutex.
> 
> Tested-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
> Tested-by: Peter Newman <peternewman@google.com>
> Reviewed-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
> Signed-off-by: James Morse <james.morse@arm.com>

Reviewed-by: Babu Moger <babu.moger@amd.com>

> 
> ---
> Changes since v6:
>  * Use the __ inatomic helpers and add lockdep_assert_held() annotations to
>    document how this is safe.
>  * Fixed a resctrl_closid_is_free()/closid_allocated() rename in the commit
>    message.
>  * Use RESCTRL_RESERVED_CLOSID to improve readability.
> ---
>  arch/x86/kernel/cpu/resctrl/rdtgroup.c | 16 +++++++++++-----
>  1 file changed, 11 insertions(+), 5 deletions(-)
> 
> diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> index 9864cb49d58c..f6051a3e7262 100644
> --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> @@ -111,7 +111,7 @@ void rdt_staged_configs_clear(void)
>   * - Our choices on how to configure each resource become progressively more
>   *   limited as the number of resources grows.
>   */
> -static int closid_free_map;
> +static unsigned long closid_free_map;
>  static int closid_free_map_len;
>  
>  int closids_supported(void)
> @@ -131,7 +131,7 @@ static void closid_init(void)
>  	closid_free_map = BIT_MASK(rdt_min_closid) - 1;
>  
>  	/* CLOSID 0 is always reserved for the default group */
> -	closid_free_map &= ~1;
> +	__clear_bit(RESCTRL_RESERVED_CLOSID, &closid_free_map);
>  	closid_free_map_len = rdt_min_closid;
>  }
>  
> @@ -139,17 +139,21 @@ static int closid_alloc(void)
>  {
>  	u32 closid = ffs(closid_free_map);
>  
> +	lockdep_assert_held(&rdtgroup_mutex);
> +
>  	if (closid == 0)
>  		return -ENOSPC;
>  	closid--;
> -	closid_free_map &= ~(1 << closid);
> +	__clear_bit(closid, &closid_free_map);
>  
>  	return closid;
>  }
>  
>  void closid_free(int closid)
>  {
> -	closid_free_map |= 1 << closid;
> +	lockdep_assert_held(&rdtgroup_mutex);
> +
> +	__set_bit(closid, &closid_free_map);
>  }
>  
>  /**
> @@ -161,7 +165,9 @@ void closid_free(int closid)
>   */
>  static bool closid_allocated(unsigned int closid)
>  {
> -	return (closid_free_map & (1 << closid)) == 0;
> +	lockdep_assert_held(&rdtgroup_mutex);
> +
> +	return !test_bit(closid, &closid_free_map);
>  }
>  
>  /**

-- 
Thanks
Babu Moger
Re: [PATCH v7 09/24] x86/resctrl: Use __set_bit()/__clear_bit() instead of open coding
Posted by James Morse 2 years ago
Hi Babu,

On 09/11/2023 20:38, Moger, Babu wrote:
> On 10/25/23 13:03, James Morse wrote:
>> The resctrl CLOSID allocator uses a single 32bit word to track which
>> CLOSID are free. The setting and clearing of bits is open coded.
>>
>> A subsequent patch adds closid_allocated(), which adds more open
>> coded bitmaps operations. These will eventually need changing to use
>> the bitops helpers so that a CLOSID bitmap of the correct size can be
>> allocated dynamically.
>>
>> Convert the existing open coded bit manipulations of closid_free_map
>> to use __set_bit() and friends. These don't need to be atomic as this
>> list is protected by the mutex.

> Reviewed-by: Babu Moger <babu.moger@amd.com>


Thanks!

James
Re: [PATCH v7 09/24] x86/resctrl: Use __set_bit()/__clear_bit() instead of open coding
Posted by Reinette Chatre 2 years, 1 month ago
Hi James,

On 10/25/2023 11:03 AM, James Morse wrote:
> The resctrl CLOSID allocator uses a single 32bit word to track which
> CLOSID are free. The setting and clearing of bits is open coded.
> 
> A subsequent patch adds closid_allocated(), which adds more open

(Note use of "A subsequent patch ")

> coded bitmaps operations. These will eventually need changing to use
> the bitops helpers so that a CLOSID bitmap of the correct size can be
> allocated dynamically.
> 
> Convert the existing open coded bit manipulations of closid_free_map
> to use __set_bit() and friends. These don't need to be atomic as this
> list is protected by the mutex.
> 
> Tested-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
> Tested-by: Peter Newman <peternewman@google.com>
> Reviewed-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
> Signed-off-by: James Morse <james.morse@arm.com>
> 
> ---
> Changes since v6:
>  * Use the __ inatomic helpers and add lockdep_assert_held() annotations to
>    document how this is safe.
>  * Fixed a resctrl_closid_is_free()/closid_allocated() rename in the commit
>    message.
>  * Use RESCTRL_RESERVED_CLOSID to improve readability.
> ---
>  arch/x86/kernel/cpu/resctrl/rdtgroup.c | 16 +++++++++++-----
>  1 file changed, 11 insertions(+), 5 deletions(-)
> 
> diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> index 9864cb49d58c..f6051a3e7262 100644
> --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> @@ -111,7 +111,7 @@ void rdt_staged_configs_clear(void)
>   * - Our choices on how to configure each resource become progressively more
>   *   limited as the number of resources grows.
>   */
> -static int closid_free_map;
> +static unsigned long closid_free_map;
>  static int closid_free_map_len;
>  
>  int closids_supported(void)
> @@ -131,7 +131,7 @@ static void closid_init(void)
>  	closid_free_map = BIT_MASK(rdt_min_closid) - 1;
>  
>  	/* CLOSID 0 is always reserved for the default group */

Seems appropriate for the comment to be updated to the new define also.

With that addressed you can add:
Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>

Reinette
Re: [PATCH v7 09/24] x86/resctrl: Use __set_bit()/__clear_bit() instead of open coding
Posted by James Morse 2 years ago
Hi Reinette,

On 09/11/2023 17:44, Reinette Chatre wrote:
> On 10/25/2023 11:03 AM, James Morse wrote:
>> The resctrl CLOSID allocator uses a single 32bit word to track which
>> CLOSID are free. The setting and clearing of bits is open coded.
>>
>> A subsequent patch adds closid_allocated(), which adds more open
> 
> (Note use of "A subsequent patch ")

Yup, I've dropped this paragraph.


>> coded bitmaps operations. These will eventually need changing to use
>> the bitops helpers so that a CLOSID bitmap of the correct size can be
>> allocated dynamically.
>>
>> Convert the existing open coded bit manipulations of closid_free_map
>> to use __set_bit() and friends. These don't need to be atomic as this
>> list is protected by the mutex.

>> diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
>> index 9864cb49d58c..f6051a3e7262 100644
>> --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
>> +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
>> @@ -131,7 +131,7 @@ static void closid_init(void)
>>  	closid_free_map = BIT_MASK(rdt_min_closid) - 1;
>>  
>>  	/* CLOSID 0 is always reserved for the default group */
> 
> Seems appropriate for the comment to be updated to the new define also.

Sure,

> With that addressed you can add:
> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>

Thanks!

James