Skip to content

Commit c443563

Browse files
vireshkrafaeljw
authored andcommitted
cpufreq: governor: New sysfs show/store callbacks for governor tunables
The ondemand and conservative governors use the global-attr or freq-attr structures to represent sysfs attributes corresponding to their tunables (which of them is actually used depends on whether or not different policy objects can use the same governor with different tunables at the same time and, consequently, on where those attributes are located in sysfs). Unfortunately, in the freq-attr case, the standard cpufreq show/store sysfs attribute callbacks are applied to the governor tunable attributes and they always acquire the policy->rwsem lock before carrying out the operation. That may lead to an ABBA deadlock if governor tunable attributes are removed under policy->rwsem while one of them is being accessed concurrently (if sysfs attributes removal wins the race, it will wait for the access to complete with policy->rwsem held while the attribute callback will block on policy->rwsem indefinitely). We attempted to address this issue by dropping policy->rwsem around governor tunable attributes removal (that is, around invocations of the ->governor callback with the event arg equal to CPUFREQ_GOV_POLICY_EXIT) in cpufreq_set_policy(), but that opened up race conditions that had not been possible with policy->rwsem held all the time. Therefore policy->rwsem cannot be dropped in cpufreq_set_policy() at any point, but the deadlock situation described above must be avoided too. To that end, use the observation that in principle governor tunables may be represented by the same data type regardless of whether the governor is system-wide or per-policy and introduce a new structure, struct governor_attr, for representing them and new corresponding macros for creating show/store sysfs callbacks for them. Also make their parent kobject use a new kobject type whose default show/store callbacks are not related to the standard core cpufreq ones in any way (and they don't acquire policy->rwsem in particular). Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org> Tested-by: Juri Lelli <juri.lelli@arm.com> Tested-by: Shilpasri G Bhat <shilpa.bhat@linux.vnet.ibm.com> [ rjw: Subject & changelog + rebase ] Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent ff4b178 commit c443563

File tree

4 files changed

+144
-103
lines changed

4 files changed

+144
-103
lines changed

drivers/cpufreq/cpufreq_conservative.c

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -235,54 +235,33 @@ static ssize_t store_freq_step(struct dbs_data *dbs_data, const char *buf,
235235
return count;
236236
}
237237

238-
show_store_one(cs, down_threshold);
239-
show_store_one(cs, freq_step);
240-
show_store_one_common(cs, sampling_rate);
241-
show_store_one_common(cs, sampling_down_factor);
242-
show_store_one_common(cs, up_threshold);
243-
show_store_one_common(cs, ignore_nice_load);
244-
show_one_common(cs, min_sampling_rate);
245-
246-
gov_sys_pol_attr_rw(sampling_rate);
247-
gov_sys_pol_attr_rw(sampling_down_factor);
248-
gov_sys_pol_attr_rw(up_threshold);
249-
gov_sys_pol_attr_rw(down_threshold);
250-
gov_sys_pol_attr_rw(ignore_nice_load);
251-
gov_sys_pol_attr_rw(freq_step);
252-
gov_sys_pol_attr_ro(min_sampling_rate);
253-
254-
static struct attribute *dbs_attributes_gov_sys[] = {
255-
&min_sampling_rate_gov_sys.attr,
256-
&sampling_rate_gov_sys.attr,
257-
&sampling_down_factor_gov_sys.attr,
258-
&up_threshold_gov_sys.attr,
259-
&down_threshold_gov_sys.attr,
260-
&ignore_nice_load_gov_sys.attr,
261-
&freq_step_gov_sys.attr,
238+
gov_show_one_common(sampling_rate);
239+
gov_show_one_common(sampling_down_factor);
240+
gov_show_one_common(up_threshold);
241+
gov_show_one_common(ignore_nice_load);
242+
gov_show_one_common(min_sampling_rate);
243+
gov_show_one(cs, down_threshold);
244+
gov_show_one(cs, freq_step);
245+
246+
gov_attr_rw(sampling_rate);
247+
gov_attr_rw(sampling_down_factor);
248+
gov_attr_rw(up_threshold);
249+
gov_attr_rw(ignore_nice_load);
250+
gov_attr_ro(min_sampling_rate);
251+
gov_attr_rw(down_threshold);
252+
gov_attr_rw(freq_step);
253+
254+
static struct attribute *cs_attributes[] = {
255+
&min_sampling_rate.attr,
256+
&sampling_rate.attr,
257+
&sampling_down_factor.attr,
258+
&up_threshold.attr,
259+
&down_threshold.attr,
260+
&ignore_nice_load.attr,
261+
&freq_step.attr,
262262
NULL
263263
};
264264

265-
static struct attribute_group cs_attr_group_gov_sys = {
266-
.attrs = dbs_attributes_gov_sys,
267-
.name = "conservative",
268-
};
269-
270-
static struct attribute *dbs_attributes_gov_pol[] = {
271-
&min_sampling_rate_gov_pol.attr,
272-
&sampling_rate_gov_pol.attr,
273-
&sampling_down_factor_gov_pol.attr,
274-
&up_threshold_gov_pol.attr,
275-
&down_threshold_gov_pol.attr,
276-
&ignore_nice_load_gov_pol.attr,
277-
&freq_step_gov_pol.attr,
278-
NULL
279-
};
280-
281-
static struct attribute_group cs_attr_group_gov_pol = {
282-
.attrs = dbs_attributes_gov_pol,
283-
.name = "conservative",
284-
};
285-
286265
/************************** sysfs end ************************/
287266

288267
static int cs_init(struct dbs_data *dbs_data, bool notify)
@@ -331,8 +310,7 @@ static struct dbs_governor cs_dbs_gov = {
331310
.owner = THIS_MODULE,
332311
},
333312
.governor = GOV_CONSERVATIVE,
334-
.attr_group_gov_sys = &cs_attr_group_gov_sys,
335-
.attr_group_gov_pol = &cs_attr_group_gov_pol,
313+
.kobj_type = { .default_attrs = cs_attributes },
336314
.get_cpu_cdbs = get_cpu_cdbs,
337315
.get_cpu_dbs_info_s = get_cpu_dbs_info_s,
338316
.gov_dbs_timer = cs_dbs_timer,

drivers/cpufreq/cpufreq_governor.c

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,58 @@
2525
DEFINE_MUTEX(dbs_data_mutex);
2626
EXPORT_SYMBOL_GPL(dbs_data_mutex);
2727

28-
static struct attribute_group *get_sysfs_attr(struct dbs_governor *gov)
28+
static inline struct dbs_data *to_dbs_data(struct kobject *kobj)
2929
{
30-
return have_governor_per_policy() ?
31-
gov->attr_group_gov_pol : gov->attr_group_gov_sys;
30+
return container_of(kobj, struct dbs_data, kobj);
3231
}
3332

33+
static inline struct governor_attr *to_gov_attr(struct attribute *attr)
34+
{
35+
return container_of(attr, struct governor_attr, attr);
36+
}
37+
38+
static ssize_t governor_show(struct kobject *kobj, struct attribute *attr,
39+
char *buf)
40+
{
41+
struct dbs_data *dbs_data = to_dbs_data(kobj);
42+
struct governor_attr *gattr = to_gov_attr(attr);
43+
int ret = -EIO;
44+
45+
if (gattr->show)
46+
ret = gattr->show(dbs_data, buf);
47+
48+
return ret;
49+
}
50+
51+
static ssize_t governor_store(struct kobject *kobj, struct attribute *attr,
52+
const char *buf, size_t count)
53+
{
54+
struct dbs_data *dbs_data = to_dbs_data(kobj);
55+
struct governor_attr *gattr = to_gov_attr(attr);
56+
int ret = -EIO;
57+
58+
mutex_lock(&dbs_data->mutex);
59+
60+
if (gattr->store)
61+
ret = gattr->store(dbs_data, buf, count);
62+
63+
mutex_unlock(&dbs_data->mutex);
64+
65+
return ret;
66+
}
67+
68+
/*
69+
* Sysfs Ops for accessing governor attributes.
70+
*
71+
* All show/store invocations for governor specific sysfs attributes, will first
72+
* call the below show/store callbacks and the attribute specific callback will
73+
* be called from within it.
74+
*/
75+
static const struct sysfs_ops governor_sysfs_ops = {
76+
.show = governor_show,
77+
.store = governor_store,
78+
};
79+
3480
void dbs_check_cpu(struct cpufreq_policy *policy)
3581
{
3682
int cpu = policy->cpu;
@@ -352,6 +398,7 @@ static int cpufreq_governor_init(struct cpufreq_policy *policy)
352398
}
353399

354400
dbs_data->usage_count = 1;
401+
mutex_init(&dbs_data->mutex);
355402

356403
ret = gov->init(dbs_data, !policy->governor->initialized);
357404
if (ret)
@@ -374,12 +421,15 @@ static int cpufreq_governor_init(struct cpufreq_policy *policy)
374421
policy_dbs->dbs_data = dbs_data;
375422
policy->governor_data = policy_dbs;
376423

377-
ret = sysfs_create_group(get_governor_parent_kobj(policy),
378-
get_sysfs_attr(gov));
424+
gov->kobj_type.sysfs_ops = &governor_sysfs_ops;
425+
ret = kobject_init_and_add(&dbs_data->kobj, &gov->kobj_type,
426+
get_governor_parent_kobj(policy),
427+
"%s", gov->gov.name);
379428
if (!ret)
380429
return 0;
381430

382431
/* Failure, so roll back. */
432+
pr_err("cpufreq: Governor initialization failed (dbs_data kobject init error %d)\n", ret);
383433

384434
policy->governor_data = NULL;
385435

@@ -404,15 +454,15 @@ static int cpufreq_governor_exit(struct cpufreq_policy *policy)
404454
return -EBUSY;
405455

406456
if (!--dbs_data->usage_count) {
407-
sysfs_remove_group(get_governor_parent_kobj(policy),
408-
get_sysfs_attr(gov));
457+
kobject_put(&dbs_data->kobj);
409458

410459
policy->governor_data = NULL;
411460

412461
if (!have_governor_per_policy())
413462
gov->gdbs_data = NULL;
414463

415464
gov->exit(dbs_data, policy->governor->initialized == 1);
465+
mutex_destroy(&dbs_data->mutex);
416466
kfree(dbs_data);
417467
} else {
418468
policy->governor_data = NULL;

drivers/cpufreq/cpufreq_governor.h

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,44 @@ struct dbs_data {
160160
unsigned int sampling_rate;
161161
unsigned int sampling_down_factor;
162162
unsigned int up_threshold;
163+
164+
struct kobject kobj;
165+
/* Protect concurrent updates to governor tunables from sysfs */
166+
struct mutex mutex;
167+
};
168+
169+
/* Governor's specific attributes */
170+
struct dbs_data;
171+
struct governor_attr {
172+
struct attribute attr;
173+
ssize_t (*show)(struct dbs_data *dbs_data, char *buf);
174+
ssize_t (*store)(struct dbs_data *dbs_data, const char *buf,
175+
size_t count);
163176
};
164177

178+
#define gov_show_one(_gov, file_name) \
179+
static ssize_t show_##file_name \
180+
(struct dbs_data *dbs_data, char *buf) \
181+
{ \
182+
struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
183+
return sprintf(buf, "%u\n", tuners->file_name); \
184+
}
185+
186+
#define gov_show_one_common(file_name) \
187+
static ssize_t show_##file_name \
188+
(struct dbs_data *dbs_data, char *buf) \
189+
{ \
190+
return sprintf(buf, "%u\n", dbs_data->file_name); \
191+
}
192+
193+
#define gov_attr_ro(_name) \
194+
static struct governor_attr _name = \
195+
__ATTR(_name, 0444, show_##_name, NULL)
196+
197+
#define gov_attr_rw(_name) \
198+
static struct governor_attr _name = \
199+
__ATTR(_name, 0644, show_##_name, store_##_name)
200+
165201
/* Common to all CPUs of a policy */
166202
struct policy_dbs_info {
167203
struct cpufreq_policy *policy;
@@ -236,8 +272,7 @@ struct dbs_governor {
236272
#define GOV_ONDEMAND 0
237273
#define GOV_CONSERVATIVE 1
238274
int governor;
239-
struct attribute_group *attr_group_gov_sys; /* one governor - system */
240-
struct attribute_group *attr_group_gov_pol; /* one governor - policy */
275+
struct kobj_type kobj_type;
241276

242277
/*
243278
* Common data for platforms that don't set

drivers/cpufreq/cpufreq_ondemand.c

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -432,54 +432,33 @@ static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf,
432432
return count;
433433
}
434434

435-
show_store_one(od, io_is_busy);
436-
show_store_one(od, powersave_bias);
437-
show_store_one_common(od, sampling_rate);
438-
show_store_one_common(od, up_threshold);
439-
show_store_one_common(od, sampling_down_factor);
440-
show_store_one_common(od, ignore_nice_load);
441-
show_one_common(od, min_sampling_rate);
442-
443-
gov_sys_pol_attr_rw(sampling_rate);
444-
gov_sys_pol_attr_rw(io_is_busy);
445-
gov_sys_pol_attr_rw(up_threshold);
446-
gov_sys_pol_attr_rw(sampling_down_factor);
447-
gov_sys_pol_attr_rw(ignore_nice_load);
448-
gov_sys_pol_attr_rw(powersave_bias);
449-
gov_sys_pol_attr_ro(min_sampling_rate);
450-
451-
static struct attribute *dbs_attributes_gov_sys[] = {
452-
&min_sampling_rate_gov_sys.attr,
453-
&sampling_rate_gov_sys.attr,
454-
&up_threshold_gov_sys.attr,
455-
&sampling_down_factor_gov_sys.attr,
456-
&ignore_nice_load_gov_sys.attr,
457-
&powersave_bias_gov_sys.attr,
458-
&io_is_busy_gov_sys.attr,
435+
gov_show_one_common(sampling_rate);
436+
gov_show_one_common(up_threshold);
437+
gov_show_one_common(sampling_down_factor);
438+
gov_show_one_common(ignore_nice_load);
439+
gov_show_one_common(min_sampling_rate);
440+
gov_show_one(od, io_is_busy);
441+
gov_show_one(od, powersave_bias);
442+
443+
gov_attr_rw(sampling_rate);
444+
gov_attr_rw(io_is_busy);
445+
gov_attr_rw(up_threshold);
446+
gov_attr_rw(sampling_down_factor);
447+
gov_attr_rw(ignore_nice_load);
448+
gov_attr_rw(powersave_bias);
449+
gov_attr_ro(min_sampling_rate);
450+
451+
static struct attribute *od_attributes[] = {
452+
&min_sampling_rate.attr,
453+
&sampling_rate.attr,
454+
&up_threshold.attr,
455+
&sampling_down_factor.attr,
456+
&ignore_nice_load.attr,
457+
&powersave_bias.attr,
458+
&io_is_busy.attr,
459459
NULL
460460
};
461461

462-
static struct attribute_group od_attr_group_gov_sys = {
463-
.attrs = dbs_attributes_gov_sys,
464-
.name = "ondemand",
465-
};
466-
467-
static struct attribute *dbs_attributes_gov_pol[] = {
468-
&min_sampling_rate_gov_pol.attr,
469-
&sampling_rate_gov_pol.attr,
470-
&up_threshold_gov_pol.attr,
471-
&sampling_down_factor_gov_pol.attr,
472-
&ignore_nice_load_gov_pol.attr,
473-
&powersave_bias_gov_pol.attr,
474-
&io_is_busy_gov_pol.attr,
475-
NULL
476-
};
477-
478-
static struct attribute_group od_attr_group_gov_pol = {
479-
.attrs = dbs_attributes_gov_pol,
480-
.name = "ondemand",
481-
};
482-
483462
/************************** sysfs end ************************/
484463

485464
static int od_init(struct dbs_data *dbs_data, bool notify)
@@ -544,8 +523,7 @@ static struct dbs_governor od_dbs_gov = {
544523
.owner = THIS_MODULE,
545524
},
546525
.governor = GOV_ONDEMAND,
547-
.attr_group_gov_sys = &od_attr_group_gov_sys,
548-
.attr_group_gov_pol = &od_attr_group_gov_pol,
526+
.kobj_type = { .default_attrs = od_attributes },
549527
.get_cpu_cdbs = get_cpu_cdbs,
550528
.get_cpu_dbs_info_s = get_cpu_dbs_info_s,
551529
.gov_dbs_timer = od_dbs_timer,

0 commit comments

Comments
 (0)