Per-package locks potentially reduce the contention when compared to the
system-wide approach of the previous CQM/CMT driver.

Lockdep needs lock_class_key's to be statically initialized and/or use
nesting, but nesting is currently hard-coded for up to 8 levels and it's
fragile to depend on lockdep internals.
To circumvent this problem, statically define CMT_MAX_NR_PKGS number of
lock_class_key's.

Additional details in code's comments.

Signed-off-by: David Carrillo-Cisneros <davi...@google.com>
---
 arch/x86/events/intel/cmt.c | 22 ++++++++++++++++++++++
 arch/x86/events/intel/cmt.h |  8 ++++++++
 2 files changed, 30 insertions(+)

diff --git a/arch/x86/events/intel/cmt.c b/arch/x86/events/intel/cmt.c
index 267a9ec..f12a06b 100644
--- a/arch/x86/events/intel/cmt.c
+++ b/arch/x86/events/intel/cmt.c
@@ -7,6 +7,14 @@
 #include "cmt.h"
 #include "../perf_event.h"
 
+/* Increase as needed as Intel CPUs grow. */
+#define CMT_MAX_NR_PKGS                8
+
+#ifdef CONFIG_LOCKDEP
+static struct lock_class_key   mutex_keys[CMT_MAX_NR_PKGS];
+static struct lock_class_key   lock_keys[CMT_MAX_NR_PKGS];
+#endif
+
 static DEFINE_MUTEX(cmt_mutex);
 
 static unsigned int cmt_l3_scale;      /* cmt hw units to bytes. */
@@ -51,6 +59,12 @@ static struct pkg_data *alloc_pkg_data(int cpu)
        int numa_node = cpu_to_node(cpu);
        u16 pkgid = topology_logical_package_id(cpu);
 
+       if (pkgid >= CMT_MAX_NR_PKGS) {
+               pr_err("CMT_MAX_NR_PKGS of %d is insufficient for logical 
packages.\n",
+                      CMT_MAX_NR_PKGS);
+               return ERR_PTR(-ENOSPC);
+       }
+
        if (c->x86_cache_occ_scale != cmt_l3_scale) {
                /* 0 scale must have been converted to 1 automatically. */
                if (c->x86_cache_occ_scale || cmt_l3_scale != 1) {
@@ -65,9 +79,17 @@ static struct pkg_data *alloc_pkg_data(int cpu)
 
        pkgd->max_rmid = c->x86_cache_max_rmid;
 
+       mutex_init(&pkgd->mutex);
+       raw_spin_lock_init(&pkgd->lock);
+
        pkgd->work_cpu = cpu;
        pkgd->pkgid = pkgid;
 
+#ifdef CONFIG_LOCKDEP
+       lockdep_set_class(&pkgd->mutex, &mutex_keys[pkgid]);
+       lockdep_set_class(&pkgd->lock, &lock_keys[pkgid]);
+#endif
+
        __min_max_rmid = min(__min_max_rmid, pkgd->max_rmid);
 
        return pkgd;
diff --git a/arch/x86/events/intel/cmt.h b/arch/x86/events/intel/cmt.h
index 8c16797..55416db 100644
--- a/arch/x86/events/intel/cmt.h
+++ b/arch/x86/events/intel/cmt.h
@@ -11,11 +11,16 @@
  * Rules:
  *  - cmt_mutex: Hold for CMT init/terminate, event init/terminate,
  *  cgroup start/stop.
+ *  - Hold pkg->mutex and pkg->lock in _all_ active packages to traverse or
+ *  change the monr hierarchy.
+ *  - pkgd->lock: Hold in current package to access that pkgd's members.
  */
 
 /**
  * struct pkg_data - Per-package CMT data.
  *
+ * @mutex:                     Hold when modifying this pkg_data.
+ * @lock:                      Hold to protect pmonrs in this pkg_data.
  * @work_cpu:                  CPU to run rotation and other batch jobs.
  *                             It must be in the package associated to its
  *                             instance of pkg_data.
@@ -23,6 +28,9 @@
  * @pkgid:                     The logical package id for this pkgd.
  */
 struct pkg_data {
+       struct mutex            mutex;
+       raw_spinlock_t          lock;
+
        unsigned int            work_cpu;
        u32                     max_rmid;
        u16                     pkgid;
-- 
2.8.0.rc3.226.g39d4020

Reply via email to