linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/11] PM: EM: Add netlink support for the energy model.
@ 2025-05-29  0:13 Changwoo Min
  2025-05-29  0:13 ` [PATCH 01/11] PM: EM: Add ENERGY_MODEL_NETLINK Kconfig Changwoo Min
                   ` (11 more replies)
  0 siblings, 12 replies; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min, Rafael J. Wysocki

There is a need to access the energy model from the userspace. One such
example is the sched_ext schedulers [1]. The userspace part of the
sched_ext schedules could feed the (post-processed) energy-model
information to the BPF part of the scheduler.

Currently, debugfs is the only way to read the energy model from userspace;
however, it lacks proper notification mechanisms when a performance domain
and its associated energy model change.

This patch set introduces a generic netlink for the energy model, as
discussed in [2]. It allows a userspace program to read the performance
domain and its energy model. It notifies the userspace program when a
performance domain is created or deleted or its energy model is updated
through a multicast interface.

Specifically, it supports two commands:
  - EM_GENL_CMD_PD_GET_ID: Get the list of information for all performance domains.
  - EM_GENL_CMD_PD_GET_TBL: Get the energy model table of a performance domain.

Also, it supports three notification events:
  - EM_GENL_EVENT_PD_CREATE: When a performance domain is created.
  - EM_GENL_EVENT_PD_DELETE: When a performance domain is deleted.
  - EM_GENL_EVENT_PD_UPDATE: When the energy model table of a performance domain is updated.

The userspace code example using the EM netlink interface is at [3].

[1] https://lwn.net/Articles/922405/
[2] https://lore.kernel.org/lkml/a82423bc-8c38-4d57-93da-c4f20011cc92@arm.com/
[3] https://github.com/multics69/em-netlink/tree/patch-v1

CC: Lukasz Luba <lukasz.luba@arm.com>
CC: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
CC: Tejun Heo <tj@kernel.org>
Signed-off-by: Changwoo Min <changwoo@igalia.com>

Changwoo Min (11):
  PM: EM: Add ENERGY_MODEL_NETLINK Kconfig.
  PM: EM: Add a skeleton code for netlink notification.
  PM: EM: Initialize the netlink notification during booting.
  PM: EM: Add the infrastructure for command processing.
  PM: EM: Assign a unique ID when creating a performance domain.
  PM: EM: Expose the ID of a performance domain via debugfs.
  PM: EM: Add an iterator and accessor for the performance domain.
  PM: EM: Implement EM_GENL_CMD_PD_GET_ID.
  PM: EM: Implement EM_GENL_CMD_PD_GET_TBL.
  PM: EM: Implement event notification.
  PM: EM: Notify an event when the performance domain changes.

 include/linux/energy_model.h      |  20 +-
 include/uapi/linux/energy_model.h |  89 ++++++++
 kernel/power/Kconfig              |  10 +
 kernel/power/Makefile             |   1 +
 kernel/power/em_netlink.c         | 324 ++++++++++++++++++++++++++++++
 kernel/power/em_netlink.h         |  45 +++++
 kernel/power/energy_model.c       |  99 ++++++++-
 7 files changed, 586 insertions(+), 2 deletions(-)
 create mode 100644 include/uapi/linux/energy_model.h
 create mode 100644 kernel/power/em_netlink.c
 create mode 100644 kernel/power/em_netlink.h

-- 
2.49.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH 01/11] PM: EM: Add ENERGY_MODEL_NETLINK Kconfig.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-05-29  0:13 ` [PATCH 02/11] PM: EM: Add a skeleton code for netlink notification Changwoo Min
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

Define a Kconfig, ENERGY_MODEL_NETLINK. That enables the netlink interface,
which allows a userspace program to read the energy model and receive
multicast events upon changes to the energy model.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 kernel/power/Kconfig | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/kernel/power/Kconfig b/kernel/power/Kconfig
index 54a623680019..b45933370b4e 100644
--- a/kernel/power/Kconfig
+++ b/kernel/power/Kconfig
@@ -390,3 +390,13 @@ config ENERGY_MODEL
 	  The exact usage of the energy model is subsystem-dependent.
 
 	  If in doubt, say N.
+
+if ENERGY_MODEL
+
+config ENERGY_MODEL_NETLINK
+	bool "Energy Model netlink management"
+	depends on NET
+	help
+	  The energy model framework has a netlink interface to notify the
+	  changes in the energy model. It is recommended to enable the feature.
+endif
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 02/11] PM: EM: Add a skeleton code for netlink notification.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
  2025-05-29  0:13 ` [PATCH 01/11] PM: EM: Add ENERGY_MODEL_NETLINK Kconfig Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-06-02 19:53   ` Lukas Wunner
  2025-05-29  0:13 ` [PATCH 03/11] PM: EM: Initialize the netlink notification during booting Changwoo Min
                   ` (9 subsequent siblings)
  11 siblings, 1 reply; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

Add a boilerplate code for netlink notification to register and unregister
the new protocol family. It defines the supported commands and event types
and adds the minimalistic code for the protocol family registration.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 include/uapi/linux/energy_model.h | 40 ++++++++++++++++++
 kernel/power/Makefile             |  1 +
 kernel/power/em_netlink.c         | 69 +++++++++++++++++++++++++++++++
 kernel/power/em_netlink.h         | 26 ++++++++++++
 4 files changed, 136 insertions(+)
 create mode 100644 include/uapi/linux/energy_model.h
 create mode 100644 kernel/power/em_netlink.c
 create mode 100644 kernel/power/em_netlink.h

diff --git a/include/uapi/linux/energy_model.h b/include/uapi/linux/energy_model.h
new file mode 100644
index 000000000000..42a19e614c7d
--- /dev/null
+++ b/include/uapi/linux/energy_model.h
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_LINUX_ENERGY_MODEL_H
+#define _UAPI_LINUX_ENERGY_MODEL_H
+
+/* Adding event notification support elements */
+#define EM_GENL_FAMILY_NAME		"energy_model"
+#define EM_GENL_VERSION			0x01
+#define EM_GENL_EVENT_GROUP_NAME	"event"
+
+/* Attributes of em_genl_family */
+enum em_genl_attr {
+	EM_GENL_ATTR_UNSPEC,
+	__EM_GENL_ATTR_MAX,
+};
+#define EM_GENL_ATTR_MAX (__EM_GENL_ATTR_MAX - 1)
+
+/* Events of em_genl_family */
+enum em_genl_event {
+	EM_GENL_EVENT_UNSPEC,
+	EM_GENL_EVENT_PD_CREATE,	/* Performance domain creation */
+	EM_GENL_EVENT_PD_DELETE,	/* Performance domain deletion */
+	EM_GENL_EVENT_PD_UPDATE,	/* The runtime EM table for the
+					   performance domain is updated */
+	__EM_GENL_EVENT_MAX,
+};
+#define EM_GENL_EVENT_MAX (__EM_GENL_EVENT_MAX - 1)
+
+/* Commands supported by the em_genl_family */
+enum em_genl_cmd {
+	EM_GENL_CMD_UNSPEC,
+	EM_GENL_CMD_PD_GET_ID,		/* Get the list of information
+					   for all performance domains */
+	EM_GENL_CMD_PD_GET_TBL,		/* Get the energy model table
+					   of a performance domain */
+	__EM_GENL_CMD_MAX,
+};
+#define EM_GENL_CMD_MAX (__EM_GENL_CMD_MAX - 1)
+
+
+#endif /* _UAPI_LINUX_ENERGY_MODEL_H */
diff --git a/kernel/power/Makefile b/kernel/power/Makefile
index 874ad834dc8d..6bf157b5fffd 100644
--- a/kernel/power/Makefile
+++ b/kernel/power/Makefile
@@ -22,3 +22,4 @@ obj-$(CONFIG_PM_WAKELOCKS)	+= wakelock.o
 obj-$(CONFIG_MAGIC_SYSRQ)	+= poweroff.o
 
 obj-$(CONFIG_ENERGY_MODEL)	+= energy_model.o
+obj-$(CONFIG_ENERGY_MODEL_NETLINK) += em_netlink.o
diff --git a/kernel/power/em_netlink.c b/kernel/power/em_netlink.c
new file mode 100644
index 000000000000..30d83fb5a3a8
--- /dev/null
+++ b/kernel/power/em_netlink.c
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ *
+ * Generic netlink for energy model.
+ *
+ * Copyright (c) 2025 Valve Corporation.
+ * Author: Changwoo Min <changwoo@igalia.com>
+ */
+
+#define pr_fmt(fmt) "energy_model: " fmt
+
+#include <linux/energy_model.h>
+#include <net/sock.h>
+#include <net/genetlink.h>
+#include <uapi/linux/energy_model.h>
+
+#include "em_netlink.h"
+
+static const struct genl_multicast_group em_genl_mcgrps[] = {
+	[EM_GENL_EVENT_GROUP]  = { .name = EM_GENL_EVENT_GROUP_NAME,  },
+};
+
+static const struct nla_policy em_genl_policy[EM_GENL_ATTR_MAX + 1] = {
+};
+
+static struct genl_family em_genl_family;
+
+
+static int em_genl_cmd_doit(struct sk_buff *skb, struct genl_info *info)
+{
+	return -ENOTSUPP;
+}
+
+static const struct genl_small_ops em_genl_ops[] = {
+	{
+		.cmd = EM_GENL_CMD_PD_GET_ID,
+		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+		.doit = em_genl_cmd_doit,
+	},
+	{
+		.cmd = EM_GENL_CMD_PD_GET_TBL,
+		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+		.doit = em_genl_cmd_doit,
+	},
+};
+
+static struct genl_family em_genl_family __ro_after_init = {
+	.hdrsize	= 0,
+	.name		= EM_GENL_FAMILY_NAME,
+	.version	= EM_GENL_VERSION,
+	.maxattr	= EM_GENL_ATTR_MAX,
+	.policy		= em_genl_policy,
+	.small_ops	= em_genl_ops,
+	.n_small_ops	= ARRAY_SIZE(em_genl_ops),
+	.resv_start_op	= __EM_GENL_CMD_MAX,
+	.mcgrps		= em_genl_mcgrps,
+	.n_mcgrps	= ARRAY_SIZE(em_genl_mcgrps),
+};
+
+int __init em_netlink_init(void)
+{
+	return genl_register_family(&em_genl_family);
+}
+
+void __init em_netlink_exit(void)
+{
+	genl_unregister_family(&em_genl_family);
+}
+
diff --git a/kernel/power/em_netlink.h b/kernel/power/em_netlink.h
new file mode 100644
index 000000000000..8cedc6495916
--- /dev/null
+++ b/kernel/power/em_netlink.h
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ *
+ * Generic netlink for energy model.
+ *
+ * Copyright (c) 2025 Valve Corporation.
+ * Author: Changwoo Min <changwoo@igalia.com>
+ */
+
+enum em_genl_multicast_groups {
+	EM_GENL_EVENT_GROUP = 0,
+	EM_GENL_MAX_GROUP = EM_GENL_EVENT_GROUP,
+};
+
+/* Netlink notification function */
+#ifdef CONFIG_ENERGY_MODEL_NETLINK
+int __init em_netlink_init(void);
+void __init em_netlink_exit(void);
+#else
+static inline int em_netlink_init(void)
+{
+	return 0;
+}
+
+static inline void em_netlink_exit(void) {}
+#endif /* CONFIG_ENERGY_MODEL_NETLINK */
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 03/11] PM: EM: Initialize the netlink notification during booting.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
  2025-05-29  0:13 ` [PATCH 01/11] PM: EM: Add ENERGY_MODEL_NETLINK Kconfig Changwoo Min
  2025-05-29  0:13 ` [PATCH 02/11] PM: EM: Add a skeleton code for netlink notification Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-05-29  0:13 ` [PATCH 04/11] PM: EM: Add the infrastructure for command processing Changwoo Min
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

Initialize and register the netlink during booting. The initialization is
called at the postcore level, which is late enough after the generic
netlink is initialized.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 kernel/power/energy_model.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index ea7995a25780..b15b685c22bd 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -17,6 +17,8 @@
 #include <linux/sched/topology.h>
 #include <linux/slab.h>
 
+#include "em_netlink.h"
+
 /*
  * Mutex serializing the registrations of performance domains and letting
  * callbacks defined by drivers sleep.
@@ -936,3 +938,18 @@ void em_rebuild_sched_domains(void)
 	 */
 	schedule_work(&rebuild_sd_work);
 }
+
+static int __init em_init(void)
+{
+	int result;
+
+	result = em_netlink_init();
+	if (result)
+		goto error;
+
+	return 0;
+
+error:
+	return result;
+}
+postcore_initcall(em_init);
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 04/11] PM: EM: Add the infrastructure for command processing.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
                   ` (2 preceding siblings ...)
  2025-05-29  0:13 ` [PATCH 03/11] PM: EM: Initialize the netlink notification during booting Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-06-02 19:59   ` Lukas Wunner
  2025-05-29  0:13 ` [PATCH 05/11] PM: EM: Assign a unique ID when creating a performance domain Changwoo Min
                   ` (7 subsequent siblings)
  11 siblings, 1 reply; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

The infrastructure for command processing receives a command from a
userspace and calls a callback corresponding to the request command.

Note that the callback functions are just boilerplates in this commit,
and the actual implementations will be provided in the following commits
when a callback for a specific command is implemented.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 kernel/power/em_netlink.c | 53 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)

diff --git a/kernel/power/em_netlink.c b/kernel/power/em_netlink.c
index 30d83fb5a3a8..edbaecebd0b4 100644
--- a/kernel/power/em_netlink.c
+++ b/kernel/power/em_netlink.c
@@ -23,14 +23,65 @@ static const struct genl_multicast_group em_genl_mcgrps[] = {
 static const struct nla_policy em_genl_policy[EM_GENL_ATTR_MAX + 1] = {
 };
 
+struct param {
+	struct nlattr **attrs;
+	struct sk_buff *msg;
+};
+
+typedef int (*cb_t)(struct param *);
+
 static struct genl_family em_genl_family;
 
+/*************************** Command encoding ********************************/
 
-static int em_genl_cmd_doit(struct sk_buff *skb, struct genl_info *info)
+static int em_genl_cmd_pd_get_id(struct param *p)
 {
 	return -ENOTSUPP;
 }
 
+static int em_genl_cmd_pd_get_tbl(struct param *p)
+{
+	return -ENOTSUPP;
+}
+
+static const cb_t cmd_cb[] = {
+	[EM_GENL_CMD_PD_GET_ID]			= em_genl_cmd_pd_get_id,
+	[EM_GENL_CMD_PD_GET_TBL]		= em_genl_cmd_pd_get_tbl,
+};
+
+static int em_genl_cmd_doit(struct sk_buff *skb, struct genl_info *info)
+{
+	struct param p = { .attrs = info->attrs };
+	struct sk_buff *msg;
+	void *hdr;
+	int cmd = info->genlhdr->cmd;
+	int ret = -EMSGSIZE;
+
+	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+	if (!msg)
+		return -ENOMEM;
+	p.msg = msg;
+
+	hdr = genlmsg_put_reply(msg, info, &em_genl_family, 0, cmd);
+	if (!hdr)
+		goto out_free_msg;
+
+	ret = cmd_cb[cmd](&p);
+	if (ret)
+		goto out_cancel_msg;
+
+	genlmsg_end(msg, hdr);
+
+	return genlmsg_reply(msg, info);
+
+out_cancel_msg:
+	genlmsg_cancel(msg, hdr);
+out_free_msg:
+	nlmsg_free(msg);
+
+	return ret;
+}
+
 static const struct genl_small_ops em_genl_ops[] = {
 	{
 		.cmd = EM_GENL_CMD_PD_GET_ID,
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 05/11] PM: EM: Assign a unique ID when creating a performance domain.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
                   ` (3 preceding siblings ...)
  2025-05-29  0:13 ` [PATCH 04/11] PM: EM: Add the infrastructure for command processing Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-05-29  0:13 ` [PATCH 06/11] PM: EM: Expose the ID of a performance domain via debugfs Changwoo Min
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

