[PATCH v3 37/46] perf/x86/intel/cmt: add cont_monitoring to perf cgroup

From: David Carrillo-Cisneros
Date: Sat Oct 29 2016 - 20:43:04 EST


Expose the attribute intel_cmt.cont_monitoring to perf cgroups using
the newly introduced hook PERF_CGROUP_ARCH_CGRP_SUBSYS_ATTS.

The format of new attribute is a semi-colon separated list of per-package
hexadecimal flags with missing an empty string assigned to zero.

echo "1;2" > g1/intel_cmt.cont_monitoring

Implies 0x1 for pkg 0 and 0x2 for pkg 1, 0x0 for all other pkgs.

This patch introduces the basic ideas of per-package uflags through
a cgroup attribute. The format can be changed to match Intel CAT's
schemata's file format once that is settled.

Signed-off-by: David Carrillo-Cisneros <davidcc@xxxxxxxxxx>
---
arch/x86/events/intel/cmt.c | 173 ++++++++++++++++++++++++++++++++++++++
arch/x86/include/asm/perf_event.h | 10 +++
2 files changed, 183 insertions(+)

diff --git a/arch/x86/events/intel/cmt.c b/arch/x86/events/intel/cmt.c
index 194038b..3ade923 100644
--- a/arch/x86/events/intel/cmt.c
+++ b/arch/x86/events/intel/cmt.c
@@ -2323,4 +2323,177 @@ inline void __intel_cmt_no_event_sched_in(void)
#endif
}

+#ifdef CONFIG_CGROUP_PERF
+
+static int cmt_monitoring_seq_show(struct seq_file *m, void *v)
+{
+ struct perf_cgroup *cgrp = perf_cgroup_from_css(seq_css(m));
+ struct monr *monr = NULL;
+ int p, nr_pkgs = topology_max_packages();
+
+ mutex_lock(&cmt_mutex);
+
+ if (perf_cgroup_mon_started(cgrp))
+ monr = monr_from_perf_cgroup(cgrp);
+
+ for (p = 0; p < nr_pkgs; p++) {
+ seq_printf(m, "%x", monr ? monr->pkg_uflags[p] : 0);
+ if (p + 1 < nr_pkgs)
+ seq_puts(m, ";");
+ }
+
+ seq_puts(m, "\n");
+
+ mutex_unlock(&cmt_mutex);
+ return 0;
+}
+
+/**
+ * Parses uflags string of the form: m1;m2;...;mP where m* are hexadecimal
+ * masks and P is less or equal than topology_max_packages(). Values not
+ * provided in the mask are assumed to be zero.
+ * On success, it allocates and returns an array.
+ */
+static enum cmt_user_flags *parse_pkg_uflags(char *buf, size_t nbytes)
+{
+ enum cmt_user_flags *uflags;
+ char *local_buf, *b, *m;
+ int err = 0, pmask, nr_pkgs = topology_max_packages();
+ u16 p = 0;
+
+ uflags = kcalloc(nr_pkgs, sizeof(*uflags), GFP_KERNEL);
+ if (!uflags)
+ return ERR_PTR(-ENOMEM);
+
+ local_buf = kcalloc(nbytes, sizeof(char), GFP_KERNEL);
+ if (!local_buf) {
+ err = -ENOMEM;
+ goto error;
+ }
+ memcpy(local_buf, buf, nbytes);
+ b = local_buf;
+ while ((m = strsep(&b, ";")) != NULL) {
+ if (p >= nr_pkgs) {
+ err = -EINVAL;
+ goto error;
+ }
+ if (!*m) {
+ uflags[p] = 0;
+ continue;
+ }
+ err = kstrtoint(m, 16, &pmask);
+ if (err)
+ goto error;
+ uflags[p] = pmask;
+ if (uflags[p] > CMT_UF_MAX) {
+ err = -EINVAL;
+ goto error;
+ }
+ /*
+ * Non-zero flags must have CMT_UF_HAS_USER set. Otherwise
+ * there could be that monrs are not used.
+ */
+ if (uflags[p] && (!(uflags[p] & CMT_UF_HAS_USER))) {
+ err = -EINVAL;
+ goto error;
+ }
+ p++;
+ }
+
+ kfree(local_buf);
+
+ return uflags;
+
+error:
+ kfree(local_buf);
+ kfree(uflags);
+
+ return ERR_PTR(err);
+}
+
+static ssize_t cmt_monitoring_write(struct kernfs_open_file *of,
+ char *buf, size_t nbytes, loff_t off)
+{
+ struct cgroup_subsys_state *css = of_css(of);
+ struct monr *monr;
+ int err = 0;
+ bool is_mon;
+ enum cmt_user_flags *uflags;
+
+ /* root is read-only */
+ if (css == get_root_perf_css())
+ return -EINVAL;
+
+ buf = strstrip(buf);
+
+ mutex_lock(&cmt_mutex);
+ monr_hrchy_acquire_mutexes();
+
+ /* Monitoring active, use new flags. */
+ uflags = parse_pkg_uflags(buf, nbytes);
+ if (IS_ERR(uflags)) {
+ err = PTR_ERR(uflags);
+ goto exit_unlock;
+ }
+
+ is_mon = perf_cgroup_mon_started(perf_cgroup_from_css(css));
+ if (!is_mon) {
+ if (pkg_uflags_has_user(uflags)) {
+ err = __css_start_monitoring(css);
+ if (err)
+ goto exit_free;
+ } else {
+ /*
+ * uflags must be all zero or parse_pkg_uflags
+ * would have failed.
+ */
+ goto exit_free;
+ }
+ }
+
+ /* At this point the monr is guaranteed to be this css's monr. */
+ monr = monr_from_css(css);
+
+ /* Disregard if flags have not changed. */
+ if (!memcmp(uflags, monr->pkg_uflags, pkg_uflags_size))
+ goto exit_free;
+
+ /*
+ * will update monr->pkg_flags. Do not exit on error, continue to
+ * clean up monr if unused.
+ */
+ err = monr_apply_uflags(monr, uflags);
+
+ if (!monr_has_user(monr))
+ monr_destroy(monr);
+
+exit_free:
+ kfree(uflags);
+exit_unlock:
+ monr_hrchy_release_mutexes();
+ mutex_unlock(&cmt_mutex);
+ return err ?: nbytes;
+}
+
+struct cftype perf_event_cgrp_arch_subsys_cftypes[] = {
+ {
+ /*
+ * allows per-package specification of uflags. It takes a
+ * semi-colon separated list of hex uflags values. The hex
+ * value in the i-th position corresponds to the uflags of
+ * the package with logical id == i + 1 . Empty and missing hex
+ * values receive 0.
+ * e.g. "1;2" -> uflag 0x1 for pkg 0, uflag 0x2 for pkg 2,
+ * and 0x0 for all others.
+ */
+ .name = "cmt_monitoring",
+ .seq_show = cmt_monitoring_seq_show,
+ .write = cmt_monitoring_write,
+ },
+
+ {} /* terminate */
+};
+
+#endif
+
device_initcall(intel_cmt_init);
diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
index 783bdbb..babee97 100644
--- a/arch/x86/include/asm/perf_event.h
+++ b/arch/x86/include/asm/perf_event.h
@@ -315,6 +315,16 @@ int perf_cgroup_arch_css_online(struct cgroup_subsys_state *css);
perf_cgroup_arch_css_offline
void perf_cgroup_arch_css_offline(struct cgroup_subsys_state *css);

+extern struct cftype perf_event_cgrp_arch_subsys_cftypes[];
+
+#define PERF_CGROUP_ARCH_CGRP_SUBSYS_ATTS \
+ .dfl_cftypes = perf_event_cgrp_arch_subsys_cftypes, \
+ .legacy_cftypes = perf_event_cgrp_arch_subsys_cftypes,
+
+#else
+
+#define PERF_CGROUP_ARCH_CGRP_SUBSYS_ATTS
+
#endif
#endif

--
2.8.0.rc3.226.g39d4020