1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
// 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"
#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)
{
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;
}
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)
{
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;
}
/**************************** 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 */
return nlmsg_total_size(genlmsg_msg_size(id_sz));
}
void em_notify_pd_deleted(const struct em_perf_domain *pd)
{
struct sk_buff *msg;
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)) {
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);
}
postcore_initcall(em_netlink_init);
|