Skip to contentSkip to conversation

perf,x86: add Intel Memory Bandwidth Monitoring (MBM) PMU

PATCH Superseded Kanaka Juvva 3 participants 3 messages Jul 22, 2015 – Aug 3, 2015

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf,x86: add Intel Memory Bandwidth Monitoring (MBM) PMU

This patch adds Memory Bandwidth Monitoring events to the exitsing intel_cqm pmu in the Linux Kernel.

Intel MBM builds on Cache Monitoring Technology (CMT) infrastructure to allow monitoring of bandwidth from one level of the cache hierarchy to the next - in this case focusing on the L3 cache, which is typically backed directly by system memory. As a result of this implementation, memory bandwidth can be monitored.

MBM counters are available in Intel Xeon CPUs. The following events are implemented in the kernel and expose two MBM stats via perf_event interface:

- llc_local_bw: bandwidth consumption of all coress on socket
- llc_total_bw: bandwidth consumption of all cores on socket + bandwidth for QPI accesses

At present, MBM events are checked at one second interval provided by the HRTIMER of the event. Counters can overflow atmost once in a second and thus must be read atleast once in a second. Overflow is detected and handled. Running average value is calculated for each bandwidth type upon reading a new value from the MSR. Sliding window stores the values read from MSR. Sliding window size is configurable.

---
 arch/x86/include/asm/cpufeature.h          |   2 +
 arch/x86/kernel/cpu/common.c               |   4 +-
 arch/x86/kernel/cpu/perf_event_intel_cqm.c | 624 +++++++++++++++++++++++++++--
 3 files changed, 588 insertions(+), 42 deletions(-)
arch/x86/include/asm/cpufeature.h +2 -0
index 3d6606f..c36abdf 100644
@@ -251,6 +251,8 @@
 
 /* Intel-defined CPU QoS Sub-leaf, CPUID level 0x0000000F:1 (edx), word 12 */
 #define X86_FEATURE_CQM_OCCUP_LLC (12*32+ 0) /* LLC occupancy monitoring if 1 */
+#define X86_FEATURE_CQM_MBM_TOTAL (12*32+1) /* LLC Total MBM monitoring  */
+#define X86_FEATURE_CQM_MBM_LOCAL (12*32+2) /* LLC Local MBM monitoring  */
 
 /*
  * BUG word(s)
arch/x86/kernel/cpu/common.c +3 -1
index 922c5e0..7dcc88c 100644
@@ -643,7 +643,9 @@ void get_cpu_cap(struct cpuinfo_x86 *c)
 			/* QoS sub-leaf, EAX=0Fh, ECX=1 */
 			cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
 			c->x86_capability[12] = edx;
-			if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) {
+			if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC))
+			      || ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL))
+			      ||  (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) {
 				c->x86_cache_max_rmid = ecx;
 				c->x86_cache_occ_scale = ebx;
 			}
arch/x86/kernel/cpu/perf_event_intel_cqm.c +583 -41

881 lines not on this page · raw message

Signature · 1 line
2.1.0
#1 lore raw

arch/x86/kernel/cpu/perf_event_intel_cqm.c line 15

Quoted 9 lines · Kanaka Juvva
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
index 1880761..dc1ce58 100644
@@ -12,10 +12,20 @@
 #define MSR_IA32_PQR_ASSOC	0x0c8f
 #define MSR_IA32_QM_CTR		0x0c8e
 #define MSR_IA32_QM_EVTSEL	0x0c8d
+#define MAX_MBM_CNTR            0xffffff

Can we please have a consistent name space MBM_... here?

arch/x86/kernel/cpu/perf_event_intel_cqm.c line 26

Quoted 13 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +9 -2
+#define MBM_SOCKET_MAX          8
+#define MBM_TIME_DELTA_MAX      1000
+#define MBM_TIME_DELTA_MIN      1000
+#define MBM_SCALING_FACTOR      1000
+#define MBM_SCALING_HALF        (MBM_SCALING_FACTOR/2)
+#define MBM_FIFO_SIZE_MIN       10
+#define MBM_FIFO_SIZE_MAX       300
 
-static u32 cqm_max_rmid = -1;
-static unsigned int cqm_l3_scale; /* supposedly cacheline size */
 
+static u32 cqm_max_rmid = -1;
+static unsigned int cqm_l3_scale;/* supposedly cacheline size */

Pointless white space change

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+static unsigned mbm_window_size = MBM_FIFO_SIZE_MIN;
+static bool cqm_llc_occ, cqm_mbm;
 /**
  * struct intel_pqr_state - State cache for the PQR MSR
  * @rmid:		The cached Resource Monitoring ID
@@ -42,6 +52,34 @@ struct intel_pqr_state {
  * interrupts disabled, which is sufficient for the protection.
  */

arch/x86/kernel/cpu/perf_event_intel_cqm.c struct intel_pqr_state { · line 63

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +9 -0
 static DEFINE_PER_CPU(struct intel_pqr_state, pqr_state);
+static DEFINE_PER_CPU(struct mbm_pmu *, mbm_pmu);
+static DEFINE_PER_CPU(struct mbm_pmu *, mbm_pmu_to_free);
+
+/*
+ * mbm pmu is used for storing mbm_local and mbm_total
+ * events
+ */
+struct mbm_pmu {
+	spinlock_t       lock;

What is this lock protecting and how does it nest into perf locking?

arch/x86/kernel/cpu/perf_event_intel_cqm.c struct intel_pqr_state { · line 64

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	int              n_active; /* number of active events */

If you want to document your struct members, please use kerneldoc and not these hard to read tail comments.

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+	struct list_head active_list;
+	struct pmu       *pmu; /* pointer to mbm perf_event */
+	ktime_t          timer_interval; /* in ktime_t unit */
+	struct hrtimer   hrtimer;

arch/x86/kernel/cpu/perf_event_intel_cqm.c struct intel_pqr_state { · line 78

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+};
+
+struct sample {
+	u64 bytes; /* mbm counter value read*/
+	u64 cum_avg;   /* current running average of bandwidth */
+	ktime_t prev_time;
+	u64 index; /* current sample no */
+	u32 mbmfifo[MBM_FIFO_SIZE_MAX]; /* window of last n bw values */
+	u32  fifoin; /* mbmfifo in counter for sliding window */
+	u32  fifoout; /* mbmfifo out counter for sliding window */

So above you use a proper layout which is actualy readable (aside of the tail comments). Can you please stick with a consistent coding style?

arch/x86/kernel/cpu/perf_event_intel_cqm.c struct intel_pqr_state { · line 82

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+};
+
+struct sample *mbm_total; /* curent stats for  mbm_total events */
+struct sample *mbm_local; /* current stats for mbm_local events */

static ? And please get rid of these tail comments.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static cpumask_t cqm_cpumask; · line 107

Quoted 7 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
 /*
  * Protects cache_cgroups and cqm_rmid_free_lru and cqm_rmid_limbo_lru.
@@ -66,6 +104,9 @@ static cpumask_t cqm_cpumask;
 #define RMID_VAL_UNAVAIL	(1ULL << 62)
 
 #define QOS_L3_OCCUP_EVENT_ID	(1 << 0)
+#define QOS_MBM_TOTAL_EVENT_ID  (1 << 1)

Please use tabs not spaces.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static cpumask_t cqm_cpumask; · line 108

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+#define QOS_MBM_LOCAL_EVENT_ID1 0x3

What's ID1 for heavens sake? Looks like

(QOS_L3_OCCUP_EVENT_ID | QOS_MBM_TOTAL_EVENT_ID)

So this wants a descriptive ID name and a comment.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u32 intel_cqm_rotation_rmid; · line 136

Quoted 12 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -1
+#define QOS_MBM_LOCAL_EVENT_ID  (1 << 2)
 
 #define QOS_EVENT_MASK	QOS_L3_OCCUP_EVENT_ID
 
@@ -90,7 +131,8 @@ static u32 intel_cqm_rotation_rmid;
  */
 static inline bool __rmid_valid(u32 rmid)
 {
-	if (!rmid || rmid == INVALID_RMID)
+	WARN_ON_ONCE(rmid > cqm_max_rmid);
+	if (!rmid || (rmid == INVALID_RMID) || (rmid > cqm_max_rmid))
 		return false;

How is that related to this patch? Looks like a fix or prerequisite change to the existing code.

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
 	return true;
@@ -125,6 +167,7 @@ struct cqm_rmid_entry {
 	enum rmid_recycle_state state;
 	struct list_head list;
 	unsigned long queue_time;
+	bool config;
 };
 
 /*

arch/x86/kernel/cpu/perf_event_intel_cqm.c static inline struct cqm_rmid_entry *__rmid_entry(u32 rmid) · line 225

Quoted 7 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
@@ -176,6 +219,17 @@ static inline struct cqm_rmid_entry *__rmid_entry(u32 rmid)
 	return entry;
 }
 
+static void mbm_reset_stats(u32 rmid)
+{
+	u32  vrmid =  topology_physical_package_id(smp_processor_id()) *
+						   cqm_max_rmid + rmid;

Can you please explain that magic calculation? It's far from obvious why this would be correct.

Quoted 11 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +7 -0
+
+	if ((!cqm_mbm) || (!mbm_local) || (!mbm_total))
+		return;
+	memset(&mbm_local[vrmid], 0, sizeof(struct sample));
+	memset(&mbm_total[vrmid], 0, sizeof(struct sample));
+}
+
 /*
  * Returns < 0 on fail.
  *

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u32 __get_rmid(void) · line 248

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -1
@@ -190,8 +244,10 @@ static u32 __get_rmid(void)
 	if (list_empty(&cqm_rmid_free_lru))
 		return INVALID_RMID;
 
-	entry = list_first_entry(&cqm_rmid_free_lru, struct cqm_rmid_entry, list);
+	entry = list_first_entry(&cqm_rmid_free_lru,
+				  struct cqm_rmid_entry, list);

And the point of this change is?

Quoted 32 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +6 -0
 	list_del(&entry->list);
+	mbm_reset_stats(entry->rmid);
 
 	return entry->rmid;
 }
@@ -207,6 +263,7 @@ static void __put_rmid(u32 rmid)
 
 	entry->queue_time = jiffies;
 	entry->state = RMID_YOUNG;
+	mbm_reset_stats(rmid);
 
 	list_add_tail(&entry->list, &cqm_rmid_limbo_lru);
 }
@@ -232,6 +289,8 @@ static int intel_cqm_setup_rmid_cache(void)
 
 		INIT_LIST_HEAD(&entry->list);
 		entry->rmid = r;
+		entry->config = false;
+
 		cqm_rmid_ptrs[r] = entry;
 
 		list_add_tail(&entry->list, &cqm_rmid_free_lru);
@@ -254,6 +313,8 @@ fail:
 		kfree(cqm_rmid_ptrs[r]);
 
 	kfree(cqm_rmid_ptrs);
+	kfree(mbm_local);
+	kfree(mbm_total);
 	return -ENOMEM;
 }
 

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void __intel_cqm_event_count(void *info); · line 467

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
@@ -403,9 +464,11 @@ static void __intel_cqm_event_count(void *info);
 static u32 intel_cqm_xchg_rmid(struct perf_event *group, u32 rmid)
 {
 	struct perf_event *event;
+

Superfluous newline.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void __intel_cqm_event_count(void *info); · line 471

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
 	struct list_head *head = &group->hw.cqm_group_entry;
 	u32 old_rmid = group->hw.cqm_rmid;
 
+

Another one. Sigh!

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 560

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
 	lockdep_assert_held(&cache_mutex);
 
 	/*
@@ -494,6 +557,166 @@ static bool intel_cqm_sched_in_event(u32 rmid)
 	return false;
 }
 
+static u32  mbm_fifo_sum_lastn_out(int rmid, bool is_localbw)

Random space after u32

Quoted 17 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +17 -0
+{
+	u32 val = 0, i, j, index;
+
+	if (is_localbw) {
+		if (++mbm_local[rmid].fifoout >=  mbm_window_size)
+			mbm_local[rmid].fifoout =  0;
+		index =  mbm_local[rmid].fifoout;
+		for (i = 0; i < mbm_window_size - 1; i++) {
+			if (index + i >= mbm_window_size)
+				j = index + i - mbm_window_size;
+			else
+				j = index + i;
+			val += mbm_local[rmid].mbmfifo[j];
+		}
+		return val;
+	}
+

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 587

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+	if (++mbm_total[rmid].fifoout >=  mbm_window_size)
+		mbm_total[rmid].fifoout = 0;
+	index =  mbm_total[rmid].fifoout;
+	for (i = 0; i < mbm_window_size - 1; i++) {
+		if (index + i >= mbm_window_size)
+			j = index + i - mbm_window_size;
+		else
+			j = index + i;
+		val += mbm_total[rmid].mbmfifo[j];
+	}

The concept of helper functions to avoid code duplication is definitely applicable here.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 591

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+	return val;
+}
+
+static int  mbm_fifo_in(int rmid, u32 val, bool is_localbw)

Random space after int.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 601

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+{
+	if (is_localbw) {
+		mbm_local[rmid].mbmfifo[mbm_local[rmid].fifoin] = val;
+		if (++mbm_local[rmid].fifoin >=  mbm_window_size)
+			mbm_local[rmid].fifoin =  0;
+	} else {
+		mbm_total[rmid].mbmfifo[mbm_total[rmid].fifoin] = val;
+		if (++mbm_total[rmid].fifoin >=  mbm_window_size)
+			mbm_total[rmid].fifoin =  0;
+	}

static void mbm_fifo_in(struct sample *, u32 val)

would get rid of code duplication.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 602

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	return 0;

What's the purpose of having a return value here?

Quoted 9 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +9 -0
+}
+
+/*
+ * __rmid_read_mbm checks whether it is LOCAL or GLOBAL MBM event and reads
+ * its MSR counter. if (MSR current value < MSR previous value) it is an
+ * overflow and overflow is handled. If MSR is read within last 100ms,
+ * then the value is ignored; this will suppress small deltas. We don't
+ * process MBM samples that are within 100ms. Bandwidth is calculated as:
+ * bandwidth = difference of last two msr counter values/time difference.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 621

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+ * cum_avg = Running Average bandwidth of last 'n' samples that are processed
+ * Sliding window is used to save the last 'n' samples. Hence,
+ * n = sliding_window_size
+ * cum_avg is scaled down by a  factor MBM_SCALING_FACTOR and rounded to nearest
+ * integer. User interface reads the BW in MB/sec.
+ * Rounding algorithm : (X + 0.5):
+ * where X is scaled BW value. To avoid floating point arithmetic :
+ * BW- unscaled value
+ * (BW + MBM_SCALING_HALF)/MBM_SCALING_FACTOR is computed which gives the
+ * scaled bandwidth.

This is completely unreadable.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 632

Quoted 11 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +11 -0
+ */
+static u64 __rmid_read_mbm(unsigned int rmid, bool read_mbm_local)
+{
+	u64 val, tmp, diff_time, cma, bytes, index;
+	bool overflow = false;
+	ktime_t cur_time;
+	u32 tmp32 = rmid;
+	u32 vrmid = topology_physical_package_id(smp_processor_id()) *
+						 cqm_max_rmid + rmid;
+
+	rmid = vrmid;

This is completely backwards.

	tmp32 = rmid;  
	rmid = vrmid;
	do_stuff(rmid);
	rmid = tmp32;
	do_other_stuff(rmid);

Why can't you use vrmid for do_stuff() and leave rmid alone? Just because it would make the code simpler to read?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 633

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	cur_time = ktime_get_real();

Why would this operate on wall time and not on clock monotonic time?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 645

Quoted 12 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +12 -0
+	if (read_mbm_local) {
+		cma = mbm_local[rmid].cum_avg;
+		diff_time = ktime_ms_delta(cur_time,
+				   mbm_local[rmid].prev_time);
+		if (diff_time < 100)
+			return cma;
+		mbm_local[rmid].prev_time = ktime_set(0,
+				(unsigned long)ktime_to_ns(cur_time));
+		bytes = mbm_local[rmid].bytes;
+		index = mbm_local[rmid].index;
+		rmid = tmp32;
+		wrmsr(MSR_IA32_QM_EVTSEL, QOS_MBM_LOCAL_EVENT_ID1, rmid);

Why is this using ID1?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 658

Quoted 13 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +13 -0
+	} else {
+		cma = mbm_total[rmid].cum_avg;
+		diff_time = ktime_ms_delta(cur_time,
+					   mbm_total[rmid].prev_time);
+		if (diff_time < 100)
+			return cma;
+		mbm_total[rmid].prev_time = ktime_set(0,
+					(unsigned long)ktime_to_ns(cur_time));
+		bytes = mbm_total[rmid].bytes;
+		index = mbm_total[rmid].index;
+		rmid = tmp32;
+		wrmsr(MSR_IA32_QM_EVTSEL, QOS_MBM_TOTAL_EVENT_ID, rmid);
+	   }

Random space after tab.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 663

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -0
+	rdmsrl(MSR_IA32_QM_CTR, val);
+	if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
+		return val;
+
+	tmp = val;

You really have a faible for random storage. tmp gets assigned to mbm_*[rmid].bytes further down. This makes no sense at all.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 664

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	if (val < bytes) /* overflow handle it */ {

These tail comments are crap.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 668

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+		val = MAX_MBM_CNTR - bytes + val;
+		overflow = true;
+	} else
+		val = val - bytes;

That else clause is missing braces.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 670

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+	if (diff_time < MBM_TIME_DELTA_MAX - MBM_TIME_DELTA_MIN)
+		val =  (val * MBM_TIME_DELTA_MAX) / diff_time;

That resolves to

     	if (diff_time < 0)

Which is always false because diff_time is u64.

What's the logic behind this?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 674

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+
+	if ((diff_time > MBM_TIME_DELTA_MAX) && (!cma))
+		/* First sample, we can't use the time delta */
+		diff_time = MBM_TIME_DELTA_MAX;

And this?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 676

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+
+	rmid = vrmid;

More obfuscation

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 678

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+	if ((diff_time <= MBM_TIME_DELTA_MAX + MBM_TIME_DELTA_MIN)  ||
+			overflow) {

Again, that resolves to

if (overflow)

And even if MBM_TIME_DELTA_MAX would be not equal MBM_TIME_DELTA_MIN then this wants some explanation about

MBM_TIME_DELTA_MAX - MBM_TIME_DELTA_MIN

versus

MBM_TIME_DELTA_MAX + MBM_TIME_DELTA_MIN

and the whole logic behind this time delta magic, if there is any.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid)

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +8 -0
+		int bw, ret;
+
+		if (index   & (index < mbm_window_size))
+			val = cma * MBM_SCALING_FACTOR  + val / index -
+					cma / index;
+
+		val = (val + MBM_SCALING_HALF) / MBM_SCALING_FACTOR;
+		if (index >= mbm_window_size) {

These conditionals along with the magic math are undocumented.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 693

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +8 -0
+			/*
+			 * Compute the sum of recent n-1 samples and slide the
+			 * window by 1
+			 */
+			ret = mbm_fifo_sum_lastn_out(rmid, read_mbm_local);
+			/* recalculate the running average with current bw */
+			ret = (ret + val) / mbm_window_size;
+			if (ret < 0)

The average of positive numbers becomes negative ?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 696

Quoted 3 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
+				ret = 0;
+			bw = val;
+			val = ret;

Your back and forth assignements of random variables is not making the code any more readable.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 702

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +6 -0
+		} else
+			bw = val;
+		/* save the recent bw in fifo */
+		mbm_fifo_in(rmid, bw, read_mbm_local);
+
+		if (read_mbm_local) {

Oh well. You have this read_mbm_local conditional 3 times in this function. Why don't you make the function oblivious of this and hand in a pointer to mbm_local or mbm_total? Would be too simple and not enough obfuscated, right?

Quoted 21 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +17 -0
+			mbm_local[rmid].index++;
+			mbm_local[rmid].cum_avg = val;
+			mbm_local[rmid].bytes = tmp;
+			mbm_local[rmid].prev_time = ktime_set(0,
+					(unsigned long)ktime_to_ns(cur_time));
+		} else {
+			mbm_total[rmid].index++;
+			mbm_total[rmid].cum_avg = val;
+			mbm_total[rmid].bytes = tmp;
+			mbm_total[rmid].prev_time = ktime_set(0,
+					(unsigned long)ktime_to_ns(cur_time));
+		}
+		return val;
+	}
+	return  cma;
+}
+
 /*
  * Initially use this constant for both the limbo queue time and the
  * rotation timer interval, pmu::hrtimer_interval_ms.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_rmid_stabilize(unsigned int *available) · line 794

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -1
@@ -568,7 +791,8 @@ static bool intel_cqm_rmid_stabilize(unsigned int *available)
 	/*
 	 * Test whether an RMID is free for each package.
 	 */
-	on_each_cpu_mask(&cqm_cpumask, intel_cqm_stable, NULL, true);
+	if (entry->config)

This entry->config change wants to be a separate patch. It's completely non obvious how this changes the behaviour of the existing code.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_setup_event(struct perf_event *event, · line 1075

Quoted 12 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -1
+		on_each_cpu_mask(&cqm_cpumask, intel_cqm_stable, NULL, true);
 
 	list_for_each_entry_safe(entry, tmp, &cqm_rmid_limbo_lru, list) {
 		/*
@@ -846,8 +1070,9 @@ static void intel_cqm_setup_event(struct perf_event *event,
 				  struct perf_event **group)
 {
 	struct perf_event *iter;
-	bool conflict = false;
+
 	u32 rmid;
+	bool conflict = false;

Fits the overall picture of this patch: Sloppy

Quoted 20 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +14 -0
 	list_for_each_entry(iter, &cache_groups, hw.cqm_groups_entry) {
 		rmid = iter->hw.cqm_rmid;
@@ -875,6 +1100,40 @@ static void intel_cqm_setup_event(struct perf_event *event,
 	event->hw.cqm_rmid = rmid;
 }
 
+static void intel_cqm_event_update(struct perf_event *event)
+{
+	unsigned int rmid;
+	u64 val = 0;
+
+	/*
+	 * Task events are handled by intel_cqm_event_count().
+	 */
+	if (event->cpu == -1)
+		return;
+
+	rmid = event->hw.cqm_rmid;
+	if (!__rmid_valid(rmid))
+		return;

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_setup_event(struct perf_event *event, · line 1126

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+
+	switch (event->attr.config) {
+	case QOS_MBM_TOTAL_EVENT_ID:
+		val = __rmid_read_mbm(rmid, false);
+		break;
+	case QOS_MBM_LOCAL_EVENT_ID:
+		val = __rmid_read_mbm(rmid, true);
+		break;
+	default:
+		break;

return?

Quoted 40 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +26 -0
+	}
+	/*
+	 * Ignore this reading on error states and do not update the value.
+	 */
+	if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
+		return;
+
+	local64_set(&event->count, val);
+}
+
 static void intel_cqm_event_read(struct perf_event *event)
 {
 	unsigned long flags;
@@ -887,6 +1146,12 @@ static void intel_cqm_event_read(struct perf_event *event)
 	if (event->cpu == -1)
 		return;
 
+	if  ((event->attr.config & QOS_MBM_TOTAL_EVENT_ID) ||
+	     (event->attr.config & QOS_MBM_LOCAL_EVENT_ID))
+		intel_cqm_event_update(event);
+
+	if (!(event->attr.config &  QOS_L3_OCCUP_EVENT_ID))
+		return;
 	raw_spin_lock_irqsave(&cache_lock, flags);
 	rmid = event->hw.cqm_rmid;
 
@@ -906,6 +1171,28 @@ out:
 	raw_spin_unlock_irqrestore(&cache_lock, flags);
 }
 
+static void __intel_cqm_event_total_bw_count(void *info)
+{
+	struct rmid_read *rr = info;
+	u64 val;
+
+	val = __rmid_read_mbm(rr->rmid, false);
+	if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
+		return;
+	atomic64_add(val, &rr->value);
+}

arch/x86/kernel/cpu/perf_event_intel_cqm.c out: · line 1193

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+
+static void __intel_cqm_event_local_bw_count(void *info)
+{
+	struct rmid_read *rr = info;
+	u64 val;
+
+	val = __rmid_read_mbm(rr->rmid, true);
+	if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
+		return;
+	atomic64_add(val, &rr->value);

You're really a fan of copy and paste.

Quoted 14 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +6 -1
+}
+
 static void __intel_cqm_event_count(void *info)
 {
 	struct rmid_read *rr = info;
@@ -967,7 +1254,21 @@ static u64 intel_cqm_event_count(struct perf_event *event)
 	if (!__rmid_valid(rr.rmid))
 		goto out;
 
-	on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_count, &rr, 1);
+	switch (event->attr.config) {
+	case  QOS_L3_OCCUP_EVENT_ID:
+		on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_count, &rr, 1);
+		break;

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u64 intel_cqm_event_count(struct perf_event *event) · line 1270

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+	case  QOS_MBM_TOTAL_EVENT_ID:
+		on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_total_bw_count,
+				 &rr, 1);
+		break;
+	case  QOS_MBM_LOCAL_EVENT_ID:
+		on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_local_bw_count,
+				 &rr, 1);
+		break;
+	default:
+		break;

So there is nothing to do and you happily proceed?

Quoted 16 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+	}
 
 	raw_spin_lock_irqsave(&cache_lock, flags);
 	if (event->hw.cqm_rmid == rr.rmid)
@@ -977,6 +1278,39 @@ out:
 	return __perf_event_count(event);
 }
 
 static void intel_cqm_event_start(struct perf_event *event, int mode)
 {
 	struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
@@ -995,19 +1329,48 @@ static void intel_cqm_event_start(struct perf_event *event, int mode)
 	}
 
 	state->rmid = rmid;
+	if (event->attr.config & QOS_L3_OCCUP_EVENT_ID) {

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_event_start(struct perf_event *event, int mode) · line 1342

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +9 -0
+		struct cqm_rmid_entry *entry;
+
+		entry = __rmid_entry(rmid);
+		entry->config = true;
+	}
 	wrmsr(MSR_IA32_PQR_ASSOC, rmid, state->closid);
+
+	if (((event->attr.config & QOS_MBM_TOTAL_EVENT_ID) ||
+	     (event->attr.config & QOS_MBM_LOCAL_EVENT_ID))  && (cqm_mbm)) {
+		int cpu = get_cpu();

pmu->start() is called with interrupts disabled. So why do you need get_cpu() here?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_event_start(struct perf_event *event, int mode) · line 1347

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -0
+		struct mbm_pmu *pmu = per_cpu(mbm_pmu, cpu);
+
+		if (pmu) {
+			if (pmu->n_active == 0)
+				mbm_hrtimer_init(pmu);

That pmu timer wants to be initialized when the pmu is initialized.

Quoted 36 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +17 -1
+			pmu->n_active++;
+			list_add_tail(&event->active_entry,
+				      &pmu->active_list);
+			if (pmu->n_active == 1)
+				mbm_start_hrtimer(pmu);
+		}
+		put_cpu();
+	}
 }
 
 static void intel_cqm_event_stop(struct perf_event *event, int mode)
 {
 	struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
+	struct mbm_pmu *pmu = __this_cpu_read(mbm_pmu);
 
 	if (event->hw.cqm_state & PERF_HES_STOPPED)
 		return;
 
 	event->hw.cqm_state |= PERF_HES_STOPPED;
 
-	intel_cqm_event_read(event);
+	if (event->attr.config & QOS_L3_OCCUP_EVENT_ID)
+		intel_cqm_event_read(event);
+
+	if ((event->attr.config & QOS_MBM_TOTAL_EVENT_ID) ||
+	    (event->attr.config & QOS_MBM_LOCAL_EVENT_ID))
+		intel_cqm_event_update(event);
 
 	if (!--state->rmid_usecnt) {
 		state->rmid = 0;
@@ -1015,8 +1378,18 @@ static void intel_cqm_event_stop(struct perf_event *event, int mode)
 	} else {
 		WARN_ON_ONCE(!state->rmid);
 	}
+
+	if (pmu) {

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_event_stop(struct perf_event *event, int mode) · line 1392

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +8 -0
+		WARN_ON_ONCE(pmu->n_active <= 0);
+		pmu->n_active--;
+		if (pmu->n_active == 0)
+			mbm_stop_hrtimer(pmu);
+		list_del(&event->active_entry);
+	}
+
 }
 
