PM: Fix suspend_console and resume_console to use only one semaphore

This fixes a race where a thread acquires the console while the
console is suspended, and the console is resumed before this
thread releases it. In this case, the secondary console
semaphore would be left locked, and the primary semaphore would
be released twice. This in turn would cause the console switch
on suspend or resume to hang forever.

Note that suspend_console does not actually lock the console
for clients that use acquire_console_sem, it only locks it for
clients that use try_acquire_console_sem. If we change
suspend_console to fully lock the console, then the kernel
may deadlock on suspend. One client of try_acquire_console_sem
is acquire_console_semaphore_for_printk, which uses it to
prevent printk from using the console while it is suspended.

Signed-off-by: Arve Hjønnevåg <arve@android.com>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Len Brown <lenb@kernel.org>
Cc: Greg KH <gregkh@suse.de>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Arve Hjønnevåg and committed by Linus Torvalds 403f3075 b090f9fa

+9 -6
+9 -6
kernel/printk.c
··· 73 * driver system. 74 */ 75 static DECLARE_MUTEX(console_sem); 76 - static DECLARE_MUTEX(secondary_console_sem); 77 struct console *console_drivers; 78 EXPORT_SYMBOL_GPL(console_drivers); 79 ··· 890 printk("Suspending console(s) (use no_console_suspend to debug)\n"); 891 acquire_console_sem(); 892 console_suspended = 1; 893 } 894 895 void resume_console(void) 896 { 897 if (!console_suspend_enabled) 898 return; 899 console_suspended = 0; 900 release_console_sem(); 901 } ··· 913 void acquire_console_sem(void) 914 { 915 BUG_ON(in_interrupt()); 916 - if (console_suspended) { 917 - down(&secondary_console_sem); 918 - return; 919 - } 920 down(&console_sem); 921 console_locked = 1; 922 console_may_schedule = 1; 923 } ··· 925 { 926 if (down_trylock(&console_sem)) 927 return -1; 928 console_locked = 1; 929 console_may_schedule = 0; 930 return 0; ··· 982 unsigned wake_klogd = 0; 983 984 if (console_suspended) { 985 - up(&secondary_console_sem); 986 return; 987 } 988
··· 73 * driver system. 74 */ 75 static DECLARE_MUTEX(console_sem); 76 struct console *console_drivers; 77 EXPORT_SYMBOL_GPL(console_drivers); 78 ··· 891 printk("Suspending console(s) (use no_console_suspend to debug)\n"); 892 acquire_console_sem(); 893 console_suspended = 1; 894 + up(&console_sem); 895 } 896 897 void resume_console(void) 898 { 899 if (!console_suspend_enabled) 900 return; 901 + down(&console_sem); 902 console_suspended = 0; 903 release_console_sem(); 904 } ··· 912 void acquire_console_sem(void) 913 { 914 BUG_ON(in_interrupt()); 915 down(&console_sem); 916 + if (console_suspended) 917 + return; 918 console_locked = 1; 919 console_may_schedule = 1; 920 } ··· 926 { 927 if (down_trylock(&console_sem)) 928 return -1; 929 + if (console_suspended) { 930 + up(&console_sem); 931 + return -1; 932 + } 933 console_locked = 1; 934 console_may_schedule = 0; 935 return 0; ··· 979 unsigned wake_klogd = 0; 980 981 if (console_suspended) { 982 + up(&console_sem); 983 return; 984 } 985