It is necessary to refer to a specific performance domain from a
userspace. For example, the energy model of a particular performance
domain is updated.

To this end, assign a unique ID to each performance domain to address it,
and manage them in a global linked list to look up a specific one by
matching ID. IDA is used for ID assignment, and the mutex is used to
protect the global list from concurrent access.

Note that the mutex (em_pd_list_mutex) is not supposed to hold while
holding em_pd_mutex to avoid ABBA deadlock.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 include/linux/energy_model.h |  4 ++++
 kernel/power/energy_model.c  | 30 +++++++++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h
index 7fa1eb3cc823..2f5c73fcdfe5 100644
--- a/include/linux/energy_model.h
+++ b/include/linux/energy_model.h
@@ -54,6 +54,8 @@ struct em_perf_table {
 /**
  * struct em_perf_domain - Performance domain
  * @em_table:		Pointer to the runtime modifiable em_perf_table
+ * @node:		node in	em_pd_list (in energy_model.c)
+ * @id:			A unique ID number for each performance domain
  * @nr_perf_states:	Number of performance states
  * @min_perf_state:	Minimum allowed Performance State index
  * @max_perf_state:	Maximum allowed Performance State index
@@ -71,6 +73,8 @@ struct em_perf_table {
  */
 struct em_perf_domain {
 	struct em_perf_table __rcu *em_table;
+	struct list_head node;
+	int id;
 	int nr_perf_states;
 	int min_perf_state;
 	int max_perf_state;
diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index b15b685c22bd..529f8a63ab3d 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -25,6 +25,16 @@
  */
 static DEFINE_MUTEX(em_pd_mutex);
 
+/*
+ * Manage performance domains with IDs. One can iterate the performance domains
+ * through the list and pick one with their associated ID. The mutex serializes
+ * the list access. When holding em_pd_list_mutex, em_pd_mutex should not be
+ * taken to avoid potential deadlock.
+ */
+static DEFINE_IDA(em_pd_ida);
+static LIST_HEAD(em_pd_list);
+static DEFINE_MUTEX(em_pd_list_mutex);
+
 static void em_cpufreq_update_efficiencies(struct device *dev,
 					   struct em_perf_state *table);
 static void em_check_capacity_update(void);
@@ -398,7 +408,7 @@ static int em_create_pd(struct device *dev, int nr_states,
 	struct em_perf_table *em_table;
 	struct em_perf_domain *pd;
 	struct device *cpu_dev;
-	int cpu, ret, num_cpus;
+	int cpu, ret, num_cpus, id;
 
 	if (_is_cpu_device(dev)) {
 		num_cpus = cpumask_weight(cpus);
@@ -422,6 +432,13 @@ static int em_create_pd(struct device *dev, int nr_states,
 
 	pd->nr_perf_states = nr_states;
 
+	INIT_LIST_HEAD(&pd->node);
+
+	id = ida_alloc(&em_pd_ida, GFP_KERNEL);
+	if (id < 0)
+		return -ENOMEM;
+	pd->id = id;
+
 	em_table = em_table_alloc(pd);
 	if (!em_table)
 		goto free_pd;
@@ -446,6 +463,7 @@ static int em_create_pd(struct device *dev, int nr_states,
 	kfree(em_table);
 free_pd:
 	kfree(pd);
+	ida_free(&em_pd_ida, id);
 	return -EINVAL;
 }
 
@@ -641,6 +659,10 @@ int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
 	if (_is_cpu_device(dev))
 		em_check_capacity_update();
 
+	mutex_lock(&em_pd_list_mutex);
+	list_add_tail(&dev->em_pd->node, &em_pd_list);
+	mutex_unlock(&em_pd_list_mutex);
+
 	return ret;
 }
 EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
@@ -659,6 +681,10 @@ void em_dev_unregister_perf_domain(struct device *dev)
 	if (_is_cpu_device(dev))
 		return;
 
+	mutex_lock(&em_pd_list_mutex);
+	list_del_init(&dev->em_pd->node);
+	mutex_unlock(&em_pd_list_mutex);
+
 	/*
 	 * The mutex separates all register/unregister requests and protects
 	 * from potential clean-up/setup issues in the debugfs directories.
@@ -670,6 +696,8 @@ void em_dev_unregister_perf_domain(struct device *dev)
 	em_table_free(rcu_dereference_protected(dev->em_pd->em_table,
 						lockdep_is_held(&em_pd_mutex)));
 
+	ida_free(&em_pd_ida, dev->em_pd->id);
+
 	kfree(dev->em_pd);
 	dev->em_pd = NULL;
 	mutex_unlock(&em_pd_mutex);
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 06/11] PM: EM: Expose the ID of a performance domain via debugfs.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
                   ` (4 preceding siblings ...)
  2025-05-29  0:13 ` [PATCH 05/11] PM: EM: Assign a unique ID when creating a performance domain Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-05-29  0:13 ` [PATCH 07/11] PM: EM: Add an iterator and accessor for the performance domain Changwoo Min
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

For ease of debugging, let's expose the assigned ID of a performance
domain through debugfs (e.g., /sys/kernel/debug/energy_model/cpu0/id).

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 kernel/power/energy_model.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index 529f8a63ab3d..f441c9c1b848 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -128,6 +128,16 @@ static int em_debug_flags_show(struct seq_file *s, void *unused)
 }
 DEFINE_SHOW_ATTRIBUTE(em_debug_flags);
 
+static int em_debug_id_show(struct seq_file *s, void *unused)
+{
+	struct em_perf_domain *pd = s->private;
+
+	seq_printf(s, "%d\n", pd->id);
+
+	return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(em_debug_id);
+
 static void em_debug_create_pd(struct device *dev)
 {
 	struct em_dbg_info *em_dbg;
@@ -144,6 +154,8 @@ static void em_debug_create_pd(struct device *dev)
 	debugfs_create_file("flags", 0444, d, dev->em_pd,
 			    &em_debug_flags_fops);
 
+	debugfs_create_file("id", 0444, d, dev->em_pd, &em_debug_id_fops);
+
 	em_dbg = devm_kcalloc(dev, dev->em_pd->nr_perf_states,
 			      sizeof(*em_dbg), GFP_KERNEL);
 	if (!em_dbg)
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 07/11] PM: EM: Add an iterator and accessor for the performance domain.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
                   ` (5 preceding siblings ...)
  2025-05-29  0:13 ` [PATCH 06/11] PM: EM: Expose the ID of a performance domain via debugfs Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-05-29  0:13 ` [PATCH 08/11] PM: EM: Implement EM_GENL_CMD_PD_GET_ID Changwoo Min
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

Add an iterator function (for_each_em_perf_domain) that iterates all the
performance domains in the global list. A passed callback function (cb) is
called for each performance domain.

Additionally, add a lookup function (em_perf_domain_get_by_id) that
searches for a performance domain by matching the ID in the global list.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 include/linux/energy_model.h | 16 +++++++++++++++-
 kernel/power/energy_model.c  | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h
index 2f5c73fcdfe5..26ed475cd3c4 100644
--- a/include/linux/energy_model.h
+++ b/include/linux/energy_model.h
@@ -341,6 +341,9 @@ struct em_perf_state *em_perf_state_from_pd(struct em_perf_domain *pd)
 	return rcu_dereference(pd->em_table)->state;
 }
 
+int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),
+			    void *data);
+struct em_perf_domain *em_perf_domain_get_by_id(int id);
 #else
 struct em_data_callback {};
 #define EM_ADV_DATA_CB(_active_power_cb, _cost_cb) { }
@@ -410,6 +413,17 @@ int em_update_performance_limits(struct em_perf_domain *pd,
 }
 static inline void em_adjust_cpu_capacity(unsigned int cpu) {}
 static inline void em_rebuild_sched_domains(void) {}
-#endif
+static inline
+int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),
+			    void *data)
+{
+	return -EINVAL;
+}
+static inline
+struct em_perf_domain *em_perf_domain_get_by_id(int id)
+{
+	return NULL;
+}
+#endif /* CONFIG_ENERGY_MODEL */
 
 #endif
diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index f441c9c1b848..6ed847046a2f 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -979,6 +979,40 @@ void em_rebuild_sched_domains(void)
 	schedule_work(&rebuild_sd_work);
 }
 
+int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),
+			    void *data)
+{
+	struct em_perf_domain *pd;
+
+	lockdep_assert_not_held(&em_pd_mutex);
+	guard(mutex)(&em_pd_list_mutex);
+
+	list_for_each_entry(pd, &em_pd_list, node) {
+		int ret;
+
+		ret = cb(pd, data);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+struct em_perf_domain *em_perf_domain_get_by_id(int id)
+{
+	struct em_perf_domain *pd;
+
+	lockdep_assert_not_held(&em_pd_mutex);
+	guard(mutex)(&em_pd_list_mutex);
+
+	list_for_each_entry(pd, &em_pd_list, node) {
+		if (pd->id == id)
+			return pd;
+	}
+
+	return NULL;
+}
+
 static int __init em_init(void)
 {
 	int result;
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 08/11] PM: EM: Implement EM_GENL_CMD_PD_GET_ID.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
                   ` (6 preceding siblings ...)
  2025-05-29  0:13 ` [PATCH 07/11] PM: EM: Add an iterator and accessor for the performance domain Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-05-29  0:13 ` [PATCH 09/11] PM: EM: Implement EM_GENL_CMD_PD_GET_TBL Changwoo Min
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

When a userspace requests EM_GENL_CMD_PD_GET_ID, the kernel responds with
information on all performance domains. The message format of the response
is as follows:

EM_GENL_ATTR_PD (NLA_NESTED)
	EM_PD_ENTRY_GENL_ATTR_PD (NLA_NESTED)*
		EM_PD_GENL_ATTR_ID (NLA_U32)
		EM_PD_GENL_ATTR_FLAGS (NLA_U64)
		EM_PD_GENL_ATTR_CPUS (NLA_STRING)

Where EM_PD_ENTRY_GENL_ATTR_PD can be repeated as many times as there are
performance domains, and EM_PD_GENL_ATTR_CPUS is a hexadecimal string
representing a CPU bitmask.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 include/uapi/linux/energy_model.h | 25 ++++++++++++++
 kernel/power/em_netlink.c         | 55 ++++++++++++++++++++++++++++++-
 2 files changed, 79 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/energy_model.h b/include/uapi/linux/energy_model.h
index 42a19e614c7d..66339ace6fcb 100644
--- a/include/uapi/linux/energy_model.h
+++ b/include/uapi/linux/energy_model.h
@@ -10,10 +10,35 @@
 /* Attributes of em_genl_family */
 enum em_genl_attr {
 	EM_GENL_ATTR_UNSPEC,
+	EM_GENL_ATTR_PAD = EM_GENL_ATTR_UNSPEC,
+	EM_GENL_ATTR_PD,	/* Performance domain */
 	__EM_GENL_ATTR_MAX,
 };
 #define EM_GENL_ATTR_MAX (__EM_GENL_ATTR_MAX - 1)
 
+enum em_pd_entry_genl_attr {
+	EM_PD_ENTRY_GENL_ATTR_UNSPEC,
+	EM_PD_ENTRY_GENL_ATTR_PAD = EM_PD_ENTRY_GENL_ATTR_UNSPEC,
+	EM_PD_ENTRY_GENL_ATTR_PD,
+	__EM_PD_ENTRY_GENL_ATTR_MAX,
+};
+#define EM_PD_ENTRY_GENL_ATTR_MAX (__EM_PD_ENTRY_GENL_ATTR_MAX - 1)
+
+enum em_pd_genl_attr {
+	EM_PD_GENL_ATTR_UNSPEC,
+	EM_PD_GENL_ATTR_PAD = EM_PD_GENL_ATTR_UNSPEC,
+
+	/* Performance domain */
+	EM_PD_GENL_ATTR_ID,
+	EM_PD_GENL_ATTR_FLAGS,
+	EM_PD_GENL_ATTR_CPUS,
+
+	__EM_PD_GENL_ATTR_MAX,
+};
+#define EM_PD_GENL_ATTR_MAX (__EM_PD_GENL_ATTR_MAX - 1)
+
+#define EM_PD_CPUS_LENGTH		256
+
 /* Events of em_genl_family */
 enum em_genl_event {
 	EM_GENL_EVENT_UNSPEC,
diff --git a/kernel/power/em_netlink.c b/kernel/power/em_netlink.c
index edbaecebd0b4..ea975ca6272f 100644
--- a/kernel/power/em_netlink.c
+++ b/kernel/power/em_netlink.c
@@ -21,6 +21,8 @@ static const struct genl_multicast_group em_genl_mcgrps[] = {
 };
 
 static const struct nla_policy em_genl_policy[EM_GENL_ATTR_MAX + 1] = {
+	/* Performance domain */
+	[EM_GENL_ATTR_PD]			= { .type = NLA_NESTED },
 };
 
 struct param {
@@ -34,9 +36,60 @@ static struct genl_family em_genl_family;
 
 /*************************** Command encoding ********************************/
 
+static int __em_genl_cmd_pd_get_id(struct em_perf_domain *pd, void *data)
+{
+	char cpus_buf[EM_PD_CPUS_LENGTH];
+	struct sk_buff *msg = data;
+	struct nlattr *entry;
+
+	entry = nla_nest_start(msg, EM_PD_ENTRY_GENL_ATTR_PD);
+	if (!entry)
+		goto out_cancel_nest;
+
+	if (nla_put_u32(msg, EM_PD_GENL_ATTR_ID, pd->id))
+		goto out_cancel_nest;
+
+	if (nla_put_u64_64bit(msg, EM_PD_GENL_ATTR_FLAGS, pd->flags,
+			      EM_PD_GENL_ATTR_PAD))
+		goto out_cancel_nest;
+
+	snprintf(cpus_buf, sizeof(cpus_buf), "%*pb",
+		 cpumask_pr_args(to_cpumask(pd->cpus)));
+	if (nla_put_string(msg, EM_PD_GENL_ATTR_CPUS, cpus_buf))
+		goto out_cancel_nest;
+
+	nla_nest_end(msg, entry);
+
+	return 0;
+
+out_cancel_nest:
+	nla_nest_cancel(msg, entry);
+
+	return -EMSGSIZE;
+}
+
 static int em_genl_cmd_pd_get_id(struct param *p)
 {
-	return -ENOTSUPP;
+	struct sk_buff *msg = p->msg;
+	struct nlattr *start_pd;
+	int ret;
+
+	start_pd = nla_nest_start(msg, EM_GENL_ATTR_PD);
+	if (!start_pd)
+		return -EMSGSIZE;
+
+	ret = for_each_em_perf_domain(__em_genl_cmd_pd_get_id, msg);
+	if (ret)
+		goto out_cancel_nest;
+
+	nla_nest_end(msg, start_pd);
+
+	return 0;
+
+out_cancel_nest:
+	nla_nest_cancel(msg, start_pd);
+
+	return ret;
 }
 
 static int em_genl_cmd_pd_get_tbl(struct param *p)
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 09/11] PM: EM: Implement EM_GENL_CMD_PD_GET_TBL.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
                   ` (7 preceding siblings ...)
  2025-05-29  0:13 ` [PATCH 08/11] PM: EM: Implement EM_GENL_CMD_PD_GET_ID Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-05-29  0:13 ` [PATCH 10/11] PM: EM: Implement event notification Changwoo Min
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

When a userspace requests EM_GENL_CMD_PD_GET_TBL with an ID of a
performance domain, the kernel reports back the energy model table of the
specified performance domain. The message format of the response is as
follows:

EM_GENL_ATTR_PD_TBL (NLA_NESTED)
	EM_TBL_ENTRY_GENL_ATTR_PD (NLA_NESTED)*
		EM_TBL_GENL_ATTR_PS_PERFORMANCE (NLA_U64)
		EM_TBL_GENL_ATTR_PS_FREQUENCY (NLA_U64)
		EM_TBL_GENL_ATTR_PS_POWER (NLA_U64)
		EM_TBL_GENL_ATTR_PS_COST (NLA_U64)
		EM_TBL_GENL_ATTR_PS_FLAGS (NLA_U64)

Where EM_TBL_ENTRY_GENL_ATTR_PD can be repeated as many times as there are
performance states (struct em_perf_state).

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 include/uapi/linux/energy_model.h | 24 ++++++++++++
 kernel/power/em_netlink.c         | 64 ++++++++++++++++++++++++++++++-
 2 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/energy_model.h b/include/uapi/linux/energy_model.h
index 66339ace6fcb..9517ca957a78 100644
--- a/include/uapi/linux/energy_model.h
+++ b/include/uapi/linux/energy_model.h
@@ -12,6 +12,7 @@ enum em_genl_attr {
 	EM_GENL_ATTR_UNSPEC,
 	EM_GENL_ATTR_PAD = EM_GENL_ATTR_UNSPEC,
 	EM_GENL_ATTR_PD,	/* Performance domain */
+	EM_GENL_ATTR_PD_TBL,	/* Performance table of a performance domain */
 	__EM_GENL_ATTR_MAX,
 };
 #define EM_GENL_ATTR_MAX (__EM_GENL_ATTR_MAX - 1)
@@ -39,6 +40,29 @@ enum em_pd_genl_attr {
 
 #define EM_PD_CPUS_LENGTH		256
 
+enum em_tbl_entry_genl_attr {
+	EM_TBL_ENTRY_GENL_ATTR_UNSPEC,
+	EM_TBL_ENTRY_GENL_ATTR_PAD = EM_TBL_ENTRY_GENL_ATTR_UNSPEC,
+	EM_TBL_ENTRY_GENL_ATTR_PD,
+	__EM_TBL_ENTRY_GENL_ATTR_MAX,
+};
+#define EM_TBL_ENTRY_GENL_ATTR_MAX (__EM_TBL_ENTRY_GENL_ATTR_MAX - 1)
+
+enum em_tbl_genl_attr {
+	EM_TBL_GENL_ATTR_UNSPEC,
+	EM_TBL_GENL_ATTR_PAD = EM_TBL_GENL_ATTR_UNSPEC,
+
+	/* Performance table of a performance domain */
+	EM_TBL_GENL_ATTR_PS_PERFORMANCE,
+	EM_TBL_GENL_ATTR_PS_FREQUENCY,
+	EM_TBL_GENL_ATTR_PS_POWER,
+	EM_TBL_GENL_ATTR_PS_COST,
+	EM_TBL_GENL_ATTR_PS_FLAGS,
+
+	__EM_TBL_GENL_ATTR_MAX,
+};
+#define EM_TBL_GENL_ATTR_MAX (__EM_TBL_GENL_ATTR_MAX - 1)
+
 /* Events of em_genl_family */
 enum em_genl_event {
 	EM_GENL_EVENT_UNSPEC,
diff --git a/kernel/power/em_netlink.c b/kernel/power/em_netlink.c
index ea975ca6272f..5f3d5aaa97d7 100644
--- a/kernel/power/em_netlink.c
+++ b/kernel/power/em_netlink.c
@@ -23,6 +23,8 @@ static const struct genl_multicast_group em_genl_mcgrps[] = {
 static const struct nla_policy em_genl_policy[EM_GENL_ATTR_MAX + 1] = {
 	/* Performance domain */
 	[EM_GENL_ATTR_PD]			= { .type = NLA_NESTED },
+	/* Performance table of a performance domain */
+	[EM_GENL_ATTR_PD_TBL]			= { .type = NLA_NESTED },
 };
 
 struct param {
@@ -94,7 +96,67 @@ static int em_genl_cmd_pd_get_id(struct param *p)
 
 static int em_genl_cmd_pd_get_tbl(struct param *p)
 {
-	return -ENOTSUPP;
+	struct sk_buff *msg = p->msg;
+	struct em_perf_domain *pd;
+	struct em_perf_state *table, *ps;
+	struct nlattr *start_tbl, *entry;
+	int id, i;
+
+	if (!p->attrs[EM_PD_GENL_ATTR_ID])
+		return -EINVAL;
+
+	id = nla_get_u32(p->attrs[EM_PD_GENL_ATTR_ID]);
+
+	pd = em_perf_domain_get_by_id(id);
+	if (!pd)
+		return -EINVAL;
+
+	start_tbl = nla_nest_start(msg, EM_GENL_ATTR_PD_TBL);
+	if (!start_tbl )
+		return -EMSGSIZE;
+
+	rcu_read_lock();
+	table = em_perf_state_from_pd(pd);
+
+	for (i = 0; i < pd->nr_perf_states; i++) {
+		ps = &table[i];
+
+		entry = nla_nest_start(msg, EM_TBL_ENTRY_GENL_ATTR_PD);
+		if (!entry)
+			goto out_cancel_nest;
+
+		if (nla_put_u64_64bit(msg, EM_TBL_GENL_ATTR_PS_PERFORMANCE,
+				      ps->performance, EM_TBL_GENL_ATTR_PAD))
+			goto out_cancel_nest2;
+		if (nla_put_u64_64bit(msg, EM_TBL_GENL_ATTR_PS_FREQUENCY,
+				      ps->frequency, EM_TBL_GENL_ATTR_PAD))
+			goto out_cancel_nest2;
+		if (nla_put_u64_64bit(msg, EM_TBL_GENL_ATTR_PS_POWER,
+				      ps->power, EM_TBL_GENL_ATTR_PAD))
+			goto out_cancel_nest2;
+		if (nla_put_u64_64bit(msg, EM_TBL_GENL_ATTR_PS_COST,
+				      ps->cost, EM_TBL_GENL_ATTR_PAD))
+			goto out_cancel_nest2;
+		if (nla_put_u64_64bit(msg, EM_TBL_GENL_ATTR_PS_FLAGS,
+				      ps->flags, EM_TBL_GENL_ATTR_PAD))
+			goto out_cancel_nest2;
+
+		nla_nest_end(msg, entry);
+	}
+	rcu_read_unlock();
+
+	nla_nest_end(msg, start_tbl);
+
+	return 0;
+
+out_cancel_nest2:
+	nla_nest_cancel(msg, entry);
+
+out_cancel_nest:
+	rcu_read_unlock();
+
+	nla_nest_cancel(msg, start_tbl);
+	return -EMSGSIZE;
 }
 
 static const cb_t cmd_cb[] = {
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 10/11] PM: EM: Implement event notification.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
                   ` (8 preceding siblings ...)
  2025-05-29  0:13 ` [PATCH 09/11] PM: EM: Implement EM_GENL_CMD_PD_GET_TBL Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-05-29  0:13 ` [PATCH 11/11] PM: EM: Notify an event when the performance domain changes Changwoo Min
  2025-06-02 11:52 ` [PATCH 00/11] PM: EM: Add netlink support for the energy model Lukasz Luba
  11 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

Add the event notification infrastructure and implement event
notifications for three events -- when a performance domain is created
(EM_GENL_EVENT_PD_CREATE), deleted (EM_GENL_EVENT_PD_DELETE), or its
energy model is updated (EM_GENL_EVENT_PD_UPDATE).

The event contains the ID of the performance domain (EM_PD_GENL_ATTR_ID),
so the userspace can identify the changed performance domain for further
processing.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 kernel/power/em_netlink.c | 89 +++++++++++++++++++++++++++++++++++++++
 kernel/power/em_netlink.h | 19 +++++++++
 2 files changed, 108 insertions(+)

diff --git a/kernel/power/em_netlink.c b/kernel/power/em_netlink.c
index 5f3d5aaa97d7..85d4077ec1b3 100644
--- a/kernel/power/em_netlink.c
+++ b/kernel/power/em_netlink.c
@@ -30,12 +30,101 @@ static const struct nla_policy em_genl_policy[EM_GENL_ATTR_MAX + 1] = {
 struct param {
 	struct nlattr **attrs;
 	struct sk_buff *msg;
+	int pd_id;
 };
 
 typedef int (*cb_t)(struct param *);
 
 static struct genl_family em_genl_family;
 
+/**************************** Event encoding *********************************/
+static int __em_genl_event_pd_id(struct param *p)
+{
+	if (nla_put_u32(p->msg, EM_PD_GENL_ATTR_ID, p->pd_id))
+		return -EMSGSIZE;
+
+	return 0;
+}
+
+static int em_genl_event_pd_create(struct param *p)
+{
+	return __em_genl_event_pd_id(p);
+}
+
+static int em_genl_event_pd_delete(struct param *p)
+{
+	return __em_genl_event_pd_id(p);
+}
+
+static int em_genl_event_pd_update(struct param *p)
+{
+	return __em_genl_event_pd_id(p);
+}
+
+static const cb_t event_cb[] = {
+	[EM_GENL_EVENT_PD_CREATE] = em_genl_event_pd_create,
+	[EM_GENL_EVENT_PD_DELETE] = em_genl_event_pd_delete,
+	[EM_GENL_EVENT_PD_UPDATE] = em_genl_event_pd_update,
+};
+
+static int em_genl_send_event(enum em_genl_event event, struct param *p)
+{
+	struct sk_buff *msg;
+	int ret = -EMSGSIZE;
+	void *hdr;
+
+	if (!genl_has_listeners(&em_genl_family, &init_net, EM_GENL_EVENT_GROUP))
+		return 0;
+
+	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+	if (!msg)
+		return -ENOMEM;
+	p->msg = msg;
+
+	hdr = genlmsg_put(msg, 0, 0, &em_genl_family, 0, event);
+	if (!hdr)
+		goto out_free_msg;
+
+	ret = event_cb[event](p);
+	if (ret)
+		goto out_cancel_msg;
+
+	genlmsg_end(msg, hdr);
+
+	genlmsg_multicast(&em_genl_family, msg, 0, EM_GENL_EVENT_GROUP, GFP_KERNEL);
+
+	return 0;
+
+out_cancel_msg:
+	genlmsg_cancel(msg, hdr);
+out_free_msg:
+	nlmsg_free(msg);
+
+	return ret;
+}
+
+int em_notify_pd_create(const struct em_perf_domain *pd)
+{
+	struct param p = { .pd_id = pd->id };
+
+	return em_genl_send_event(EM_GENL_EVENT_PD_CREATE, &p);
+}
+
+
+int em_notify_pd_delete(const struct em_perf_domain *pd)
+{
+	struct param p = { .pd_id = pd->id };
+
+	return em_genl_send_event(EM_GENL_EVENT_PD_DELETE, &p);
+}
+
+int em_notify_pd_update(const struct em_perf_domain *pd)
+{
+	struct param p = { .pd_id = pd->id };
+
+	return em_genl_send_event(EM_GENL_EVENT_PD_UPDATE, &p);
+}
+
 /*************************** Command encoding ********************************/
 
 static int __em_genl_cmd_pd_get_id(struct em_perf_domain *pd, void *data)
diff --git a/kernel/power/em_netlink.h b/kernel/power/em_netlink.h
index 8cedc6495916..34d5a98eec72 100644
--- a/kernel/power/em_netlink.h
+++ b/kernel/power/em_netlink.h
@@ -16,6 +16,10 @@ enum em_genl_multicast_groups {
 #ifdef CONFIG_ENERGY_MODEL_NETLINK
 int __init em_netlink_init(void);
 void __init em_netlink_exit(void);
+
+int em_notify_pd_create(const struct em_perf_domain *pd);
+int em_notify_pd_delete(const struct em_perf_domain *pd);
+int em_notify_pd_update(const struct em_perf_domain *pd);
 #else
 static inline int em_netlink_init(void)
 {
@@ -23,4 +27,19 @@ static inline int em_netlink_init(void)
 }
 
 static inline void em_netlink_exit(void) {}
+
+static inline int em_notify_pd_create(const struct em_perf_domain *pd)
+{
+	return 0;
+}
+
+static inline int em_notify_pd_delete(const struct em_perf_domain *pd)
+{
+	return 0;
+}
+
+static inline int em_notify_pd_update(const struct em_perf_domain *pd)
+{
+	return 0;
+}
 #endif /* CONFIG_ENERGY_MODEL_NETLINK */
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 11/11] PM: EM: Notify an event when the performance domain changes.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
                   ` (9 preceding siblings ...)
  2025-05-29  0:13 ` [PATCH 10/11] PM: EM: Implement event notification Changwoo Min
@ 2025-05-29  0:13 ` Changwoo Min
  2025-06-02 11:52 ` [PATCH 00/11] PM: EM: Add netlink support for the energy model Lukasz Luba
  11 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-05-29  0:13 UTC (permalink / raw)
  To: lukasz.luba, rafael, len.brown, pavel
  Cc: christian.loehle, tj, kernel-dev, linux-pm, linux-kernel,
	Changwoo Min

Send an event to userspace when a performance domain is created or deleted,
or its energy model is updated.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 kernel/power/energy_model.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index 6ed847046a2f..5764821faef3 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -352,6 +352,8 @@ int em_dev_update_perf_domain(struct device *dev,
 	em_table_free(old_table);
 
 	mutex_unlock(&em_pd_mutex);
+
+	em_notify_pd_update(pd);
 	return 0;
 }
 EXPORT_SYMBOL_GPL(em_dev_update_perf_domain);
@@ -675,6 +677,8 @@ int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
 	list_add_tail(&dev->em_pd->node, &em_pd_list);
 	mutex_unlock(&em_pd_list_mutex);
 
+	em_notify_pd_create(dev->em_pd);
+
 	return ret;
 }
 EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
@@ -697,6 +701,8 @@ void em_dev_unregister_perf_domain(struct device *dev)
 	list_del_init(&dev->em_pd->node);
 	mutex_unlock(&em_pd_list_mutex);
 
+	em_notify_pd_delete(dev->em_pd);
+
 	/*
 	 * The mutex separates all register/unregister requests and protects
 	 * from potential clean-up/setup issues in the debugfs directories.
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH 00/11] PM: EM: Add netlink support for the energy model.
  2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
                   ` (10 preceding siblings ...)
  2025-05-29  0:13 ` [PATCH 11/11] PM: EM: Notify an event when the performance domain changes Changwoo Min
@ 2025-06-02 11:52 ` Lukasz Luba
  11 siblings, 0 replies; 17+ messages in thread
From: Lukasz Luba @ 2025-06-02 11:52 UTC (permalink / raw)
  To: Changwoo Min
  Cc: christian.loehle, tj, pavel, len.brown, rafael, kernel-dev,
	linux-pm, linux-kernel, Rafael J. Wysocki

Hi Changwoo,

On 5/29/25 01:13, Changwoo Min wrote:
> There is a need to access the energy model from the userspace. One such
> example is the sched_ext schedulers [1]. The userspace part of the
> sched_ext schedules could feed the (post-processed) energy-model
> information to the BPF part of the scheduler.
> 
> Currently, debugfs is the only way to read the energy model from userspace;
> however, it lacks proper notification mechanisms when a performance domain
> and its associated energy model change.
> 
> This patch set introduces a generic netlink for the energy model, as
> discussed in [2]. It allows a userspace program to read the performance
> domain and its energy model. It notifies the userspace program when a
> performance domain is created or deleted or its energy model is updated
> through a multicast interface.
> 
> Specifically, it supports two commands:
>    - EM_GENL_CMD_PD_GET_ID: Get the list of information for all performance domains.
>    - EM_GENL_CMD_PD_GET_TBL: Get the energy model table of a performance domain.
> 
> Also, it supports three notification events:
>    - EM_GENL_EVENT_PD_CREATE: When a performance domain is created.
>    - EM_GENL_EVENT_PD_DELETE: When a performance domain is deleted.
>    - EM_GENL_EVENT_PD_UPDATE: When the energy model table of a performance domain is updated.
> 
> The userspace code example using the EM netlink interface is at [3].
> 
> [1] https://lwn.net/Articles/922405/
> [2] https://lore.kernel.org/lkml/a82423bc-8c38-4d57-93da-c4f20011cc92@arm.com/
> [3] https://github.com/multics69/em-netlink/tree/patch-v1

Thanks for the patch set and for the reference code in user-space.
I will give it a try and then do the review.

Regards,
Lukasz

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 02/11] PM: EM: Add a skeleton code for netlink notification.
  2025-05-29  0:13 ` [PATCH 02/11] PM: EM: Add a skeleton code for netlink notification Changwoo Min
@ 2025-06-02 19:53   ` Lukas Wunner
  2025-06-03  6:01     ` Changwoo Min
  0 siblings, 1 reply; 17+ messages in thread
From: Lukas Wunner @ 2025-06-02 19:53 UTC (permalink / raw)
  To: Changwoo Min
  Cc: lukasz.luba, rafael, len.brown, pavel, christian.loehle, tj,
	kernel-dev, linux-pm, linux-kernel

> diff --git a/include/uapi/linux/energy_model.h b/include/uapi/linux/energy_model.h
> new file mode 100644
> index 000000000000..42a19e614c7d
> --- /dev/null
> +++ b/include/uapi/linux/energy_model.h
> @@ -0,0 +1,40 @@
> +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
> +#ifndef _UAPI_LINUX_ENERGY_MODEL_H
> +#define _UAPI_LINUX_ENERGY_MODEL_H
> +

It looks like you created the header file manually.  There is tooling
to auto-generate all the boilerplate code from a YAML description in
Documentation/netlink/specs/ and my (limited) understanding is that
using it is mandatory for all newly introduced Netlink protocols.

I just had to wrap my head around all that for SPDM (a device
authentication protocol), see the top-most commit on this branch,
which is in a WIP state though:

https://github.com/l1k/linux/commits/doe

Basically you create the uapi and kernel header files plus kernel source
like this:

tools/net/ynl/pyynl/ynl_gen_c.py --spec Documentation/netlink/specs/em.yaml \
  --mode uapi --header
tools/net/ynl/pyynl/ynl_gen_c.py --spec Documentation/netlink/specs/em.yaml \
  --mode kernel --header
tools/net/ynl/pyynl/ynl_gen_c.py --spec Documentation/netlink/specs/em.yaml \
  --mode kernel --source

And then you add both the YAML file as well as the generated files to
the commit.  The reason you have to do that is because Python is
optional for building the kernel per Documentation/process/changes.rst,
so the files cannot be generated at compile time.  It is possible though
to regenerate them with tools/net/ynl/ynl-regen.sh whenever the YAML file
is changed.

The tooling is somewhat brittle, see 396786af1cea.  In theory ynl_gen_c.py
is capable of auto-generating code for user space applications as well
but it crashed when parsing my YAML file.  So there are more bugs,
just haven't had the time yet to fix them.


> +int __init em_netlink_init(void)
> +{
> +	return genl_register_family(&em_genl_family);
> +}
> +
> +void __init em_netlink_exit(void)
> +{
> +	genl_unregister_family(&em_genl_family);
> +}
> +

It looks like em_netlink_exit() isn't invoked anywhere, so why define
it in the first place?  You only need this if the feature can be modular
(which it cannot - it's gated by a bool Kconfig option).  Then you'd
call em_netlink_exit() in module_exit().

Also, you may want to consider moving this to patch [03/11], where
em_netlink_init() is actually invoked.  And you may want to move the
postcore_initcall() to this file so that you can declare em_netlink_init()
static, don't need em_init() and don't need the empty inline stubs.

Thanks,

Lukas

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 04/11] PM: EM: Add the infrastructure for command processing.
  2025-05-29  0:13 ` [PATCH 04/11] PM: EM: Add the infrastructure for command processing Changwoo Min
@ 2025-06-02 19:59   ` Lukas Wunner
  2025-06-04 16:11     ` Changwoo Min
  0 siblings, 1 reply; 17+ messages in thread
From: Lukas Wunner @ 2025-06-02 19:59 UTC (permalink / raw)
  To: Changwoo Min
  Cc: lukasz.luba, rafael, len.brown, pavel, christian.loehle, tj,
	kernel-dev, linux-pm, linux-kernel

On Thu, May 29, 2025 at 09:13:08AM +0900, Changwoo Min wrote:
> +static int em_genl_cmd_doit(struct sk_buff *skb, struct genl_info *info)
> +{
> +	struct param p = { .attrs = info->attrs };
> +	struct sk_buff *msg;
> +	void *hdr;
> +	int cmd = info->genlhdr->cmd;
> +	int ret = -EMSGSIZE;
> +
> +	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
> +	if (!msg)
> +		return -ENOMEM;

Just a heads-up, I know everyone recommends NLMSG_GOODSIZE but in reality
it's not that great because netlink_trim() reallocates the skb and copies
the entire linear buffer if it determines that the skb is half-empty.
Performance suffers as a result.  So it's actually better to calculate
the exact message length prior to allocation.  See the SPDM commit
referenced in my previous e-mail.  Another lesson I had to learn the
hard way. :(

Thanks,

Lukas

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 02/11] PM: EM: Add a skeleton code for netlink notification.
  2025-06-02 19:53   ` Lukas Wunner
@ 2025-06-03  6:01     ` Changwoo Min
  0 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-06-03  6:01 UTC (permalink / raw)
  To: Lukas Wunner
  Cc: lukasz.luba, rafael, len.brown, pavel, christian.loehle, tj,
	kernel-dev, linux-pm, linux-kernel

Hi Lukas,

Thank you for the comments!

On 6/3/25 04:53, Lukas Wunner wrote:
>> diff --git a/include/uapi/linux/energy_model.h b/include/uapi/linux/energy_model.h
>> new file mode 100644
>> index 000000000000..42a19e614c7d
>> --- /dev/null
>> +++ b/include/uapi/linux/energy_model.h
>> @@ -0,0 +1,40 @@
>> +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
>> +#ifndef _UAPI_LINUX_ENERGY_MODEL_H
>> +#define _UAPI_LINUX_ENERGY_MODEL_H
>> +
> 
> It looks like you created the header file manually.

Right, I followed the structure and design of thermal_netlink.{ch}.

>                                                     There is tooling
> to auto-generate all the boilerplate code from a YAML description in
> Documentation/netlink/specs/ and my (limited) understanding is that
> using it is mandatory for all newly introduced Netlink protocols.

Thank you for the suggestion! Using YNL will definitely be easier
to maintain in the long run. I will work on defining the YNL and
generating the boilerplate code using YNL. It will require some
reorg of files to keep the autogenerated files intact.

Besides the boilerplate generation, what do you think about the
current commands and events defined? Does it look reasonable? If
you have any feedback, I will incorporate it in the next version.

> 
> I just had to wrap my head around all that for SPDM (a device
> authentication protocol), see the top-most commit on this branch,
> which is in a WIP state though:
> 
> https://github.com/l1k/linux/commits/doe
> 
> Basically you create the uapi and kernel header files plus kernel source
> like this:
> 
> tools/net/ynl/pyynl/ynl_gen_c.py --spec Documentation/netlink/specs/em.yaml \
>    --mode uapi --header
> tools/net/ynl/pyynl/ynl_gen_c.py --spec Documentation/netlink/specs/em.yaml \
>    --mode kernel --header
> tools/net/ynl/pyynl/ynl_gen_c.py --spec Documentation/netlink/specs/em.yaml \
>    --mode kernel --source
> 
> And then you add both the YAML file as well as the generated files to
> the commit.  The reason you have to do that is because Python is
> optional for building the kernel per Documentation/process/changes.rst,
> so the files cannot be generated at compile time.  It is possible though
> to regenerate them with tools/net/ynl/ynl-regen.sh whenever the YAML file
> is changed.
> 
> The tooling is somewhat brittle, see 396786af1cea.  In theory ynl_gen_c.py
> is capable of auto-generating code for user space applications as well
> but it crashed when parsing my YAML file.  So there are more bugs,
> just haven't had the time yet to fix them.
> 
> 
>> +int __init em_netlink_init(void)
>> +{
>> +	return genl_register_family(&em_genl_family);
>> +}
>> +
>> +void __init em_netlink_exit(void)
>> +{
>> +	genl_unregister_family(&em_genl_family);
>> +}
>> +
> 
> It looks like em_netlink_exit() isn't invoked anywhere, so why define
> it in the first place?  You only need this if the feature can be modular
> (which it cannot - it's gated by a bool Kconfig option).  Then you'd
> call em_netlink_exit() in module_exit().

You are right. I will drop em_netlink_exit().

> 
> Also, you may want to consider moving this to patch [03/11], where
> em_netlink_init() is actually invoked.  And you may want to move the
> postcore_initcall() to this file so that you can declare em_netlink_init()
> static, don't need em_init() and don't need the empty inline stubs.

Thanks for the suggestion. That's simpler. I will change it as suggested.

Regards,
Changwoo Min

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 04/11] PM: EM: Add the infrastructure for command processing.
  2025-06-02 19:59   ` Lukas Wunner
@ 2025-06-04 16:11     ` Changwoo Min
  0 siblings, 0 replies; 17+ messages in thread
From: Changwoo Min @ 2025-06-04 16:11 UTC (permalink / raw)
  To: Lukas Wunner
  Cc: lukasz.luba, rafael, len.brown, pavel, christian.loehle, tj,
	kernel-dev, linux-pm, linux-kernel



On 6/2/25 04:59, Lukas Wunner wrote:
> On Thu, May 29, 2025 at 09:13:08AM +0900, Changwoo Min wrote:
>> +static int em_genl_cmd_doit(struct sk_buff *skb, struct genl_info *info)
>> +{
>> +	struct param p = { .attrs = info->attrs };
>> +	struct sk_buff *msg;
>> +	void *hdr;
>> +	int cmd = info->genlhdr->cmd;
>> +	int ret = -EMSGSIZE;
>> +
>> +	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
>> +	if (!msg)
>> +		return -ENOMEM;
> 
> Just a heads-up, I know everyone recommends NLMSG_GOODSIZE but in reality
> it's not that great because netlink_trim() reallocates the skb and copies
> the entire linear buffer if it determines that the skb is half-empty.
> Performance suffers as a result.  So it's actually better to calculate
> the exact message length prior to allocation.
Thank you, Lukas, for sharing the experience. I will address it in the
next version.

Regards,
Changwoo Min

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2025-06-04 16:11 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-29  0:13 [PATCH 00/11] PM: EM: Add netlink support for the energy model Changwoo Min
2025-05-29  0:13 ` [PATCH 01/11] PM: EM: Add ENERGY_MODEL_NETLINK Kconfig Changwoo Min
2025-05-29  0:13 ` [PATCH 02/11] PM: EM: Add a skeleton code for netlink notification Changwoo Min
2025-06-02 19:53   ` Lukas Wunner
2025-06-03  6:01     ` Changwoo Min
2025-05-29  0:13 ` [PATCH 03/11] PM: EM: Initialize the netlink notification during booting Changwoo Min
2025-05-29  0:13 ` [PATCH 04/11] PM: EM: Add the infrastructure for command processing Changwoo Min
2025-06-02 19:59   ` Lukas Wunner
2025-06-04 16:11     ` Changwoo Min
2025-05-29  0:13 ` [PATCH 05/11] PM: EM: Assign a unique ID when creating a performance domain Changwoo Min
2025-05-29  0:13 ` [PATCH 06/11] PM: EM: Expose the ID of a performance domain via debugfs Changwoo Min
2025-05-29  0:13 ` [PATCH 07/11] PM: EM: Add an iterator and accessor for the performance domain Changwoo Min
2025-05-29  0:13 ` [PATCH 08/11] PM: EM: Implement EM_GENL_CMD_PD_GET_ID Changwoo Min
2025-05-29  0:13 ` [PATCH 09/11] PM: EM: Implement EM_GENL_CMD_PD_GET_TBL Changwoo Min
2025-05-29  0:13 ` [PATCH 10/11] PM: EM: Implement event notification Changwoo Min
2025-05-29  0:13 ` [PATCH 11/11] PM: EM: Notify an event when the performance domain changes Changwoo Min
2025-06-02 11:52 ` [PATCH 00/11] PM: EM: Add netlink support for the energy model Lukasz Luba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).