Skip to contentSkip to conversation

cgroup,x86/intel_rdt : *pre h/w* Intel code data prioritization support and cgroup changes

PATCH New Vikas Shivappa 1 participant 7 messages Aug 23, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover cgroup,x86/intel_rdt : *pre h/w* Intel code data prioritization support and cgroup changes

This patchset has some preparatory patches to cgroup to add a wrapper to to cgroup_rmdir to destroy the cgroup directories recursively, new mount and umount interfaces for ss. It then has patches to support Intel code data prioritization which is an extension of cache allocation and lets allocate code and data cache seperately.

Details of the feature can be found in the Intel SDM june 2015 volume 3,

	section 17.16.

The preparatory patches and some part of enabling are tested but otherwise this is mostly a *pre hardware* patch and I still need to test it once i setup the hardware.

This patch is dependent on cache allocation patch series V11.

http://marc.info/?l=linux-kernel&m=143526051306160

Known issues:
- Need changes to the formating of the change logs
- Remove some RDT specific flags in cgroup.c
- Patch is for initial testing. Will add more documentation on the new APIs if needed.

[PATCH 1/6] cgroup: Adds cgroup_destroy_children
[PATCH 2/6] cgroup: Add css_mount and css_umount
[PATCH 3/6] x86/intel_rdt: Intel Code Data Prioritization detection [PATCH 4/6] x86/intel_rdt: Adds support to enable CDP [PATCH 5/6] x86/intel_rdt: Class of service management for code data [PATCH 6/6] x86/intel_rdt: Support dcache and icache mask for Code

#1 lore raw

PATCH 1/6 cgroup: Adds cgroup_destroy_children

cgroup currently does not support removing a cgroup directory if it has children.
Adds a api cgroup_destroy_children which destroys all the children in the cgroup hierarchy starting from the bottom. After each cgroup node is destroyed we wait till the css is offlined and freed.

---
 include/linux/cgroup.h |  4 +++
 kernel/cgroup.c        | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
include/linux/cgroup.h +4 -0
index b9cb94c..292ed97 100644
@@ -609,6 +609,7 @@ char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
 int cgroup_rm_cftypes(struct cftype *cfts);
+void cgroup_destroy_children(struct cgroup_subsys_state *css);
 
 bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
 
@@ -705,6 +706,9 @@ struct cgroup_subsys {
 	 * specifies the mask of subsystems that this one depends on.
 	 */
 	unsigned int depends_on;
+
+	/* used to wait for freeing of csses */
+	wait_queue_head_t pending_css_free_waitq;
 };
 
 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
kernel/cgroup.c +85 -0

109 lines not on this page · raw message

Signature · 1 line
1.9.1
#2 lore raw

PATCH 2/6 cgroup: Add css_mount and css_umount

In reply to Vikas Shivappa

Add subsystem APIs css_mount and css_umount which will be called during cgroup_mount and umount respectively. This will let the subsystem perform any specific setup during mount/umount.
This will be used in code data prioritization code later.

---
 include/linux/cgroup.h |  2 ++
 kernel/cgroup.c        | 12 ++++++++++++
 2 files changed, 14 insertions(+)
include/linux/cgroup.h +2 -0
index 292ed97..3ded186 100644
@@ -655,6 +655,8 @@ struct cgroup_subsys {
 		     struct cgroup_subsys_state *old_css,
 		     struct task_struct *task);
 	void (*bind)(struct cgroup_subsys_state *root_css);
+	void (*css_mount)(void *mount_info);
+	void (*css_umount)(void);
 
 	int disabled;
 	int early_init;
kernel/cgroup.c +12 -0
index 668f614..e139504 100644
@@ -1345,6 +1345,7 @@ struct cgroup_sb_opts {
 	char *name;
 	/* User explicitly requested empty subsystem */
 	bool none;
+	void *mount_info;
 };
 
 static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
@@ -1787,6 +1788,11 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
 	if (ret)
 		goto out_unlock;
 
