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

Input: applespi - use guard notation when acquiring spinlock

This makes the code more compact and error handling more robust
by ensuring that locks are released in all code paths when control
leaves critical section.

Link: https://lore.kernel.org/r/20240825051627.2848495-3-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+18 -54
+18 -54
drivers/input/keyboard/applespi.c
··· 717 717 static void applespi_msg_complete(struct applespi_data *applespi, 718 718 bool is_write_msg, bool is_read_compl) 719 719 { 720 - unsigned long flags; 721 - 722 - spin_lock_irqsave(&applespi->cmd_msg_lock, flags); 720 + guard(spinlock_irqsave)(&applespi->cmd_msg_lock); 723 721 724 722 if (is_read_compl) 725 723 applespi->read_active = false; ··· 731 733 applespi->cmd_msg_queued = 0; 732 734 applespi_send_cmd_msg(applespi); 733 735 } 734 - 735 - spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); 736 736 } 737 737 738 738 static void applespi_async_write_complete(void *context) ··· 884 888 885 889 static void applespi_init(struct applespi_data *applespi, bool is_resume) 886 890 { 887 - unsigned long flags; 888 - 889 - spin_lock_irqsave(&applespi->cmd_msg_lock, flags); 891 + guard(spinlock_irqsave)(&applespi->cmd_msg_lock); 890 892 891 893 if (is_resume) 892 894 applespi->want_mt_init_cmd = true; 893 895 else 894 896 applespi->want_tp_info_cmd = true; 895 897 applespi_send_cmd_msg(applespi); 896 - 897 - spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); 898 898 } 899 899 900 900 static int applespi_set_capsl_led(struct applespi_data *applespi, 901 901 bool capslock_on) 902 902 { 903 - unsigned long flags; 904 - int sts; 905 - 906 - spin_lock_irqsave(&applespi->cmd_msg_lock, flags); 903 + guard(spinlock_irqsave)(&applespi->cmd_msg_lock); 907 904 908 905 applespi->want_cl_led_on = capslock_on; 909 - sts = applespi_send_cmd_msg(applespi); 910 - 911 - spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); 912 - 913 - return sts; 906 + return applespi_send_cmd_msg(applespi); 914 907 } 915 908 916 909 static void applespi_set_bl_level(struct led_classdev *led_cdev, ··· 907 922 { 908 923 struct applespi_data *applespi = 909 924 container_of(led_cdev, struct applespi_data, backlight_info); 910 - unsigned long flags; 911 925 912 - spin_lock_irqsave(&applespi->cmd_msg_lock, flags); 926 + guard(spinlock_irqsave)(&applespi->cmd_msg_lock); 913 927 914 928 if (value == 0) { 915 929 applespi->want_bl_level = value; ··· 924 940 } 925 941 926 942 applespi_send_cmd_msg(applespi); 927 - 928 - spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); 929 943 } 930 944 931 945 static int applespi_event(struct input_dev *dev, unsigned int type, ··· 1410 1428 /* process packet header */ 1411 1429 if (!applespi_verify_crc(applespi, applespi->rx_buffer, 1412 1430 APPLESPI_PACKET_SIZE)) { 1413 - unsigned long flags; 1414 - 1415 - spin_lock_irqsave(&applespi->cmd_msg_lock, flags); 1431 + guard(spinlock_irqsave)(&applespi->cmd_msg_lock); 1416 1432 1417 1433 if (applespi->drain) { 1418 1434 applespi->read_active = false; ··· 1418 1438 1419 1439 wake_up_all(&applespi->drain_complete); 1420 1440 } 1421 - 1422 - spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); 1423 1441 1424 1442 return; 1425 1443 } ··· 1551 1573 { 1552 1574 struct applespi_data *applespi = context; 1553 1575 int sts; 1554 - unsigned long flags; 1555 1576 1556 1577 trace_applespi_irq_received(ET_RD_IRQ, PT_READ); 1557 1578 1558 - spin_lock_irqsave(&applespi->cmd_msg_lock, flags); 1579 + guard(spinlock_irqsave)(&applespi->cmd_msg_lock); 1559 1580 1560 1581 if (!applespi->suspended) { 1561 1582 sts = applespi_async(applespi, &applespi->rd_m, ··· 1566 1589 else 1567 1590 applespi->read_active = true; 1568 1591 } 1569 - 1570 - spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); 1571 1592 1572 1593 return ACPI_INTERRUPT_HANDLED; 1573 1594 } ··· 1794 1819 1795 1820 static void applespi_drain_writes(struct applespi_data *applespi) 1796 1821 { 1797 - unsigned long flags; 1798 - 1799 - spin_lock_irqsave(&applespi->cmd_msg_lock, flags); 1822 + guard(spinlock_irqsave)(&applespi->cmd_msg_lock); 1800 1823 1801 1824 applespi->drain = true; 1802 1825 wait_event_lock_irq(applespi->drain_complete, !applespi->write_active, 1803 1826 applespi->cmd_msg_lock); 1804 - 1805 - spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); 1806 1827 } 1807 1828 1808 1829 static void applespi_drain_reads(struct applespi_data *applespi) 1809 1830 { 1810 - unsigned long flags; 1811 - 1812 - spin_lock_irqsave(&applespi->cmd_msg_lock, flags); 1831 + guard(spinlock_irqsave)(&applespi->cmd_msg_lock); 1813 1832 1814 1833 wait_event_lock_irq(applespi->drain_complete, !applespi->read_active, 1815 1834 applespi->cmd_msg_lock); 1816 1835 1817 1836 applespi->suspended = true; 1818 - 1819 - spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); 1820 1837 } 1821 1838 1822 1839 static void applespi_remove(struct spi_device *spi) ··· 1875 1908 struct spi_device *spi = to_spi_device(dev); 1876 1909 struct applespi_data *applespi = spi_get_drvdata(spi); 1877 1910 acpi_status acpi_sts; 1878 - unsigned long flags; 1879 1911 1880 1912 /* ensure our flags and state reflect a newly resumed device */ 1881 - spin_lock_irqsave(&applespi->cmd_msg_lock, flags); 1913 + scoped_guard(spinlock_irqsave, &applespi->cmd_msg_lock) { 1914 + applespi->drain = false; 1915 + applespi->have_cl_led_on = false; 1916 + applespi->have_bl_level = 0; 1917 + applespi->cmd_msg_queued = 0; 1918 + applespi->read_active = false; 1919 + applespi->write_active = false; 1882 1920 1883 - applespi->drain = false; 1884 - applespi->have_cl_led_on = false; 1885 - applespi->have_bl_level = 0; 1886 - applespi->cmd_msg_queued = 0; 1887 - applespi->read_active = false; 1888 - applespi->write_active = false; 1889 - 1890 - applespi->suspended = false; 1891 - 1892 - spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); 1921 + applespi->suspended = false; 1922 + } 1893 1923 1894 1924 /* switch on the SPI interface */ 1895 1925 applespi_enable_spi(applespi);