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

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