Commit b371cbb5 authored by John Ogness's avatar John Ogness Committed by Petr Mladek

printk: convert @syslog_lock to mutex

@syslog_lock was a raw_spin_lock to simplify the transition of
removing @logbuf_lock and the safe buffers. With that transition
complete, and since all uses of @syslog_lock are within sleepable
contexts, @syslog_lock can become a mutex.

Note that until now register_console() would disable interrupts
using irqsave, which implies that it may be called with interrupts
disabled. And indeed, there is one possible call chain on parisc
where this happens:

handle_interruption(code=1) /* High-priority machine check (HPMC) */
  pdc_console_restart()
    pdc_console_init_force()
      register_console()

However, register_console() calls console_lock(), which might sleep.
So it has never been allowed to call register_console() from an
atomic context and the above call chain is a bug.

Note that the removal of read_syslog_seq_irq() is slightly changing
the behavior of SYSLOG_ACTION_READ by testing against a possibly
outdated @seq value. However, the value of @seq could have changed
after the test, so it is not a new window. A follow-up commit closes
this window.
Signed-off-by: default avatarJohn Ogness <john.ogness@linutronix.de>
Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20210715193359.25946-6-john.ogness@linutronix.de
parent 85e3e7fb
...@@ -356,7 +356,7 @@ enum log_flags { ...@@ -356,7 +356,7 @@ enum log_flags {
}; };
/* syslog_lock protects syslog_* variables and write access to clear_seq. */ /* syslog_lock protects syslog_* variables and write access to clear_seq. */
static DEFINE_RAW_SPINLOCK(syslog_lock); static DEFINE_MUTEX(syslog_lock);
#ifdef CONFIG_PRINTK #ifdef CONFIG_PRINTK
DECLARE_WAIT_QUEUE_HEAD(log_wait); DECLARE_WAIT_QUEUE_HEAD(log_wait);
...@@ -1497,9 +1497,9 @@ static int syslog_print(char __user *buf, int size) ...@@ -1497,9 +1497,9 @@ static int syslog_print(char __user *buf, int size)
size_t n; size_t n;
size_t skip; size_t skip;
raw_spin_lock_irq(&syslog_lock); mutex_lock(&syslog_lock);
if (!prb_read_valid(prb, syslog_seq, &r)) { if (!prb_read_valid(prb, syslog_seq, &r)) {
raw_spin_unlock_irq(&syslog_lock); mutex_unlock(&syslog_lock);
break; break;
} }
if (r.info->seq != syslog_seq) { if (r.info->seq != syslog_seq) {
...@@ -1528,7 +1528,7 @@ static int syslog_print(char __user *buf, int size) ...@@ -1528,7 +1528,7 @@ static int syslog_print(char __user *buf, int size)
syslog_partial += n; syslog_partial += n;
} else } else
n = 0; n = 0;
raw_spin_unlock_irq(&syslog_lock); mutex_unlock(&syslog_lock);
if (!n) if (!n)
break; break;
...@@ -1592,9 +1592,9 @@ static int syslog_print_all(char __user *buf, int size, bool clear) ...@@ -1592,9 +1592,9 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
} }
if (clear) { if (clear) {
raw_spin_lock_irq(&syslog_lock); mutex_lock(&syslog_lock);
latched_seq_write(&clear_seq, seq); latched_seq_write(&clear_seq, seq);
raw_spin_unlock_irq(&syslog_lock); mutex_unlock(&syslog_lock);
} }
kfree(text); kfree(text);
...@@ -1603,21 +1603,9 @@ static int syslog_print_all(char __user *buf, int size, bool clear) ...@@ -1603,21 +1603,9 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
static void syslog_clear(void) static void syslog_clear(void)
{ {
raw_spin_lock_irq(&syslog_lock); mutex_lock(&syslog_lock);
latched_seq_write(&clear_seq, prb_next_seq(prb)); latched_seq_write(&clear_seq, prb_next_seq(prb));
raw_spin_unlock_irq(&syslog_lock); mutex_unlock(&syslog_lock);
}
/* Return a consistent copy of @syslog_seq. */
static u64 read_syslog_seq_irq(void)
{
u64 seq;
raw_spin_lock_irq(&syslog_lock);
seq = syslog_seq;
raw_spin_unlock_irq(&syslog_lock);
return seq;
} }
int do_syslog(int type, char __user *buf, int len, int source) int do_syslog(int type, char __user *buf, int len, int source)
...@@ -1626,6 +1614,7 @@ int do_syslog(int type, char __user *buf, int len, int source) ...@@ -1626,6 +1614,7 @@ int do_syslog(int type, char __user *buf, int len, int source)
bool clear = false; bool clear = false;
static int saved_console_loglevel = LOGLEVEL_DEFAULT; static int saved_console_loglevel = LOGLEVEL_DEFAULT;
int error; int error;
u64 seq;
error = check_syslog_permissions(type, source); error = check_syslog_permissions(type, source);
if (error) if (error)
...@@ -1644,8 +1633,12 @@ int do_syslog(int type, char __user *buf, int len, int source) ...@@ -1644,8 +1633,12 @@ int do_syslog(int type, char __user *buf, int len, int source)
if (!access_ok(buf, len)) if (!access_ok(buf, len))
return -EFAULT; return -EFAULT;
error = wait_event_interruptible(log_wait, /* Get a consistent copy of @syslog_seq. */
prb_read_valid(prb, read_syslog_seq_irq(), NULL)); mutex_lock(&syslog_lock);
seq = syslog_seq;
mutex_unlock(&syslog_lock);
error = wait_event_interruptible(log_wait, prb_read_valid(prb, seq, NULL));
if (error) if (error)
return error; return error;
error = syslog_print(buf, len); error = syslog_print(buf, len);
...@@ -1693,10 +1686,10 @@ int do_syslog(int type, char __user *buf, int len, int source) ...@@ -1693,10 +1686,10 @@ int do_syslog(int type, char __user *buf, int len, int source)
break; break;
/* Number of chars in the log buffer */ /* Number of chars in the log buffer */
case SYSLOG_ACTION_SIZE_UNREAD: case SYSLOG_ACTION_SIZE_UNREAD:
raw_spin_lock_irq(&syslog_lock); mutex_lock(&syslog_lock);
if (!prb_read_valid_info(prb, syslog_seq, &info, NULL)) { if (!prb_read_valid_info(prb, syslog_seq, &info, NULL)) {
/* No unread messages. */ /* No unread messages. */
raw_spin_unlock_irq(&syslog_lock); mutex_unlock(&syslog_lock);
return 0; return 0;
} }
if (info.seq != syslog_seq) { if (info.seq != syslog_seq) {
...@@ -1714,7 +1707,6 @@ int do_syslog(int type, char __user *buf, int len, int source) ...@@ -1714,7 +1707,6 @@ int do_syslog(int type, char __user *buf, int len, int source)
} else { } else {
bool time = syslog_partial ? syslog_time : printk_time; bool time = syslog_partial ? syslog_time : printk_time;
unsigned int line_count; unsigned int line_count;
u64 seq;
prb_for_each_info(syslog_seq, prb, seq, &info, prb_for_each_info(syslog_seq, prb, seq, &info,
&line_count) { &line_count) {
...@@ -1724,7 +1716,7 @@ int do_syslog(int type, char __user *buf, int len, int source) ...@@ -1724,7 +1716,7 @@ int do_syslog(int type, char __user *buf, int len, int source)
} }
error -= syslog_partial; error -= syslog_partial;
} }
raw_spin_unlock_irq(&syslog_lock); mutex_unlock(&syslog_lock);
break; break;
/* Size of the log buffer */ /* Size of the log buffer */
case SYSLOG_ACTION_SIZE_BUFFER: case SYSLOG_ACTION_SIZE_BUFFER:
...@@ -2929,7 +2921,6 @@ static int try_enable_new_console(struct console *newcon, bool user_specified) ...@@ -2929,7 +2921,6 @@ static int try_enable_new_console(struct console *newcon, bool user_specified)
*/ */
void register_console(struct console *newcon) void register_console(struct console *newcon)
{ {
unsigned long flags;
struct console *bcon = NULL; struct console *bcon = NULL;
int err; int err;
...@@ -3034,9 +3025,9 @@ void register_console(struct console *newcon) ...@@ -3034,9 +3025,9 @@ void register_console(struct console *newcon)
exclusive_console_stop_seq = console_seq; exclusive_console_stop_seq = console_seq;
/* Get a consistent copy of @syslog_seq. */ /* Get a consistent copy of @syslog_seq. */
raw_spin_lock_irqsave(&syslog_lock, flags); mutex_lock(&syslog_lock);
console_seq = syslog_seq; console_seq = syslog_seq;
raw_spin_unlock_irqrestore(&syslog_lock, flags); mutex_unlock(&syslog_lock);
} }
console_unlock(); console_unlock();
console_sysfs_notify(); console_sysfs_notify();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment