Skip to content

Commit 62c95ea

Browse files
committed
cpufreq: intel_pstate: Use mutex guard for driver locking
Use guard(mutex)(&intel_pstate_driver_lock), or the scoped variant of it, wherever intel_pstate_driver_lock needs to be held. This allows some local variables and goto statements to be dropped as they are not necessary any more. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com> Link: https://patch.msgid.link/2807232.mvXUDI8C0e@rafael.j.wysocki
1 parent 25ca663 commit 62c95ea

1 file changed

Lines changed: 33 additions & 66 deletions

File tree

drivers/cpufreq/intel_pstate.c

Lines changed: 33 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,8 @@ static void set_power_ctl_ee_state(bool input)
13931393
{
13941394
u64 power_ctl;
13951395

1396-
mutex_lock(&intel_pstate_driver_lock);
1396+
guard(mutex)(&intel_pstate_driver_lock);
1397+
13971398
rdmsrq(MSR_IA32_POWER_CTL, power_ctl);
13981399
if (input) {
13991400
power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE);
@@ -1403,7 +1404,6 @@ static void set_power_ctl_ee_state(bool input)
14031404
power_ctl_ee_state = POWER_CTL_EE_DISABLE;
14041405
}
14051406
wrmsrq(MSR_IA32_POWER_CTL, power_ctl);
1406-
mutex_unlock(&intel_pstate_driver_lock);
14071407
}
14081408

14091409
static void intel_pstate_hwp_enable(struct cpudata *cpudata);
@@ -1525,13 +1525,9 @@ static int intel_pstate_update_status(const char *buf, size_t size);
15251525
static ssize_t show_status(struct kobject *kobj,
15261526
struct kobj_attribute *attr, char *buf)
15271527
{
1528-
ssize_t ret;
1529-
1530-
mutex_lock(&intel_pstate_driver_lock);
1531-
ret = intel_pstate_show_status(buf);
1532-
mutex_unlock(&intel_pstate_driver_lock);
1528+
guard(mutex)(&intel_pstate_driver_lock);
15331529

1534-
return ret;
1530+
return intel_pstate_show_status(buf);
15351531
}
15361532

15371533
static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
@@ -1540,11 +1536,13 @@ static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
15401536
char *p = memchr(buf, '\n', count);
15411537
int ret;
15421538

1543-
mutex_lock(&intel_pstate_driver_lock);
1539+
guard(mutex)(&intel_pstate_driver_lock);
1540+
15441541
ret = intel_pstate_update_status(buf, p ? p - buf : count);
1545-
mutex_unlock(&intel_pstate_driver_lock);
1542+
if (ret < 0)
1543+
return ret;
15461544

1547-
return ret < 0 ? ret : count;
1545+
return count;
15481546
}
15491547

