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

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