+

Sigh

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +7 -0
 static int intel_cqm_event_add(struct perf_event *event, int mode)
 {
 
+static ssize_t
+sliding_window_size_store(struct device *dev,
+			  struct device_attribute *attr,
+			  const char *buf, size_t count)
+{
+	unsigned int bytes;
+	int ret;

arch/x86/kernel/cpu/perf_event_intel_cqm.c max_recycle_threshold_store(struct device *dev, · line 1655

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+
+	ret = kstrtouint(buf, 0, &bytes);
+	if (ret)
+		return ret;
+
+	mutex_lock(&cache_mutex);
+	if (bytes > 0 && bytes <= MBM_FIFO_SIZE_MAX)
+		mbm_window_size = bytes;
+	else
+		bytes = MBM_FIFO_SIZE_MIN;

What kind of twisted logic is that?

Quoted 16 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +8 -0
+
+	mutex_unlock(&cache_mutex);
+
+	return count;
+}
+
 static DEVICE_ATTR_RW(max_recycle_threshold);
+static DEVICE_ATTR_RW(sliding_window_size);
 
 static struct attribute *intel_cqm_attrs[] = {
 	&dev_attr_max_recycle_threshold.attr,
+	&dev_attr_sliding_window_size.attr,
 	NULL,
 };

arch/x86/kernel/cpu/perf_event_intel_cqm.c static inline void cqm_pick_event_reader(int cpu) · line 1702

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -1
@@ -1241,16 +1699,17 @@ static inline void cqm_pick_event_reader(int cpu)
 
 	for_each_cpu(i, &cqm_cpumask) {
 		if (phys_id == topology_physical_package_id(i))
-			return;	/* already got reader for this socket */
+			return; /* already got reader for this socket */

More random crap.

Quoted 20 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -1
 	}
 
 	cpumask_set_cpu(cpu, &cqm_cpumask);
 }
 
-static void intel_cqm_cpu_prepare(unsigned int cpu)
+static int intel_cqm_cpu_prepare(unsigned int cpu)
 {
 	struct intel_pqr_state *state = &per_cpu(pqr_state, cpu);
 	struct cpuinfo_x86 *c = &cpu_data(cpu);
+	struct mbm_pmu *pmu = per_cpu(mbm_pmu, cpu);
 
 	state->rmid = 0;
 	state->closid = 0;
@@ -1258,12 +1717,27 @@ static void intel_cqm_cpu_prepare(unsigned int cpu)
 
 	WARN_ON(c->x86_cache_max_rmid != cqm_max_rmid);
 	WARN_ON(c->x86_cache_occ_scale != cqm_l3_scale);
+
+	if ((!pmu) && (cqm_mbm)) {

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_cpu_prepare(unsigned int cpu) · line 1731

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+		pmu = kzalloc_node(sizeof(*mbm_pmu), GFP_KERNEL, NUMA_NO_NODE);
+		if (!pmu)
+			return  -ENOMEM;
+		spin_lock_init(&pmu->lock);
+		INIT_LIST_HEAD(&pmu->active_list);
+		pmu->pmu = &intel_cqm_pmu;
+		pmu->n_active = 0;
+		pmu->timer_interval = ms_to_ktime(MBM_TIME_DELTA_MAX);
+		per_cpu(mbm_pmu, cpu) = pmu;
+		per_cpu(mbm_pmu_to_free, cpu) = NULL;

So that mbm_pmu_to_free per cpu variable is NULL already and never becomes anything else than NULL. What's the purpose of setting it NULL again? And what's the purpose of it in general?

Quoted 13 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
+	}
+	return 0;
 }
 
 static void intel_cqm_cpu_exit(unsigned int cpu)
 {
 	int phys_id = topology_physical_package_id(cpu);
 	int i;
+	struct mbm_pmu *pmu = per_cpu(mbm_pmu, cpu);
 
 	/*
 	 * Is @cpu a designated cqm reader?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_cpu_exit(unsigned int cpu) · line 1762

Quoted 9 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +6 -0
@@ -1280,6 +1754,13 @@ static void intel_cqm_cpu_exit(unsigned int cpu)
 			break;
 		}
 	}
+
+	/* cancel overflow polling timer for CPU */
+	if (pmu)
+		mbm_stop_hrtimer(pmu);
+	kfree(mbm_local);
+	kfree(mbm_total);

So this frees the module global storage if one cpu is unplugged and leaves the pointer initialized so the rest of the code can happily dereference it.

Quoted 14 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -1
+
 }
 
 static int intel_cqm_cpu_notifier(struct notifier_block *nb,
@@ -1289,7 +1770,7 @@ static int intel_cqm_cpu_notifier(struct notifier_block *nb,
 
 	switch (action & ~CPU_TASKS_FROZEN) {
 	case CPU_UP_PREPARE:
-		intel_cqm_cpu_prepare(cpu);
+		return intel_cqm_cpu_prepare(cpu);
 		break;
 	case CPU_DOWN_PREPARE:
 		intel_cqm_cpu_exit(cpu);

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1789

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
@@ -1305,17 +1786,74 @@ static int intel_cqm_cpu_notifier(struct notifier_block *nb,
 static const struct x86_cpu_id intel_cqm_match[] = {
 	{ .vendor = X86_VENDOR_INTEL, .feature = X86_FEATURE_CQM_OCCUP_LLC },
 	{}
+	}, intel_mbm_match[] = {

What's wrong with having a separate

static const struct x86_cpu_id intel_mbm_match[] = {

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1792

Quoted 3 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+	{ .vendor = X86_VENDOR_INTEL, .feature = X86_FEATURE_CQM_MBM_LOCAL },
+	{}
 };

That again would make the change obvious, but that's not on your agenda.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1797

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -1
 static int __init intel_cqm_init(void)
 {
 	char *str, scale[20];
-	int i, cpu, ret;
+	int i = 0, cpu, ret;

Initializing i is required because?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1810

Quoted 13 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +9 -1
-	if (!x86_match_cpu(intel_cqm_match))
+	if (!x86_match_cpu(intel_cqm_match) &&
+	   (!x86_match_cpu(intel_mbm_match)))
 		return -ENODEV;
 
 	cqm_l3_scale = boot_cpu_data.x86_cache_occ_scale;
+	cqm_max_rmid = boot_cpu_data.x86_cache_max_rmid;
+
+	if (x86_match_cpu(intel_cqm_match)) {
+		cqm_llc_occ = true;
+		intel_cqm_events_group.attrs = intel_cmt_events_attr;
+	} else
+		cqm_llc_occ = false;

I see you do not trust the compiler to put cqm_llc_occ into the BSS section and neither the kernel to zero it out proper.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1822

Quoted 12 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +12 -0
+
+	if (x86_match_cpu(intel_mbm_match)) {
+		u32 mbm_scale_rounded = 0;
+
+		cqm_mbm = true;
+		cqm_l3_scale = boot_cpu_data.x86_cache_occ_scale;
+		/*
+		 * MBM counter values are  in bytes. To conver this to MB/sec,
+		 * we  scale the MBM scale factor by 1000. Another 1000 factor
+		 * scaling down is done
+		 * after reading the counter val i.e. in the function
+		 * __rmid_read_mbm()

So the event attribute string is in KB/sec and at some other place you convert it to MB/sec? There seems to be some hidden logic for this, but that's definitely not decodeable for me.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1824

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+		 */
+		mbm_scale_rounded =  (cqm_l3_scale + 500) / 1000;

div_round_up

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1835

Quoted 11 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +11 -0
+		cqm_max_rmid = boot_cpu_data.x86_cache_max_rmid;
+		snprintf(scale, sizeof(scale), "%u", mbm_scale_rounded);
+		str = kstrdup(scale, GFP_KERNEL);
+		if (!str) {
+			ret = -ENOMEM;
+			goto out;
+		}
+
+		if (cqm_llc_occ)
+			intel_cqm_events_group.attrs =
+				intel_cmt_mbm_events_attr;

And this line break is required because?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1840

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -0
+		else
+			intel_cqm_events_group.attrs = intel_mbm_events_attr;
+
+		event_attr_intel_cqm_llc_local_bw_scale.event_str
+		      = event_attr_intel_cqm_llc_total_bw_scale.event_str = str;

You really love to write unreadable crap. This is neither perl nor the obfuscated c-code contest. What's wrong with two separate lines which assign 'str' to each of the event attributes?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1840

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+		mbm_local = kzalloc_node(sizeof(struct sample) *
+				(cqm_max_rmid + 1) * MBM_SOCKET_MAX,

Calculating the size in a separate line with a proper variable would make it readable.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1844

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+					 GFP_KERNEL, NUMA_NO_NODE);
+		if (!mbm_local) {

Leaks str

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1852

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +8 -0
+			ret = -ENOMEM;
+			goto out;
+		}
+		mbm_total = kzalloc_node(sizeof(struct sample) *
+				(cqm_max_rmid + 1) * MBM_SOCKET_MAX,
+					 GFP_KERNEL, NUMA_NO_NODE);
+		if (!mbm_total) {
+			ret = -ENOMEM;

Leaks str and mbm_local

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1852

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+			goto out;
+		}

What about sticking that mess into a separate function?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1854

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+	} else
+		cqm_mbm = false;

Sigh.

Quoted 14 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -4
 	/*
 	 * It's possible that not all resources support the same number
@@ -1328,44 +1866,48 @@ static int __init intel_cqm_init(void)
 	 */
 	cpu_notifier_register_begin();
 
-	for_each_online_cpu(cpu) {
-		struct cpuinfo_x86 *c = &cpu_data(cpu);
+	if (cqm_llc_occ) {
+		for_each_online_cpu(cpu) {
+			struct cpuinfo_x86 *c = &cpu_data(cpu);
 
-		if (c->x86_cache_max_rmid < cqm_max_rmid)
-			cqm_max_rmid = c->x86_cache_max_rmid;

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void) · line 1878

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -4
+			if (c->x86_cache_max_rmid < cqm_max_rmid)
+				cqm_max_rmid = c->x86_cache_max_rmid;
 
-		if (c->x86_cache_occ_scale != cqm_l3_scale) {
-			pr_err("Multiple LLC scale values, disabling\n");
-			ret = -EINVAL;
-			goto out;
+			if (c->x86_cache_occ_scale != cqm_l3_scale) {
+				pr_err("Multiple LLC scale values, disabling\n");
+				ret = -EINVAL;

More memory leaks

Quoted 23 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -9
+				goto out;
+			}
 		}
-	}
 
-	/*
-	 * A reasonable upper limit on the max threshold is the number
-	 * of lines tagged per RMID if all RMIDs have the same number of
-	 * lines tagged in the LLC.
-	 *
-	 * For a 35MB LLC and 56 RMIDs, this is ~1.8% of the LLC.
-	 */
-	__intel_cqm_max_threshold =
+		/*
+		 * A reasonable upper limit on the max threshold is the number
+		 * of lines tagged per RMID if all RMIDs have the same number of
+		 * lines tagged in the LLC.
+		 *
+		 * For a 35MB LLC and 56 RMIDs, this is ~1.8% of the LLC.
+		 */
+		__intel_cqm_max_threshold =
 		boot_cpu_data.x86_cache_size * 1024 / (cqm_max_rmid + 1);
 

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void) · line 1896

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -6
-	snprintf(scale, sizeof(scale), "%u", cqm_l3_scale);
-	str = kstrdup(scale, GFP_KERNEL);
-	if (!str) {
-		ret = -ENOMEM;
-		goto out;
-	}
+		snprintf(scale, sizeof(scale), "%u", cqm_l3_scale);
+		str = kstrdup(scale, GFP_KERNEL);
+		if (!str) {
+			ret = -ENOMEM;

Ditto.

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -1
+			goto out;
+		}
 
-	event_attr_intel_cqm_llc_scale.event_str = str;
+		event_attr_intel_cqm_llc_scale.event_str = str;
+	}

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void) · line 1910

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -1
 
 	ret = intel_cqm_setup_rmid_cache();
 	if (ret)
 		goto out;
 
 	for_each_online_cpu(i) {
-		intel_cqm_cpu_prepare(i);
+		ret = intel_cqm_cpu_prepare(i);
+		if (ret)
+			goto out;

And that leaves even more half initialized stuff around.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void) · line 1912

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +0 -0
 		cqm_pick_event_reader(i);
 	}

I fear your assessment in

http://lkml.kernel.org/r/06033C969873E840BDE9FC9B584F66B58BB89D@ORSMSX116.amr.corp.intel.com

is slightly wrong.

That's a completely convoluted mess. Aside of that its broken all over the place.

This wants to be split up into preparatory patches which remodel the existing code and move out cqm stuff into helper functions, so the mbm stuff can be plugged in with its own helpers nicely.

Thanks,

tglx

#2 lore raw

Thomas,

Acknowledged. I'll update the patch and generate separate patch(s) wherever cqm changes are required. One thing was checkpatch.pl gave errors in current cqm code. I had to break lines as it complained about some line(s) exceeding 80 chars.

Regards.
-Kanaka

Quoted 1,078 lines

-----Original Message-----
From: Thomas Gleixner [mailto:[email protected]]
Sent: Tuesday, July 28, 2015 12:59 AM
To: Kanaka Juvva
Cc: Juvva, Kanaka D; Williamson, Glenn P; Fleming, Matt; Auld, Will; Andi Kleen; LKML; Luck, Tony; Peter Zijlstra; Tejun Heo; Autee, Priya V; [email protected]; [email protected]; H. Peter Anvin; Shivappa, Vikas
Subject: Re: [PATCH v2] perf,x86: add Intel Memory Bandwidth Monitoring (MBM) PMU

arch/x86/kernel/cpu/perf_event_intel_cqm.c line 15

Quoted 10 lines · Kanaka Juvva
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
index 1880761..dc1ce58 100644
@@ -12,10 +12,20 @@
 #define MSR_IA32_PQR_ASSOC	0x0c8f
 #define MSR_IA32_QM_CTR		0x0c8e
 #define MSR_IA32_QM_EVTSEL	0x0c8d
+#define MAX_MBM_CNTR            0xffffff

Can we please have a consistent name space MBM_... here?

arch/x86/kernel/cpu/perf_event_intel_cqm.c line 26

Quoted 13 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +9 -2
+#define MBM_SOCKET_MAX          8
+#define MBM_TIME_DELTA_MAX      1000
+#define MBM_TIME_DELTA_MIN      1000
+#define MBM_SCALING_FACTOR      1000
+#define MBM_SCALING_HALF        (MBM_SCALING_FACTOR/2)
+#define MBM_FIFO_SIZE_MIN       10
+#define MBM_FIFO_SIZE_MAX       300
 
-static u32 cqm_max_rmid = -1;
-static unsigned int cqm_l3_scale; /* supposedly cacheline size */
 
+static u32 cqm_max_rmid = -1;
+static unsigned int cqm_l3_scale;/* supposedly cacheline size */

Pointless white space change

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+static unsigned mbm_window_size = MBM_FIFO_SIZE_MIN; static bool
+cqm_llc_occ, cqm_mbm;
 /**
  * struct intel_pqr_state - State cache for the PQR MSR
  * @rmid:		The cached Resource Monitoring ID
@@ -42,6 +52,34 @@ struct intel_pqr_state {
  * interrupts disabled, which is sufficient for the protection.
  */

arch/x86/kernel/cpu/perf_event_intel_cqm.c struct intel_pqr_state { · line 63

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +9 -0
 static DEFINE_PER_CPU(struct intel_pqr_state, pqr_state);
+static DEFINE_PER_CPU(struct mbm_pmu *, mbm_pmu); static
+DEFINE_PER_CPU(struct mbm_pmu *, mbm_pmu_to_free);
+
+/*
+ * mbm pmu is used for storing mbm_local and mbm_total
+ * events
+ */
+struct mbm_pmu {
+	spinlock_t       lock;

What is this lock protecting and how does it nest into perf locking?

arch/x86/kernel/cpu/perf_event_intel_cqm.c struct intel_pqr_state { · line 64

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	int              n_active; /* number of active events */

If you want to document your struct members, please use kerneldoc and not these hard to read tail comments.

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+	struct list_head active_list;
+	struct pmu       *pmu; /* pointer to mbm perf_event */
+	ktime_t          timer_interval; /* in ktime_t unit */
+	struct hrtimer   hrtimer;

arch/x86/kernel/cpu/perf_event_intel_cqm.c struct intel_pqr_state { · line 78

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+};
+
+struct sample {
+	u64 bytes; /* mbm counter value read*/
+	u64 cum_avg;   /* current running average of bandwidth */
+	ktime_t prev_time;
+	u64 index; /* current sample no */
+	u32 mbmfifo[MBM_FIFO_SIZE_MAX]; /* window of last n bw values */
+	u32  fifoin; /* mbmfifo in counter for sliding window */
+	u32  fifoout; /* mbmfifo out counter for sliding window */

So above you use a proper layout which is actualy readable (aside of the tail comments). Can you please stick with a consistent coding style?

arch/x86/kernel/cpu/perf_event_intel_cqm.c struct intel_pqr_state { · line 82

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+};
+
+struct sample *mbm_total; /* curent stats for  mbm_total events */
+struct sample *mbm_local; /* current stats for mbm_local events */

static ? And please get rid of these tail comments.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static cpumask_t cqm_cpumask; · line 107

Quoted 7 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
 /*
  * Protects cache_cgroups and cqm_rmid_free_lru and cqm_rmid_limbo_lru.
@@ -66,6 +104,9 @@ static cpumask_t cqm_cpumask;
 #define RMID_VAL_UNAVAIL	(1ULL << 62)
 
 #define QOS_L3_OCCUP_EVENT_ID	(1 << 0)
+#define QOS_MBM_TOTAL_EVENT_ID  (1 << 1)

Please use tabs not spaces.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static cpumask_t cqm_cpumask; · line 108

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+#define QOS_MBM_LOCAL_EVENT_ID1 0x3

What's ID1 for heavens sake? Looks like

(QOS_L3_OCCUP_EVENT_ID | QOS_MBM_TOTAL_EVENT_ID)

So this wants a descriptive ID name and a comment.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u32 intel_cqm_rotation_rmid; · line 135

Quoted 11 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -1
+#define QOS_MBM_LOCAL_EVENT_ID  (1 << 2)
 
 #define QOS_EVENT_MASK	QOS_L3_OCCUP_EVENT_ID
 
@@ -90,7 +131,8 @@ static u32 intel_cqm_rotation_rmid;
  */
 static inline bool __rmid_valid(u32 rmid)  {
-	if (!rmid || rmid == INVALID_RMID)
+	WARN_ON_ONCE(rmid > cqm_max_rmid);
+	if (!rmid || (rmid == INVALID_RMID) || (rmid > cqm_max_rmid))
 		return false;

How is that related to this patch? Looks like a fix or prerequisite change to the existing code.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static inline struct cqm_rmid_entry

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
 	return true;
@@ -125,6 +167,7 @@ struct cqm_rmid_entry {
 	enum rmid_recycle_state state;
 	struct list_head list;
 	unsigned long queue_time;
+	bool config;
 };
 
 /*
@@ -176,6 +219,17 @@ static inline struct cqm_rmid_entry

*__rmid_entry(u32 rmid)

arch/x86/kernel/cpu/perf_event_intel_cqm.c static inline struct cqm_rmid_entry *__rmid_entry(u32 rmid) · line 225

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
 	return entry;
 }
 
+static void mbm_reset_stats(u32 rmid) {
+	u32  vrmid =  topology_physical_package_id(smp_processor_id()) *
+						   cqm_max_rmid + rmid;

Can you please explain that magic calculation? It's far from obvious why this would be correct.

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +6 -0
+
+	if ((!cqm_mbm) || (!mbm_local) || (!mbm_total))
+		return;
+	memset(&mbm_local[vrmid], 0, sizeof(struct sample));
+	memset(&mbm_total[vrmid], 0, sizeof(struct sample)); }
+
 /*
  * Returns < 0 on fail.
  *

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u32 __get_rmid(void) · line 193

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +0 -1
@@ -190,8 +244,10 @@ static u32 __get_rmid(void)
 	if (list_empty(&cqm_rmid_free_lru))
 		return INVALID_RMID;
 
-	entry = list_first_entry(&cqm_rmid_free_lru, struct cqm_rmid_entry,

list);

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u32 __get_rmid(void) · line 248

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+	entry = list_first_entry(&cqm_rmid_free_lru,
+				  struct cqm_rmid_entry, list);

And the point of this change is?

Quoted 11 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
 	list_del(&entry->list);
+	mbm_reset_stats(entry->rmid);
 
 	return entry->rmid;
 }
@@ -207,6 +263,7 @@ static void __put_rmid(u32 rmid)
 
 	entry->queue_time = jiffies;
 	entry->state = RMID_YOUNG;
+	mbm_reset_stats(rmid);
 

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void __put_rmid(u32 rmid)

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
 	list_add_tail(&entry->list, &cqm_rmid_limbo_lru);  } @@ -232,6
+289,8 @@ static int intel_cqm_setup_rmid_cache(void)
 
 		INIT_LIST_HEAD(&entry->list);
 		entry->rmid = r;
+		entry->config = false;
+
 		cqm_rmid_ptrs[r] = entry;
 
 		list_add_tail(&entry->list, &cqm_rmid_free_lru); @@ -254,6

+313,8

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void __intel_cqm_event_count(void *info); · line 466

Quoted 13 lines

@@ fail:

arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
 		kfree(cqm_rmid_ptrs[r]);
 
 	kfree(cqm_rmid_ptrs);
+	kfree(mbm_local);
+	kfree(mbm_total);
 	return -ENOMEM;
 }
@@ -403,9 +464,11 @@ static void __intel_cqm_event_count(void *info);
 static u32 intel_cqm_xchg_rmid(struct perf_event *group, u32 rmid)  {
 	struct perf_event *event;
+

Superfluous newline.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void __intel_cqm_event_count(void *info); · line 471

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
 	struct list_head *head = &group->hw.cqm_group_entry;
 	u32 old_rmid = group->hw.cqm_rmid;
 
+

Another one. Sigh!

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 560

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
 	lockdep_assert_held(&cache_mutex);
 
 	/*
@@ -494,6 +557,166 @@ static bool intel_cqm_sched_in_event(u32 rmid)
 	return false;
 }
 
+static u32  mbm_fifo_sum_lastn_out(int rmid, bool is_localbw)

Random space after u32

Quoted 17 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +17 -0
+{
+	u32 val = 0, i, j, index;
+
+	if (is_localbw) {
+		if (++mbm_local[rmid].fifoout >=  mbm_window_size)
+			mbm_local[rmid].fifoout =  0;
+		index =  mbm_local[rmid].fifoout;
+		for (i = 0; i < mbm_window_size - 1; i++) {
+			if (index + i >= mbm_window_size)
+				j = index + i - mbm_window_size;
+			else
+				j = index + i;
+			val += mbm_local[rmid].mbmfifo[j];
+		}
+		return val;
+	}
+

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 587

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+	if (++mbm_total[rmid].fifoout >=  mbm_window_size)
+		mbm_total[rmid].fifoout = 0;
+	index =  mbm_total[rmid].fifoout;
+	for (i = 0; i < mbm_window_size - 1; i++) {
+		if (index + i >= mbm_window_size)
+			j = index + i - mbm_window_size;
+		else
+			j = index + i;
+		val += mbm_total[rmid].mbmfifo[j];
+	}

The concept of helper functions to avoid code duplication is definitely applicable here.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 591

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+	return val;
+}
+
+static int  mbm_fifo_in(int rmid, u32 val, bool is_localbw)

Random space after int.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 601

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+{
+	if (is_localbw) {
+		mbm_local[rmid].mbmfifo[mbm_local[rmid].fifoin] = val;
+		if (++mbm_local[rmid].fifoin >=  mbm_window_size)
+			mbm_local[rmid].fifoin =  0;
+	} else {
+		mbm_total[rmid].mbmfifo[mbm_total[rmid].fifoin] = val;
+		if (++mbm_total[rmid].fifoin >=  mbm_window_size)
+			mbm_total[rmid].fifoin =  0;
+	}

static void mbm_fifo_in(struct sample *, u32 val)

would get rid of code duplication.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 602

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	return 0;

What's the purpose of having a return value here?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid)

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+}
+
+/*
+ * __rmid_read_mbm checks whether it is LOCAL or GLOBAL MBM event and

reads

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid)

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +6 -0
+ * its MSR counter. if (MSR current value < MSR previous value) it is an
+ * overflow and overflow is handled. If MSR is read within last 100ms,
+ * then the value is ignored; this will suppress small deltas. We don't
+ * process MBM samples that are within 100ms. Bandwidth is calculated as:
+ * bandwidth = difference of last two msr counter values/time difference.
+ * cum_avg = Running Average bandwidth of last 'n' samples that are

processed

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid)

Quoted 3 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
+ * Sliding window is used to save the last 'n' samples. Hence,
+ * n = sliding_window_size
+ * cum_avg is scaled down by a  factor MBM_SCALING_FACTOR and rounded

to nearest

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid)

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -0
+ * integer. User interface reads the BW in MB/sec.
+ * Rounding algorithm : (X + 0.5):
+ * where X is scaled BW value. To avoid floating point arithmetic :
+ * BW- unscaled value
+ * (BW + MBM_SCALING_HALF)/MBM_SCALING_FACTOR is computed which

gives the

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 621

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+ * scaled bandwidth.

This is completely unreadable.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 632

Quoted 11 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +11 -0
+ */
+static u64 __rmid_read_mbm(unsigned int rmid, bool read_mbm_local)
+{
+	u64 val, tmp, diff_time, cma, bytes, index;
+	bool overflow = false;
+	ktime_t cur_time;
+	u32 tmp32 = rmid;
+	u32 vrmid = topology_physical_package_id(smp_processor_id()) *
+						 cqm_max_rmid + rmid;
+
+	rmid = vrmid;

This is completely backwards.

	tmp32 = rmid;
	rmid = vrmid;
	do_stuff(rmid);
	rmid = tmp32;
	do_other_stuff(rmid);

Why can't you use vrmid for do_stuff() and leave rmid alone? Just because it would make the code simpler to read?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 633

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	cur_time = ktime_get_real();

Why would this operate on wall time and not on clock monotonic time?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid)

Quoted 12 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +12 -0
+	if (read_mbm_local) {
+		cma = mbm_local[rmid].cum_avg;
+		diff_time = ktime_ms_delta(cur_time,
+				   mbm_local[rmid].prev_time);
+		if (diff_time < 100)
+			return cma;
+		mbm_local[rmid].prev_time = ktime_set(0,
+				(unsigned long)ktime_to_ns(cur_time));
+		bytes = mbm_local[rmid].bytes;
+		index = mbm_local[rmid].index;
+		rmid = tmp32;
+		wrmsr(MSR_IA32_QM_EVTSEL, QOS_MBM_LOCAL_EVENT_ID1,

rmid);

Why is this using ID1?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid)

Quoted 12 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +12 -0
+	} else {
+		cma = mbm_total[rmid].cum_avg;
+		diff_time = ktime_ms_delta(cur_time,
+					   mbm_total[rmid].prev_time);
+		if (diff_time < 100)
+			return cma;
+		mbm_total[rmid].prev_time = ktime_set(0,
+					(unsigned long)ktime_to_ns(cur_time));
+		bytes = mbm_total[rmid].bytes;
+		index = mbm_total[rmid].index;
+		rmid = tmp32;
+		wrmsr(MSR_IA32_QM_EVTSEL, QOS_MBM_TOTAL_EVENT_ID,

rmid);

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 658

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	   }

Random space after tab.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 663

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -0
+	rdmsrl(MSR_IA32_QM_CTR, val);
+	if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
+		return val;
+
+	tmp = val;

You really have a faible for random storage. tmp gets assigned to mbm_*[rmid].bytes further down. This makes no sense at all.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 664

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	if (val < bytes) /* overflow handle it */ {

These tail comments are crap.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 668

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+		val = MAX_MBM_CNTR - bytes + val;
+		overflow = true;
+	} else
+		val = val - bytes;

That else clause is missing braces.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 670

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+	if (diff_time < MBM_TIME_DELTA_MAX - MBM_TIME_DELTA_MIN)
+		val =  (val * MBM_TIME_DELTA_MAX) / diff_time;

That resolves to

     	if (diff_time < 0)

Which is always false because diff_time is u64.

What's the logic behind this?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 674

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+
+	if ((diff_time > MBM_TIME_DELTA_MAX) && (!cma))
+		/* First sample, we can't use the time delta */
+		diff_time = MBM_TIME_DELTA_MAX;

And this?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 676

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+
+	rmid = vrmid;

More obfuscation

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 677

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	if ((diff_time <= MBM_TIME_DELTA_MAX + MBM_TIME_DELTA_MIN)

||

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 678

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+			overflow) {

Again, that resolves to

if (overflow)

And even if MBM_TIME_DELTA_MAX would be not equal
MBM_TIME_DELTA_MIN
then this wants some explanation about

MBM_TIME_DELTA_MAX - MBM_TIME_DELTA_MIN

versus

MBM_TIME_DELTA_MAX + MBM_TIME_DELTA_MIN

and the whole logic behind this time delta magic, if there is any.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid)

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +8 -0
+		int bw, ret;
+
+		if (index   & (index < mbm_window_size))
+			val = cma * MBM_SCALING_FACTOR  + val / index -
+					cma / index;
+
+		val = (val + MBM_SCALING_HALF) / MBM_SCALING_FACTOR;
+		if (index >= mbm_window_size) {

These conditionals along with the magic math are undocumented.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 693

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +8 -0
+			/*
+			 * Compute the sum of recent n-1 samples and slide the
+			 * window by 1
+			 */
+			ret = mbm_fifo_sum_lastn_out(rmid, read_mbm_local);
+			/* recalculate the running average with current bw */
+			ret = (ret + val) / mbm_window_size;
+			if (ret < 0)

The average of positive numbers becomes negative ?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 696

Quoted 3 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
+				ret = 0;
+			bw = val;
+			val = ret;

Your back and forth assignements of random variables is not making the code any more readable.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_sched_in_event(u32 rmid) · line 702

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +6 -0
+		} else
+			bw = val;
+		/* save the recent bw in fifo */
+		mbm_fifo_in(rmid, bw, read_mbm_local);
+
+		if (read_mbm_local) {

Oh well. You have this read_mbm_local conditional 3 times in this function. Why don't you make the function oblivious of this and hand in a pointer to mbm_local or mbm_total? Would be too simple and not enough obfuscated, right?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_rmid_stabilize(unsigned int

Quoted 21 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +17 -0
+			mbm_local[rmid].index++;
+			mbm_local[rmid].cum_avg = val;
+			mbm_local[rmid].bytes = tmp;
+			mbm_local[rmid].prev_time = ktime_set(0,
+					(unsigned long)ktime_to_ns(cur_time));
+		} else {
+			mbm_total[rmid].index++;
+			mbm_total[rmid].cum_avg = val;
+			mbm_total[rmid].bytes = tmp;
+			mbm_total[rmid].prev_time = ktime_set(0,
+					(unsigned long)ktime_to_ns(cur_time));
+		}
+		return val;
+	}
+	return  cma;
+}
+
 /*
  * Initially use this constant for both the limbo queue time and the
  * rotation timer interval, pmu::hrtimer_interval_ms.
@@ -568,7 +791,8 @@ static bool intel_cqm_rmid_stabilize(unsigned int

*available)

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_rmid_stabilize(unsigned int *available) · line 794

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -1
 	/*
 	 * Test whether an RMID is free for each package.
 	 */
-	on_each_cpu_mask(&cqm_cpumask, intel_cqm_stable, NULL, true);
+	if (entry->config)

This entry->config change wants to be a separate patch. It's completely non obvious how this changes the behaviour of the existing code.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static bool intel_cqm_rmid_stabilize(unsigned int · line 795

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+		on_each_cpu_mask(&cqm_cpumask, intel_cqm_stable, NULL,

true);

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_setup_event(struct perf_event

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +0 -0
 	list_for_each_entry_safe(entry, tmp, &cqm_rmid_limbo_lru, list) {
 		/*
@@ -846,8 +1070,9 @@ static void intel_cqm_setup_event(struct perf_event

*event,

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_setup_event(struct perf_event *event, · line 1075

Quoted 7 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -1
 				  struct perf_event **group)
 {
 	struct perf_event *iter;
-	bool conflict = false;
+
 	u32 rmid;
+	bool conflict = false;

Fits the overall picture of this patch: Sloppy

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_setup_event(struct

Quoted 3 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +0 -0
 	list_for_each_entry(iter, &cache_groups, hw.cqm_groups_entry) {
 		rmid = iter->hw.cqm_rmid;
@@ -875,6 +1100,40 @@ static void intel_cqm_setup_event(struct

perf_event *event,

Quoted 17 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +14 -0
 	event->hw.cqm_rmid = rmid;
 }
 
+static void intel_cqm_event_update(struct perf_event *event)
+{
+	unsigned int rmid;
+	u64 val = 0;
+
+	/*
+	 * Task events are handled by intel_cqm_event_count().
+	 */
+	if (event->cpu == -1)
+		return;
+
+	rmid = event->hw.cqm_rmid;
+	if (!__rmid_valid(rmid))
+		return;

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_setup_event(struct perf_event *event, · line 1126

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+
+	switch (event->attr.config) {
+	case QOS_MBM_TOTAL_EVENT_ID:
+		val = __rmid_read_mbm(rmid, false);
+		break;
+	case QOS_MBM_LOCAL_EVENT_ID:
+		val = __rmid_read_mbm(rmid, true);
+		break;
+	default:
+		break;

return?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_event_read(struct perf_event

Quoted 14 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+	}
+	/*
+	 * Ignore this reading on error states and do not update the value.
+	 */
+	if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
+		return;
+
+	local64_set(&event->count, val);
+}
+
 static void intel_cqm_event_read(struct perf_event *event)
 {
 	unsigned long flags;
@@ -887,6 +1146,12 @@ static void intel_cqm_event_read(struct perf_event

*event)

Quoted 26 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +16 -0
 	if (event->cpu == -1)
 		return;
 
+	if  ((event->attr.config & QOS_MBM_TOTAL_EVENT_ID) ||
+	     (event->attr.config & QOS_MBM_LOCAL_EVENT_ID))
+		intel_cqm_event_update(event);
+
+	if (!(event->attr.config &  QOS_L3_OCCUP_EVENT_ID))
+		return;
 	raw_spin_lock_irqsave(&cache_lock, flags);
 	rmid = event->hw.cqm_rmid;
 
@@ -906,6 +1171,28 @@ out:
 	raw_spin_unlock_irqrestore(&cache_lock, flags);
 }
 
+static void __intel_cqm_event_total_bw_count(void *info)
+{
+	struct rmid_read *rr = info;
+	u64 val;
+
+	val = __rmid_read_mbm(rr->rmid, false);
+	if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
+		return;
+	atomic64_add(val, &rr->value);
+}

arch/x86/kernel/cpu/perf_event_intel_cqm.c out: · line 1193

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+
+static void __intel_cqm_event_local_bw_count(void *info)
+{
+	struct rmid_read *rr = info;
+	u64 val;
+
+	val = __rmid_read_mbm(rr->rmid, true);
+	if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
+		return;
+	atomic64_add(val, &rr->value);

You're really a fan of copy and paste.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u64 intel_cqm_event_count(struct perf_event

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+}
+
 static void __intel_cqm_event_count(void *info)
 {
 	struct rmid_read *rr = info;
@@ -967,7 +1254,21 @@ static u64 intel_cqm_event_count(struct perf_event

*event)

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u64 intel_cqm_event_count(struct perf_event *event)

Quoted 7 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -1
 	if (!__rmid_valid(rr.rmid))
 		goto out;
 
-	on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_count, &rr, 1);
+	switch (event->attr.config) {
+	case  QOS_L3_OCCUP_EVENT_ID:
+		on_each_cpu_mask(&cqm_cpumask,

__intel_cqm_event_count, &rr, 1);

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u64 intel_cqm_event_count(struct perf_event *event)

Quoted 3 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
+		break;
+	case  QOS_MBM_TOTAL_EVENT_ID:
+		on_each_cpu_mask(&cqm_cpumask,

__intel_cqm_event_total_bw_count,

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u64 intel_cqm_event_count(struct perf_event *event)

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+				 &rr, 1);
+		break;
+	case  QOS_MBM_LOCAL_EVENT_ID:
+		on_each_cpu_mask(&cqm_cpumask,

__intel_cqm_event_local_bw_count,

arch/x86/kernel/cpu/perf_event_intel_cqm.c static u64 intel_cqm_event_count(struct perf_event *event) · line 1270

Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -0
+				 &rr, 1);
+		break;
+	default:
+		break;

So there is nothing to do and you happily proceed?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_event_start(struct

Quoted 12 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	}
 
 	raw_spin_lock_irqsave(&cache_lock, flags);
 	if (event->hw.cqm_rmid == rr.rmid)
@@ -977,6 +1278,39 @@ out:
 	return __perf_event_count(event);
 }
 
 static void intel_cqm_event_start(struct perf_event *event, int mode)
 {
 	struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
@@ -995,19 +1329,48 @@ static void intel_cqm_event_start(struct

perf_event *event, int mode)

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_event_start(struct perf_event *event, int mode)

Quoted 13 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +9 -0
 	}
 
 	state->rmid = rmid;
+	if (event->attr.config & QOS_L3_OCCUP_EVENT_ID) {
+		struct cqm_rmid_entry *entry;
+
+		entry = __rmid_entry(rmid);
+		entry->config = true;
+	}
 	wrmsr(MSR_IA32_PQR_ASSOC, rmid, state->closid);
+
+	if (((event->attr.config & QOS_MBM_TOTAL_EVENT_ID) ||
+	     (event->attr.config & QOS_MBM_LOCAL_EVENT_ID))  &&

(cqm_mbm)) {

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_event_start(struct perf_event *event, int mode) · line 1342

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+		int cpu = get_cpu();

pmu->start() is called with interrupts disabled. So why do you need get_cpu() here?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_event_start(struct perf_event *event, int mode) · line 1347

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -0
+		struct mbm_pmu *pmu = per_cpu(mbm_pmu, cpu);
+
+		if (pmu) {
+			if (pmu->n_active == 0)
+				mbm_hrtimer_init(pmu);

That pmu timer wants to be initialized when the pmu is initialized.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_event_stop(struct

Quoted 31 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +15 -1
+			pmu->n_active++;
+			list_add_tail(&event->active_entry,
+				      &pmu->active_list);
+			if (pmu->n_active == 1)
+				mbm_start_hrtimer(pmu);
+		}
+		put_cpu();
+	}
 }
 
 static void intel_cqm_event_stop(struct perf_event *event, int mode)
 {
 	struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
+	struct mbm_pmu *pmu = __this_cpu_read(mbm_pmu);
 
 	if (event->hw.cqm_state & PERF_HES_STOPPED)
 		return;
 
 	event->hw.cqm_state |= PERF_HES_STOPPED;
 
-	intel_cqm_event_read(event);
+	if (event->attr.config & QOS_L3_OCCUP_EVENT_ID)
+		intel_cqm_event_read(event);
+
+	if ((event->attr.config & QOS_MBM_TOTAL_EVENT_ID) ||
+	    (event->attr.config & QOS_MBM_LOCAL_EVENT_ID))
+		intel_cqm_event_update(event);
 
 	if (!--state->rmid_usecnt) {
 		state->rmid = 0;
@@ -1015,8 +1378,18 @@ static void intel_cqm_event_stop(struct

perf_event *event, int mode)

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
 	} else {
 		WARN_ON_ONCE(!state->rmid);
 	}
+
+	if (pmu) {

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_event_stop(struct perf_event *event, int mode) · line 1392

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +8 -0
+		WARN_ON_ONCE(pmu->n_active <= 0);
+		pmu->n_active--;
+		if (pmu->n_active == 0)
+			mbm_stop_hrtimer(pmu);
+		list_del(&event->active_entry);
+	}
+
 }
 
+

Sigh

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +7 -0
 static int intel_cqm_event_add(struct perf_event *event, int mode)
 {
 
+static ssize_t
+sliding_window_size_store(struct device *dev,
+			  struct device_attribute *attr,
+			  const char *buf, size_t count)
+{
+	unsigned int bytes;
+	int ret;

arch/x86/kernel/cpu/perf_event_intel_cqm.c max_recycle_threshold_store(struct device *dev, · line 1655

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +10 -0
+
+	ret = kstrtouint(buf, 0, &bytes);
+	if (ret)
+		return ret;
+
+	mutex_lock(&cache_mutex);
+	if (bytes > 0 && bytes <= MBM_FIFO_SIZE_MAX)
+		mbm_window_size = bytes;
+	else
+		bytes = MBM_FIFO_SIZE_MIN;

What kind of twisted logic is that?

Quoted 16 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +8 -0
+
+	mutex_unlock(&cache_mutex);
+
+	return count;
+}
+
 static DEVICE_ATTR_RW(max_recycle_threshold);
+static DEVICE_ATTR_RW(sliding_window_size);
 
 static struct attribute *intel_cqm_attrs[] = {
 	&dev_attr_max_recycle_threshold.attr,
+	&dev_attr_sliding_window_size.attr,
 	NULL,
 };

arch/x86/kernel/cpu/perf_event_intel_cqm.c static inline void cqm_pick_event_reader(int cpu) · line 1702

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -1
@@ -1241,16 +1699,17 @@ static inline void cqm_pick_event_reader(int cpu)
 
 	for_each_cpu(i, &cqm_cpumask) {
 		if (phys_id == topology_physical_package_id(i))
-			return;	/* already got reader for this socket */
+			return; /* already got reader for this socket */

More random crap.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_cpu_prepare(unsigned int

Quoted 15 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -1
 	}
 
 	cpumask_set_cpu(cpu, &cqm_cpumask);
 }
 
-static void intel_cqm_cpu_prepare(unsigned int cpu)
+static int intel_cqm_cpu_prepare(unsigned int cpu)
 {
 	struct intel_pqr_state *state = &per_cpu(pqr_state, cpu);
 	struct cpuinfo_x86 *c = &cpu_data(cpu);
+	struct mbm_pmu *pmu = per_cpu(mbm_pmu, cpu);
 
 	state->rmid = 0;
 	state->closid = 0;
@@ -1258,12 +1717,27 @@ static void intel_cqm_cpu_prepare(unsigned int

cpu)

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_cpu_prepare(unsigned int cpu)

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
 	WARN_ON(c->x86_cache_max_rmid != cqm_max_rmid);
 	WARN_ON(c->x86_cache_occ_scale != cqm_l3_scale);
+
+	if ((!pmu) && (cqm_mbm)) {
+		pmu = kzalloc_node(sizeof(*mbm_pmu), GFP_KERNEL,

NUMA_NO_NODE);

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_cpu_prepare(unsigned int cpu) · line 1731

Quoted 9 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +9 -0
+		if (!pmu)
+			return  -ENOMEM;
+		spin_lock_init(&pmu->lock);
+		INIT_LIST_HEAD(&pmu->active_list);
+		pmu->pmu = &intel_cqm_pmu;
+		pmu->n_active = 0;
+		pmu->timer_interval = ms_to_ktime(MBM_TIME_DELTA_MAX);
+		per_cpu(mbm_pmu, cpu) = pmu;
+		per_cpu(mbm_pmu_to_free, cpu) = NULL;

So that mbm_pmu_to_free per cpu variable is NULL already and never becomes anything else than NULL. What's the purpose of setting it NULL again? And what's the purpose of it in general?

Quoted 13 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
+	}
+	return 0;
 }
 
 static void intel_cqm_cpu_exit(unsigned int cpu)
 {
 	int phys_id = topology_physical_package_id(cpu);
 	int i;
+	struct mbm_pmu *pmu = per_cpu(mbm_pmu, cpu);
 
 	/*
 	 * Is @cpu a designated cqm reader?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static void intel_cqm_cpu_exit(unsigned int cpu) · line 1762

Quoted 9 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +6 -0
@@ -1280,6 +1754,13 @@ static void intel_cqm_cpu_exit(unsigned int cpu)
 			break;
 		}
 	}
+
+	/* cancel overflow polling timer for CPU */
+	if (pmu)
+		mbm_stop_hrtimer(pmu);
+	kfree(mbm_local);
+	kfree(mbm_total);

So this frees the module global storage if one cpu is unplugged and leaves the pointer initialized so the rest of the code can happily dereference it.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+
 }
 
 static int intel_cqm_cpu_notifier(struct notifier_block *nb,
@@ -1289,7 +1770,7 @@ static int intel_cqm_cpu_notifier(struct

notifier_block *nb,

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct

Quoted 9 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -1
 	switch (action & ~CPU_TASKS_FROZEN) {
 	case CPU_UP_PREPARE:
-		intel_cqm_cpu_prepare(cpu);
+		return intel_cqm_cpu_prepare(cpu);
 		break;
 	case CPU_DOWN_PREPARE:
 		intel_cqm_cpu_exit(cpu);
@@ -1305,17 +1786,74 @@ static int intel_cqm_cpu_notifier(struct

notifier_block *nb,

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct · line 1787

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +0 -0
 static const struct x86_cpu_id intel_cqm_match[] = {
 	{ .vendor = X86_VENDOR_INTEL, .feature =

X86_FEATURE_CQM_OCCUP_LLC },

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1789

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
 	{}
+	}, intel_mbm_match[] = {

What's wrong with having a separate

static const struct x86_cpu_id intel_mbm_match[] = {

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct · line 1790

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	{ .vendor = X86_VENDOR_INTEL, .feature =

X86_FEATURE_CQM_MBM_LOCAL },

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1792

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -0
+	{}
 };

That again would make the change obvious, but that's not on your agenda.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1797

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +1 -1
 static int __init intel_cqm_init(void)
 {
 	char *str, scale[20];
-	int i, cpu, ret;
+	int i = 0, cpu, ret;

Initializing i is required because?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1810

Quoted 13 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +9 -1
-	if (!x86_match_cpu(intel_cqm_match))
+	if (!x86_match_cpu(intel_cqm_match) &&
+	   (!x86_match_cpu(intel_mbm_match)))
 		return -ENODEV;
 
 	cqm_l3_scale = boot_cpu_data.x86_cache_occ_scale;
+	cqm_max_rmid = boot_cpu_data.x86_cache_max_rmid;
+
+	if (x86_match_cpu(intel_cqm_match)) {
+		cqm_llc_occ = true;
+		intel_cqm_events_group.attrs = intel_cmt_events_attr;
+	} else
+		cqm_llc_occ = false;

I see you do not trust the compiler to put cqm_llc_occ into the BSS section and neither the kernel to zero it out proper.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1822

Quoted 12 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +12 -0
+
+	if (x86_match_cpu(intel_mbm_match)) {
+		u32 mbm_scale_rounded = 0;
+
+		cqm_mbm = true;
+		cqm_l3_scale = boot_cpu_data.x86_cache_occ_scale;
+		/*
+		 * MBM counter values are  in bytes. To conver this to MB/sec,
+		 * we  scale the MBM scale factor by 1000. Another 1000 factor
+		 * scaling down is done
+		 * after reading the counter val i.e. in the function
+		 * __rmid_read_mbm()

So the event attribute string is in KB/sec and at some other place you convert it to MB/sec? There seems to be some hidden logic for this, but that's definitely not decodeable for me.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1824

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+		 */
+		mbm_scale_rounded =  (cqm_l3_scale + 500) / 1000;

div_round_up

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1835

Quoted 11 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +11 -0
+		cqm_max_rmid = boot_cpu_data.x86_cache_max_rmid;
+		snprintf(scale, sizeof(scale), "%u", mbm_scale_rounded);
+		str = kstrdup(scale, GFP_KERNEL);
+		if (!str) {
+			ret = -ENOMEM;
+			goto out;
+		}
+
+		if (cqm_llc_occ)
+			intel_cqm_events_group.attrs =
+				intel_cmt_mbm_events_attr;

And this line break is required because?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct · line 1835

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+		else
+			intel_cqm_events_group.attrs =

intel_mbm_events_attr;

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1840

Quoted 3 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -0
+
+		event_attr_intel_cqm_llc_local_bw_scale.event_str
+		      = event_attr_intel_cqm_llc_total_bw_scale.event_str = str;

You really love to write unreadable crap. This is neither perl nor the obfuscated c-code contest. What's wrong with two separate lines which assign 'str' to each of the event attributes?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct · line 1840

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+		mbm_local = kzalloc_node(sizeof(struct sample) *
+				(cqm_max_rmid + 1) * MBM_SOCKET_MAX,

Calculating the size in a separate line with a proper variable would make it readable.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1844

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+					 GFP_KERNEL, NUMA_NO_NODE);
+		if (!mbm_local) {

Leaks str

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct notifier_block *nb, · line 1852

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +8 -0
+			ret = -ENOMEM;
+			goto out;
+		}
+		mbm_total = kzalloc_node(sizeof(struct sample) *
+				(cqm_max_rmid + 1) * MBM_SOCKET_MAX,
+					 GFP_KERNEL, NUMA_NO_NODE);
+		if (!mbm_total) {
+			ret = -ENOMEM;

Leaks str and mbm_local

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct · line 1852

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+			goto out;
+		}

What about sticking that mess into a separate function?

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int intel_cqm_cpu_notifier(struct · line 1854

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -0
+	} else
+		cqm_mbm = false;

Sigh.

Quoted 14 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -4
 	/*
 	 * It's possible that not all resources support the same number
@@ -1328,44 +1866,48 @@ static int __init intel_cqm_init(void)
 	 */
 	cpu_notifier_register_begin();
 
-	for_each_online_cpu(cpu) {
-		struct cpuinfo_x86 *c = &cpu_data(cpu);
+	if (cqm_llc_occ) {
+		for_each_online_cpu(cpu) {
+			struct cpuinfo_x86 *c = &cpu_data(cpu);
 
-		if (c->x86_cache_max_rmid < cqm_max_rmid)
-			cqm_max_rmid = c->x86_cache_max_rmid;

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void) · line 1878

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -4
+			if (c->x86_cache_max_rmid < cqm_max_rmid)
+				cqm_max_rmid = c->x86_cache_max_rmid;
 
-		if (c->x86_cache_occ_scale != cqm_l3_scale) {
-			pr_err("Multiple LLC scale values, disabling\n");
-			ret = -EINVAL;
-			goto out;
+			if (c->x86_cache_occ_scale != cqm_l3_scale) {
+				pr_err("Multiple LLC scale values, disabling\n");
+				ret = -EINVAL;

More memory leaks

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +2 -2
+				goto out;
+			}
 		}
-	}
 
-	/*

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void)

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -7
-	 * A reasonable upper limit on the max threshold is the number
-	 * of lines tagged per RMID if all RMIDs have the same number of
-	 * lines tagged in the LLC.
-	 *
-	 * For a 35MB LLC and 56 RMIDs, this is ~1.8% of the LLC.
-	 */
-	__intel_cqm_max_threshold =
+		/*
+		 * A reasonable upper limit on the max threshold is the number
+		 * of lines tagged per RMID if all RMIDs have the same number

of

Quoted 7 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +5 -0
+		 * lines tagged in the LLC.
+		 *
+		 * For a 35MB LLC and 56 RMIDs, this is ~1.8% of the LLC.
+		 */
+		__intel_cqm_max_threshold =
 		boot_cpu_data.x86_cache_size * 1024 / (cqm_max_rmid + 1);
 

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void) · line 1896

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -6
-	snprintf(scale, sizeof(scale), "%u", cqm_l3_scale);
-	str = kstrdup(scale, GFP_KERNEL);
-	if (!str) {
-		ret = -ENOMEM;
-		goto out;
-	}
+		snprintf(scale, sizeof(scale), "%u", cqm_l3_scale);
+		str = kstrdup(scale, GFP_KERNEL);
+		if (!str) {
+			ret = -ENOMEM;

Ditto.

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +4 -1
+			goto out;
+		}
 
-	event_attr_intel_cqm_llc_scale.event_str = str;
+		event_attr_intel_cqm_llc_scale.event_str = str;
+	}

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void) · line 1910

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +3 -1
 
 	ret = intel_cqm_setup_rmid_cache();
 	if (ret)
 		goto out;
 
 	for_each_online_cpu(i) {
-		intel_cqm_cpu_prepare(i);
+		ret = intel_cqm_cpu_prepare(i);
+		if (ret)
+			goto out;

And that leaves even more half initialized stuff around.

arch/x86/kernel/cpu/perf_event_intel_cqm.c static int __init intel_cqm_init(void) · line 1912

Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_cqm.c +0 -0
 		cqm_pick_event_reader(i);
 	}

I fear your assessment in

http://lkml.kernel.org/r/06033C969873E840BDE9FC9B584F66B58BB89D@ORS
MSX116.amr.corp.intel.com

is slightly wrong.

That's a completely convoluted mess. Aside of that its broken all over the place.

This wants to be split up into preparatory patches which remodel the existing code and move out cqm stuff into helper functions, so the mbm stuff can be plugged in with its own helpers nicely.

Thanks,

tglx