[PATCH v6 04/21] x86/resctrl: Group struct rdt_hw_domain cleanup

James Morse posted 21 patches 3 years, 7 months ago
[PATCH v6 04/21] x86/resctrl: Group struct rdt_hw_domain cleanup
Posted by James Morse 3 years, 7 months ago
domain_add_cpu() and domain_remove_cpu() need to kfree() the child
arrays that were allocated by domain_setup_ctrlval().

As this memory is moved around, and new arrays are created, adjusting
the error handling cleanup code becomes noisier.

To simplify this, move all the kfree() calls into a domain_free() helper.
This depends on struct rdt_hw_domain being kzalloc()d, allowing it to
unconditionally kfree() all the child arrays.

Reviewed-by: Jamie Iles <quic_jiles@quicinc.com>
Tested-by: Xin Hao <xhao@linux.alibaba.com>
Reviewed-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
Tested-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
Tested-by: Cristian Marussi <cristian.marussi@arm.com>
Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
Signed-off-by: James Morse <james.morse@arm.com>
---
Changes since v2:
 * Made domain_free() static.

Changes since v1:
 * This patch is new
---
 arch/x86/kernel/cpu/resctrl/core.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
index 25f30148478b..e37889f7a1a5 100644
--- a/arch/x86/kernel/cpu/resctrl/core.c
+++ b/arch/x86/kernel/cpu/resctrl/core.c
@@ -414,6 +414,13 @@ void setup_default_ctrlval(struct rdt_resource *r, u32 *dc, u32 *dm)
 	}
 }
 
+static void domain_free(struct rdt_hw_domain *hw_dom)
+{
+	kfree(hw_dom->ctrl_val);
+	kfree(hw_dom->mbps_val);
+	kfree(hw_dom);
+}
+
 static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
 {
 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
@@ -488,7 +495,7 @@ static void domain_add_cpu(int cpu, struct rdt_resource *r)
 	rdt_domain_reconfigure_cdp(r);
 
 	if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
-		kfree(hw_dom);
+		domain_free(hw_dom);
 		return;
 	}
 
@@ -497,9 +504,7 @@ static void domain_add_cpu(int cpu, struct rdt_resource *r)
 	err = resctrl_online_domain(r, d);
 	if (err) {
 		list_del(&d->list);
-		kfree(hw_dom->ctrl_val);
-		kfree(hw_dom->mbps_val);
-		kfree(hw_dom);
+		domain_free(hw_dom);
 	}
 }
 
@@ -547,12 +552,10 @@ static void domain_remove_cpu(int cpu, struct rdt_resource *r)
 		if (d->plr)
 			d->plr->d = NULL;
 
-		kfree(hw_dom->ctrl_val);
-		kfree(hw_dom->mbps_val);
 		bitmap_free(d->rmid_busy_llc);
 		kfree(d->mbm_total);
 		kfree(d->mbm_local);
-		kfree(hw_dom);
+		domain_free(hw_dom);
 		return;
 	}
 
-- 
2.30.2
Re: [PATCH v6 04/21] x86/resctrl: Group struct rdt_hw_domain cleanup
Posted by haoxin 3 years, 6 months ago
在 2022/9/2 下午11:48, James Morse 写道:
> domain_add_cpu() and domain_remove_cpu() need to kfree() the child
> arrays that were allocated by domain_setup_ctrlval().
>
> As this memory is moved around, and new arrays are created, adjusting
> the error handling cleanup code becomes noisier.
>
> To simplify this, move all the kfree() calls into a domain_free() helper.
> This depends on struct rdt_hw_domain being kzalloc()d, allowing it to
> unconditionally kfree() all the child arrays.
>
> Reviewed-by: Jamie Iles <quic_jiles@quicinc.com>
> Tested-by: Xin Hao <xhao@linux.alibaba.com>
> Reviewed-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
> Tested-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
> Tested-by: Cristian Marussi <cristian.marussi@arm.com>
> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
> Signed-off-by: James Morse <james.morse@arm.com>
> ---
> Changes since v2:
>   * Made domain_free() static.
>
> Changes since v1:
>   * This patch is new
> ---
>   arch/x86/kernel/cpu/resctrl/core.c | 17 ++++++++++-------
>   1 file changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
> index 25f30148478b..e37889f7a1a5 100644
> --- a/arch/x86/kernel/cpu/resctrl/core.c
> +++ b/arch/x86/kernel/cpu/resctrl/core.c
> @@ -414,6 +414,13 @@ void setup_default_ctrlval(struct rdt_resource *r, u32 *dc, u32 *dm)
>   	}
>   }
>   
> +static void domain_free(struct rdt_hw_domain *hw_dom)
add inline ?
> +{
> +	kfree(hw_dom->ctrl_val);
> +	kfree(hw_dom->mbps_val);
> +	kfree(hw_dom);
> +}
> +
>   static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
>   {
>   	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
> @@ -488,7 +495,7 @@ static void domain_add_cpu(int cpu, struct rdt_resource *r)
>   	rdt_domain_reconfigure_cdp(r);
>   
>   	if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
> -		kfree(hw_dom);
> +		domain_free(hw_dom);
>   		return;
>   	}
>   
> @@ -497,9 +504,7 @@ static void domain_add_cpu(int cpu, struct rdt_resource *r)
>   	err = resctrl_online_domain(r, d);
>   	if (err) {
>   		list_del(&d->list);
> -		kfree(hw_dom->ctrl_val);
> -		kfree(hw_dom->mbps_val);
> -		kfree(hw_dom);
> +		domain_free(hw_dom);
>   	}
>   }
>   
> @@ -547,12 +552,10 @@ static void domain_remove_cpu(int cpu, struct rdt_resource *r)
>   		if (d->plr)
>   			d->plr->d = NULL;
>   
> -		kfree(hw_dom->ctrl_val);
> -		kfree(hw_dom->mbps_val);
>   		bitmap_free(d->rmid_busy_llc);
>   		kfree(d->mbm_total);
>   		kfree(d->mbm_local);
> -		kfree(hw_dom);
> +		domain_free(hw_dom);
>   		return;
>   	}
>   
Re: [PATCH v6 04/21] x86/resctrl: Group struct rdt_hw_domain cleanup
Posted by James Morse 3 years, 6 months ago
Hi Hao Xin,

