diff mbox series

ALSA: pcm: Fix starvation on down_write_nonblock()

Message ID 1543044763-31843-1-git-send-email-chanho.min@lge.com (mailing list archive)
State New, archived
Headers show
Series ALSA: pcm: Fix starvation on down_write_nonblock() | expand

Commit Message

Chanho Min Nov. 24, 2018, 7:32 a.m. UTC
Commit 67ec1072b053 (ALSA: pcm: Fix rwsem deadlock for non-atomic PCM stream)
fixes deadlock for non-atomic PCM stream. But, This patch causes antother stuck.
If writer is RT thread and reader is a normal thread, the reader thread will
be difficult to get scheduled. It may not give chance to release read locks
and writer gets stuck for a long time or forever if they are pinned to single
cpu.

To fix this, The writer gives reader a chance to be scheduled by using the
minimum msleep() instaed of spinning. This is for concept, We may need to
change the function name and comments or suggest another approach.

Signed-off-by: Chanho Min <chanho.min@lge.com>
---
 sound/core/pcm_native.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

kernel test robot Nov. 24, 2018, 10:56 a.m. UTC | #1
Hi Chanho,

I love your patch! Yet something to improve:

[auto build test ERROR on sound/for-next]
[also build test ERROR on v4.20-rc3 next-20181123]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Chanho-Min/ALSA-pcm-Fix-starvation-on-down_write_nonblock/20181124-182630
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
config: x86_64-randconfig-x001-201846 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

   sound/core/pcm_native.c: In function 'down_write_nonblock':
>> sound/core/pcm_native.c:99:3: error: implicit declaration of function 'msleep' [-Werror=implicit-function-declaration]
      msleep(1);
      ^~~~~~
   cc1: some warnings being treated as errors

vim +/msleep +99 sound/core/pcm_native.c

    89	
    90	/* Writer in rwsem may block readers even during its waiting in queue,
    91	 * and this may lead to a deadlock when the code path takes read sem
    92	 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
    93	 * snd_pcm_stream_lock()).  As a (suboptimal) workaround, let writer to
    94	 * spin until it gets the lock.
    95	 */
    96	static inline void down_write_nonblock(struct rw_semaphore *lock)
    97	{
    98		while (!down_write_trylock(lock))
  > 99			msleep(1);
   100	}
   101	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
kernel test robot Nov. 25, 2018, 11:30 a.m. UTC | #2
Hi Chanho,

I love your patch! Yet something to improve:

