2022-06-22 17:07:30

by James Morse

[permalink] [raw]
Subject: [PATCH v5 04/21] 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.

Reviewed-by: Jamie Iles <[email protected]>
Tested-by: Xin Hao <[email protected]>
Reviewed-by: Shaopeng Tan <[email protected]>
Tested-by: Shaopeng Tan <[email protected]>
Tested-by: Cristian Marussi <[email protected]>
Signed-off-by: James Morse <[email protected]>
---
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


2022-06-29 08:55:10

by Shaopeng Tan (Fujitsu)

[permalink] [raw]
Subject: RE: [PATCH v5 04/21] x86/resctrl: Group struct rdt_hw_domain cleanup

Hi James,

> 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 <[email protected]>
> Tested-by: Xin Hao <[email protected]>
> Reviewed-by: Shaopeng Tan <[email protected]>
> Tested-by: Shaopeng Tan <[email protected]>
> Tested-by: Cristian Marussi <[email protected]>
> Signed-off-by: James Morse <[email protected]>
> ---
> 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);

domain_free(hw_dom) is executed when fails allocated hw_dom->ctrl_val
by kmalloc_array() in domain_setup_ctrlval(r, d),
but hw_dom->ctrl_val is freed in domain_free(hw_dom).

Also, hw_dom->mbps_val is not allocated at this time,
but it is freed in domain_free(hw_dom).

In addition,I tested this patch series on Intel(R) Xeon(R) Gold 6254 CPU with resctrl selftest.
It is no problem.

Best regards,
Shaopeng
> 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

2022-06-29 11:16:39

by James Morse

[permalink] [raw]
Subject: Re: [PATCH v5 04/21] x86/resctrl: Group struct rdt_hw_domain cleanup

Hi Shaopeng,

On 29/06/2022 09:33, [email protected] wrote:
>> 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) {
>> + 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);

> domain_free(hw_dom) is executed when fails allocated hw_dom->ctrl_val
> by kmalloc_array() in domain_setup_ctrlval(r, d),
> but hw_dom->ctrl_val is freed in domain_free(hw_dom).
>
> Also, hw_dom->mbps_val is not allocated at this time,
> but it is freed in domain_free(hw_dom).

Yes, this is deliberate. These cases end up doing:
| kfree(NULL);
which is harmless. kfree() checks for a NULL argument and does nothing.

The alternative would be to spread the cleanup all over the place, so it only calls
kfree() on something that has been allocated - this would be more complex and easier to
miss something.


> In addition,I tested this patch series on Intel(R) Xeon(R) Gold 6254 CPU with resctrl selftest.
> It is no problem.

Thanks!

James

2022-07-01 06:48:32

by Shaopeng Tan (Fujitsu)

[permalink] [raw]
Subject: RE: [PATCH v5 04/21] x86/resctrl: Group struct rdt_hw_domain cleanup

Hi James

> On 29/06/2022 09:33, [email protected] wrote:
> >> 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) {
> >> + 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);
>
> > domain_free(hw_dom) is executed when fails allocated hw_dom->ctrl_val
> > by kmalloc_array() in domain_setup_ctrlval(r, d), but hw_dom->ctrl_val
> > is freed in domain_free(hw_dom).
> >
> > Also, hw_dom->mbps_val is not allocated at this time, but it is freed
> > in domain_free(hw_dom).
>
> Yes, this is deliberate. These cases end up doing:
> | kfree(NULL);
> which is harmless. kfree() checks for a NULL argument and does nothing.
>
> The alternative would be to spread the cleanup all over the place, so it only calls
> kfree() on something that has been allocated - this would be more complex and
> easier to miss something.

Thank you for explaining. I learned.

Best regards,
Shaopeng