Skip to content

Commit d2cdc6a

Browse files
committed
powercap/drivers/dtpm: Use container_of instead of a private data field
The dtpm framework provides an API to allocate a dtpm node. However when a backend dtpm driver needs to allocate a dtpm node it must define its own structure and store the pointer of this structure in the private field of the dtpm structure. It is more elegant to use the container_of macro and add the dtpm structure inside the dtpm backend specific structure. The code will be able to deal properly with the dtpm structure as a generic entity, making all this even more self-encapsulated. The dtpm_alloc() function does no longer make sense as the dtpm structure will be allocated when allocating the device specific dtpm structure. The dtpm_init() is provided instead. Signed-off-by: Daniel Lezcano <[email protected]> Reviewed-by: Lukasz Luba <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 7a89d7e commit d2cdc6a

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

drivers/powercap/dtpm.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -357,24 +357,18 @@ static struct powercap_zone_ops zone_ops = {
357357
};
358358

359359
/**
360-
* dtpm_alloc - Allocate and initialize a dtpm struct
361-
* @name: a string specifying the name of the node
362-
*
363-
* Return: a struct dtpm pointer, NULL in case of error
360+
* dtpm_init - Allocate and initialize a dtpm struct
361+
* @dtpm: The dtpm struct pointer to be initialized
362+
* @ops: The dtpm device specific ops, NULL for a virtual node
364363
*/
365-
struct dtpm *dtpm_alloc(struct dtpm_ops *ops)
364+
void dtpm_init(struct dtpm *dtpm, struct dtpm_ops *ops)
366365
{
367-
struct dtpm *dtpm;
368-
369-
dtpm = kzalloc(sizeof(*dtpm), GFP_KERNEL);
370366
if (dtpm) {
371367
INIT_LIST_HEAD(&dtpm->children);
372368
INIT_LIST_HEAD(&dtpm->sibling);
373369
dtpm->weight = 1024;
374370
dtpm->ops = ops;
375371
}
376-
377-
return dtpm;
378372
}
379373