+	for_each_subsys(ss, i) {
+		if ((opts.subsys_mask && (1U << i)) && ss->css_mount)
+			ss->css_mount(opts.mount_info);
+	}
+
 	/* look for a matching existing root */
 	if (opts.flags & CGRP_ROOT_SANE_BEHAVIOR) {
 		cgrp_dfl_root_visible = true;
@@ -1928,7 +1934,13 @@ static void cgroup_kill_sb(struct super_block *sb)
 {
 	struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
 	struct cgroup_root *root = cgroup_root_from_kf(kf_root);
+	struct cgroup_subsys *ss;
+	int i;
 
+	for_each_subsys(ss, i) {
+		if ((root->subsys_mask && (1U << i)) && ss->css_umount)
+			ss->css_umount();
+	}
 	/*
 	 * If @root doesn't have any mounts or children, start killing it.
 	 * This prevents new mounts by disabling percpu_ref_tryget_live().
Signature · 1 line
1.9.1
#3 lore raw

PATCH 3/6 x86/intel_rdt: Intel Code Data Prioritization detection

In reply to Vikas Shivappa

This patch adds enumeration support for code data prioritization(CDP) feature found in future Intel Xeon processors.

CDP is an extension to Cache Allocation and lets threads allocate subset of L3 cache for code and data separately. The allocation is represented by the code or data cache mask MSRs (IA32_L3_QOS_MASK_n). Each Class of service would be associated with one dcache_mask and one icache_mask MSR. The association for a CLOSid 'n' is shown below :

data_mask_address (n) = base + (n <<1)
code_mask_address (n) = base + (n <<1) +1.
During scheduling the kernel writes the CLOSid
of the thread to IA32_PQR_ASSOC_MSR.

This patch includes CPUID enumeration routines which includes CPUID enumeration as well probing MSR_IA32_PQOS_CFG.

---
 arch/x86/include/asm/cpufeature.h |  5 +++-
 arch/x86/include/asm/intel_rdt.h  |  1 +
 arch/x86/include/asm/rdt_common.h |  1 +
 arch/x86/kernel/cpu/common.c      |  1 +
 arch/x86/kernel/cpu/intel_rdt.c   | 58 ++++++++++++++++++++++++++-------------
 5 files changed, 46 insertions(+), 20 deletions(-)
arch/x86/include/asm/cpufeature.h +4 -1
index ae5ae9d..c0d435a 100644
@@ -12,7 +12,7 @@
 #include <asm/disabled-features.h>
 #endif
 
-#define NCAPINTS	14	/* N 32-bit words worth of info */
+#define NCAPINTS	15	/* N 32-bit words worth of info */
 #define NBUGINTS	1	/* N 32-bit bug flags */
 
 /*
@@ -256,6 +256,9 @@
 /* Intel-defined CPU features, CPUID level 0x00000010:0 (ebx), word 13 */
 #define X86_FEATURE_CAT_L3	(13*32 + 1) /* Cache Allocation L3 */
 
+/* Intel-defined CPU QoS Sub-leaf, CPUID level 0x00000010:1 (ecx), word 14 */
+#define X86_FEATURE_CDP_L3	(14*32 + 2) /* Code data prioritization L3 */
+
 /*
  * BUG word(s)
  */
arch/x86/include/asm/intel_rdt.h +1 -0
index 78df3d7..68f220e 100644
@@ -16,6 +16,7 @@ extern void __intel_rdt_sched_in(void);
 
 struct rdt_subsys_info {
 	unsigned long *closmap;
+	bool cdp_supported;
 };
 
 struct intel_rdt {
arch/x86/include/asm/rdt_common.h +1 -0
index 01502c5..8b75128 100644
@@ -2,6 +2,7 @@
 #define _X86_RDT_H_
 
 #define MSR_IA32_PQR_ASSOC	0x0c8f
+#define MSR_IA32_PQOS_CFG	0x0c81
 
 /**
  * struct intel_pqr_state - State cache for the PQR MSR
arch/x86/kernel/cpu/common.c +1 -0
index fd014f5..cf5f962 100644
@@ -682,6 +682,7 @@ void get_cpu_cap(struct cpuinfo_x86 *c)
 			cpuid_count(0x00000010, 1, &eax, &ebx, &ecx, &edx);
 			c->x86_cache_max_closid = edx + 1;
 			c->x86_cache_max_cbm_len = eax + 1;
+			c->x86_capability[14] = ecx;
 		}
 	}
 
arch/x86/kernel/cpu/intel_rdt.c +39 -19

86 lines not on this page · raw message

Signature · 1 line
1.9.1
#4 lore raw

PATCH 4/6 x86/intel_rdt: Adds support to enable CDP

In reply to Vikas Shivappa

At any time the intel_rdt cgroup operates in 2 modes (legacy cache
allocation mode/default or code data prioritization mode).
When CDP is enabled the number of available CLOSids is halved and its doubled when CDP is disabled. When CDP is enabled each CLOSid maps to a data cache mask and instruction cache mask.

The enabling itself is done by writing to the IA32_PQOS_CFG MSR and can dynamically be enabled or disabled.
On every mode change all the bit mask MSRs are reset to all 1s or fully open. This implies that all the tasks also need to be now moved to the root cgroup since root cgroup represents the cgroup with a mask of all 1s. Thereby we destroy all the child cgroups in the hierarchy. The enabling and disabling is done during mount/umount and depending on the mode we switched into , the corresponding files are only displayed in the cgroup directory. Each cgroup would
expose its dcache(data cache) and icache(instruction cache) mask when CDP is enabled and expose the cache_mask(common cache mask) when operating in Cache allocation mode.

---
 arch/x86/include/asm/intel_rdt.h |  11 +-
 arch/x86/kernel/cpu/intel_rdt.c  | 253 ++++++++++++++++++++++++++++++++-------
 include/linux/cgroup.h           |   1 +
 kernel/cgroup.c                  |  32 ++++-
 4 files changed, 247 insertions(+), 50 deletions(-)
arch/x86/include/asm/intel_rdt.h +10 -1
index 68f220e..55c496a 100644
@@ -9,6 +9,8 @@
 #define MAX_CBM_LENGTH			32
 #define IA32_L3_CBM_BASE		0xc90
 #define CBM_FROM_INDEX(x)		(IA32_L3_CBM_BASE + x)
+#define DCACHE_MASK_INDEX(x)		CBM_FROM_INDEX((x << 1))
+#define ICACHE_MASK_INDEX(x)		CBM_FROM_INDEX((x << 1) + 1)
 
 DECLARE_PER_CPU(struct intel_pqr_state, pqr_state);
 extern struct static_key rdt_enable_key;
@@ -17,6 +19,7 @@ extern void __intel_rdt_sched_in(void);
 struct rdt_subsys_info {
 	unsigned long *closmap;
 	bool cdp_supported;
+	bool cdp_enable;
 };
 
 struct intel_rdt {
@@ -24,11 +27,17 @@ struct intel_rdt {
 	u32 closid;
 };
 
-struct clos_cbm_map {
+struct cat_clos_mask_map {
 	unsigned long cache_mask;
 	unsigned int clos_refcnt;
 };
 
+struct cdp_clos_mask_map {
+	unsigned long icache_mask;
+	unsigned long dcache_mask;
+	unsigned int clos_refcnt;
+};
+
 /*
  * Return rdt group corresponding to this container.
  */
arch/x86/kernel/cpu/intel_rdt.c +207 -46

372 lines not on this page · raw message

include/linux/cgroup.h +1 -0
index 3ded186..48f106c 100644
@@ -195,6 +195,7 @@ enum {
 	 * specified at mount time and thus is implemented here.
 	 */
 	CGRP_CPUSET_CLONE_CHILDREN,
+	CGRP_RDT_DESTROY_CHILDREN,
 };
 
 struct cgroup {
kernel/cgroup.c +29 -3
index e139504..38b4de5 100644
@@ -1346,6 +1346,8 @@ struct cgroup_sb_opts {
 	/* User explicitly requested empty subsystem */
 	bool none;
 	void *mount_info;
+	bool rdt_cdp_enable;
+	bool rdt_mount;
 };
 
 static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
@@ -1392,6 +1394,13 @@ static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
 			opts->cpuset_clone_children = true;
 			continue;
 		}
+		if (!strcmp(token, "cdp_enable")) {
+			opts->rdt_cdp_enable = true;
+			continue;
+		}
+		if (!strcmp(token, "intel_rdt")) {
+			opts->rdt_mount = true;
+		}
 		if (!strcmp(token, "xattr")) {
 			opts->flags |= CGRP_ROOT_XATTR;
 			continue;
@@ -1640,6 +1649,8 @@ static void init_cgroup_root(struct cgroup_root *root,
 		strcpy(root->name, opts->name);
 	if (opts->cpuset_clone_children)
 		set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
+	if (opts->rdt_mount)
+		set_bit(CGRP_RDT_DESTROY_CHILDREN, &root->cgrp.flags);
 }
 
 static int cgroup_setup_root(struct cgroup_root *root, unsigned int ss_mask)
@@ -1789,8 +1800,11 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
 		goto out_unlock;
 
 	for_each_subsys(ss, i) {
-		if ((opts.subsys_mask && (1U << i)) && ss->css_mount)
+		if ((opts.subsys_mask && (1U << i)) && ss->css_mount) {
+			if (opts.rdt_mount)
+				opts.mount_info = (void *)opts.rdt_cdp_enable;
 			ss->css_mount(opts.mount_info);
+		}
 	}
 
 	/* look for a matching existing root */
@@ -1934,12 +1948,24 @@ static void cgroup_kill_sb(struct super_block *sb)
 {
 	struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
 	struct cgroup_root *root = cgroup_root_from_kf(kf_root);
+	bool root_has_no_children = false;
+	struct cgroup *cgrp = &root->cgrp;
 	struct cgroup_subsys *ss;
 	int i;
 
+	root_has_no_children = list_empty(&root->cgrp.self.children);
+
+	/*
+	 * Call the subsystem specific umount API.
+	 */
 	for_each_subsys(ss, i) {
-		if ((root->subsys_mask && (1U << i)) && ss->css_umount)
+		if ((root->subsys_mask && (1U << i)) && ss->css_umount) {
+			mutex_lock(&cgroup_mutex);
 			ss->css_umount();
+			if (!css_has_online_children(&cgrp->self))
+				root_has_no_children = true;
+			mutex_unlock(&cgroup_mutex);
+		}
 	}
 	/*
 	 * If @root doesn't have any mounts or children, start killing it.
@@ -1948,7 +1974,7 @@ static void cgroup_kill_sb(struct super_block *sb)
 	 *
 	 * And don't kill the default root.
 	 */
-	if (!list_empty(&root->cgrp.self.children) ||
+	if (!root_has_no_children ||
 	    root == &cgrp_dfl_root)
 		cgroup_put(&root->cgrp);
 	else
Signature · 1 line
1.9.1
#5 lore raw

PATCH 5/6 x86/intel_rdt: Class of service management for code data prioritization

In reply to Vikas Shivappa

Add support to manage CLOSid(class of service id) for code data prioritization(CDP). Includes allocating, freeing closid and closid_get and closid_put.
During mount if the mode is changed between cdp and cache allocation only, all the CLOSids are freed. When a new cgroup is created it inherits its parents CLOSid in CDP just like in Cache allocation.

---
 arch/x86/kernel/cpu/intel_rdt.c | 127 +++++++++++++++++++++++++---------------
 1 file changed, 81 insertions(+), 46 deletions(-)
arch/x86/kernel/cpu/intel_rdt.c +81 -46
index 155ac51..285db1e 100644
@@ -166,6 +166,85 @@ static void closcbm_map_dump(void)
 	}
 }
 
+static void closid_map_init(void)
+{
+	u32 maxid = boot_cpu_data.x86_cache_max_closid;
+
+	bitmap_zero(rdtss_info.closmap, maxid);
+}
+
+static inline void closid_get(u32 closid)
+{
+	lockdep_assert_held(&rdt_group_mutex);
+
+	if (!rdtss_info.cdp_enable)
+		cat_cm_map[closid].clos_refcnt++;
+	else
+		cdp_cm_map[closid].clos_refcnt++;
+}
+
+static int closid_alloc(struct intel_rdt *ir)
+{
+	u32 maxid;
+	u32 id;
+
+	lockdep_assert_held(&rdt_group_mutex);
+
+	maxid = boot_cpu_data.x86_cache_max_closid;
+	id = find_next_zero_bit(rdtss_info.closmap, maxid, 0);
+	if (id == maxid)
+		return -ENOSPC;
+
+	set_bit(id, rdtss_info.closmap);
+	closid_get(id);
+	ir->closid = id;
+
+	return 0;
+}
+
+static inline void closid_free(u32 closid)
+{
+	clear_bit(closid, rdtss_info.closmap);
+	if (!rdtss_info.cdp_enable) {
+		cat_cm_map[closid].cache_mask = 0;
+	} else {
+		cdp_cm_map[closid].dcache_mask = 0;
+		cdp_cm_map[closid].icache_mask = 0;
+	}
+}
+
+static inline void closid_cat_put(u32 closid)
+{
+	struct cat_clos_mask_map *ccm = &cat_cm_map[closid];
+
+	lockdep_assert_held(&rdt_group_mutex);
+	if (WARN_ON(!ccm->clos_refcnt))
+		return;
+
+	if (!--ccm->clos_refcnt)
+		closid_free(closid);
+}
+
+static inline void closid_cdp_put(u32 closid)
+{
+	struct cdp_clos_mask_map *ccm = &cdp_cm_map[closid];
+
+	lockdep_assert_held(&rdt_group_mutex);
+	if (WARN_ON(!ccm->clos_refcnt))
+		return;
+
+	if (!--ccm->clos_refcnt)
+		closid_free(closid);
+}
+
+static inline void closid_put(u32 closid)
+{
+	if (!rdtss_info.cdp_enable)
+		closid_cat_put(closid);
+	else
+		closid_cdp_put(closid);
+}
+
 static void cdp_cm_map_reset(int maxid, unsigned long max_cbm_mask)
 {
 	size_t sizeb;
@@ -266,6 +345,8 @@ static void rdt_css_mount(void* info)
 	else
 		cdp_disable();
 
+	closid_map_init();
+
 	rdtss_info.cdp_enable = enable_cdp;
 	mutex_unlock(&rdt_group_mutex);
 }
@@ -288,52 +369,6 @@ static inline void rdt_cdp_init(int cdp_maxid, unsigned long max_cbm_mask)
 	rdtss_info.cdp_supported = true;
 }
 
-static inline void closid_get(u32 closid)
-{
-	struct cat_clos_mask_map *ccm = &cat_cm_map[closid];
-
-	lockdep_assert_held(&rdt_group_mutex);
-
-	ccm->clos_refcnt++;
-}
-
-static int closid_alloc(struct intel_rdt *ir)
-{
-	u32 maxid;
-	u32 id;
-
-	lockdep_assert_held(&rdt_group_mutex);
-
-	maxid = boot_cpu_data.x86_cache_max_closid;
-	id = find_next_zero_bit(rdtss_info.closmap, maxid, 0);
-	if (id == maxid)
-		return -ENOSPC;
-
-	set_bit(id, rdtss_info.closmap);
-	closid_get(id);
-	ir->closid = id;
-
-	return 0;
-}
-
-static inline void closid_free(u32 closid)
-{
-	clear_bit(closid, rdtss_info.closmap);
-	cat_cm_map[closid].cache_mask = 0;
-}
-
-static inline void closid_put(u32 closid)
-{
-	struct cat_clos_mask_map *ccm = &cat_cm_map[closid];
-
-	lockdep_assert_held(&rdt_group_mutex);
-	if (WARN_ON(!ccm->clos_refcnt))
-		return;
-
-	if (!--ccm->clos_refcnt)
-		closid_free(closid);
-}
-
 void __intel_rdt_sched_in(void)
 {
 	struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
Signature · 1 line
1.9.1
#6 lore raw

PATCH 6/6 x86/intel_rdt: Support dcache and icache mask for Code data prioritization

In reply to Vikas Shivappa

Add support to read and write the per cgroup data and instruction cache masks.
- When a new cgroup directory is created it inherits the parents dcache and icache mask. So a mkdir never fails.
- When user changes the mask, we look to see if we can reuse an existing CLOSid or else allocate a new one. When we run of CLOSids , we simply return -ENOSPC.

---
 arch/x86/kernel/cpu/intel_rdt.c | 177 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 163 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/intel_rdt.c +163 -14
index 285db1e..d0eaf04 100644
@@ -428,6 +428,22 @@ static int intel_cache_alloc_cbm_read(struct seq_file *m, void *v)
 	return 0;
 }
 
+static int cdp_dcache_read(struct seq_file *m, void *v)
+{
+	struct intel_rdt *ir = css_rdt(seq_css(m));
+
+	seq_printf(m, "%08lx\n", cdp_cm_map[ir->closid].dcache_mask);
+	return 0;
+}
+
+static int cdp_icache_read(struct seq_file *m, void *v)
+{
+	struct intel_rdt *ir = css_rdt(seq_css(m));
+
+	seq_printf(m, "%08lx\n", cdp_cm_map[ir->closid].icache_mask);
+	return 0;
+}
+
 static inline bool cbm_is_contiguous(unsigned long var)
 {
 	unsigned long maxcbm = MAX_CBM_LENGTH;
@@ -445,13 +461,24 @@ static inline bool cbm_is_contiguous(unsigned long var)
 	return true;
 }
 
-static int cbm_validate(struct intel_rdt *ir, unsigned long cbmvalue)
+static int cache_mask_validate(struct intel_rdt *ir, unsigned long cbmvalue)
 {
+	u32 max_cbm = boot_cpu_data.x86_cache_max_cbm_len;
 	struct cgroup_subsys_state *css;
 	struct intel_rdt *par, *c;
 	unsigned long *cbm_tmp;
+	u64 max_mask;
 	int err = 0;
 
+	if (ir == &rdt_root_group)
+		return -EPERM;
+
+	max_mask = (1ULL << max_cbm) - 1;
+	if (cbmvalue & ~max_mask) {
+		err = -EINVAL;
+		goto out_err;
+	}
+
 	if (!cbm_is_contiguous(cbmvalue)) {
 		pr_err("bitmask should have >=%d bits and be contiguous\n",
 			 min_bitmask_len);
@@ -486,10 +513,12 @@ out_err:
 static bool cbm_search(unsigned long cbm, u32 *closid)
 {
 	u32 maxid = boot_cpu_data.x86_cache_max_closid;
+	unsigned long cache_mask;
 	u32 i;
 
 	for (i = 0; i < maxid; i++) {
-		if (bitmap_equal(&cbm, &cat_cm_map[i].cache_mask, MAX_CBM_LENGTH)) {
+		cache_mask = cat_cm_map[i].cache_mask;
+		if (bitmap_equal(&cbm, &cache_mask, MAX_CBM_LENGTH)) {
 			*closid = i;
 			return true;
 		}
@@ -511,30 +540,19 @@ static bool cbm_search(unsigned long cbm, u32 *closid)
 static int intel_cache_alloc_cbm_write(struct cgroup_subsys_state *css,
 				 struct cftype *cft, u64 cbmvalue)
 {
-	u32 max_cbm = boot_cpu_data.x86_cache_max_cbm_len;
 	struct intel_rdt *ir = css_rdt(css);
 	ssize_t err = 0;
-	u64 max_mask;
 	u32 closid;
 
-	if (ir == &rdt_root_group)
-		return -EPERM;
-
 	/*
 	 * Need global mutex as cbm write may allocate a closid.
 	 */
 	mutex_lock(&rdt_group_mutex);
 
-	max_mask = (1ULL << max_cbm) - 1;
-	if (cbmvalue & ~max_mask) {
-		err = -EINVAL;
-		goto out;
-	}
-
 	if (cbmvalue == cat_cm_map[ir->closid].cache_mask)
 		goto out;
 
-	err = cbm_validate(ir, cbmvalue);
+	err = cache_mask_validate(ir, cbmvalue);
 	if (err)
 		goto out;
 
@@ -567,6 +585,133 @@ out:
 	return err;
 }
 
+static void cdp_clos_map_dump(void)
+{
+	u32 i;
+
+	pr_debug("CBMMAP\n");
+	for (i = 0; i < boot_cpu_data.x86_cache_max_closid; i++) {
+		pr_debug("dcache_mask: 0x%x, icache_mask=0x%x,clos_refcnt:%u\n",
+			(unsigned int)cdp_cm_map[i].dcache_mask,
+			(unsigned int)cdp_cm_map[i].icache_mask,
+			cdp_cm_map[i].clos_refcnt);
+	}
+}
+
+static bool cdp_mask_search(unsigned long dcache_mask,
+			    unsigned long icache_mask, u32 *closid)
+{
+	u32 maxid = boot_cpu_data.x86_cache_max_closid;
+	unsigned long dcm, icm;
+	u32 i;
+
+	for (i = 0; i < maxid; i++) {
+		dcm = cdp_cm_map[i].dcache_mask;
+		icm = cdp_cm_map[i].icache_mask;
+		if (bitmap_equal(&dcache_mask, &dcm, MAX_CBM_LENGTH)
+		    && bitmap_equal(&icache_mask, &icm, MAX_CBM_LENGTH)) {
+			*closid = i;
+			return true;
+		}
+	}
+
+	return false;
+}
+
+static int cdp_mask_write(struct intel_rdt *ir, u64 dcache_mask,
+			  u64 icache_mask)
+{
+	int err = 0;
+	u32 closid;
+
+	/*
+	 * Try to get a reference for a different CLOSid and release the
+	 * reference to the current CLOSid.
+	 * Need to put down the reference here and get it back in case we
+	 * run out of closids. Otherwise we run into a problem when
+	 * we could be using the last closid that could have been available.
+	 */
+	closid_put(ir->closid);
+	if (cdp_mask_search(dcache_mask, icache_mask, &closid)) {
+		ir->closid = closid;
+		closid_get(closid);
+	} else {
+		err = closid_alloc(ir);
+		if (err) {
+			closid_get(ir->closid);
+			goto out;
+		}
+		closid = ir->closid;
+		cdp_cm_map[closid].dcache_mask = dcache_mask;
+		cdp_cm_map[closid].icache_mask = icache_mask;
+		msr_update_all(DCACHE_MASK_INDEX(closid), dcache_mask);
+		msr_update_all(ICACHE_MASK_INDEX(closid), icache_mask);
+	}
+out:
+
+	return err;
+}
+
+static int cdp_dcache_write(struct cgroup_subsys_state *css,
+				 struct cftype *cft, u64 new_dcache_mask)
+{
+	unsigned long curr_icache_mask, curr_dcache_mask;
+	struct intel_rdt *ir = css_rdt(css);
+	int err = 0;
+
+	/*
+	 * Need global mutex as cache mask write may allocate a closid.
+	 */
+	mutex_lock(&rdt_group_mutex);
+
+	curr_dcache_mask = cdp_cm_map[ir->closid].dcache_mask;
+	curr_icache_mask = cdp_cm_map[ir->closid].icache_mask;
+
+	if (new_dcache_mask == curr_dcache_mask)
+		goto out;
+
+	err = cache_mask_validate(ir, new_dcache_mask);
+	if (err)
+		goto out;
+
+	err = cdp_mask_write(ir, new_dcache_mask, curr_icache_mask);
+	cdp_clos_map_dump();
+out:
+	mutex_unlock(&rdt_group_mutex);
+
+	return err;
+}
+
+static int cdp_icache_write(struct cgroup_subsys_state *css,
+				 struct cftype *cft, u64 new_icache_mask)
+{
+	unsigned long curr_icache_mask, curr_dcache_mask;
+	struct intel_rdt *ir = css_rdt(css);
+	int err = 0;
+
+	/*
+	 * Need global mutex as cache mask write may allocate a closid.
+	 */
+	mutex_lock(&rdt_group_mutex);
+
+	curr_dcache_mask = cdp_cm_map[ir->closid].dcache_mask;
+	curr_icache_mask = cdp_cm_map[ir->closid].icache_mask;
+
+	if (new_icache_mask == curr_icache_mask)
+		goto out;
+
+	err = cache_mask_validate(ir, new_icache_mask);
+	if (err)
+		goto out;
+
+	err = cdp_mask_write(ir, curr_dcache_mask, new_icache_mask);
+	cdp_clos_map_dump();
+out:
+	mutex_unlock(&rdt_group_mutex);
+
+	return err;
+}
+
 static inline bool rdt_cpumask_update(int cpu)
 {
 	static cpumask_t tmp;
@@ -722,10 +867,14 @@ static struct cftype rdt_files[] = {
 	{
 		.name 		= "icache_mask",
 		.flags 		= CFTYPE_ONLY_ON_ROOT | CFTYPE_NOT_ON_ROOT,
+		.seq_show 	= cdp_icache_read,
+		.write_u64 	= cdp_icache_write,
 	},
 	{
 		.name		= "dcache_mask",
 		.flags		= CFTYPE_ONLY_ON_ROOT | CFTYPE_NOT_ON_ROOT,
+		.seq_show 	= cdp_dcache_read,
+		.write_u64 	= cdp_dcache_write,
 	},
 	{ }	/* terminate */
 };
Signature · 1 line
1.9.1