[auto build test ERROR on sound/for-next]
[also build test ERROR on v4.20-rc3 next-20181123]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Chanho-Min/ALSA-pcm-Fix-starvation-on-down_write_nonblock/20181124-182630
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
config: x86_64-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

   include/linux/slab.h:332:43: warning: dubious: x & !y
   include/linux/slab.h:332:43: warning: dubious: x & !y
   include/linux/slab.h:332:43: warning: dubious: x & !y
   include/linux/slab.h:332:43: warning: dubious: x & !y
   sound/core/pcm_native.c:590:51: warning: incorrect type in assignment (different base types)
   sound/core/pcm_native.c:590:51:    expected restricted snd_pcm_state_t [usertype] state
   sound/core/pcm_native.c:590:51:    got int [signed] state
   sound/core/pcm_native.c:755:38: warning: incorrect type in argument 2 (different base types)
   sound/core/pcm_native.c:755:38:    expected int [signed] state
   sound/core/pcm_native.c:755:38:    got restricted snd_pcm_state_t [usertype] <noident>
   sound/core/pcm_native.c:767:38: warning: incorrect type in argument 2 (different base types)
   sound/core/pcm_native.c:767:38:    expected int [signed] state
   sound/core/pcm_native.c:767:38:    got restricted snd_pcm_state_t [usertype] <noident>
   sound/core/pcm_native.c:816:38: warning: incorrect type in argument 2 (different base types)
   sound/core/pcm_native.c:816:38:    expected int [signed] state
   sound/core/pcm_native.c:816:38:    got restricted snd_pcm_state_t [usertype] <noident>
   sound/core/pcm_native.c:1226:32: warning: incorrect type in assignment (different base types)
   sound/core/pcm_native.c:1226:32:    expected restricted snd_pcm_state_t [usertype] state
   sound/core/pcm_native.c:1226:32:    got int [signed] state
   sound/core/pcm_native.c:1250:31: warning: incorrect type in argument 3 (different base types)
   sound/core/pcm_native.c:1250:31:    expected int [signed] state
   sound/core/pcm_native.c:1250:31:    got restricted snd_pcm_state_t [usertype] <noident>
   sound/core/pcm_native.c:1257:40: warning: incorrect type in argument 3 (different base types)
   sound/core/pcm_native.c:1257:40:    expected int [signed] state
   sound/core/pcm_native.c:1257:40:    got restricted snd_pcm_state_t [usertype] <noident>
   sound/core/pcm_native.c:1283:28: warning: restricted snd_pcm_state_t degrades to integer
   sound/core/pcm_native.c:1285:40: warning: incorrect type in assignment (different base types)
   sound/core/pcm_native.c:1285:40:    expected restricted snd_pcm_state_t [usertype] state
   sound/core/pcm_native.c:1285:40:    got int [signed] state
   sound/core/pcm_native.c:1309:64: warning: incorrect type in argument 3 (different base types)
   sound/core/pcm_native.c:1309:64:    expected int [signed] state
   sound/core/pcm_native.c:1309:64:    got restricted snd_pcm_state_t [usertype] state
   sound/core/pcm_native.c:1325:38: warning: incorrect type in argument 3 (different base types)
   sound/core/pcm_native.c:1325:38:    expected int [signed] state
   sound/core/pcm_native.c:1325:38:    got restricted snd_pcm_state_t [usertype] <noident>
   sound/core/pcm_native.c:1684:38: warning: incorrect type in argument 2 (different base types)
   sound/core/pcm_native.c:1684:38:    expected int [signed] state
   sound/core/pcm_native.c:1684:38:    got restricted snd_pcm_state_t [usertype] <noident>
   sound/core/pcm_native.c:1750:61: warning: incorrect type in argument 2 (different base types)
   sound/core/pcm_native.c:1750:61:    expected int [signed] state
   sound/core/pcm_native.c:1750:61:    got restricted snd_pcm_state_t [usertype] <noident>
   sound/core/pcm_native.c:1751:63: warning: incorrect type in argument 2 (different base types)
   sound/core/pcm_native.c:1751:63:    expected int [signed] state
   sound/core/pcm_native.c:1751:63:    got restricted snd_pcm_state_t [usertype] <noident>
   sound/core/pcm_native.c:1768:76: warning: incorrect type in initializer (different base types)
   sound/core/pcm_native.c:1768:76:    expected int [signed] new_state
   sound/core/pcm_native.c:1768:76:    got restricted snd_pcm_state_t
   include/linux/slab.h:332:43: warning: dubious: x & !y
