Blame SOURCES/0006-Handle-thermal-events-to-mask-CPUs.patch

7bf5db
From 560291389e33db108d2fb6d954ef059f953c6e33 Mon Sep 17 00:00:00 2001
7bf5db
From: "Chang S. Bae" <chang.seok.bae@intel.com>
7bf5db
Date: Fri, 11 Feb 2022 13:48:01 -0800
7bf5db
Subject: [PATCH 06/14] Handle thermal events to mask CPUs
7bf5db
7bf5db
The Hardware Feedback Interface event is delivered as the Netlink's
7bf5db
THERMAL_GENL_ATTR_CPU_CAPABILITY attribute. Add code to receive and parse
7bf5db
these attributes: logical CPU number, performance, and efficiency
7bf5db
enumeration values (0-1023).
7bf5db
7bf5db
When an event indicates CPU performance and efficiency are zeros, then the
7bf5db
CPU needs to be banned from IRQs. Rebuild object tree to make this banned
7bf5db
list effective.
7bf5db
7bf5db
Creating the banned CPU list here is a bit tricky here because the amount
7bf5db
of data that each Netlink notification can carry out is not enough in some
7bf5db
systems with many cores. This means the handler has to wait for multiple
7bf5db
notifications to determine banned CPUs per thermal event.
7bf5db
7bf5db
Establish a logic to maintain the list based on the kernel behaviors. Also,
7bf5db
always push the current list as of the banned CPUs, because there is no
7bf5db
clear line whether each notification is at the end of a thermal event or
7bf5db
not.
7bf5db
7bf5db
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
7bf5db
---
7bf5db
 thermal.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++---
7bf5db
 1 file changed, 165 insertions(+), 7 deletions(-)
7bf5db
7bf5db
diff --git a/thermal.c b/thermal.c
7bf5db
index 16a6f39..64a9cdf 100644
7bf5db
--- a/thermal.c
7bf5db
+++ b/thermal.c
7bf5db
@@ -6,6 +6,7 @@
7bf5db
 
7bf5db
 #include <stdio.h>
7bf5db
 
7bf5db
+#include <sys/sysinfo.h>
7bf5db
 #include <netlink/genl/genl.h>
7bf5db
 #include <netlink/genl/family.h>
7bf5db
 #include <netlink/genl/ctrl.h>
7bf5db
@@ -14,8 +15,62 @@
7bf5db
 
7bf5db
 cpumask_t thermal_banned_cpus;
7bf5db
 
7bf5db
-#define INVALID_NL_FD	-1
7bf5db
-#define MAX_RECV_ERRS	2
7bf5db
+/* Events of thermal_genl_family */
7bf5db
+enum thermal_genl_event {
7bf5db
+	THERMAL_GENL_EVENT_UNSPEC,
7bf5db
+	THERMAL_GENL_EVENT_TZ_CREATE,		/* Thermal zone creation */
7bf5db
+	THERMAL_GENL_EVENT_TZ_DELETE,		/* Thermal zone deletion */
7bf5db
+	THERMAL_GENL_EVENT_TZ_DISABLE,		/* Thermal zone disabled */
7bf5db
+	THERMAL_GENL_EVENT_TZ_ENABLE,		/* Thermal zone enabled */
7bf5db
+	THERMAL_GENL_EVENT_TZ_TRIP_UP,		/* Trip point crossed the way up */
7bf5db
+	THERMAL_GENL_EVENT_TZ_TRIP_DOWN,	/* Trip point crossed the way down */
7bf5db
+	THERMAL_GENL_EVENT_TZ_TRIP_CHANGE,	/* Trip point changed */
7bf5db
+	THERMAL_GENL_EVENT_TZ_TRIP_ADD,		/* Trip point added */
7bf5db
+	THERMAL_GENL_EVENT_TZ_TRIP_DELETE,	/* Trip point deleted */
7bf5db
+	THERMAL_GENL_EVENT_CDEV_ADD,		/* Cdev bound to the thermal zone */
7bf5db
+	THERMAL_GENL_EVENT_CDEV_DELETE,		/* Cdev unbound */
7bf5db
+	THERMAL_GENL_EVENT_CDEV_STATE_UPDATE,	/* Cdev state updated */
7bf5db
+	THERMAL_GENL_EVENT_TZ_GOV_CHANGE,	/* Governor policy changed  */
7bf5db
+	THERMAL_GENL_EVENT_CAPACITY_CHANGE,	/* CPU capacity changed */
7bf5db
+	__THERMAL_GENL_EVENT_MAX,
7bf5db
+};
7bf5db
+#define THERMAL_GENL_EVENT_MAX (__THERMAL_GENL_EVENT_MAX - 1)
7bf5db
+
7bf5db
+/* Attributes of thermal_genl_family */
7bf5db
+enum thermal_genl_attr {
7bf5db
+	THERMAL_GENL_ATTR_UNSPEC,
7bf5db
+	THERMAL_GENL_ATTR_TZ,
7bf5db
+	THERMAL_GENL_ATTR_TZ_ID,
7bf5db
+	THERMAL_GENL_ATTR_TZ_TEMP,
7bf5db
+	THERMAL_GENL_ATTR_TZ_TRIP,
7bf5db
+	THERMAL_GENL_ATTR_TZ_TRIP_ID,
7bf5db
+	THERMAL_GENL_ATTR_TZ_TRIP_TYPE,
7bf5db
+	THERMAL_GENL_ATTR_TZ_TRIP_TEMP,
7bf5db
+	THERMAL_GENL_ATTR_TZ_TRIP_HYST,
7bf5db
+	THERMAL_GENL_ATTR_TZ_MODE,
7bf5db
+	THERMAL_GENL_ATTR_TZ_NAME,
7bf5db
+	THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT,
7bf5db
+	THERMAL_GENL_ATTR_TZ_GOV,
7bf5db
+	THERMAL_GENL_ATTR_TZ_GOV_NAME,
7bf5db
+	THERMAL_GENL_ATTR_CDEV,
7bf5db
+	THERMAL_GENL_ATTR_CDEV_ID,
7bf5db
+	THERMAL_GENL_ATTR_CDEV_CUR_STATE,
7bf5db
+	THERMAL_GENL_ATTR_CDEV_MAX_STATE,
7bf5db
+	THERMAL_GENL_ATTR_CDEV_NAME,
7bf5db
+	THERMAL_GENL_ATTR_GOV_NAME,
7bf5db
+	THERMAL_GENL_ATTR_CAPACITY,
7bf5db
+	THERMAL_GENL_ATTR_CAPACITY_CPU_COUNT,
7bf5db
+	THERMAL_GENL_ATTR_CAPACITY_CPU_ID,
7bf5db
+	THERMAL_GENL_ATTR_CAPACITY_CPU_PERF,
7bf5db
+	THERMAL_GENL_ATTR_CAPACITY_CPU_EFF,
7bf5db
+	__THERMAL_GENL_ATTR_MAX,
7bf5db
+};
7bf5db
+#define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
7bf5db
+
7bf5db
+#define INVALID_NL_FD		-1
7bf5db
+#define MAX_RECV_ERRS		2
7bf5db
+#define SYSCONF_ERR		-1
7bf5db
+#define INVALID_EVENT_VALUE	-1
7bf5db
 