15501548
static ssize_t show_turbo_pct(struct kobject *kobj,
@@ -1554,12 +1552,10 @@ static ssize_t show_turbo_pct(struct kobject *kobj,
15541552
int total, no_turbo, turbo_pct;
15551553
uint32_t turbo_fp;
15561554

1557-
mutex_lock(&intel_pstate_driver_lock);
1555+
guard(mutex)(&intel_pstate_driver_lock);
15581556

1559-
if (!intel_pstate_driver) {
1560-
mutex_unlock(&intel_pstate_driver_lock);
1557+
if (!intel_pstate_driver)
15611558
return -EAGAIN;
1562-
}
15631559

15641560
cpu = all_cpu_data[0];
15651561

@@ -1568,8 +1564,6 @@ static ssize_t show_turbo_pct(struct kobject *kobj,
15681564
turbo_fp = div_fp(no_turbo, total);
15691565
turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
15701566

1571-
mutex_unlock(&intel_pstate_driver_lock);
1572-
15731567
return sprintf(buf, "%u\n", turbo_pct);
15741568
}
15751569

@@ -1579,38 +1573,26 @@ static ssize_t show_num_pstates(struct kobject *kobj,
15791573
struct cpudata *cpu;
15801574
int total;
15811575

1582-
mutex_lock(&intel_pstate_driver_lock);
1576+
guard(mutex)(&intel_pstate_driver_lock);
15831577

1584-
if (!intel_pstate_driver) {
1585-
mutex_unlock(&intel_pstate_driver_lock);
1578+
if (!intel_pstate_driver)
15861579
return -EAGAIN;
1587-
}
15881580

15891581
cpu = all_cpu_data[0];
15901582
total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
15911583

1592-
mutex_unlock(&intel_pstate_driver_lock);
1593-
15941584
return sprintf(buf, "%u\n", total);
15951585
}
15961586

15971587
static ssize_t show_no_turbo(struct kobject *kobj,
15981588
struct kobj_attribute *attr, char *buf)
15991589
{
1600-
ssize_t ret;
1590+
guard(mutex)(&intel_pstate_driver_lock);
16011591

1602-
mutex_lock(&intel_pstate_driver_lock);
1603-
1604-
if (!intel_pstate_driver) {
1605-
mutex_unlock(&intel_pstate_driver_lock);
1592+
if (!intel_pstate_driver)
16061593
return -EAGAIN;
1607-
}
1608-
1609-
ret = sprintf(buf, "%u\n", global.no_turbo);
16101594

1611-
mutex_unlock(&intel_pstate_driver_lock);
1612-
1613-
return ret;
1595+
return sprintf(buf, "%u\n", global.no_turbo);
16141596
}
16151597

16161598
static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b,
@@ -1622,29 +1604,25 @@ static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b,
16221604
if (sscanf(buf, "%u", &input) != 1)
16231605
return -EINVAL;
16241606

1625-
mutex_lock(&intel_pstate_driver_lock);
1607+
guard(mutex)(&intel_pstate_driver_lock);
16261608

1627-
if (!intel_pstate_driver) {
1628-
count = -EAGAIN;
1629-
goto unlock_driver;
1630-
}
1609+
if (!intel_pstate_driver)
1610+
return -EAGAIN;
16311611

16321612
no_turbo = !!clamp_t(int, input, 0, 1);
16331613

16341614
WRITE_ONCE(global.turbo_disabled, turbo_is_disabled());
16351615
if (global.turbo_disabled && !no_turbo) {
16361616
pr_notice("Turbo disabled by BIOS or unavailable on processor\n");
1637-
count = -EPERM;
16381617
if (global.no_turbo)
1639-
goto unlock_driver;
1640-
else
1641-
no_turbo = 1;
1642-
}
1618+
return -EPERM;
16431619

1644-
if (no_turbo == global.no_turbo) {
1645-
goto unlock_driver;
1620+
no_turbo = 1;
16461621
}
16471622

1623+
if (no_turbo == global.no_turbo)
1624+
return count;
1625+
16481626
WRITE_ONCE(global.no_turbo, no_turbo);
16491627

16501628
mutex_lock(&intel_pstate_limits_lock);
@@ -1663,9 +1641,6 @@ static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b,
16631641
intel_pstate_update_limits_for_all();
16641642
arch_set_max_freq_ratio(no_turbo);
16651643

1666-
unlock_driver:
1667-
mutex_unlock(&intel_pstate_driver_lock);
1668-
16691644
return count;
16701645
}
16711646

@@ -1715,12 +1690,10 @@ static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b,
17151690
if (ret != 1)
17161691
return -EINVAL;
17171692

1718-
mutex_lock(&intel_pstate_driver_lock);
1693+
guard(mutex)(&intel_pstate_driver_lock);
17191694

1720-
if (!intel_pstate_driver) {
1721-
mutex_unlock(&intel_pstate_driver_lock);
1695+
if (!intel_pstate_driver)
17221696
return -EAGAIN;
1723-
}
17241697

17251698
mutex_lock(&intel_pstate_limits_lock);
17261699

@@ -1733,8 +1706,6 @@ static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b,
17331706
else
17341707
update_qos_requests(FREQ_QOS_MAX);
17351708

1736-
mutex_unlock(&intel_pstate_driver_lock);
1737-
17381709
return count;
17391710
}
17401711

@@ -1748,12 +1719,10 @@ static ssize_t store_min_perf_pct(struct kobject *a, struct kobj_attribute *b,
17481719
if (ret != 1)
17491720
return -EINVAL;
17501721

1751-
mutex_lock(&intel_pstate_driver_lock);
1722+
guard(mutex)(&intel_pstate_driver_lock);
17521723

1753-
if (!intel_pstate_driver) {
1754-
mutex_unlock(&intel_pstate_driver_lock);
1724+
if (!intel_pstate_driver)
17551725
return -EAGAIN;
1756-
}
17571726

17581727
mutex_lock(&intel_pstate_limits_lock);
17591728

@@ -1767,8 +1736,6 @@ static ssize_t store_min_perf_pct(struct kobject *a, struct kobj_attribute *b,
17671736
else
17681737
update_qos_requests(FREQ_QOS_MIN);
17691738

1770-
mutex_unlock(&intel_pstate_driver_lock);
1771-
17721739
return count;
17731740
}
17741741

@@ -1789,10 +1756,10 @@ static ssize_t store_hwp_dynamic_boost(struct kobject *a,
17891756
if (ret)
17901757
return ret;
17911758

1792-
mutex_lock(&intel_pstate_driver_lock);
1759+
guard(mutex)(&intel_pstate_driver_lock);
1760+
17931761
hwp_boost = !!input;
17941762
intel_pstate_update_policies();
1795-
mutex_unlock(&intel_pstate_driver_lock);
17961763

17971764
return count;
17981765
}
@@ -3914,9 +3881,9 @@ static int __init intel_pstate_init(void)
39143881

39153882
}
39163883

3917-
mutex_lock(&intel_pstate_driver_lock);
3918-
rc = intel_pstate_register_driver(default_driver);
3919-
mutex_unlock(&intel_pstate_driver_lock);
3884+
scoped_guard(mutex, &intel_pstate_driver_lock) {
3885+
rc = intel_pstate_register_driver(default_driver);
3886+
}
39203887
if (rc) {
39213888
intel_pstate_sysfs_remove();
39223889
return rc;

0 commit comments

Comments
 (0)