PM: EM: Add netlink support for the energy model
Subsystems (6)
Changwoo Min
reviewerv5 cover PM: EM: Add netlink support for the energy model
Addressed all the comments from Lukasz and rebased the code to the head of the linus tree.
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_CMD_GET_PDS: Get the list of information for all performance domains.
- EM_CMD_GET_PD_TABLE: Get the energy model table of a performance domain.
Also, it supports three notification events:
- EM_CMD_PD_CREATED: When a performance domain is created.
- EM_CMD_PD_DELETED: When a performance domain is deleted.
- EM_CMD_PD_UPDATED: When the energy model table of a performance domain is updated.
This can be tested using the tool, tools/net/ynl/pyynl/cli.py, for example, with the following commands:
$> tools/net/ynl/pyynl/cli.py \
--spec Documentation/netlink/specs/em.yaml \
--do get-pds
$> tools/net/ynl/pyynl/cli.py \
--spec Documentation/netlink/specs/em.yaml \
--do get-pd-table --json '{"pd-id": 0}'
$> tools/net/ynl/pyynl/cli.py \
--spec Documentation/netlink/specs/em.yaml \
--subscribe event --sleep 10[1] https://lwn.net/Articles/922405/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/#t
ChangeLog v4 -> v5:
- Rebase the code to the head of the linus tree.
- Remove the redundant em_check_capacity_update() call from
em_dev_register_pd_no_update().
- Move patch 3 ("PM: EM: Add an iterator and accessor for the performance domain") after patch 5 ("PM: EM: Add a skeleton code for netlink notification").
- Move the declaration of for_each_em_perf_domain() and em_perf_domain_get_by_id() from energy_model.h to em_netlink.h.
- Fix a typo in patch 7 ("PM: EM: Implement em_nl_get_pd_table_doit()") and change the variable declaration order in em_nl_get_pd_table_doit() following the reverse Christmas tree order.
- Remove the empty skeleton code of em_notify_pd_created/updated() from patch 8 ("PM: EM: Implement em_notify_pd_deleted()") and introduce them later where they are actually implemented.
- Change the return type of em_notify_pd_created/updated/deleted() from int to void, since we don't check it anyway.
ChangeLog v3 -> v4:
- Move patches [3-5] to the first.
- Remove the ending period (".") from all of the patch subjects.
- Rebase the code to v6.17-rc4.
ChangeLog v2 -> v3:
- Properly initialize a return variable in em_notify_pd_created/updated() at an error path (09/10), reported by the kernel test robot [3].
- Remove redundant initialization of a return variable in
em_notify_pd_deleted() at an error path (08/10).
ChangeLog v1 -> v2:
- Use YNL to generate boilerplate code. Overhaul the naming conventions (command, event, notification, attribute) to follow the typical conventions of other YNL-based netlink implementations.
- Calculate the exact message size instead of using NLMSG_GOODSIZE when allocating a message (genlmsg_new). This avoids the reallocation of a message.
- Remove an unnecessary function, em_netlink_exit(), and initialize the netlink (em_netlink_init) at em_netlink.c without touching energy_model.c.
Changwoo Min (10):
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 em.yaml and autogen files
PM: EM: Add a skeleton code for netlink notification
PM: EM: Add an iterator and accessor for the performance domain
PM: EM: Implement em_nl_get_pds_doit()
PM: EM: Implement em_nl_get_pd_table_doit()
PM: EM: Implement em_notify_pd_deleted()
PM: EM: Implement em_notify_pd_created/updated()
PM: EM: Notify an event when the performance domain changes
Documentation/netlink/specs/em.yaml | 113 ++++++++++ MAINTAINERS | 3 + include/linux/energy_model.h | 4 + include/uapi/linux/energy_model.h | 62 ++++++ kernel/power/Makefile | 5 +- kernel/power/em_netlink.c | 311 ++++++++++++++++++++++++++++ kernel/power/em_netlink.h | 39 ++++ kernel/power/em_netlink_autogen.c | 48 +++++ kernel/power/em_netlink_autogen.h | 23 ++ kernel/power/energy_model.c | 83 +++++++- 10 files changed, 689 insertions(+), 2 deletions(-) create mode 100644 Documentation/netlink/specs/em.yaml 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 create mode 100644 kernel/power/em_netlink_autogen.c create mode 100644 kernel/power/em_netlink_autogen.h
Signature · 1 line
2.51.0
Changwoo Min
reviewerPATCH v5 1/10 PM: EM: Assign a unique ID when creating a performance domain
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.
--- include/linux/energy_model.h | 4 ++++ kernel/power/energy_model.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-)
include/linux/energy_model.h +4 -0
@@ -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;
kernel/power/energy_model.c +29 -1
@@ -23,6 +23,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 );
@@ -396,7 +406,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);
@@ -420,6 +430,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;
@@ -444,6 +461,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 ; }
@@ -660,6 +678,10 @@ int em_dev_register_pd_no_update(struct device *dev, unsigned int nr_states, unlock:mutex_unlock (&em_pd_mutex); +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_pd_no_update);
@@ -678,6 +700,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.
@@ -689,6 +715,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);
Signature · 1 line
2.51.0
Re: [PATCH v5 01/10] PM: EM: Assign a unique ID when creating a performance domain
Quoted 115 lines · 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 holdingem_pd_mutexto avoid ABBA deadlock.Signed-off-by Changwoo Min <[email protected]>--- include/linux/energy_model.h | 4 ++++ kernel/power/energy_model.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-)include/linux/energy_model.h +4
-0@@ -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;kernel/power/energy_model.c +27
-1@@ -23,6 +23,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 );@@ -396,7 +406,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);@@ -420,6 +430,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;@@ -444,6 +461,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 ; }@@ -660,6 +678,10 @@ int em_dev_register_pd_no_update(struct device *dev, unsigned int nr_states, unlock:mutex_unlock (&em_pd_mutex); +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_pd_no_update);@@ -678,6 +700,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.
kernel/power/energy_model.c void em_dev_unregister_perf_domain(struct device *dev) · line 722
Quoted 8 lines
kernel/power/energy_model.c +2
-0@@ -689,6 +715,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);
LGTM,
Changwoo Min
reviewerPATCH v5 2/10 PM: EM: Expose the ID of a performance domain via debugfs
In reply to Changwoo MinFor ease of debugging, let's expose the assigned ID of a performance domain through debugfs (e.g., /sys/kernel/debug/energy_model/cpu0/id).
--- kernel/power/energy_model.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
kernel/power/energy_model.c +12 -0
@@ -126,6 +126,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;
@@ -142,6 +152,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)
Signature · 1 line
2.51.0
Changwoo Min
reviewerPATCH v5 3/10 PM: EM: Add em.yaml and autogen files
In reply to Changwoo MinAdd a generic netlink spec in YAML format and autogenerate boilerplate code using ynl-regen.sh to introduce a generic netlink for the energy model. 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_CMD_GET_PDS: Get the list of information for all performance domains.
- EM_CMD_GET_PD_TABLE: Get the energy model table of a performance domain.
Also, it supports three notification events:
- EM_CMD_PD_CREATED: When a performance domain is created.
- EM_CMD_PD_DELETED: When a performance domain is deleted.
- EM_CMD_PD_UPDATED: When the energy model table of a performance domain is updated.
Finally, update MAINTAINERS to include new files.
--- Documentation/netlink/specs/em.yaml | 113 ++++++++++++++++++++++++++++ MAINTAINERS | 3 + include/uapi/linux/energy_model.h | 62 +++++++++++++++ kernel/power/em_netlink_autogen.c | 48 ++++++++++++ kernel/power/em_netlink_autogen.h | 23 ++++++ 5 files changed, 249 insertions(+) create mode 100644 Documentation/netlink/specs/em.yaml create mode 100644 include/uapi/linux/energy_model.h create mode 100644 kernel/power/em_netlink_autogen.c create mode 100644 kernel/power/em_netlink_autogen.h
Documentation/netlink/specs/em.yaml new file +113 -0
@@ -0,0 +1,113 @@ +# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) + +name: em + +doc: | + Energy model netlink interface to notify its changes. + +protocol: genetlink + +uapi-header: linux/energy_model.h + +attribute-sets: + - + name: pds + attributes: + - + name: pd + type: nest + nested-attributes: pd + multi-attr: true + - + name: pd + attributes: + - + name: pad + type: pad + - + name: pd-id + type: u32 + - + name: flags + type: u64 + - + name: cpus + type: string + - + name: pd-table + attributes: + - + name: pd-id + type: u32 + - + name: ps + type: nest + nested-attributes: ps + multi-attr: true + - + name: ps + attributes: + - + name: pad + type: pad + - + name: performance + type: u64 + - + name: frequency + type: u64 + - + name: power + type: u64 + - + name: cost + type: u64 + - + name: flags + type: u64 + +operations: + list: + - + name: get-pds + attribute-set: pds + doc: Get the list of information for all performance domains. + do: + reply: + attributes: + - pd + - + name: get-pd-table + attribute-set: pd-table + doc: Get the energy model table of a performance domain. + do: + request: + attributes: + - pd-id + reply: + attributes: + - pd-id + - ps + - + name: pd-created + doc: A performance domain is created. + notify: get-pd-table + mcgrp: event + - + name: pd-updated + doc: A performance domain is updated. + notify: get-pd-table + mcgrp: event + - + name: pd-deleted + doc: A performance domain is deleted. + attribute-set: pd-table + event: + attributes: + - pd-id + mcgrp: event + +mcast-groups: + list: + - + name: event
MAINTAINERS +3 -0
@@ -9180,6 +9180,9 @@ S: Maintained F: kernel/power/energy_model.c F: include/linux/energy_model.h F: Documentation/power/energy-model.rst +F: Documentation/netlink/specs/em.yaml +F: include/uapi/linux/energy_model.h +F: kernel/power/em_netlink_autogen.* EPAPR HYPERVISOR BYTE CHANNEL DEVICE DRIVER M: Laurentiu Tudor <[email protected]>
include/uapi/linux/energy_model.h new file +62 -0
62 lines not on this page · raw message
kernel/power/em_netlink_autogen.c new file +48 -0
48 lines not on this page · raw message
kernel/power/em_netlink_autogen.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */ +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/em.yaml */ +/* YNL-GEN kernel header */ + +#ifndef _LINUX_EM_GEN_H +#define _LINUX_EM_GEN_H + +#include <net/netlink.h> +#include <net/genetlink.h> + +#include <uapi/linux/energy_model.h> + +int em_nl_get_pds_doit (struct sk_buff *skb,struct genl_info *info); +int em_nl_get_pd_table_doit (struct sk_buff *skb,struct genl_info *info); + +enum { +EM_NLGRP_EVENT , +}; + +extern struct genl_family em_nl_family; + +#endif /* _LINUX_EM_GEN_H */
Signature · 1 line
2.51.0
Changwoo Min
reviewerPATCH v5 4/10 PM: EM: Add a skeleton code for netlink notification
In reply to Changwoo MinAdd a boilerplate code for netlink notification to register the new protocol family. Also, 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.
Finally, update MAINTAINERS to include new files.
--- MAINTAINERS | 2 +- kernel/power/Makefile | 5 ++++- kernel/power/em_netlink.c | 35 +++++++++++++++++++++++++++++++++++ kernel/power/em_netlink.h | 16 ++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 kernel/power/em_netlink.c create mode 100644 kernel/power/em_netlink.h
MAINTAINERS +1 -1
@@ -9182,7 +9182,7 @@ F: include/linux/energy_model.h F: Documentation/power/energy-model.rst F: Documentation/netlink/specs/em.yaml F: include/uapi/linux/energy_model.h -F: kernel/power/em_netlink_autogen.* +F: kernel/power/em_netlink*.* EPAPR HYPERVISOR BYTE CHANNEL DEVICE DRIVER M: Laurentiu Tudor <[email protected]>
kernel/power/Makefile +4 -1
@@ -21,4 +21,7 @@ obj-$(CONFIG_PM_WAKELOCKS) += wakelock.o obj-$(CONFIG_MAGIC_SYSRQ) += poweroff.o -obj-$(CONFIG_ENERGY_MODEL) += energy_model.o +obj-$(CONFIG_ENERGY_MODEL) += em.o +em-y := energy_model.o +em-$(CONFIG_NET) += em_netlink_autogen.o em_netlink.o +
kernel/power/em_netlink.c new file +35 -0
@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* +* Generic netlink for energy model. +* +* Copyright (c) 2025 Valve Corporation. +* Author: Changwoo Min <[email protected]> +*/ + +#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" +#include "em_netlink_autogen.h" + +int em_nl_get_pds_doit (struct sk_buff *skb,struct genl_info *info) +{ +return -EOPNOTSUPP ; +} + +int em_nl_get_pd_table_doit (struct sk_buff *skb,struct genl_info *info) +{ +return -EOPNOTSUPP ; +} + +static int __init em_netlink_init (void ) +{ +return genl_register_family (&em_nl_family); +} +postcore_initcall (em_netlink_init); +
kernel/power/em_netlink.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* +* Generic netlink for energy model. +* +* Copyright (c) 2025 Valve Corporation. +* Author: Changwoo Min <[email protected]> +*/ +#ifndef _EM_NETLINK_H +#define _EM_NETLINK_H + +#if defined(CONFIG_ENERGY_MODEL ) && defined(CONFIG_NET ) +#else +#endif + +#endif /* _EM_NETLINK_H */
Signature · 1 line
2.51.0
Re: [PATCH v5 04/10] PM: EM: Add a skeleton code for netlink notification
Quoted 96 lines · Changwoo Min
Add a boilerplate code for netlink notification to register the new protocol family. Also, 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.
Finally, update
MAINTAINERSto include new files.Signed-off-by Changwoo Min <[email protected]>--- MAINTAINERS | 2 +- kernel/power/Makefile | 5 ++++- kernel/power/em_netlink.c | 35 +++++++++++++++++++++++++++++++++++ kernel/power/em_netlink.h | 16 ++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-)create mode 100644
kernel/power/em_netlink.c
create mode 100644kernel/power/em_netlink.hMAINTAINERS +1
-1@@ -9182,7 +9182,7 @@ F: include/linux/energy_model.h F: Documentation/power/energy-model.rst F: Documentation/netlink/specs/em.yaml F: include/uapi/linux/energy_model.h -F: kernel/power/em_netlink_autogen.* +F: kernel/power/em_netlink*.* EPAPR HYPERVISOR BYTE CHANNEL DEVICE DRIVER M: Laurentiu Tudor <[email protected]>kernel/power/Makefile +4
-1@@ -21,4 +21,7 @@ obj-$(CONFIG_PM_WAKELOCKS) += wakelock.o obj-$(CONFIG_MAGIC_SYSRQ) += poweroff.o -obj-$(CONFIG_ENERGY_MODEL) += energy_model.o +obj-$(CONFIG_ENERGY_MODEL) += em.o +em-y := energy_model.o +em-$(CONFIG_NET) += em_netlink_autogen.o em_netlink.o +kernel/power/em_netlink.c new file +35
-0@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* +* Generic netlink for energy model. +* +* Copyright (c) 2025 Valve Corporation. +* Author: Changwoo Min <[email protected]> +*/ + +#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" +#include "em_netlink_autogen.h" + +int em_nl_get_pds_doit (struct sk_buff *skb,struct genl_info *info) +{ +return -EOPNOTSUPP ; +} + +int em_nl_get_pd_table_doit (struct sk_buff *skb,struct genl_info *info) +{ +return -EOPNOTSUPP ; +} + +static int __init em_netlink_init (void ) +{ +return genl_register_family (&em_nl_family); +} +postcore_initcall (em_netlink_init); +kernel/power/em_netlink.h new file +6
-0@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* +* Generic netlink for energy model. +* +* Copyright (c) 2025 Valve Corporation.
kernel/power/em_netlink.h line 16
Quoted 10 lines
kernel/power/em_netlink.h new file +10
-0+* Author: Changwoo Min <[email protected]> +*/ +#ifndef _EM_NETLINK_H +#define _EM_NETLINK_H + +#if defined(CONFIG_ENERGY_MODEL ) && defined(CONFIG_NET ) +#else +#endif + +#endif /* _EM_NETLINK_H */
LGTM,
Changwoo Min
reviewerPATCH v5 5/10 PM: EM: Add an iterator and accessor for the performance domain
In reply to Changwoo MinAdd 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.
--- kernel/power/em_netlink.h | 14 ++++++++++++++ kernel/power/energy_model.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+)
kernel/power/em_netlink.h +14 -0
@@ -10,7 +10,21 @@#define _EM_NETLINK_H#if defined(CONFIG_ENERGY_MODEL ) && defined(CONFIG_NET ) +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 +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 #endif /* _EM_NETLINK_H */
kernel/power/energy_model.c +36 -0
@@ -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.
@@ -998,3 +1000,37 @@ 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 ; +}
Signature · 1 line
2.51.0
Re: [PATCH v5 05/10] PM: EM: Add an iterator and accessor for the performance domain
Quoted 80 lines · 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 <[email protected]>--- kernel/power/em_netlink.h | 14 ++++++++++++++ kernel/power/energy_model.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+)kernel/power/em_netlink.h +14
-0@@ -10,7 +10,21 @@#define _EM_NETLINK_H#if defined(CONFIG_ENERGY_MODEL ) && defined(CONFIG_NET ) +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 +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 #endif /* _EM_NETLINK_H */ kernel/power/energy_model.c +26
-0@@ -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. @@ -998,3 +1000,37 @@ 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; +
kernel/power/energy_model.c void em_rebuild_sched_domains(void) · line 1036
Quoted 10 lines
kernel/power/energy_model.c +10
-0+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 ; +}
LGTM,
kernel test robot
build botRe: [PATCH v5 05/10] PM: EM: Add an iterator and accessor for the performance domain
In reply to Changwoo Minkernel test robot noticed the following build errors:
Hi Changwoo,
kernel test robot noticed the following build errors:
[auto build test ERROR on amd-pstate/linux-next]
[also build test ERROR on amd-pstate/bleeding-edge linus/master v6.18-rc1 next-20251014] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Changwoo-Min/PM-EM-Assign-a-unique-ID-when-creating-a-performance-domain/20251014-082420 base: https://git.kernel.org/pub/scm/linux/kernel/git/superm1/linux.git linux-next patch link: https://lore.kernel.org/r/20251014001055.772422-6-changwoo%40igalia.com patch subject: [PATCH v5 05/10] PM: EM: Add an iterator and accessor for the performance domain config: i386-buildonly-randconfig-001-20251015 (https://download.01.org/0day-ci/archive/20251015/[email protected]/config) compiler: gcc-13 (Debian 13.3.0-16) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251015/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> kernel/power/energy_model.c:1003:5: error: redefinition of 'for_each_em_perf_domain'
1003 | int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),
| ^~~~~~~~~~~~~~~~~~~~~~~
In file included from kernel/power/energy_model.c:20:
kernel/power/em_netlink.h:18:5: note: previous definition of 'for_each_em_perf_domain' with type 'int(int (*)(struct em_perf_domain *, void *), void *)'
18 | int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),
| ^~~~~~~~~~~~~~~~~~~~~~~
>> kernel/power/energy_model.c:1022:24: error: redefinition of 'em_perf_domain_get_by_id' 1022 | struct em_perf_domain *em_perf_domain_get_by_id(int id)
| ^~~~~~~~~~~~~~~~~~~~~~~~
kernel/power/em_netlink.h:24:24: note: previous definition of 'em_perf_domain_get_by_id' with type 'struct em_perf_domain *(int)'
24 | struct em_perf_domain *em_perf_domain_get_by_id(int id)
| ^~~~~~~~~~~~~~~~~~~~~~~~
vim +/for_each_em_perf_domain +1003 kernel/power/energy_model.c
1002
> 1003 int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),
1004 void *data)
1005 {
1006 struct em_perf_domain *pd;
1007
1008 lockdep_assert_not_held(&em_pd_mutex);
1009 guard(mutex)(&em_pd_list_mutex);
1010
1011 list_for_each_entry(pd, &em_pd_list, node) {
1012 int ret;
1013
1014 ret = cb(pd, data);
1015 if (ret)
1016 return ret;
1017 }
1018
1019 return 0;
1020 }
1021
> 1022 struct em_perf_domain *em_perf_domain_get_by_id(int id)Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Rafael J. Wysocki
maintainerRe: [PATCH v5 05/10] PM: EM: Add an iterator and accessor for the performance domain
Quoted 33 lines · kernel test robot
Hi Changwoo,
kernel test robot noticed the following build errors:
[auto build test ERROR on amd-pstate/linux-next]
[also build test ERROR on amd-pstate/bleeding-edge linus/master v6.18-rc1 next-20251014] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]url: https://github.com/intel-lab-lkp/linux/commits/Changwoo-Min/PM-EM-Assign-a-unique-ID-when-creating-a-performance-domain/20251014-082420 base: https://git.kernel.org/pub/scm/linux/kernel/git/superm1/linux.git linux-next patch link: https://lore.kernel.org/r/20251014001055.772422-6-changwoo%40igalia.com patch subject: [PATCH v5 05/10] PM: EM: Add an iterator and accessor for the performance domain config: i386-buildonly-randconfig-001-20251015 (https://download.01.org/0day-ci/archive/20251015/[email protected]/config) compiler: gcc-13 (Debian 13.3.0-16) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251015/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>):
Quoted 1 line
Quoted 1 line
kernel/power/energy_model.c:1003:5: error: redefinition of 'for_each_em_perf_domain'1003 | int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *), | ^~~~~~~~~~~~~~~~~~~~~~~ In file included from kernel/power/energy_model.c:20: kernel/power/em_netlink.h:18:5: note: previous definition of 'for_each_em_perf_domain' with type 'int(int (*)(struct em_perf_domain *, void *), void *)' 18 | int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *), | ^~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines
Quoted 1 line
Quoted 1 line
kernel/power/energy_model.c:1022:24: error: redefinition of 'em_perf_domain_get_by_id'1022 | struct
em_perf_domain*em_perf_domain_get_by_id(int id)
| ^~~~~~~~~~~~~~~~~~~~~~~~
kernel/power/em_netlink.h:24:24: note: previous definition of 'em_perf_domain_get_by_id' with type 'structem_perf_domain*(int)'
24 | structem_perf_domain*em_perf_domain_get_by_id(int id)
| ^~~~~~~~~~~~~~~~~~~~~~~~
Please update the patch to address this report and resend it, thanks!
Quoted 27 lines
vim +/for_each_em_perf_domain +1003
kernel/power/energy_model.c1002
Quoted 1 line
1003 int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),
1004 void *data)
1005 {
1006 structem_perf_domain*pd;
1007
1008lockdep_assert_not_held(&em_pd_mutex);
1009 guard(mutex)(&em_pd_list_mutex);
1010
1011list_for_each_entry(pd, &em_pd_list, node){
1012 int ret;
1013
1014 ret =cb(pd, data);
1015 if (ret)
1016 return ret;
1017 }
1018
1019 return 0;
1020 }
1021Quoted 1 line
1022 struct
em_perf_domain*em_perf_domain_get_by_id(int id)--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Changwoo Min
reviewerRe: [PATCH v5 05/10] PM: EM: Add an iterator and accessor for the performance domain
Hi Rafael,
Quoted 35 lines · Rafael J. Wysocki
Quoted 34 lines · kernel test robot
Hi Changwoo,
kernel test robot noticed the following build errors:
[auto build test ERROR on amd-pstate/linux-next]
[also build test ERROR on amd-pstate/bleeding-edge linus/master v6.18-rc1 next-20251014] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]url: https://github.com/intel-lab-lkp/linux/commits/Changwoo-Min/PM-EM-Assign-a-unique-ID-when-creating-a-performance-domain/20251014-082420 base: https://git.kernel.org/pub/scm/linux/kernel/git/superm1/linux.git linux-next patch link: https://lore.kernel.org/r/20251014001055.772422-6-changwoo%40igalia.com patch subject: [PATCH v5 05/10] PM: EM: Add an iterator and accessor for the performance domain config: i386-buildonly-randconfig-001-20251015 (https://download.01.org/0day-ci/archive/20251015/[email protected]/config) compiler: gcc-13 (Debian 13.3.0-16) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251015/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>):
Quoted 1 line
Quoted 1 line
kernel/power/energy_model.c:1003:5: error: redefinition of 'for_each_em_perf_domain'1003 | int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *), | ^~~~~~~~~~~~~~~~~~~~~~~ In file included from kernel/power/energy_model.c:20: kernel/power/em_netlink.h:18:5: note: previous definition of 'for_each_em_perf_domain' with type 'int(int (*)(struct em_perf_domain *, void *), void *)' 18 | int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *), | ^~~~~~~~~~~~~~~~~~~~~~~Quoted 1 line
Quoted 1 line
kernel/power/energy_model.c:1022:24: error: redefinition of 'em_perf_domain_get_by_id'
Quoted 8 lines
Quoted 6 lines
1022 | struct
em_perf_domain*em_perf_domain_get_by_id(int id)
| ^~~~~~~~~~~~~~~~~~~~~~~~
kernel/power/em_netlink.h:24:24: note: previous definition of 'em_perf_domain_get_by_id' with type 'structem_perf_domain*(int)'
24 | structem_perf_domain*em_perf_domain_get_by_id(int id)
| ^~~~~~~~~~~~~~~~~~~~~~~~Please update the patch to address this report and resend it, thanks!
Sure, I will send v6 with these two fixes in.
Regards,
Changwoo Min
Changwoo Min
reviewerPATCH v5 6/10 PM: EM: Implement em_nl_get_pds_doit()
In reply to Changwoo MinWhen a userspace requests EM_CMD_GET_PDS, the kernel responds with information on all performance domains. The message format of the response is as follows:
EM_A_PDS_PD (NLA_NESTED)*
EM_A_PD_PD_ID (NLA_U32)
EM_A_PD_FLAGS (NLA_U64)
EM_A_PD_CPUS (NLA_STRING)where EM_A_PDS_PD can be repeated as many times as there are performance domains, and EM_A_PD_CPUS is a hexadecimal string representing a CPU bitmask.
--- kernel/power/em_netlink.c | 82 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-)
kernel/power/em_netlink.c +81 -1
@@ -17,9 +17,89 @@#include "em_netlink.h" #include "em_netlink_autogen.h" +#define EM_A_PD_CPUS_LEN 256 + +/*************************** Command encoding ********************************/ +static int __em_nl_get_pd_size (struct em_perf_domain *pd,void *data) +{ +char cpus_buf[EM_A_PD_CPUS_LEN ]; +int *tot_msg_sz = data; +int msg_sz, cpus_sz; + + cpus_sz =snprintf (cpus_buf,sizeof (cpus_buf),"%*pb" , +cpumask_pr_args (to_cpumask (pd->cpus))); + + msg_sz =nla_total_size (0 ) +/* EM_A_PDS_PD */ +nla_total_size (sizeof (u32)) +/* EM_A_PD_PD_ID */ +nla_total_size_64bit (sizeof (u64)) +/* EM_A_PD_FLAGS */ +nla_total_size (cpus_sz);/* EM_A_PD_CPUS */ + + *tot_msg_sz +=nlmsg_total_size (genlmsg_msg_size (msg_sz)); +return 0 ; +} + +static int __em_nl_get_pd (struct em_perf_domain *pd,void *data) +{ +char cpus_buf[EM_A_PD_CPUS_LEN ]; +struct sk_buff *msg = data; +struct nlattr *entry; + + entry =nla_nest_start (msg,EM_A_PDS_PD ); +if (!entry) + goto out_cancel_nest; + +if (nla_put_u32 (msg,EM_A_PD_PD_ID , pd->id)) + goto out_cancel_nest; + +if (nla_put_u64_64bit (msg,EM_A_PD_FLAGS , pd->flags,EM_A_PD_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_A_PD_CPUS , cpus_buf)) + goto out_cancel_nest; + +nla_nest_end (msg, entry); + +return 0 ; + +out_cancel_nest: +nla_nest_cancel (msg, entry); + +return -EMSGSIZE ; +} +int em_nl_get_pds_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +struct sk_buff *msg; +void *hdr; +int cmd = info->genlhdr->cmd; +int ret = -EMSGSIZE , msg_sz =0 ; + +for_each_em_perf_domain (__em_nl_get_pd_size, &msg_sz); + + msg =genlmsg_new (msg_sz,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + hdr =genlmsg_put_reply (msg, info, &em_nl_family,0 , cmd); +if (!hdr) + goto out_free_msg; + + ret =for_each_em_perf_domain (__em_nl_get_pd, msg); +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; }int em_nl_get_pd_table_doit (struct sk_buff *skb,struct genl_info *info)
Signature · 1 line
2.51.0
Changwoo Min
reviewerPATCH v5 7/10 PM: EM: Implement em_nl_get_pd_table_doit()
In reply to Changwoo MinWhen a userspace requests EM_CMD_GET_PD_TABLE 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_A_PD_TABLE_PD_ID (NLA_U32)EM_A_PD_TABLE_PS (NLA_NESTED)*
EM_A_PS_PERFORMANCE (NLA_U64)
EM_A_PS_FREQUENCY (NLA_U64)
EM_A_PS_POWER (NLA_U64)
EM_A_PS_COST (NLA_U64)
EM_A_PS_FLAGS (NLA_U64)where EM_A_PD_TABLE_PS can be repeated as many times as there are performance states (struct em_perf_state).
--- kernel/power/em_netlink.c | 108 +++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-)
kernel/power/em_netlink.c +107 -1
@@ -102,9 +102,115 @@ int em_nl_get_pds_doit(struct sk_buff *skb, struct genl_info *info)return ret; } +static struct em_perf_domain *__em_nl_get_pd_table_id (struct nlattr **attrs) +{ +struct em_perf_domain *pd; +int id; + +if (!attrs[EM_A_PD_TABLE_PD_ID ]) +return NULL ; + + id =nla_get_u32 (attrs[EM_A_PD_TABLE_PD_ID ]); + pd =em_perf_domain_get_by_id (id); +return pd; +} + +static int __em_nl_get_pd_table_size (const struct em_perf_domain *pd) +{ +int id_sz, ps_sz; + + id_sz =nla_total_size (sizeof (u32));/* EM_A_PD_TABLE_PD_ID */ + ps_sz =nla_total_size (0 ) +/* EM_A_PD_TABLE_PS */ +nla_total_size_64bit (sizeof (u64)) +/* EM_A_PS_PERFORMANCE */ +nla_total_size_64bit (sizeof (u64)) +/* EM_A_PS_FREQUENCY */ +nla_total_size_64bit (sizeof (u64)) +/* EM_A_PS_POWER */ +nla_total_size_64bit (sizeof (u64)) +/* EM_A_PS_COST */ +nla_total_size_64bit (sizeof (u64));/* EM_A_PS_FLAGS */ + ps_sz *= pd->nr_perf_states; + +return nlmsg_total_size (genlmsg_msg_size (id_sz + ps_sz)); +} + +static int __em_nl_get_pd_table (struct sk_buff *msg,const struct em_perf_domain *pd) +{ +struct em_perf_state *table, *ps; +struct nlattr *entry; +int i; + +if (nla_put_u32 (msg,EM_A_PD_TABLE_PD_ID , pd->id)) + goto out_err; + +rcu_read_lock (); + table =em_perf_state_from_pd ((struct em_perf_domain *)pd); + +for (i =0 ; i < pd->nr_perf_states; i++) { + ps = &table[i]; + + entry =nla_nest_start (msg,EM_A_PD_TABLE_PS ); +if (!entry) + goto out_unlock_ps; + +if (nla_put_u64_64bit (msg,EM_A_PS_PERFORMANCE , + ps->performance,EM_A_PS_PAD )) + goto out_cancel_ps_nest; +if (nla_put_u64_64bit (msg,EM_A_PS_FREQUENCY , + ps->frequency,EM_A_PS_PAD )) + goto out_cancel_ps_nest; +if (nla_put_u64_64bit (msg,EM_A_PS_POWER , + ps->power,EM_A_PS_PAD )) + goto out_cancel_ps_nest; +if (nla_put_u64_64bit (msg,EM_A_PS_COST , + ps->cost,EM_A_PS_PAD )) + goto out_cancel_ps_nest; +if (nla_put_u64_64bit (msg,EM_A_PS_FLAGS , + ps->flags,EM_A_PS_PAD )) + goto out_cancel_ps_nest; + +nla_nest_end (msg, entry); + } +rcu_read_unlock (); +return 0 ; + +out_cancel_ps_nest: +nla_nest_cancel (msg, entry); +out_unlock_ps: +rcu_read_unlock (); +out_err: +return -EMSGSIZE ; +} +int em_nl_get_pd_table_doit (struct sk_buff *skb,struct genl_info *info) { -return -EOPNOTSUPP ; +int cmd = info->genlhdr->cmd; +int msg_sz, ret = -EMSGSIZE ; +struct em_perf_domain *pd; +struct sk_buff *msg; +void *hdr; + + pd =__em_nl_get_pd_table_id (info->attrs); +if (!pd) +return -EINVAL ; + + msg_sz =__em_nl_get_pd_table_size (pd); + + msg =genlmsg_new (msg_sz,GFP_KERNEL ); +if (!msg) +return -ENOMEM ; + + hdr =genlmsg_put_reply (msg, info, &em_nl_family,0 , cmd); +if (!hdr) + goto out_free_msg; + + ret =__em_nl_get_pd_table (msg, pd); +if (ret) + goto out_free_msg; + +genlmsg_end (msg, hdr); +return genlmsg_reply (msg, info); + +out_free_msg: +nlmsg_free (msg); +return ret; }static int __init em_netlink_init (void )
Signature · 1 line
2.51.0
Changwoo Min
reviewerPATCH v5 8/10 PM: EM: Implement em_notify_pd_deleted()
In reply to Changwoo MinAdd the event notification infrastructure and implement the event notification for when a performance domain is deleted (EM_CMD_PD_DELETED).
The event contains the ID of the performance domain (EM_A_PD_TABLE_PD_ID) so the userspace can identify the changed performance domain for further processing.
--- kernel/power/em_netlink.c | 46 +++++++++++++++++++++++++++++++++++++++ kernel/power/em_netlink.h | 3 +++ 2 files changed, 49 insertions(+)
kernel/power/em_netlink.c +46 -0
@@ -213,6 +213,52 @@ int em_nl_get_pd_table_doit(struct sk_buff *skb, struct genl_info *info)return ret; } + +/**************************** Event encoding *********************************/ +static int __em_notify_pd_deleted_size (const struct em_perf_domain *pd) +{ +int id_sz =nla_total_size (sizeof (u32));/* EM_A_PD_TABLE_PD_ID */ + +return nlmsg_total_size (genlmsg_msg_size (id_sz)); +} + +void em_notify_pd_deleted (const struct em_perf_domain *pd) +{ +struct sk_buff *msg; +int ret = -EMSGSIZE ; +void *hdr; +int msg_sz; + +if (!genl_has_listeners (&em_nl_family, &init_net,EM_NLGRP_EVENT )) +return ; + + msg_sz =__em_notify_pd_deleted_size (pd); + + msg =genlmsg_new (msg_sz,GFP_KERNEL ); +if (!msg) +return ; + + hdr =genlmsg_put (msg,0 ,0 , &em_nl_family,0 ,EM_CMD_PD_DELETED ); +if (!hdr) + goto out_free_msg; + +if (nla_put_u32 (msg,EM_A_PD_TABLE_PD_ID , pd->id)) { + ret = -EMSGSIZE ; + goto out_free_msg; + } + +genlmsg_end (msg, hdr); + +genlmsg_multicast (&em_nl_family, msg,0 ,EM_NLGRP_EVENT ,GFP_KERNEL ); + +return ; + +out_free_msg: +nlmsg_free (msg); +return ; +} + +/**************************** Initialization *********************************/ static int __init em_netlink_init (void ) {return genl_register_family (&em_nl_family);
kernel/power/em_netlink.h +3 -0
@@ -13,6 +13,7 @@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); +void em_notify_pd_deleted (const struct em_perf_domain *pd);#else static inline int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),
@@ -25,6 +26,8 @@ struct em_perf_domain *em_perf_domain_get_by_id(int id) {return NULL ; } + +static inline void em_notify_pd_deleted (const struct em_perf_domain *pd) {}#endif #endif /* _EM_NETLINK_H */
Signature · 1 line
2.51.0
Re: [PATCH v5 08/10] PM: EM: Implement em_notify_pd_deleted()
Quoted 83 lines · Changwoo Min
Add the event notification infrastructure and implement the event notification for when a performance domain is deleted (
EM_CMD_PD_DELETED).The event contains the ID of the performance domain (
EM_A_PD_TABLE_PD_ID) so the userspace can identify the changed performance domain for further processing.Signed-off-by Changwoo Min <[email protected]>--- kernel/power/em_netlink.c | 46 +++++++++++++++++++++++++++++++++++++++ kernel/power/em_netlink.h | 3 +++ 2 files changed, 49 insertions(+)kernel/power/em_netlink.c +46
-0@@ -213,6 +213,52 @@ int em_nl_get_pd_table_doit(struct sk_buff *skb, struct genl_info *info)return ret; } + +/**************************** Event encoding *********************************/ +static int __em_notify_pd_deleted_size (const struct em_perf_domain *pd) +{ +int id_sz =nla_total_size (sizeof (u32));/* EM_A_PD_TABLE_PD_ID */ + +return nlmsg_total_size (genlmsg_msg_size (id_sz)); +} + +void em_notify_pd_deleted (const struct em_perf_domain *pd) +{ +struct sk_buff *msg; +int ret = -EMSGSIZE ; +void *hdr; +int msg_sz; + +if (!genl_has_listeners (&em_nl_family, &init_net,EM_NLGRP_EVENT )) +return ; + + msg_sz =__em_notify_pd_deleted_size (pd); + + msg =genlmsg_new (msg_sz,GFP_KERNEL ); +if (!msg) +return ; + + hdr =genlmsg_put (msg,0 ,0 , &em_nl_family,0 ,EM_CMD_PD_DELETED ); +if (!hdr) + goto out_free_msg; + +if (nla_put_u32 (msg,EM_A_PD_TABLE_PD_ID , pd->id)) { + ret = -EMSGSIZE ; + goto out_free_msg; + } + +genlmsg_end (msg, hdr); + +genlmsg_multicast (&em_nl_family, msg,0 ,EM_NLGRP_EVENT ,GFP_KERNEL ); + +return ; + +out_free_msg: +nlmsg_free (msg); +return ; +} + +/**************************** Initialization *********************************/ static int __init em_netlink_init (void ) {return genl_register_family (&em_nl_family);kernel/power/em_netlink.h +1
-0@@ -13,6 +13,7 @@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); +void em_notify_pd_deleted (const struct em_perf_domain *pd);#else static inline int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),
kernel/power/em_netlink.h struct em_perf_domain *em_perf_domain_get_by_id(int id) · line 33
Quoted 8 lines
kernel/power/em_netlink.h +2
-0@@ -25,6 +26,8 @@ struct em_perf_domain *em_perf_domain_get_by_id(int id) {return NULL ; } + +static inline void em_notify_pd_deleted (const struct em_perf_domain *pd) {}#endif #endif /* _EM_NETLINK_H */
LGTM,
kernel test robot
build botRe: [PATCH v5 08/10] PM: EM: Implement em_notify_pd_deleted()
In reply to Changwoo Minkernel test robot noticed the following build warnings:
Hi Changwoo,
kernel test robot noticed the following build warnings:
[auto build test WARNING on amd-pstate/linux-next]
[also build test WARNING on amd-pstate/bleeding-edge linus/master v6.18-rc1 next-20251014] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Changwoo-Min/PM-EM-Assign-a-unique-ID-when-creating-a-performance-domain/20251014-082420 base: https://git.kernel.org/pub/scm/linux/kernel/git/superm1/linux.git linux-next patch link: https://lore.kernel.org/r/20251014001055.772422-9-changwoo%40igalia.com patch subject: [PATCH v5 08/10] PM: EM: Implement em_notify_pd_deleted() config: i386-buildonly-randconfig-004-20251015 (https://download.01.org/0day-ci/archive/20251015/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251015/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> kernel/power/em_netlink.c:228:6: warning: variable 'ret' set but not used [-Wunused-but-set-variable]
228 | int ret = -EMSGSIZE;
| ^1 warning generated.
vim +/ret +228 kernel/power/em_netlink.c
224
225 void em_notify_pd_deleted(const struct em_perf_domain *pd)
226 {
227 struct sk_buff *msg;
> 228 int ret = -EMSGSIZE;
229 void *hdr;
230 int msg_sz;
231
232 if (!genl_has_listeners(&em_nl_family, &init_net, EM_NLGRP_EVENT))
233 return;
234
235 msg_sz = __em_notify_pd_deleted_size(pd);
236
237 msg = genlmsg_new(msg_sz, GFP_KERNEL);
238 if (!msg)
239 return;
240
241 hdr = genlmsg_put(msg, 0, 0, &em_nl_family, 0, EM_CMD_PD_DELETED);
242 if (!hdr)
243 goto out_free_msg;
244
245 if (nla_put_u32(msg, EM_A_PD_TABLE_PD_ID, pd->id)) {
246 ret = -EMSGSIZE;
247 goto out_free_msg;
248 }
249
250 genlmsg_end(msg, hdr);
251
252 genlmsg_multicast(&em_nl_family, msg, 0, EM_NLGRP_EVENT, GFP_KERNEL);
253
254 return;
255
256 out_free_msg:
257 nlmsg_free(msg);
258 return;
259 }
260
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Rafael J. Wysocki
maintainerRe: [PATCH v5 08/10] PM: EM: Implement em_notify_pd_deleted()
Quoted 24 lines · kernel test robot
Hi Changwoo,
kernel test robot noticed the following build warnings:
[auto build test WARNING on amd-pstate/linux-next]
[also build test WARNING on amd-pstate/bleeding-edge linus/master v6.18-rc1 next-20251014] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]url: https://github.com/intel-lab-lkp/linux/commits/Changwoo-Min/PM-EM-Assign-a-unique-ID-when-creating-a-performance-domain/20251014-082420 base: https://git.kernel.org/pub/scm/linux/kernel/git/superm1/linux.git linux-next patch link: https://lore.kernel.org/r/20251014001055.772422-9-changwoo%40igalia.com patch subject: [PATCH v5 08/10] PM: EM: Implement em_notify_pd_deleted() config: i386-buildonly-randconfig-004-20251015 (https://download.01.org/0day-ci/archive/20251015/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251015/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
Quoted 7 lines
All warnings (new ones prefixed by >>):
Quoted 1 line
Quoted 1 line
kernel/power/em_netlink.c:228:6: warning: variable 'ret' set but not used [-Wunused-but-set-variable]228 | int ret = -EMSGSIZE; | ^1 warning generated.
Please update the patch to address this report and resend it, thanks!
Quoted 43 lines
vim +/ret +228
kernel/power/em_netlink.c224 225 void em_notify_pd_deleted(const struct em_perf_domain *pd) 226 { 227 struct sk_buff *msg;Quoted 1 line
228 int ret =
-EMSGSIZE;229 void *hdr; 230 int msg_sz; 231 232 if (!genl_has_listeners(&em_nl_family, &init_net, EM_NLGRP_EVENT)) 233 return; 234 235 msg_sz = __em_notify_pd_deleted_size(pd); 236 237 msg = genlmsg_new(msg_sz, GFP_KERNEL); 238 if (!msg) 239 return; 240 241 hdr = genlmsg_put(msg, 0, 0, &em_nl_family, 0, EM_CMD_PD_DELETED); 242 if (!hdr) 243 goto out_free_msg; 244 245 if (nla_put_u32(msg, EM_A_PD_TABLE_PD_ID, pd->id)) { 246 ret = -EMSGSIZE; 247 goto out_free_msg; 248 } 249 250 genlmsg_end(msg, hdr); 251 252 genlmsg_multicast(&em_nl_family, msg, 0, EM_NLGRP_EVENT, GFP_KERNEL); 253 254 return; 255 256 out_free_msg: 257 nlmsg_free(msg); 258 return; 259 } 260--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Changwoo Min
reviewerPATCH v5 9/10 PM: EM: Implement em_notify_pd_created/updated()
In reply to Changwoo MinImplement two event notifications when a performance domain is created (EM_CMD_PD_CREATED) and updated (EM_CMD_PD_UPDATED). The message format of these two event notifications is the same as EM_CMD_GET_PD_TABLE -- containing the performance domain's ID and its energy model table.
--- kernel/power/em_netlink.c | 44 +++++++++++++++++++++++++++++++++++++++ kernel/power/em_netlink.h | 6 ++++++ 2 files changed, 50 insertions(+)
kernel/power/em_netlink.c +44 -0
@@ -215,6 +215,50 @@ int em_nl_get_pd_table_doit(struct sk_buff *skb, struct genl_info *info)/**************************** Event encoding *********************************/ +static void __em_notify_pd_table (const struct em_perf_domain *pd,int ntf_type) +{ +struct sk_buff *msg; +int msg_sz, ret = -EMSGSIZE ; +void *hdr; + +if (!genl_has_listeners (&em_nl_family, &init_net,EM_NLGRP_EVENT )) +return ; + + msg_sz =__em_nl_get_pd_table_size (pd); + + msg =genlmsg_new (msg_sz,GFP_KERNEL ); +if (!msg) +return ; + + hdr =genlmsg_put (msg,0 ,0 , &em_nl_family,0 , ntf_type); +if (!hdr) + goto out_free_msg; + + ret =__em_nl_get_pd_table (msg, pd); +if (ret) + goto out_free_msg; + +genlmsg_end (msg, hdr); + +genlmsg_multicast (&em_nl_family, msg,0 ,EM_NLGRP_EVENT ,GFP_KERNEL ); + +return ; + +out_free_msg: +nlmsg_free (msg); +return ; +} + +void em_notify_pd_created (const struct em_perf_domain *pd) +{ +__em_notify_pd_table (pd,EM_CMD_PD_CREATED ); +} + +void em_notify_pd_updated (const struct em_perf_domain *pd) +{ +__em_notify_pd_table (pd,EM_CMD_PD_UPDATED ); +} +static int __em_notify_pd_deleted_size (const struct em_perf_domain *pd) {int id_sz =nla_total_size (sizeof (u32));/* EM_A_PD_TABLE_PD_ID */
kernel/power/em_netlink.h +6 -0
@@ -13,7 +13,9 @@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); +void em_notify_pd_created (const struct em_perf_domain *pd);void em_notify_pd_deleted (const struct em_perf_domain *pd); +void em_notify_pd_updated (const struct em_perf_domain *pd);#else static inline int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),
@@ -27,7 +29,11 @@ struct em_perf_domain *em_perf_domain_get_by_id(int id)return NULL ; } +static inline void em_notify_pd_created (const struct em_perf_domain *pd) {} +static inline void em_notify_pd_deleted (const struct em_perf_domain *pd) {} + +static inline void em_notify_pd_updated (const struct em_perf_domain *pd) {}#endif #endif /* _EM_NETLINK_H */
Signature · 1 line
2.51.0
Re: [PATCH v5 09/10] PM: EM: Implement em_notify_pd_created/updated()
Quoted 82 lines · Changwoo Min
Implement two event notifications when a performance domain is created (
EM_CMD_PD_CREATED) and updated (EM_CMD_PD_UPDATED). The message format of these two event notifications is the same asEM_CMD_GET_PD_TABLE-- containing the performance domain's ID and its energy model table.Signed-off-by Changwoo Min <[email protected]>--- kernel/power/em_netlink.c | 44 +++++++++++++++++++++++++++++++++++++++ kernel/power/em_netlink.h | 6 ++++++ 2 files changed, 50 insertions(+)kernel/power/em_netlink.c +44
-0@@ -215,6 +215,50 @@ int em_nl_get_pd_table_doit(struct sk_buff *skb, struct genl_info *info)/**************************** Event encoding *********************************/ +static void __em_notify_pd_table (const struct em_perf_domain *pd,int ntf_type) +{ +struct sk_buff *msg; +int msg_sz, ret = -EMSGSIZE ; +void *hdr; + +if (!genl_has_listeners (&em_nl_family, &init_net,EM_NLGRP_EVENT )) +return ; + + msg_sz =__em_nl_get_pd_table_size (pd); + + msg =genlmsg_new (msg_sz,GFP_KERNEL ); +if (!msg) +return ; + + hdr =genlmsg_put (msg,0 ,0 , &em_nl_family,0 , ntf_type); +if (!hdr) + goto out_free_msg; + + ret =__em_nl_get_pd_table (msg, pd); +if (ret) + goto out_free_msg; + +genlmsg_end (msg, hdr); + +genlmsg_multicast (&em_nl_family, msg,0 ,EM_NLGRP_EVENT ,GFP_KERNEL ); + +return ; + +out_free_msg: +nlmsg_free (msg); +return ; +} + +void em_notify_pd_created (const struct em_perf_domain *pd) +{ +__em_notify_pd_table (pd,EM_CMD_PD_CREATED ); +} + +void em_notify_pd_updated (const struct em_perf_domain *pd) +{ +__em_notify_pd_table (pd,EM_CMD_PD_UPDATED ); +} +static int __em_notify_pd_deleted_size (const struct em_perf_domain *pd) {int id_sz =nla_total_size (sizeof (u32));/* EM_A_PD_TABLE_PD_ID */ kernel/power/em_netlink.h +2
-0@@ -13,7 +13,9 @@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); +void em_notify_pd_created (const struct em_perf_domain *pd);void em_notify_pd_deleted (const struct em_perf_domain *pd); +void em_notify_pd_updated (const struct em_perf_domain *pd);#else static inline int for_each_em_perf_domain(int (*cb)(struct em_perf_domain*, void *),@@ -27,7 +29,11 @@ struct em_perf_domain *em_perf_domain_get_by_id(int id)return NULL ;
kernel/power/em_netlink.h struct em_perf_domain *em_perf_domain_get_by_id(int id) · line 39
Quoted 10 lines
kernel/power/em_netlink.h +4
-0} +static inline void em_notify_pd_created (const struct em_perf_domain *pd) {} +static inline void em_notify_pd_deleted (const struct em_perf_domain *pd) {} + +static inline void em_notify_pd_updated (const struct em_perf_domain *pd) {}#endif #endif /* _EM_NETLINK_H */
Changwoo Min
reviewerPATCH v5 10/10 PM: EM: Notify an event when the performance domain changes
In reply to Changwoo MinSend an event to userspace when a performance domain is created or deleted, or its energy model is updated.
--- kernel/power/energy_model.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/power/energy_model.c +5 -0
@@ -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_updated (pd);return 0 ; }EXPORT_SYMBOL_GPL (em_dev_update_perf_domain);
@@ -696,6 +698,7 @@ int em_dev_register_pd_no_update(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_created (dev->em_pd);return ret; }EXPORT_SYMBOL_GPL (em_dev_register_pd_no_update);
@@ -718,6 +721,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_deleted (dev->em_pd); +/* * The mutex separates all register/unregister requests and protects * from potential clean-up/setup issues in the debugfs directories.
Signature · 1 line
2.51.0
Lukasz Luba
maintainerQuoted 43 lines · Changwoo Min
Addressed all the comments from Lukasz and rebased the code to the head of the linus tree.
There is a need to access the energy model from the userspace. One such example is the
sched_extschedulers [1]. The userspace part of thesched_extschedules 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_CMD_GET_PDS: Get the list of information for all performance domains.
-EM_CMD_GET_PD_TABLE: Get the energy model table of a performance domain.Also, it supports three notification events:
-EM_CMD_PD_CREATED: When a performance domain is created.
-EM_CMD_PD_DELETED: When a performance domain is deleted.
-EM_CMD_PD_UPDATED: When the energy model table of a performance domain is updated.This can be tested using the tool,
tools/net/ynl/pyynl/cli.py, for example, with the following commands:$> tools/net/ynl/pyynl/cli.py \ --spec Documentation/netlink/specs/em.yaml \ --do get-pds $> tools/net/ynl/pyynl/cli.py \ --spec Documentation/netlink/specs/em.yaml \ --do get-pd-table --json '{"pd-id": 0}' $> tools/net/ynl/pyynl/cli.py \ --spec Documentation/netlink/specs/em.yaml \ --subscribe event --sleep 10
Quoted 3 lines
[1] https://lwn.net/Articles/922405/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/#t
Thank you Changwoo, it looks good.
The patches can go now into some PM testing branches so we can check the integration/testing in wider configurations.
Regards,
Lukasz