[PATCH 2/3] x86/resctrl: Display the number of available RMIDs

From: Haifeng Xu
Date: Tue Jan 23 2024 - 04:22:18 EST


Now we can know the total number of RMIDs from:

/sys/fs/resctrl/info/L3_MON/num_rmids

But for users, they don't know the number of available RMIDs. Especially
when llc_occupancy is enabled, though the monitor group is removed, its
rmid may not be freed.

Users may confused that though the number of control groups and monitor
groups is less than num_rmids, they still fail to create a new monitor
group, so add a RFTYPE_TOP_INFO file 'free_rmids' that tells users how
many free rmids are left.

Signed-off-by: Haifeng Xu <haifeng.xu@xxxxxxxxxx>
---
arch/x86/kernel/cpu/resctrl/internal.h | 1 +
arch/x86/kernel/cpu/resctrl/monitor.c | 16 ++++++++++++++--
arch/x86/kernel/cpu/resctrl/rdtgroup.c | 16 ++++++++++++++++
3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
index a4f1aa15f0a2..4b0392bd3a65 100644
--- a/arch/x86/kernel/cpu/resctrl/internal.h
+++ b/arch/x86/kernel/cpu/resctrl/internal.h
@@ -117,6 +117,7 @@ struct rmid_read {
extern bool rdt_alloc_capable;
extern bool rdt_mon_capable;
extern unsigned int rdt_mon_features;
+extern unsigned int free_rmids;
extern struct list_head resctrl_schema_all;

enum rdt_group_type {
diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c
index f136ac046851..edf8b6e1017c 100644
--- a/arch/x86/kernel/cpu/resctrl/monitor.c
+++ b/arch/x86/kernel/cpu/resctrl/monitor.c
@@ -51,6 +51,11 @@ static unsigned int rmid_limbo_count;
*/
static struct rmid_entry *rmid_ptrs;

+/*
+ * This is the number of available RMIDs.
+ */
+unsigned int free_rmids;
+
/*
* Global boolean for rdt_monitor which is true if any
* resource monitoring is enabled.
@@ -299,6 +304,7 @@ void __check_limbo(struct rdt_domain *d, bool force_free)
if (!--entry->busy) {
rmid_limbo_count--;
list_add_tail(&entry->list, &rmid_free_lru);
+ free_rmids++;
}
}
crmid = nrmid + 1;
@@ -327,6 +333,7 @@ int alloc_rmid(void)
entry = list_first_entry(&rmid_free_lru,
struct rmid_entry, list);
list_del(&entry->list);
+ free_rmids--;

return entry->rmid;
}
@@ -362,8 +369,10 @@ static void add_rmid_to_limbo(struct rmid_entry *entry)

if (entry->busy)
rmid_limbo_count++;
- else
+ else {
list_add_tail(&entry->list, &rmid_free_lru);
+ free_rmids++;
+ }
}

void free_rmid(u32 rmid)
@@ -379,8 +388,10 @@ void free_rmid(u32 rmid)

if (is_llc_occupancy_enabled())
add_rmid_to_limbo(entry);
- else
+ else {
list_add_tail(&entry->list, &rmid_free_lru);
+ free_rmids++;
+ }
}

static struct mbm_state *get_mbm_state(struct rdt_domain *d, u32 rmid,
@@ -737,6 +748,7 @@ static int dom_data_init(struct rdt_resource *r)
*/
entry = __rmid_entry(0);
list_del(&entry->list);
+ free_rmids = nr_rmids - 1;

return 0;
}
diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
index 577d870ac45f..1eac0ca97b81 100644
--- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
+++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
@@ -925,6 +925,15 @@ static int rdt_free_closids_show(struct kernfs_open_file *of,
return 0;
}

+static int rdt_free_rmids_show(struct kernfs_open_file *of,
+ struct seq_file *seq, void *v)
+{
+ mutex_lock(&rdtgroup_mutex);
+ seq_printf(seq, "%d\n", free_rmids);
+ mutex_unlock(&rdtgroup_mutex);
+ return 0;
+}
+
static int rdt_num_closids_show(struct kernfs_open_file *of,
struct seq_file *seq, void *v)
{
@@ -1775,6 +1784,13 @@ static struct rftype res_common_files[] = {
.seq_show = rdt_free_closids_show,
.fflags = RFTYPE_TOP_INFO,
},
+ {
+ .name = "free_rmids",
+ .mode = 0444,
+ .kf_ops = &rdtgroup_kf_single_ops,
+ .seq_show = rdt_free_rmids_show,
+ .fflags = RFTYPE_TOP_INFO,
+ },
{
.name = "num_closids",
.mode = 0444,
--
2.25.1