Re: [PATCH v2 10/16] x86/resctrl: Allocate per-package structures for known events

From: Luck, Tony
Date: Mon Mar 31 2025 - 20:22:20 EST


On Mon, Mar 31, 2025 at 03:23:53PM -0700, Luck, Tony wrote:
> On Mon, Mar 31, 2025 at 09:21:49AM -0700, Reinette Chatre wrote:
> > include/linux/cleanup.h has this to say about mixing goto and free helpers:
> > "convert all resources that need a "goto" cleanup to scope-based cleanup, or convert
> > none of them"
>
> Seems an awkward restriction for this case. "pkg" is a pointer to
> a dynamic array, and each of the elements of the array might have
> been initialized by another allocation. "pkg" is under control of
> the __free() cleanup function.
>
> Maybe I could define a custom cleanup (syntax of multi-statement
> action to be figured out):
>
> DEFINE_FREE(pkg_free, struct pkg_info *,
> if (_T)
> for (int i = 0; i < num_pkgs; i++)
> kfree(_T[i].regions);
> kfree(_T)
> )
>
> struct pkg_info *pkg __free(pkg_free) = NULL;

Tried this out and it works (without any odd syntax for the multi-line
action (though I did miss that num_pkgs was a local variable, thankfully
simply initialized from topology_max_packages().


DEFINE_FREE(free_pkg_info, struct pkg_info *, \
if (_T) \
for (int i = 0; i < topology_max_packages(); i++) \
kfree(_T[i].regions); \
kfree(_T))

...

struct pkg_info *pkg __free(free_pkg_info) = NULL;


-Tony