On 07/09/2022 07:28, haoxin wrote:
> 
> 在 2022/9/2 下午11:48, James Morse 写道:
>> domain_add_cpu() and domain_remove_cpu() need to kfree() the child
>> arrays that were allocated by domain_setup_ctrlval().
>>
>> As this memory is moved around, and new arrays are created, adjusting
>> the error handling cleanup code becomes noisier.
>>
>> To simplify this, move all the kfree() calls into a domain_free() helper.
>> This depends on struct rdt_hw_domain being kzalloc()d, allowing it to
>> unconditionally kfree() all the child arrays.

>> diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
>> index 25f30148478b..e37889f7a1a5 100644
>> --- a/arch/x86/kernel/cpu/resctrl/core.c
>> +++ b/arch/x86/kernel/cpu/resctrl/core.c
>> @@ -414,6 +414,13 @@ void setup_default_ctrlval(struct rdt_resource *r, u32 *dc, u32 *dm)
>>       }
>>   }
>>   +static void domain_free(struct rdt_hw_domain *hw_dom)

> add inline ?

It's best to let the compiler decide this. As this is in a C file, and is declared static,
the compiler is free to duplicate and inline this function as it sees fit. The inline
keyword would only be needed if this were in a header file.

Looking at the built object file - the compiler chose not to duplicate this into the two
callers, presumably because of the size of the function.

Unless its relied on for correctness, or is a performance sensitive path, its best to let
the compiler make its own decision here.


Thanks,

James


>> +{
>> +    kfree(hw_dom->ctrl_val);
>> +    kfree(hw_dom->mbps_val);
>> +    kfree(hw_dom);
>> +}
>> +
>>   static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
>>   {
>>       struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
[tip: x86/cache] x86/resctrl: Group struct rdt_hw_domain cleanup
Posted by tip-bot2 for James Morse 3 years, 6 months ago
The following commit has been merged into the x86/cache branch of tip:

Commit-ID:     7add3af4178d9e25afc8d990a7d1000ccb22b6a0
Gitweb:        https://git.kernel.org/tip/7add3af4178d9e25afc8d990a7d1000ccb22b6a0
Author:        James Morse <james.morse@arm.com>
AuthorDate:    Fri, 02 Sep 2022 15:48:12 
Committer:     Borislav Petkov <bp@suse.de>
CommitterDate: Thu, 22 Sep 2022 15:27:15 +02:00

x86/resctrl: Group struct rdt_hw_domain cleanup

domain_add_cpu() and domain_remove_cpu() need to kfree() the child
arrays that were allocated by domain_setup_ctrlval().

As this memory is moved around, and new arrays are created, adjusting
the error handling cleanup code becomes noisier.

To simplify this, move all the kfree() calls into a domain_free() helper.
This depends on struct rdt_hw_domain being kzalloc()d, allowing it to
unconditionally kfree() all the child arrays.

Signed-off-by: James Morse <james.morse@arm.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Reviewed-by: Jamie Iles <quic_jiles@quicinc.com>
Reviewed-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
Tested-by: Xin Hao <xhao@linux.alibaba.com>
Tested-by: Shaopeng Tan <tan.shaopeng@fujitsu.com>
Tested-by: Cristian Marussi <cristian.marussi@arm.com>
Link: https://lore.kernel.org/r/20220902154829.30399-5-james.morse@arm.com
---
 arch/x86/kernel/cpu/resctrl/core.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
index 25f3014..e37889f 100644
--- a/arch/x86/kernel/cpu/resctrl/core.c
+++ b/arch/x86/kernel/cpu/resctrl/core.c
@@ -414,6 +414,13 @@ void setup_default_ctrlval(struct rdt_resource *r, u32 *dc, u32 *dm)
 	}
 }
 
+static void domain_free(struct rdt_hw_domain *hw_dom)
+{
+	kfree(hw_dom->ctrl_val);
+	kfree(hw_dom->mbps_val);
+	kfree(hw_dom);
+}
+
 static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
 {
 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
@@ -488,7 +495,7 @@ static void domain_add_cpu(int cpu, struct rdt_resource *r)
 	rdt_domain_reconfigure_cdp(r);
 
 	if (r->alloc_capable && domain_setup_ctrlval(r, d)) {
-		kfree(hw_dom);
+		domain_free(hw_dom);
 		return;
 	}
 
@@ -497,9 +504,7 @@ static void domain_add_cpu(int cpu, struct rdt_resource *r)
 	err = resctrl_online_domain(r, d);
 	if (err) {
 		list_del(&d->list);
-		kfree(hw_dom->ctrl_val);
-		kfree(hw_dom->mbps_val);
-		kfree(hw_dom);
+		domain_free(hw_dom);
 	}
 }
 
@@ -547,12 +552,10 @@ static void domain_remove_cpu(int cpu, struct rdt_resource *r)
 		if (d->plr)
 			d->plr->d = NULL;
 
-		kfree(hw_dom->ctrl_val);
-		kfree(hw_dom->mbps_val);
 		bitmap_free(d->rmid_busy_llc);
 		kfree(d->mbm_total);
 		kfree(d->mbm_local);
-		kfree(hw_dom);
+		domain_free(hw_dom);
 		return;
 	}