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

ALSA: usb: qcom: Use guard() for mutex locks

Replace the manual mutex lock/unlock pairs with guard() for code
simplification.

The manual mutex lock/unlock are still left in
handle_uaudio_stream_req() and its callee as they have a bit complex
locking patterns.

Only code refactoring, and no behavior change.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250829150724.6886-8-tiwai@suse.de

+22 -47
+22 -47
sound/usb/qcom/qc_audio_offload.c
··· 744 744 int if_idx; 745 745 int idx; 746 746 747 - mutex_lock(&qdev_mutex); 747 + guard(mutex)(&qdev_mutex); 748 748 /* find all active intf for set alt 0 and cleanup usb audio dev */ 749 749 for (idx = 0; idx < SNDRV_CARDS; idx++) { 750 750 if (!atomic_read(&uadev[idx].in_use)) ··· 780 780 disable_audio_stream(subs); 781 781 } 782 782 atomic_set(&uadev[idx].in_use, 0); 783 - mutex_lock(&chip->mutex); 783 + guard(mutex)(&chip->mutex); 784 784 uaudio_dev_cleanup(&uadev[idx]); 785 - mutex_unlock(&chip->mutex); 786 785 } 787 - mutex_unlock(&qdev_mutex); 788 786 } 789 787 790 788 /** ··· 808 810 809 811 chip = usb_get_intfdata(intf); 810 812 811 - mutex_lock(&qdev_mutex); 812 - mutex_lock(&chip->mutex); 813 + guard(mutex)(&qdev_mutex); 814 + guard(mutex)(&chip->mutex); 813 815 814 816 dev = &uadev[chip->card->number]; 815 817 ··· 822 824 uaudio_send_disconnect_ind(chip); 823 825 } 824 826 } 825 - 826 - mutex_unlock(&chip->mutex); 827 - mutex_unlock(&qdev_mutex); 828 827 829 828 return 0; 830 829 } ··· 1572 1577 goto response; 1573 1578 } 1574 1579 1575 - mutex_lock(&chip->mutex); 1576 - if (req_msg->enable) { 1577 - if (info_idx < 0 || chip->system_suspend || subs->opened) { 1578 - ret = -EBUSY; 1579 - mutex_unlock(&chip->mutex); 1580 - 1581 - goto response; 1580 + scoped_guard(mutex, &chip->mutex) { 1581 + if (req_msg->enable) { 1582 + if (info_idx < 0 || chip->system_suspend || subs->opened) { 1583 + ret = -EBUSY; 1584 + goto response; 1585 + } 1586 + subs->opened = 1; 1582 1587 } 1583 - subs->opened = 1; 1584 1588 } 1585 - mutex_unlock(&chip->mutex); 1586 1589 1587 1590 if (req_msg->service_interval_valid) { 1588 1591 ret = get_data_interval_from_si(subs, ··· 1603 1610 ret = prepare_qmi_response(subs, req_msg, &resp, 1604 1611 info_idx); 1605 1612 if (ret < 0) { 1606 - mutex_lock(&chip->mutex); 1613 + guard(mutex)(&chip->mutex); 1607 1614 subs->opened = 0; 1608 - mutex_unlock(&chip->mutex); 1609 1615 } 1610 1616 } else { 1611 1617 info = &uadev[pcm_card_num].info[info_idx]; ··· 1635 1643 } 1636 1644 1637 1645 disable_audio_stream(subs); 1638 - mutex_lock(&chip->mutex); 1646 + guard(mutex)(&chip->mutex); 1639 1647 subs->opened = 0; 1640 - mutex_unlock(&chip->mutex); 1641 1648 } 1642 1649 1643 1650 response: 1644 1651 if (!req_msg->enable && ret != -EINVAL && ret != -ENODEV) { 1645 - mutex_lock(&chip->mutex); 1652 + guard(mutex)(&chip->mutex); 1646 1653 if (info_idx >= 0) { 1647 1654 info = &uadev[pcm_card_num].info[info_idx]; 1648 1655 uaudio_dev_intf_cleanup(uadev[pcm_card_num].udev, ··· 1650 1659 if (atomic_read(&uadev[pcm_card_num].in_use)) 1651 1660 kref_put(&uadev[pcm_card_num].kref, 1652 1661 uaudio_dev_release); 1653 - mutex_unlock(&chip->mutex); 1654 1662 } 1655 1663 mutex_unlock(&qdev_mutex); 1656 1664 ··· 1752 1762 !usb_qmi_get_pcm_num(chip, 0)) 1753 1763 return; 1754 1764 1755 - mutex_lock(&qdev_mutex); 1756 - mutex_lock(&chip->mutex); 1765 + guard(mutex)(&qdev_mutex); 1766 + guard(mutex)(&chip->mutex); 1757 1767 if (!uadev[chip->card->number].chip) { 1758 1768 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 1759 1769 if (!sdev) 1760 - goto exit; 1770 + return; 1761 1771 1762 1772 sb = xhci_sideband_register(intf, XHCI_SIDEBAND_VENDOR, 1763 1773 uaudio_sideband_notifier); ··· 1796 1806 snd_soc_usb_connect(uaudio_qdev->auxdev->dev.parent, sdev); 1797 1807 } 1798 1808 1799 - mutex_unlock(&chip->mutex); 1800 - mutex_unlock(&qdev_mutex); 1801 - 1802 1809 return; 1803 1810 1804 1811 unreg_xhci: ··· 1805 1818 kfree(sdev); 1806 1819 uadev[chip->card->number].sdev = NULL; 1807 1820 uadev[chip->card->number].chip = NULL; 1808 - exit: 1809 - mutex_unlock(&chip->mutex); 1810 - mutex_unlock(&qdev_mutex); 1811 1821 } 1812 1822 1813 1823 /** ··· 1840 1856 if (card_num >= SNDRV_CARDS) 1841 1857 return; 1842 1858 1843 - mutex_lock(&qdev_mutex); 1844 - mutex_lock(&chip->mutex); 1859 + guard(mutex)(&qdev_mutex); 1860 + guard(mutex)(&chip->mutex); 1845 1861 dev = &uadev[card_num]; 1846 1862 1847 1863 /* Device has already been cleaned up, or never populated */ 1848 - if (!dev->chip) { 1849 - mutex_unlock(&chip->mutex); 1850 - mutex_unlock(&qdev_mutex); 1864 + if (!dev->chip) 1851 1865 return; 1852 - } 1853 1866 1854 1867 /* cleaned up already */ 1855 1868 if (!dev->udev) ··· 1867 1886 kfree(dev->sdev); 1868 1887 dev->sdev = NULL; 1869 1888 } 1870 - mutex_unlock(&chip->mutex); 1871 - 1872 - mutex_unlock(&qdev_mutex); 1873 1889 } 1874 1890 1875 1891 /** ··· 1891 1913 if (card_num >= SNDRV_CARDS) 1892 1914 return; 1893 1915 1894 - mutex_lock(&qdev_mutex); 1895 - mutex_lock(&chip->mutex); 1916 + guard(mutex)(&qdev_mutex); 1917 + guard(mutex)(&chip->mutex); 1896 1918 1897 1919 uaudio_send_disconnect_ind(chip); 1898 - 1899 - mutex_unlock(&chip->mutex); 1900 - mutex_unlock(&qdev_mutex); 1901 1920 } 1902 1921 1903 1922 static struct snd_usb_platform_ops offload_ops = {