Hardware provides a set of counters when mbm_cntr_assignable feature is supported. These counters are used for assigning the events in resctrl group when the feature is enabled. Introduce mbm_cntrs_free_map bitmap to track available and free counters and set of routines to allocate and free the counters. Signed-off-by: Babu Moger <babu.moger@xxxxxxx> --- v6: Removed the variable mbm_cntrs_free_map_len. This is not required. Removed the call mbm_cntrs_init() in arch code. This needs to be done at higher level. Used DECLARE_BITMAP to initialize mbm_cntrs_free_map. Moved all the counter interfaces mbm_cntr_alloc() and mbm_cntr_free() in here as part of separating arch and fs bits. v5: Updated the comments and commit log. Few renames num_cntrs_free_map -> mbm_cntrs_free_map num_cntrs_init -> mbm_cntrs_init Added initialization in rdt_get_tree because the default ABMC enablement happens during the init. v4: Changed the name to num_cntrs where applicable. Used bitmap apis. Added more comments for the globals. v3: Changed the bitmap name to assign_cntrs_free_map. Removed abmc from the name. v2: Changed the bitmap name to assignable_counter_free_map from abmc_counter_free_map. --- arch/x86/kernel/cpu/resctrl/internal.h | 2 ++ arch/x86/kernel/cpu/resctrl/rdtgroup.c | 33 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h index 154983a67646..6263362496a3 100644 --- a/arch/x86/kernel/cpu/resctrl/internal.h +++ b/arch/x86/kernel/cpu/resctrl/internal.h @@ -662,6 +662,8 @@ void __check_limbo(struct rdt_mon_domain *d, bool force_free); void rdt_domain_reconfigure_cdp(struct rdt_resource *r); void __init resctrl_file_fflags_init(const char *config, unsigned long fflags); +int mbm_cntr_alloc(struct rdt_resource *r); +void mbm_cntr_free(u32 cntr_id); void rdt_staged_configs_clear(void); bool closid_allocated(unsigned int closid); int resctrl_find_cleanest_closid(void); diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c index ab4fab3b7cf1..c818965e36c9 100644 --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c @@ -185,6 +185,37 @@ bool closid_allocated(unsigned int closid) return !test_bit(closid, &closid_free_map); } +/* + * Counter bitmap for tracking the available counters. + * ABMC feature provides set of hardware counters for enabling events. + * Each event takes one hardware counter. Kernel needs to keep track + * of number of available counters. + */ +static DECLARE_BITMAP(mbm_cntrs_free_map, 64); + +static void mbm_cntrs_init(struct rdt_resource *r) +{ + bitmap_fill(mbm_cntrs_free_map, r->mon.num_mbm_cntrs); +} + +int mbm_cntr_alloc(struct rdt_resource *r) +{ + int cntr_id; + + cntr_id = find_first_bit(mbm_cntrs_free_map, r->mon.num_mbm_cntrs); + if (cntr_id >= r->mon.num_mbm_cntrs) + return -ENOSPC; + + __clear_bit(cntr_id, mbm_cntrs_free_map); + + return cntr_id; +} + +void mbm_cntr_free(u32 cntr_id) +{ + __set_bit(cntr_id, mbm_cntrs_free_map); +} + /** * rdtgroup_mode_by_closid - Return mode of resource group with closid * @closid: closid if the resource group @@ -2748,6 +2779,8 @@ static int rdt_get_tree(struct fs_context *fc) closid_init(); + mbm_cntrs_init(&rdt_resources_all[RDT_RESOURCE_L3].r_resctrl); + if (resctrl_arch_mon_capable()) flags |= RFTYPE_MON; -- 2.34.1