380374
/**
@@ -465,7 +459,7 @@ int dtpm_register(const char *name, struct dtpm *dtpm, struct dtpm *parent)
465459
return 0;
466460
}
467461

468-
static int __init dtpm_init(void)
462+
static int __init init_dtpm(void)
469463
{
470464
struct dtpm_descr *dtpm_descr;
471465

@@ -480,4 +474,4 @@ static int __init dtpm_init(void)
480474

481475
return 0;
482476
}
483-
late_initcall(dtpm_init);
477+
late_initcall(init_dtpm);

drivers/powercap/dtpm_cpu.c

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,22 @@
2525
#include <linux/slab.h>
2626
#include <linux/units.h>
2727

28-
static DEFINE_PER_CPU(struct dtpm *, dtpm_per_cpu);
29-
3028
struct dtpm_cpu {
29+
struct dtpm dtpm;
3130
struct freq_qos_request qos_req;
3231
int cpu;
3332
};
3433

34+
static DEFINE_PER_CPU(struct dtpm_cpu *, dtpm_per_cpu);
35+
36+
static struct dtpm_cpu *to_dtpm_cpu(struct dtpm *dtpm)
37+
{
38+
return container_of(dtpm, struct dtpm_cpu, dtpm);
39+
}
40+
3541
static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit)
3642
{
37-
struct dtpm_cpu *dtpm_cpu = dtpm->private;
43+
struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
3844
struct em_perf_domain *pd = em_cpu_get(dtpm_cpu->cpu);
3945
struct cpumask cpus;
4046
unsigned long freq;
@@ -64,7 +70,7 @@ static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit)
6470

6571
static u64 get_pd_power_uw(struct dtpm *dtpm)
6672
{
67-
struct dtpm_cpu *dtpm_cpu = dtpm->private;
73+
struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
6874
struct em_perf_domain *pd;
6975
struct cpumask cpus;
7076
unsigned long freq;
@@ -90,7 +96,7 @@ static u64 get_pd_power_uw(struct dtpm *dtpm)
9096

9197
static int update_pd_power_uw(struct dtpm *dtpm)
9298
{
93-
struct dtpm_cpu *dtpm_cpu = dtpm->private;
99+
struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
94100
struct em_perf_domain *em = em_cpu_get(dtpm_cpu->cpu);
95101
struct cpumask cpus;
96102
int nr_cpus;
@@ -111,7 +117,7 @@ static int update_pd_power_uw(struct dtpm *dtpm)
111117

112118
static void pd_release(struct dtpm *dtpm)
113119
{
114-
struct dtpm_cpu *dtpm_cpu = dtpm->private;
120+
struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
115121

116122
if (freq_qos_request_active(&dtpm_cpu->qos_req))
117123
freq_qos_remove_request(&dtpm_cpu->qos_req);
@@ -129,20 +135,19 @@ static struct dtpm_ops dtpm_ops = {
129135
static int cpuhp_dtpm_cpu_offline(unsigned int cpu)
130136
{
131137
struct em_perf_domain *pd;
132-
struct dtpm *dtpm;
138+
struct dtpm_cpu *dtpm_cpu;
133139

134140
pd = em_cpu_get(cpu);
135141
if (!pd)
136142
return -EINVAL;
137143

138-
dtpm = per_cpu(dtpm_per_cpu, cpu);
144+
dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
139145

140-
return dtpm_update_power(dtpm);
146+
return dtpm_update_power(&dtpm_cpu->dtpm);
141147
}
142148

143149
static int cpuhp_dtpm_cpu_online(unsigned int cpu)
144150
{
145-
struct dtpm *dtpm;
146151
struct dtpm_cpu *dtpm_cpu;
147152
struct cpufreq_policy *policy;
148153
struct em_perf_domain *pd;
@@ -157,27 +162,23 @@ static int cpuhp_dtpm_cpu_online(unsigned int cpu)
157162
if (!pd)
158163
return -EINVAL;
159164

160-
dtpm = per_cpu(dtpm_per_cpu, cpu);
161-
if (dtpm)
162-
return dtpm_update_power(dtpm);
163-
164-
dtpm = dtpm_alloc(&dtpm_ops);
165-
if (!dtpm)
166-
return -EINVAL;
165+
dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
166+
if (dtpm_cpu)
167+
return dtpm_update_power(&dtpm_cpu->dtpm);
167168

168169
dtpm_cpu = kzalloc(sizeof(*dtpm_cpu), GFP_KERNEL);
169170
if (!dtpm_cpu)
170-
goto out_kfree_dtpm;
171+
return -ENOMEM;
171172

172-
dtpm->private = dtpm_cpu;
173+
dtpm_init(&dtpm_cpu->dtpm, &dtpm_ops);
173174
dtpm_cpu->cpu = cpu;
174175

175176
for_each_cpu(cpu, policy->related_cpus)
176-
per_cpu(dtpm_per_cpu, cpu) = dtpm;
177+
per_cpu(dtpm_per_cpu, cpu) = dtpm_cpu;
177178

178179
snprintf(name, sizeof(name), "cpu%d-cpufreq", dtpm_cpu->cpu);
179180

180-
ret = dtpm_register(name, dtpm, NULL);
181+
ret = dtpm_register(name, &dtpm_cpu->dtpm, NULL);
181182
if (ret)
182183
goto out_kfree_dtpm_cpu;
183184

@@ -190,17 +191,14 @@ static int cpuhp_dtpm_cpu_online(unsigned int cpu)
190191
return 0;
191192

192193
out_dtpm_unregister:
193-
dtpm_unregister(dtpm);
194+
dtpm_unregister(&dtpm_cpu->dtpm);
194195
dtpm_cpu = NULL;
195-
dtpm = NULL;
196196

197197
out_kfree_dtpm_cpu:
198198
for_each_cpu(cpu, policy->related_cpus)
199199
per_cpu(dtpm_per_cpu, cpu) = NULL;
200200
kfree(dtpm_cpu);
201201

202-
out_kfree_dtpm:
203-
kfree(dtpm);
204202
return ret;
205203
}
206204

include/linux/dtpm.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ struct dtpm {
2323
u64 power_max;
2424
u64 power_min;
2525
int weight;
26-
void *private;
2726
};
2827

2928
struct dtpm_ops {
@@ -65,7 +64,7 @@ int dtpm_update_power(struct dtpm *dtpm);
6564

6665
int dtpm_release_zone(struct powercap_zone *pcz);
6766

68-
struct dtpm *dtpm_alloc(struct dtpm_ops *ops);
67+
void dtpm_init(struct dtpm *dtpm, struct dtpm_ops *ops);
6968

7069
void dtpm_unregister(struct dtpm *dtpm);
7170

0 commit comments

Comments
 (0)