>> sound/core/pcm_native.c:99:17: error: undefined identifier 'msleep'
>> sound/core/pcm_native.c:99:23: error: not a function <noident>
   sound/core/pcm_native.c:2089:26: warning: restricted snd_pcm_format_t degrades to integer
   sound/core/pcm_native.c:2093:54: warning: incorrect type in argument 1 (different base types)
   sound/core/pcm_native.c:2093:54:    expected restricted snd_pcm_format_t [usertype] format
   sound/core/pcm_native.c:2093:54:    got unsigned int [unsigned] [assigned] k
   sound/core/pcm_native.c:2111:26: warning: restricted snd_pcm_format_t degrades to integer
   sound/core/pcm_native.c:2115:54: warning: incorrect type in argument 1 (different base types)
   sound/core/pcm_native.c:2115:54:    expected restricted snd_pcm_format_t [usertype] format
   sound/core/pcm_native.c:2115:54:    got unsigned int [unsigned] [assigned] k
   sound/core/pcm_native.c:2295:30: warning: restricted snd_pcm_access_t degrades to integer
   sound/core/pcm_native.c:2297:30: warning: restricted snd_pcm_access_t degrades to integer
   sound/core/pcm_native.c:2300:38: warning: restricted snd_pcm_access_t degrades to integer
   sound/core/pcm_native.c:2302:38: warning: restricted snd_pcm_access_t degrades to integer
   sound/core/pcm_native.c:2304:38: warning: restricted snd_pcm_access_t degrades to integer
   sound/core/pcm_native.c:2314:86: warning: restricted snd_pcm_subformat_t degrades to integer
   include/linux/slab.h:332:43: warning: dubious: x & !y
   include/linux/slab.h:332:43: warning: dubious: x & !y
   include/linux/slab.h:332:43: warning: dubious: x & !y
   sound/core/pcm_compat.c:226:13: warning: incorrect type in assignment (different base types)
   sound/core/pcm_compat.c:226:13:    expected signed int [signed] [explicitly-signed] __pu_val
   sound/core/pcm_compat.c:226:13:    got restricted snd_pcm_state_t [addressable] [assigned] [usertype] state
   sound/core/pcm_compat.c:235:13: warning: incorrect type in assignment (different base types)
   sound/core/pcm_compat.c:235:13:    expected signed int [signed] [explicitly-signed] __pu_val
   sound/core/pcm_compat.c:235:13:    got restricted snd_pcm_state_t [addressable] [assigned] [usertype] suspended_state
   sound/core/pcm_compat.c:290:13: warning: incorrect type in assignment (different base types)
   sound/core/pcm_compat.c:290:13:    expected signed int [signed] [explicitly-signed] __pu_val
   sound/core/pcm_compat.c:290:13:    got restricted snd_pcm_state_t [addressable] [assigned] [usertype] state
   sound/core/pcm_compat.c:299:13: warning: incorrect type in assignment (different base types)
   sound/core/pcm_compat.c:299:13:    expected signed int [signed] [explicitly-signed] __pu_val
   sound/core/pcm_compat.c:299:13:    got restricted snd_pcm_state_t [addressable] [assigned] [usertype] suspended_state
   include/linux/slab.h:332:43: warning: dubious: x & !y
   include/linux/slab.h:332:43: warning: dubious: x & !y
   sound/core/pcm_compat.c:525:13: warning: incorrect type in assignment (different base types)
   sound/core/pcm_compat.c:525:13:    expected signed int [signed] [explicitly-signed] __pu_val
   sound/core/pcm_compat.c:525:13:    got restricted snd_pcm_state_t [assigned] [usertype] state
   sound/core/pcm_compat.c:528:13: warning: incorrect type in assignment (different base types)
   sound/core/pcm_compat.c:528:13:    expected signed int [signed] [explicitly-signed] __pu_val
   sound/core/pcm_compat.c:528:13:    got restricted snd_pcm_state_t [assigned] [usertype] suspended_state
   sound/core/pcm_compat.c:614:13: warning: incorrect type in assignment (different base types)
   sound/core/pcm_compat.c:614:13:    expected signed int [signed] [explicitly-signed] __pu_val
   sound/core/pcm_compat.c:614:13:    got restricted snd_pcm_state_t [assigned] [usertype] state
   sound/core/pcm_compat.c:617:13: warning: incorrect type in assignment (different base types)
   sound/core/pcm_compat.c:617:13:    expected signed int [signed] [explicitly-signed] __pu_val
   sound/core/pcm_compat.c:617:13:    got restricted snd_pcm_state_t [assigned] [usertype] suspended_state
   sound/core/pcm_native.c:127:9: warning: context imbalance in '__snd_pcm_stream_lock_mode' - different lock contexts for basic block
   sound/core/pcm_native.c:137:28: warning: context imbalance in '__snd_pcm_stream_unlock_mode' - unexpected unlock
   sound/core/pcm_native.c:1097:52: warning: context imbalance in 'snd_pcm_action_group' - unexpected unlock
   sound/core/pcm_native.c: In function 'down_write_nonblock':
   sound/core/pcm_native.c:99:3: error: implicit declaration of function 'msleep' [-Werror=implicit-function-declaration]
      msleep(1);
      ^~~~~~
   cc1: some warnings being treated as errors

vim +/msleep +99 sound/core/pcm_native.c

    89	
    90	/* Writer in rwsem may block readers even during its waiting in queue,
    91	 * and this may lead to a deadlock when the code path takes read sem
    92	 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
    93	 * snd_pcm_stream_lock()).  As a (suboptimal) workaround, let writer to
    94	 * spin until it gets the lock.
    95	 */
    96	static inline void down_write_nonblock(struct rw_semaphore *lock)
    97	{
    98		while (!down_write_trylock(lock))
  > 99			msleep(1);
   100	}
   101	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
diff mbox series

Patch

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 66c90f4..88d4aab 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -96,7 +96,7 @@  static DECLARE_RWSEM(snd_pcm_link_rwsem);
 static inline void down_write_nonblock(struct rw_semaphore *lock)
 {
 	while (!down_write_trylock(lock))
-		cond_resched();
+		msleep(1);
 }
 
 #define PCM_LOCK_DEFAULT	0