Skip to content

Commit 6eac36b

Browse files
James Morsebp3tk0v
authored andcommitted
x86/resctrl: Allocate the cleanest CLOSID by searching closid_num_dirty_rmid
MPAM's PMG bits extend its PARTID space, meaning the same PMG value can be used for different control groups. This means once a CLOSID is allocated, all its monitoring ids may still be dirty, and held in limbo. Instead of allocating the first free CLOSID, on architectures where CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID is enabled, search closid_num_dirty_rmid[] to find the cleanest CLOSID. The CLOSID found is returned to closid_alloc() for the free list to be updated. Signed-off-by: James Morse <james.morse@arm.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: Shaopeng Tan <tan.shaopeng@fujitsu.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Reviewed-by: Babu Moger <babu.moger@amd.com> Tested-by: Shaopeng Tan <tan.shaopeng@fujitsu.com> Tested-by: Peter Newman <peternewman@google.com> Tested-by: Babu Moger <babu.moger@amd.com> Tested-by: Carl Worth <carl@os.amperecomputing.com> # arm64 Link: https://lore.kernel.org/r/20240213184438.16675-11-james.morse@arm.com Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
1 parent 5d920b6 commit 6eac36b

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

arch/x86/kernel/cpu/resctrl/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,5 +566,7 @@ void rdt_domain_reconfigure_cdp(struct rdt_resource *r);
566566
void __init thread_throttle_mode_init(void);
567567
void __init mbm_config_rftype_init(const char *config);
568568
void rdt_staged_configs_clear(void);
569+
bool closid_allocated(unsigned int closid);
570+
int resctrl_find_cleanest_closid(void);
569571

570572
#endif /* _ASM_X86_RESCTRL_INTERNAL_H */

arch/x86/kernel/cpu/resctrl/monitor.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,51 @@ static struct rmid_entry *resctrl_find_free_rmid(u32 closid)
386386
return ERR_PTR(-ENOSPC);
387387
}
388388

389+
/**
390+
* resctrl_find_cleanest_closid() - Find a CLOSID where all the associated
391+
* RMID are clean, or the CLOSID that has
392+
* the most clean RMID.
393+
*
394+
* MPAM's equivalent of RMID are per-CLOSID, meaning a freshly allocated CLOSID
395+
* may not be able to allocate clean RMID. To avoid this the allocator will
396+
* choose the CLOSID with the most clean RMID.
397+
*
398+
* When the CLOSID and RMID are independent numbers, the first free CLOSID will
399+
* be returned.
400+
*/
401+
int resctrl_find_cleanest_closid(void)
402+
{
403+
u32 cleanest_closid = ~0;
404+
int i = 0;
405+
406+
lockdep_assert_held(&rdtgroup_mutex);
407+
408+
if (!IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID))
409+
return -EIO;
410+
411+
for (i = 0; i < closids_supported(); i++) {
412+
int num_dirty;
413+
414+
if (closid_allocated(i))
415+
continue;
416+
417+
num_dirty = closid_num_dirty_rmid[i];
418+
if (num_dirty == 0)
419+
return i;
420+
421+
if (cleanest_closid == ~0)
422+
cleanest_closid = i;
423+
424+
if (num_dirty < closid_num_dirty_rmid[cleanest_closid])
425+
cleanest_closid = i;
426+
}
427+
428+
if (cleanest_closid == ~0)
429+
return -ENOSPC;
430+
431+
return cleanest_closid;
432+
}
433+
389434
/*
390435
* For MPAM the RMID value is not unique, and has to be considered with
391436
* the CLOSID. The (CLOSID, RMID) pair is allocated on all domains, which

arch/x86/kernel/cpu/resctrl/rdtgroup.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,22 @@ static void closid_init(void)
137137

138138
static int closid_alloc(void)
139139
{
140-
u32 closid = ffs(closid_free_map);
140+
int cleanest_closid;
141+
u32 closid;
141142

142143
lockdep_assert_held(&rdtgroup_mutex);
143144

144-
if (closid == 0)
145-
return -ENOSPC;
146-
closid--;
145+
if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) {
146+
cleanest_closid = resctrl_find_cleanest_closid();
147+
if (cleanest_closid < 0)
148+
return cleanest_closid;
149+
closid = cleanest_closid;
150+
} else {
151+
closid = ffs(closid_free_map);
152+
if (closid == 0)
153+
return -ENOSPC;
154+
closid--;
155+
}
147156
__clear_bit(closid, &closid_free_map);
148157

149158
return closid;
@@ -163,7 +172,7 @@ void closid_free(int closid)
163172
* Return: true if @closid is currently associated with a resource group,
164173
* false if @closid is free
165174
*/
166-
static bool closid_allocated(unsigned int closid)
175+
bool closid_allocated(unsigned int closid)
167176
{
168177
lockdep_assert_held(&rdtgroup_mutex);
169178

0 commit comments

Comments
 (0)