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

Input: ati_remote2 - use guard notation when acquiring mutex

Using guard notation makes the code more compact and error handling
more robust by ensuring that mutexes are released in all code paths
when control leaves critical section.

Reviewed-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
Link: https://lore.kernel.org/r/20240904044244.1042174-3-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+19 -38
+19 -38
drivers/input/misc/ati_remote2.c
··· 244 244 if (r) { 245 245 dev_err(&ar2->intf[0]->dev, 246 246 "%s(): usb_autopm_get_interface() = %d\n", __func__, r); 247 - goto fail1; 247 + return r; 248 248 } 249 249 250 - mutex_lock(&ati_remote2_mutex); 250 + scoped_guard(mutex, &ati_remote2_mutex) { 251 + if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) { 252 + r = ati_remote2_submit_urbs(ar2); 253 + if (r) 254 + break; 255 + } 251 256 252 - if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) { 253 - r = ati_remote2_submit_urbs(ar2); 254 - if (r) 255 - goto fail2; 257 + ar2->flags |= ATI_REMOTE2_OPENED; 256 258 } 257 - 258 - ar2->flags |= ATI_REMOTE2_OPENED; 259 - 260 - mutex_unlock(&ati_remote2_mutex); 261 259 262 260 usb_autopm_put_interface(ar2->intf[0]); 263 261 264 - return 0; 265 - 266 - fail2: 267 - mutex_unlock(&ati_remote2_mutex); 268 - usb_autopm_put_interface(ar2->intf[0]); 269 - fail1: 270 262 return r; 271 263 } 272 264 ··· 268 276 269 277 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 270 278 271 - mutex_lock(&ati_remote2_mutex); 279 + guard(mutex)(&ati_remote2_mutex); 272 280 273 281 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) 274 282 ati_remote2_kill_urbs(ar2); 275 283 276 284 ar2->flags &= ~ATI_REMOTE2_OPENED; 277 - 278 - mutex_unlock(&ati_remote2_mutex); 279 285 } 280 286 281 287 static void ati_remote2_input_mouse(struct ati_remote2 *ar2) ··· 703 713 return r; 704 714 } 705 715 706 - mutex_lock(&ati_remote2_mutex); 707 - 708 - if (mask != ar2->channel_mask) { 709 - r = ati_remote2_setup(ar2, mask); 710 - if (!r) 711 - ar2->channel_mask = mask; 716 + scoped_guard(mutex, &ati_remote2_mutex) { 717 + if (mask != ar2->channel_mask) { 718 + r = ati_remote2_setup(ar2, mask); 719 + if (!r) 720 + ar2->channel_mask = mask; 721 + } 712 722 } 713 - 714 - mutex_unlock(&ati_remote2_mutex); 715 723 716 724 usb_autopm_put_interface(ar2->intf[0]); 717 725 ··· 880 892 881 893 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 882 894 883 - mutex_lock(&ati_remote2_mutex); 895 + guard(mutex)(&ati_remote2_mutex); 884 896 885 897 if (ar2->flags & ATI_REMOTE2_OPENED) 886 898 ati_remote2_kill_urbs(ar2); 887 899 888 900 ar2->flags |= ATI_REMOTE2_SUSPENDED; 889 - 890 - mutex_unlock(&ati_remote2_mutex); 891 901 892 902 return 0; 893 903 } ··· 903 917 904 918 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 905 919 906 - mutex_lock(&ati_remote2_mutex); 920 + guard(mutex)(&ati_remote2_mutex); 907 921 908 922 if (ar2->flags & ATI_REMOTE2_OPENED) 909 923 r = ati_remote2_submit_urbs(ar2); 910 924 911 925 if (!r) 912 926 ar2->flags &= ~ATI_REMOTE2_SUSPENDED; 913 - 914 - mutex_unlock(&ati_remote2_mutex); 915 927 916 928 return r; 917 929 } ··· 927 943 928 944 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); 929 945 930 - mutex_lock(&ati_remote2_mutex); 946 + guard(mutex)(&ati_remote2_mutex); 931 947 932 948 r = ati_remote2_setup(ar2, ar2->channel_mask); 933 949 if (r) 934 - goto out; 950 + return r; 935 951 936 952 if (ar2->flags & ATI_REMOTE2_OPENED) 937 953 r = ati_remote2_submit_urbs(ar2); 938 954 939 955 if (!r) 940 956 ar2->flags &= ~ATI_REMOTE2_SUSPENDED; 941 - 942 - out: 943 - mutex_unlock(&ati_remote2_mutex); 944 957 945 958 return r; 946 959 }