7bf5db
 #define THERMAL_GENL_FAMILY_NAME	"thermal"
7bf5db
 #define THERMAL_GENL_EVENT_GROUP_NAME	"event"
7bf5db
@@ -254,17 +309,115 @@ err_out:
7bf5db
 	return error;
7bf5db
 }
7bf5db
 
7bf5db
-static int handle_thermal_event(struct nl_msg *msg __attribute__((unused)),
7bf5db
-				void *arg __attribute__((unused)))
7bf5db
+enum {
7bf5db
+	INDEX_CPUNUM,
7bf5db
+	INDEX_PERF,
7bf5db
+	INDEX_EFFI,
7bf5db
+	INDEX_MAX
7bf5db
+};
7bf5db
+
7bf5db
+/*
7bf5db
+ * Single netlink notification is not guaranteed to fully deliver all of
7bf5db
+ * the CPU updates per thermal event, due to the implementation choice in
7bf5db
+ * the kernel code. So, this function is intended to manage a CPU list in a
7bf5db
+ * stream of relevant notifications.
7bf5db
+ */
7bf5db
+static void update_banned_cpus(int cur_cpuidx, gboolean need_to_ban)
7bf5db
 {
7bf5db
-	log(TO_ALL, LOG_ERR, "thermal: not yet implemented to process thermal event.\n");
7bf5db
-	return NL_SKIP;
7bf5db
+	static cpumask_t banmask = { 0 }, itrmask = { 0 };
7bf5db
+	long max_cpunum = sysconf(_SC_NPROCESSORS_ONLN);
7bf5db
+
7bf5db
+	if (need_to_ban)
7bf5db
+		cpu_set(cur_cpuidx, banmask);
7bf5db
+
7bf5db
+	cpu_set(cur_cpuidx, itrmask);
7bf5db
+	if (cpus_weight(itrmask) < max_cpunum)
7bf5db
+		return;
7bf5db
+
7bf5db
+	if (cpus_equal(thermal_banned_cpus, banmask))
7bf5db
+		goto out;
7bf5db
+
7bf5db
+	cpus_copy(thermal_banned_cpus, banmask);
7bf5db
+	need_rescan = 1;
7bf5db
+out:
7bf5db
+	cpus_clear(banmask);
7bf5db
+	cpus_clear(itrmask);
7bf5db
+}
7bf5db
+
7bf5db
+static int handle_thermal_event(struct nl_msg *msg, void *arg __attribute__((unused)))
7bf5db
+{
7bf5db
+	int event_data[INDEX_MAX] = { INVALID_EVENT_VALUE };
7bf5db
+	struct nlattr *attrs[THERMAL_GENL_ATTR_MAX + 1];
7bf5db
+	struct genlmsghdr *genlhdr;
7bf5db
+	struct nlmsghdr *msnlh;
7bf5db
+	struct nlattr *cap;
7bf5db
+	int i, remain, rc;
7bf5db
+	void *pos;
7bf5db
+
7bf5db
+	/* get actual netlink message header */
7bf5db
+	msnlh = nlmsg_hdr(msg);
7bf5db
+
7bf5db
+	/* get a pointer to generic netlink header */
7bf5db
+	genlhdr = genlmsg_hdr(msnlh);
7bf5db
+	if (genlhdr->cmd != THERMAL_GENL_EVENT_CAPACITY_CHANGE) {
7bf5db
+		log(TO_ALL, LOG_DEBUG, "thermal: no CPU capacity change.\n");
7bf5db
+		return NL_SKIP;
7bf5db
+	}
7bf5db
+
7bf5db
+	/* parse generic netlink message including attributes */
7bf5db
+	rc = genlmsg_parse(
7bf5db
+		msnlh,			/* a pointer to netlink message header */
7bf5db
+		0,			/* length of user header */
7bf5db
+		attrs,			/* array to store parsed attributes */
7bf5db
+		THERMAL_GENL_ATTR_MAX,	/* maximum attribute id as expected */
7bf5db
+		NULL);			/* validation policy */
7bf5db
+	if (rc) {
7bf5db
+		log(TO_ALL, LOG_ERR, "thermal: failed to parse message for thermal event.\n");
7bf5db
+		return NL_SKIP;
7bf5db
+	}
7bf5db
+
7bf5db
+	/* get start and length of payload section */
7bf5db
+	cap = attrs[THERMAL_GENL_ATTR_CAPACITY];
7bf5db
+	pos = nla_data(cap);
7bf5db
+	remain = nla_len(cap);
7bf5db
+
7bf5db
+	for (i = 0; nla_ok(pos, remain); pos = nla_next(pos, &remain), i++) {
7bf5db
+		gboolean valid_event = TRUE, need_to_ban;
7bf5db
+		unsigned int value = nla_get_u32(pos);
7bf5db
+		int idx = i % INDEX_MAX;
7bf5db
+		int cur_cpuidx;
7bf5db
+
7bf5db
+		event_data[idx] = value;
7bf5db
+
7bf5db
+		if (idx != INDEX_EFFI)
7bf5db
+			continue;
7bf5db
+
7bf5db
+		cur_cpuidx = event_data[INDEX_CPUNUM];
7bf5db
+		valid_event = !!(cur_cpuidx <= NR_CPUS);
7bf5db
+		if (!valid_event) {
7bf5db
+			log(TO_ALL, LOG_WARNING, "thermal: invalid event - CPU %d\n",
7bf5db
+			    cur_cpuidx);
7bf5db
+			continue;
7bf5db
+		}
7bf5db
+
7bf5db
+		/*
7bf5db
+		 * A CPU with no performance and no efficiency cannot
7bf5db
+		 * handle IRQs:
7bf5db
+		 */
7bf5db
+		need_to_ban = !!(!event_data[INDEX_PERF] && !event_data[INDEX_EFFI]);
7bf5db
+		update_banned_cpus(cur_cpuidx, need_to_ban);
7bf5db
+
7bf5db
+		log(TO_ALL, LOG_DEBUG, "thermal: event - CPU %d, efficiency %d, perf %d.\n",
7bf5db
+		    cur_cpuidx, event_data[INDEX_PERF], event_data[INDEX_EFFI]);
7bf5db
+	}
7bf5db
+
7bf5db
+	return NL_OK;
7bf5db
 }
7bf5db
 
7bf5db
 static int handler_for_debug(struct nl_msg *msg __attribute__((unused)),
7bf5db
 			     void *arg __attribute__((unused)))
7bf5db
 {
7bf5db
-	return NL_SKIP;
7bf5db
+	return NL_OK;
7bf5db
 }
7bf5db
 
7bf5db
 /*
7bf5db
@@ -274,6 +427,11 @@ static gboolean register_netlink_handler(void)
7bf5db
 {
7bf5db
 	int rc;
7bf5db
 
7bf5db
+	if (sysconf(_SC_NPROCESSORS_ONLN) == SYSCONF_ERR) {
7bf5db
+		log(TO_ALL, LOG_ERR, "thermal: _SC_NPROCESSORS_ONLN not available.\n");
7bf5db
+		return TRUE;
7bf5db
+	}
7bf5db
+
7bf5db
 	rc = nl_cb_set(callback, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, handler_for_debug, NULL);
7bf5db
 	if (rc) {
7bf5db
 		log(TO_ALL, LOG_ERR, "thermal: debug handler registration failed.\n");
7bf5db
-- 
7bf5db
2.33.1
7bf5db