Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

+34 -67
+34 -67
drivers/cpufreq/intel_pstate.c
··· 1393 1393 { 1394 1394 u64 power_ctl; 1395 1395 1396 - mutex_lock(&intel_pstate_driver_lock); 1396 + guard(mutex)(&intel_pstate_driver_lock); 1397 + 1397 1398 rdmsrq(MSR_IA32_POWER_CTL, power_ctl); 1398 1399 if (input) { 1399 1400 power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE); ··· 1404 1403 power_ctl_ee_state = POWER_CTL_EE_DISABLE; 1405 1404 } 1406 1405 wrmsrq(MSR_IA32_POWER_CTL, power_ctl); 1407 - mutex_unlock(&intel_pstate_driver_lock); 1408 1406 } 1409 1407 1410 1408 static void intel_pstate_hwp_enable(struct cpudata *cpudata); ··· 1525 1525 static ssize_t show_status(struct kobject *kobj, 1526 1526 struct kobj_attribute *attr, char *buf) 1527 1527 { 1528 - ssize_t ret; 1528 + guard(mutex)(&intel_pstate_driver_lock); 1529 1529 1530 - mutex_lock(&intel_pstate_driver_lock); 1531 - ret = intel_pstate_show_status(buf); 1532 - mutex_unlock(&intel_pstate_driver_lock); 1533 - 1534 - return ret; 1530 + return intel_pstate_show_status(buf); 1535 1531 } 1536 1532 1537 1533 static ssize_t store_status(struct kobject *a, struct kobj_attribute *b, ··· 1536 1540 char *p = memchr(buf, '\n', count); 1537 1541 int ret; 1538 1542 1539 - mutex_lock(&intel_pstate_driver_lock); 1540 - ret = intel_pstate_update_status(buf, p ? p - buf : count); 1541 - mutex_unlock(&intel_pstate_driver_lock); 1543 + guard(mutex)(&intel_pstate_driver_lock); 1542 1544 1543 - return ret < 0 ? ret : count; 1545 + ret = intel_pstate_update_status(buf, p ? p - buf : count); 1546 + if (ret < 0) 1547 + return ret; 1548 + 1549 + return count; 1544 1550 } 1545 1551 1546 1552 static ssize_t show_turbo_pct(struct kobject *kobj, ··· 1552 1554 int total, no_turbo, turbo_pct; 1553 1555 uint32_t turbo_fp; 1554 1556 1555 - mutex_lock(&intel_pstate_driver_lock); 1557 + guard(mutex)(&intel_pstate_driver_lock); 1556 1558 1557 - if (!intel_pstate_driver) { 1558 - mutex_unlock(&intel_pstate_driver_lock); 1559 + if (!intel_pstate_driver) 1559 1560 return -EAGAIN; 1560 - } 1561 1561 1562 1562 cpu = all_cpu_data[0]; 1563 1563 ··· 1563 1567 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1; 1564 1568 turbo_fp = div_fp(no_turbo, total); 1565 1569 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100))); 1566 - 1567 - mutex_unlock(&intel_pstate_driver_lock); 1568 1570 1569 1571 return sprintf(buf, "%u\n", turbo_pct); 1570 1572 } ··· 1573 1579 struct cpudata *cpu; 1574 1580 int total; 1575 1581 1576 - mutex_lock(&intel_pstate_driver_lock); 1582 + guard(mutex)(&intel_pstate_driver_lock); 1577 1583 1578 - if (!intel_pstate_driver) { 1579 - mutex_unlock(&intel_pstate_driver_lock); 1584 + if (!intel_pstate_driver) 1580 1585 return -EAGAIN; 1581 - } 1582 1586 1583 1587 cpu = all_cpu_data[0]; 1584 1588 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1; 1585 - 1586 - mutex_unlock(&intel_pstate_driver_lock); 1587 1589 1588 1590 return sprintf(buf, "%u\n", total); 1589 1591 } ··· 1587 1597 static ssize_t show_no_turbo(struct kobject *kobj, 1588 1598 struct kobj_attribute *attr, char *buf) 1589 1599 { 1590 - ssize_t ret; 1600 + guard(mutex)(&intel_pstate_driver_lock); 1591 1601 1592 - mutex_lock(&intel_pstate_driver_lock); 1593 - 1594 - if (!intel_pstate_driver) { 1595 - mutex_unlock(&intel_pstate_driver_lock); 1602 + if (!intel_pstate_driver) 1596 1603 return -EAGAIN; 1597 - } 1598 1604 1599 - ret = sprintf(buf, "%u\n", global.no_turbo); 1600 - 1601 - mutex_unlock(&intel_pstate_driver_lock); 1602 - 1603 - return ret; 1605 + return sprintf(buf, "%u\n", global.no_turbo); 1604 1606 } 1605 1607 1606 1608 static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b, ··· 1604 1622 if (sscanf(buf, "%u", &input) != 1) 1605 1623 return -EINVAL; 1606 1624 1607 - mutex_lock(&intel_pstate_driver_lock); 1625 + guard(mutex)(&intel_pstate_driver_lock); 1608 1626 1609 - if (!intel_pstate_driver) { 1610 - count = -EAGAIN; 1611 - goto unlock_driver; 1612 - } 1627 + if (!intel_pstate_driver) 1628 + return -EAGAIN; 1613 1629 1614 1630 no_turbo = !!clamp_t(int, input, 0, 1); 1615 1631 1616 1632 WRITE_ONCE(global.turbo_disabled, turbo_is_disabled()); 1617 1633 if (global.turbo_disabled && !no_turbo) { 1618 1634 pr_notice("Turbo disabled by BIOS or unavailable on processor\n"); 1619 - count = -EPERM; 1620 1635 if (global.no_turbo) 1621 - goto unlock_driver; 1622 - else 1623 - no_turbo = 1; 1636 + return -EPERM; 1637 + 1638 + no_turbo = 1; 1624 1639 } 1625 1640 1626 - if (no_turbo == global.no_turbo) { 1627 - goto unlock_driver; 1628 - } 1641 + if (no_turbo == global.no_turbo) 1642 + return count; 1629 1643 1630 1644 WRITE_ONCE(global.no_turbo, no_turbo); 1631 1645 ··· 1640 1662 1641 1663 intel_pstate_update_limits_for_all(); 1642 1664 arch_set_max_freq_ratio(no_turbo); 1643 - 1644 - unlock_driver: 1645 - mutex_unlock(&intel_pstate_driver_lock); 1646 1665 1647 1666 return count; 1648 1667 } ··· 1690 1715 if (ret != 1) 1691 1716 return -EINVAL; 1692 1717 1693 - mutex_lock(&intel_pstate_driver_lock); 1718 + guard(mutex)(&intel_pstate_driver_lock); 1694 1719 1695 - if (!intel_pstate_driver) { 1696 - mutex_unlock(&intel_pstate_driver_lock); 1720 + if (!intel_pstate_driver) 1697 1721 return -EAGAIN; 1698 - } 1699 1722 1700 1723 mutex_lock(&intel_pstate_limits_lock); 1701 1724 ··· 1705 1732 intel_pstate_update_policies(); 1706 1733 else 1707 1734 update_qos_requests(FREQ_QOS_MAX); 1708 - 1709 - mutex_unlock(&intel_pstate_driver_lock); 1710 1735 1711 1736 return count; 1712 1737 } ··· 1719 1748 if (ret != 1) 1720 1749 return -EINVAL; 1721 1750 1722 - mutex_lock(&intel_pstate_driver_lock); 1751 + guard(mutex)(&intel_pstate_driver_lock); 1723 1752 1724 - if (!intel_pstate_driver) { 1725 - mutex_unlock(&intel_pstate_driver_lock); 1753 + if (!intel_pstate_driver) 1726 1754 return -EAGAIN; 1727 - } 1728 1755 1729 1756 mutex_lock(&intel_pstate_limits_lock); 1730 1757 ··· 1735 1766 intel_pstate_update_policies(); 1736 1767 else 1737 1768 update_qos_requests(FREQ_QOS_MIN); 1738 - 1739 - mutex_unlock(&intel_pstate_driver_lock); 1740 1769 1741 1770 return count; 1742 1771 } ··· 1756 1789 if (ret) 1757 1790 return ret; 1758 1791 1759 - mutex_lock(&intel_pstate_driver_lock); 1792 + guard(mutex)(&intel_pstate_driver_lock); 1793 + 1760 1794 hwp_boost = !!input; 1761 1795 intel_pstate_update_policies(); 1762 - mutex_unlock(&intel_pstate_driver_lock); 1763 1796 1764 1797 return count; 1765 1798 } ··· 3881 3914 3882 3915 } 3883 3916 3884 - mutex_lock(&intel_pstate_driver_lock); 3885 - rc = intel_pstate_register_driver(default_driver); 3886 - mutex_unlock(&intel_pstate_driver_lock); 3917 + scoped_guard(mutex, &intel_pstate_driver_lock) { 3918 + rc = intel_pstate_register_driver(default_driver); 3919 + } 3887 3920 if (rc) { 3888 3921 intel_pstate_sysfs_remove(); 3889 3922 return rc;