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

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