Make printk() console semaphore accesses sensible

The printk() logic on when/how to get the console semaphore was
unreadable, this splits the code up into a few helper functions and
makes it easier to follow what is going on.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

+48 -35
+48 -35
kernel/printk.c
··· 616 616 /* cpu currently holding logbuf_lock */ 617 617 static volatile unsigned int printk_cpu = UINT_MAX; 618 618 619 + /* 620 + * Can we actually use the console at this time on this cpu? 621 + * 622 + * Console drivers may assume that per-cpu resources have 623 + * been allocated. So unless they're explicitly marked as 624 + * being able to cope (CON_ANYTIME) don't call them until 625 + * this CPU is officially up. 626 + */ 627 + static inline int can_use_console(unsigned int cpu) 628 + { 629 + return cpu_online(cpu) || have_callable_console(); 630 + } 631 + 632 + /* 633 + * Try to get console ownership to actually show the kernel 634 + * messages from a 'printk'. Return true (and with the 635 + * console_semaphore held, and 'console_locked' set) if it 636 + * is successful, false otherwise. 637 + * 638 + * This gets called with the 'logbuf_lock' spinlock held and 639 + * interrupts disabled. It should return with 'lockbuf_lock' 640 + * released but interrupts still disabled. 641 + */ 642 + static int acquire_console_semaphore_for_printk(unsigned int cpu) 643 + { 644 + int retval = 0; 645 + 646 + if (can_use_console(cpu)) 647 + retval = !try_acquire_console_sem(); 648 + printk_cpu = UINT_MAX; 649 + spin_unlock(&logbuf_lock); 650 + return retval; 651 + } 652 + 619 653 const char printk_recursion_bug_msg [] = 620 654 KERN_CRIT "BUG: recent printk recursion!\n"; 621 655 static int printk_recursion_bug; ··· 759 725 log_level_unknown = 1; 760 726 } 761 727 762 - if (!down_trylock(&console_sem)) { 763 - /* 764 - * We own the drivers. We can drop the spinlock and 765 - * let release_console_sem() print the text, maybe ... 766 - */ 767 - console_locked = 1; 768 - printk_cpu = UINT_MAX; 769 - spin_unlock(&logbuf_lock); 728 + /* 729 + * Try to acquire and then immediately release the 730 + * console semaphore. The release will do all the 731 + * actual magic (print out buffers, wake up klogd, 732 + * etc). 733 + * 734 + * The acquire_console_semaphore_for_printk() function 735 + * will release 'logbuf_lock' regardless of whether it 736 + * actually gets the semaphore or not. 737 + */ 738 + if (acquire_console_semaphore_for_printk(this_cpu)) 739 + release_console_sem(); 770 740 771 - /* 772 - * Console drivers may assume that per-cpu resources have 773 - * been allocated. So unless they're explicitly marked as 774 - * being able to cope (CON_ANYTIME) don't call them until 775 - * this CPU is officially up. 776 - */ 777 - if (cpu_online(smp_processor_id()) || have_callable_console()) { 778 - console_may_schedule = 0; 779 - release_console_sem(); 780 - } else { 781 - /* Release by hand to avoid flushing the buffer. */ 782 - console_locked = 0; 783 - up(&console_sem); 784 - } 785 - lockdep_on(); 786 - raw_local_irq_restore(flags); 787 - } else { 788 - /* 789 - * Someone else owns the drivers. We drop the spinlock, which 790 - * allows the semaphore holder to proceed and to call the 791 - * console drivers with the output which we just produced. 792 - */ 793 - printk_cpu = UINT_MAX; 794 - spin_unlock(&logbuf_lock); 795 - lockdep_on(); 741 + lockdep_on(); 796 742 out_restore_irqs: 797 - raw_local_irq_restore(flags); 798 - } 743 + raw_local_irq_restore(flags); 799 744 800 745 preempt_enable(); 801 746 return printed_len;