Skip to content

Commit 996e966

Browse files
jognesspmladek
authored andcommitted
printk: remove logbuf_lock
Since the ringbuffer is lockless, there is no need for it to be protected by @logbuf_lock. Remove @logbuf_lock. @console_seq, @exclusive_console_stop_seq, @console_dropped are protected by @console_lock. Signed-off-by: John Ogness <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent f9f3f02 commit 996e966

File tree

3 files changed

+46
-97
lines changed

3 files changed

+46
-97
lines changed

kernel/printk/internal.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
#define PRINTK_NMI_CONTEXT_OFFSET 0x010000000
1414

15-
extern raw_spinlock_t logbuf_lock;
16-
1715
__printf(4, 0)
1816
int vprintk_store(int facility, int level,
1917
const struct dev_printk_info *dev_info,
@@ -59,7 +57,7 @@ void defer_console_output(void);
5957
__printf(1, 0) int vprintk_func(const char *fmt, va_list args) { return 0; }
6058

6159
/*
62-
* In !PRINTK builds we still export logbuf_lock spin_lock, console_sem
60+
* In !PRINTK builds we still export console_sem
6361
* semaphore and some of console functions (console_unlock()/etc.), so
6462
* printk-safe must preserve the existing local IRQ guarantees.
6563
*/

kernel/printk/printk.c

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -355,41 +355,6 @@ enum log_flags {
355355
LOG_CONT = 8, /* text is a fragment of a continuation line */
356356
};
357357

358-
/*
359-
* The logbuf_lock protects kmsg buffer, indices, counters. This can be taken
360-
* within the scheduler's rq lock. It must be released before calling
361-
* console_unlock() or anything else that might wake up a process.
362-
*/
363-
DEFINE_RAW_SPINLOCK(logbuf_lock);
364-
365-
/*
366-
* Helper macros to lock/unlock logbuf_lock and switch between
367-
* printk-safe/unsafe modes.
368-
*/
369-
#define logbuf_lock_irq() \
370-
do { \
371-
printk_safe_enter_irq(); \
372-
raw_spin_lock(&logbuf_lock); \
373-
} while (0)
374-
375-
#define logbuf_unlock_irq() \
376-
do { \
377-
raw_spin_unlock(&logbuf_lock); \
378-
printk_safe_exit_irq(); \
379-
} while (0)
380-
381-
#define logbuf_lock_irqsave(flags) \
382-
do { \
383-
printk_safe_enter_irqsave(flags); \
384-
raw_spin_lock(&logbuf_lock); \
385-
} while (0)
386-
387-
#define logbuf_unlock_irqrestore(flags) \
388-
do { \
389-
raw_spin_unlock(&logbuf_lock); \
390-
printk_safe_exit_irqrestore(flags); \
391-
} while (0)
392-
393358
/* syslog_lock protects syslog_* variables and write access to clear_seq. */
394359
static DEFINE_RAW_SPINLOCK(syslog_lock);
395360

@@ -401,6 +366,7 @@ static u64 syslog_seq;
401366
static size_t syslog_partial;
402367
static bool syslog_time;
403368

369+
/* All 3 protected by @console_sem. */
404370
/* the next printk record to write to the console */
405371
static u64 console_seq;
406372
static u64 exclusive_console_stop_seq;
@@ -766,27 +732,27 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
766732
if (ret)
767733
return ret;
768734

769-
logbuf_lock_irq();
735+
printk_safe_enter_irq();
770736
if (!prb_read_valid(prb, atomic64_read(&user->seq), r)) {
771737
if (file->f_flags & O_NONBLOCK) {
772738
ret = -EAGAIN;
773-
logbuf_unlock_irq();
739+
printk_safe_exit_irq();
774740
goto out;
775741
}
776742

777-
logbuf_unlock_irq();
743+
printk_safe_exit_irq();
778744
ret = wait_event_interruptible(log_wait,
779745
prb_read_valid(prb, atomic64_read(&user->seq), r));
780746
if (ret)
781747
goto out;
782-
logbuf_lock_irq();
748+
printk_safe_enter_irq();
783749
}
784750

785751
if (r->info->seq != atomic64_read(&user->seq)) {
786752
/* our last seen message is gone, return error and reset */
787753
atomic64_set(&user->seq, r->info->seq);
788754
ret = -EPIPE;
789-
logbuf_unlock_irq();
755+
printk_safe_exit_irq();
790756
goto out;
791757
}
792758

@@ -796,7 +762,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
796762
&r->info->dev_info);
797763

798764
atomic64_set(&user->seq, r->info->seq + 1);
799-
logbuf_unlock_irq();
765+
printk_safe_exit_irq();
800766

801767
if (len > count) {
802768
ret = -EINVAL;
@@ -831,7 +797,7 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
831797
if (offset)
832798
return -ESPIPE;
833799

834-
logbuf_lock_irq();
800+
printk_safe_enter_irq();
835801
switch (whence) {
836802
case SEEK_SET:
837803
/* the first record */
@@ -852,7 +818,7 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
852818
default:
853819
ret = -EINVAL;
854820
}
855-
logbuf_unlock_irq();
821+
printk_safe_exit_irq();
856822
return ret;
857823
}
858824

@@ -867,15 +833,15 @@ static __poll_t devkmsg_poll(struct file *file, poll_table *wait)
867833

868834
poll_wait(file, &log_wait, wait);
869835

870-
logbuf_lock_irq();
836+
printk_safe_enter_irq();
871837
if (prb_read_valid_info(prb, atomic64_read(&user->seq), &info, NULL)) {
872838
/* return error when data has vanished underneath us */
873839
if (info.seq != atomic64_read(&user->seq))
874840
ret = EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
875841
else
876842
ret = EPOLLIN|EPOLLRDNORM;
877843
}
878-
logbuf_unlock_irq();
844+
printk_safe_exit_irq();
879845

880846
return ret;
881847
}
@@ -908,9 +874,9 @@ static int devkmsg_open(struct inode *inode, struct file *file)
908874
prb_rec_init_rd(&user->record, &user->info,
909875
&user->text_buf[0], sizeof(user->text_buf));
910876

911-
logbuf_lock_irq();
877+
printk_safe_enter_irq();
912878
atomic64_set(&user->seq, prb_first_valid_seq(prb));
913-
logbuf_unlock_irq();
879+
printk_safe_exit_irq();
914880

915881
file->private_data = user;
916882
return 0;
@@ -1532,11 +1498,11 @@ static int syslog_print(char __user *buf, int size)
15321498
size_t n;
15331499
size_t skip;
15341500

1535-
logbuf_lock_irq();
1501+
printk_safe_enter_irq();
15361502
raw_spin_lock(&syslog_lock);
15371503
if (!prb_read_valid(prb, syslog_seq, &r)) {
15381504
raw_spin_unlock(&syslog_lock);
1539-
logbuf_unlock_irq();
1505+
printk_safe_exit_irq();
15401506
break;
15411507
}
15421508
if (r.info->seq != syslog_seq) {
@@ -1566,7 +1532,7 @@ static int syslog_print(char __user *buf, int size)
15661532
} else
15671533
n = 0;
15681534
raw_spin_unlock(&syslog_lock);
1569-
logbuf_unlock_irq();
1535+
printk_safe_exit_irq();
15701536

15711537
if (!n)
15721538
break;
@@ -1600,7 +1566,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
16001566
return -ENOMEM;
16011567

16021568
time = printk_time;
1603-
logbuf_lock_irq();
1569+
printk_safe_enter_irq();
16041570
/*
16051571
* Find first record that fits, including all following records,
16061572
* into the user-provided buffer for this dump.
@@ -1621,12 +1587,12 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
16211587
break;
16221588
}
16231589

1624-
logbuf_unlock_irq();
1590+
printk_safe_exit_irq();
16251591
if (copy_to_user(buf + len, text, textlen))
16261592
len = -EFAULT;
16271593
else
16281594
len += textlen;
1629-
logbuf_lock_irq();
1595+
printk_safe_enter_irq();
16301596

16311597
if (len < 0)
16321598
break;
@@ -1637,19 +1603,19 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
16371603
latched_seq_write(&clear_seq, seq);
16381604
raw_spin_unlock(&syslog_lock);
16391605
}
1640-
logbuf_unlock_irq();
1606+
printk_safe_exit_irq();
16411607

16421608
kfree(text);
16431609
return len;
16441610
}
16451611

16461612
static void syslog_clear(void)
16471613
{
1648-
logbuf_lock_irq();
1614+
printk_safe_enter_irq();
16491615
raw_spin_lock(&syslog_lock);
16501616
latched_seq_write(&clear_seq, prb_next_seq(prb));
16511617
raw_spin_unlock(&syslog_lock);
1652-
logbuf_unlock_irq();
1618+
printk_safe_exit_irq();
16531619
}
16541620

16551621
/* Return a consistent copy of @syslog_seq. */
@@ -1737,12 +1703,12 @@ int do_syslog(int type, char __user *buf, int len, int source)
17371703
break;
17381704
/* Number of chars in the log buffer */
17391705
case SYSLOG_ACTION_SIZE_UNREAD:
1740-
logbuf_lock_irq();
1706+
printk_safe_enter_irq();
17411707
raw_spin_lock(&syslog_lock);
17421708
if (!prb_read_valid_info(prb, syslog_seq, &info, NULL)) {
17431709
/* No unread messages. */
17441710
raw_spin_unlock(&syslog_lock);
1745-
logbuf_unlock_irq();
1711+
printk_safe_exit_irq();
17461712
return 0;
17471713
}
17481714
if (info.seq != syslog_seq) {
@@ -1771,7 +1737,7 @@ int do_syslog(int type, char __user *buf, int len, int source)
17711737
error -= syslog_partial;
17721738
}
17731739
raw_spin_unlock(&syslog_lock);
1774-
logbuf_unlock_irq();
1740+
printk_safe_exit_irq();
17751741
break;
17761742
/* Size of the log buffer */
17771743
case SYSLOG_ACTION_SIZE_BUFFER:
@@ -2627,7 +2593,6 @@ void console_unlock(void)
26272593
size_t len;
26282594

26292595
printk_safe_enter_irqsave(flags);
2630-
raw_spin_lock(&logbuf_lock);
26312596
skip:
26322597
if (!prb_read_valid(prb, console_seq, &r))
26332598
break;
@@ -2671,7 +2636,6 @@ void console_unlock(void)
26712636
console_msg_format & MSG_FORMAT_SYSLOG,
26722637
printk_time);
26732638
console_seq++;
2674-
raw_spin_unlock(&logbuf_lock);
26752639

26762640
/*
26772641
* While actively printing out messages, if another printk()
@@ -2698,8 +2662,6 @@ void console_unlock(void)
26982662

26992663
console_locked = 0;
27002664

2701-
raw_spin_unlock(&logbuf_lock);
2702-
27032665
up_console_sem();
27042666

27052667
/*
@@ -2708,9 +2670,7 @@ void console_unlock(void)
27082670
* there's a new owner and the console_unlock() from them will do the
27092671
* flush, no worries.
27102672
*/
2711-
raw_spin_lock(&logbuf_lock);
27122673
retry = prb_read_valid(prb, console_seq, NULL);
2713-
raw_spin_unlock(&logbuf_lock);
27142674
printk_safe_exit_irqrestore(flags);
27152675

27162676
if (retry && console_trylock())
@@ -2777,9 +2737,9 @@ void console_flush_on_panic(enum con_flush_mode mode)
27772737
if (mode == CONSOLE_REPLAY_ALL) {
27782738
unsigned long flags;
27792739

2780-
logbuf_lock_irqsave(flags);
2740+
printk_safe_enter_irqsave(flags);
27812741
console_seq = prb_first_valid_seq(prb);
2782-
logbuf_unlock_irqrestore(flags);
2742+
printk_safe_exit_irqrestore(flags);
27832743
}
27842744
console_unlock();
27852745
}
@@ -3008,7 +2968,7 @@ void register_console(struct console *newcon)
30082968
* console_unlock(); will print out the buffered messages
30092969
* for us.
30102970
*/
3011-
logbuf_lock_irqsave(flags);
2971+
printk_safe_enter_irqsave(flags);
30122972
/*
30132973
* We're about to replay the log buffer. Only do this to the
30142974
* just-registered console to avoid excessive message spam to
@@ -3026,7 +2986,7 @@ void register_console(struct console *newcon)
30262986
console_seq = syslog_seq;
30272987
raw_spin_unlock(&syslog_lock);
30282988

3029-
logbuf_unlock_irqrestore(flags);
2989+
printk_safe_exit_irqrestore(flags);
30302990
}
30312991
console_unlock();
30322992
console_sysfs_notify();
@@ -3492,9 +3452,9 @@ bool kmsg_dump_get_line(struct kmsg_dump_iter *iter, bool syslog,
34923452
unsigned long flags;
34933453
bool ret;
34943454

3495-
logbuf_lock_irqsave(flags);
3455+
printk_safe_enter_irqsave(flags);
34963456
ret = kmsg_dump_get_line_nolock(iter, syslog, line, size, len);
3497-
logbuf_unlock_irqrestore(flags);
3457+
printk_safe_exit_irqrestore(flags);
34983458

34993459
return ret;
35003460
}
@@ -3538,7 +3498,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dump_iter *iter, bool syslog,
35383498
if (iter->cur_seq < min_seq)
35393499
iter->cur_seq = min_seq;
35403500

3541-
logbuf_lock_irqsave(flags);
3501+
printk_safe_enter_irqsave(flags);
35423502
if (prb_read_valid_info(prb, iter->cur_seq, &info, NULL)) {
35433503
if (info.seq != iter->cur_seq) {
35443504
/* messages are gone, move to first available one */
@@ -3548,7 +3508,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dump_iter *iter, bool syslog,
35483508

35493509
/* last entry */
35503510
if (iter->cur_seq >= iter->next_seq) {
3551-
logbuf_unlock_irqrestore(flags);
3511+
printk_safe_exit_irqrestore(flags);
35523512
goto out;
35533513
}
35543514

@@ -3582,7 +3542,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dump_iter *iter, bool syslog,
35823542

35833543
iter->next_seq = next_seq;
35843544
ret = true;
3585-
logbuf_unlock_irqrestore(flags);
3545+
printk_safe_exit_irqrestore(flags);
35863546
out:
35873547
if (len_out)
35883548
*len_out = len;
@@ -3618,9 +3578,9 @@ void kmsg_dump_rewind(struct kmsg_dump_iter *iter)
36183578
{
36193579
unsigned long flags;
36203580

3621-
logbuf_lock_irqsave(flags);
3581+
printk_safe_enter_irqsave(flags);
36223582
kmsg_dump_rewind_nolock(iter);
3623-
logbuf_unlock_irqrestore(flags);
3583+
printk_safe_exit_irqrestore(flags);
36243584
}
36253585
EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
36263586

0 commit comments

Comments
 (0)