staging: r8188eu: Use a Mutex instead of a binary Semaphore
authorFabio M. De Francesco <fmdefrancesco@gmail.com>
Fri, 22 Oct 2021 17:19:17 +0000 (19:19 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 24 Oct 2021 12:31:20 +0000 (14:31 +0200)
Use a Mutex instead of a binary Semaphore for the purpose of enforcing
mutual exclusive access to the "pwrctrl_priv" structure.

Mutexes are sleeping locks similar to Semaphores with a 'count' of one
(like binary Semaphores), however they have a simpler interface, more
efficient performance, and additional constraints.

There is no change in the logic of the new code; however it is more
simple because it gets rid of four unnecessary wrappers:
_init_pwrlock(), _enter_pwrlock(),_exit_pwrlock(), _rtw_down_sema().

Actually, there is a change in the state in which the code waits for
acquiring locks, because it makes it in an uninterruptible state
(instead the old code used down_interruptibe()). Interruptible
waits are neither required nor wanted in this driver.

Tested with ASUSTek Computer, Inc. Realtek 8188EUS [USB-N10 Nano].

Acked-by: Larry Finger <Larry.Finger@lwfinger.net>
Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>
Link: https://lore.kernel.org/r/20211022171917.24363-1-fmdefrancesco@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/r8188eu/core/rtw_pwrctrl.c
drivers/staging/r8188eu/include/osdep_service.h
drivers/staging/r8188eu/include/rtw_pwrctrl.h
drivers/staging/r8188eu/os_dep/osdep_service.c
drivers/staging/r8188eu/os_dep/usb_intf.c

index 19cac5814ea4891b15de937f8015a911caab7963..5d595cf2a47e2bb64fb0f0d214803f9832edb460 100644 (file)
@@ -21,7 +21,7 @@ void ips_enter(struct adapter *padapter)
                return;
        }
 
-       _enter_pwrlock(&pwrpriv->lock);
+       mutex_lock(&pwrpriv->lock);
 
        pwrpriv->bips_processing = true;
 
@@ -42,7 +42,7 @@ void ips_enter(struct adapter *padapter)
        }
        pwrpriv->bips_processing = false;
 
-       _exit_pwrlock(&pwrpriv->lock);
+       mutex_unlock(&pwrpriv->lock);
 }
 
 int ips_leave(struct adapter *padapter)
@@ -53,7 +53,7 @@ int ips_leave(struct adapter *padapter)
        int result = _SUCCESS;
        int keyid;
 
-       _enter_pwrlock(&pwrpriv->lock);
+       mutex_lock(&pwrpriv->lock);
 
        if ((pwrpriv->rf_pwrstate == rf_off) && (!pwrpriv->bips_processing)) {
                pwrpriv->bips_processing = true;
@@ -87,7 +87,7 @@ int ips_leave(struct adapter *padapter)
                pwrpriv->bpower_saving = false;
        }
 
-       _exit_pwrlock(&pwrpriv->lock);
+       mutex_unlock(&pwrpriv->lock);
 
        return result;
 }
@@ -337,7 +337,7 @@ void rtw_init_pwrctrl_priv(struct adapter *padapter)
 {
        struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
 
-       _init_pwrlock(&pwrctrlpriv->lock);
+       mutex_init(&pwrctrlpriv->lock);
        pwrctrlpriv->rf_pwrstate = rf_on;
        pwrctrlpriv->ips_enter_cnts = 0;
        pwrctrlpriv->ips_leave_cnts = 0;
index 886a1b6f30b4bcd6c9aa288424996b373b174c8d..efab3a97eb46df9a2aafb4b6561430d10d468c67 100644 (file)
@@ -141,8 +141,6 @@ extern unsigned char RSN_TKIP_CIPHER[4];
 
 void *rtw_malloc2d(int h, int w, int size);
 
-u32  _rtw_down_sema(struct semaphore *sema);
-
 #define rtw_init_queue(q)                                      \
        do {                                                    \
                INIT_LIST_HEAD(&((q)->queue));                  \
index 04236e42fbf9033492b40bcc95bad3cc3510115e..b19ef796ab543b456c0262eeaea03c4d8e668054 100644 (file)
@@ -27,21 +27,6 @@ enum power_mgnt {
        PS_MODE_NUM
 };
 
-static inline void _init_pwrlock(struct semaphore  *plock)
-{
-       sema_init(plock, 1);
-}
-
-static inline void _enter_pwrlock(struct semaphore  *plock)
-{
-       _rtw_down_sema(plock);
-}
-
-static inline void _exit_pwrlock(struct semaphore  *plock)
-{
-       up(plock);
-}
-
 #define LPS_DELAY_TIME 1*HZ /*  1 sec */
 
 /*  RF state. */
@@ -60,7 +45,7 @@ enum { /*  for ips_mode */
 };
 
 struct pwrctrl_priv {
-       struct semaphore lock;
+       struct mutex lock; /* Mutex used to protect struct pwrctrl_priv */
 
        u8      pwr_mode;
        u8      smart_ps;
index 6bee194fc35d95ff9dbc6634a6b8689115894669..59bdd0abea7ef52ee24991d92266d0b9047546c9 100644 (file)
@@ -42,14 +42,6 @@ Otherwise, there will be racing condition.
 Caller must check if the list is empty before calling rtw_list_delete
 */
 
-u32 _rtw_down_sema(struct semaphore *sema)
-{
-       if (down_interruptible(sema))
-               return _FAIL;
-       else
-               return _SUCCESS;
-}
-
 inline u32 rtw_systime_to_ms(u32 systime)
 {
        return systime * 1000 / HZ;
index 40f2481afc6ffbc219b283e60804f93a261a5a47..6b93830130e7a55bc2a50fbd939f21dadf91046e 100644 (file)
@@ -234,7 +234,7 @@ static int rtw_suspend(struct usb_interface *pusb_intf, pm_message_t message)
        rtw_cancel_all_timer(padapter);
        LeaveAllPowerSaveMode(padapter);
 
-       _enter_pwrlock(&pwrpriv->lock);
+       mutex_lock(&pwrpriv->lock);
        /* s1. */
        if (pnetdev) {
                netif_carrier_off(pnetdev);
@@ -263,7 +263,7 @@ static int rtw_suspend(struct usb_interface *pusb_intf, pm_message_t message)
        rtw_free_network_queue(padapter, true);
 
        rtw_dev_unload(padapter);
-       _exit_pwrlock(&pwrpriv->lock);
+       mutex_unlock(&pwrpriv->lock);
 
        if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY))
                rtw_indicate_scan_done(padapter, 1);
@@ -292,7 +292,7 @@ static int rtw_resume(struct usb_interface *pusb_intf)
        pnetdev = padapter->pnetdev;
        pwrpriv = &padapter->pwrctrlpriv;
 
-       _enter_pwrlock(&pwrpriv->lock);
+       mutex_lock(&pwrpriv->lock);
        rtw_reset_drv_sw(padapter);
        if (pwrpriv)
                pwrpriv->bkeepfwalive = false;
@@ -304,7 +304,7 @@ static int rtw_resume(struct usb_interface *pusb_intf)
        netif_device_attach(pnetdev);
        netif_carrier_on(pnetdev);
 
-       _exit_pwrlock(&pwrpriv->lock);
+       mutex_unlock(&pwrpriv->lock);
 
        if (padapter->pid[1] != 0) {
                DBG_88E("pid[1]:%d\n", padapter->pid[1]);