Skip to content

Commit 391d627

Browse files
committed
Merge branch 'core-printk-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip
* 'core-printk-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip: lockdep: Fix trace_[soft,hard]irqs_[on,off]() recursion printk: Fix console_sem vs logbuf_lock unlock race printk: Release console_sem after logbuf_lock
2 parents 75b56ec + dd4e5d3 commit 391d627

2 files changed

Lines changed: 41 additions & 13 deletions

File tree

‎kernel/lockdep.c‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,15 +2481,10 @@ mark_held_locks(struct task_struct *curr, enum mark_type mark)
24812481
/*
24822482
* Hardirqs will be enabled:
24832483
*/
2484-
void trace_hardirqs_on_caller(unsigned long ip)
2484+
static void __trace_hardirqs_on_caller(unsigned long ip)
24852485
{
24862486
struct task_struct *curr = current;
24872487

2488-
time_hardirqs_on(CALLER_ADDR0, ip);
2489-
2490-
if (unlikely(!debug_locks || current->lockdep_recursion))
2491-
return;
2492-
24932488
if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
24942489
return;
24952490

@@ -2505,8 +2500,6 @@ void trace_hardirqs_on_caller(unsigned long ip)
25052500
/* we'll do an OFF -> ON transition: */
25062501
curr->hardirqs_enabled = 1;
25072502

2508-
if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2509-
return;
25102503
if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
25112504
return;
25122505
/*
@@ -2528,6 +2521,21 @@ void trace_hardirqs_on_caller(unsigned long ip)
25282521
curr->hardirq_enable_event = ++curr->irq_events;
25292522
debug_atomic_inc(hardirqs_on_events);
25302523
}
2524+
2525+
void trace_hardirqs_on_caller(unsigned long ip)
2526+
{
2527+
time_hardirqs_on(CALLER_ADDR0, ip);
2528+
2529+
if (unlikely(!debug_locks || current->lockdep_recursion))
2530+
return;
2531+
2532+
if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2533+
return;
2534+
2535+
current->lockdep_recursion = 1;
2536+
__trace_hardirqs_on_caller(ip);
2537+
current->lockdep_recursion = 0;
2538+
}
25312539
EXPORT_SYMBOL(trace_hardirqs_on_caller);
25322540

25332541
void trace_hardirqs_on(void)
@@ -2577,7 +2585,7 @@ void trace_softirqs_on(unsigned long ip)
25772585
{
25782586
struct task_struct *curr = current;
25792587

2580-
if (unlikely(!debug_locks))
2588+
if (unlikely(!debug_locks || current->lockdep_recursion))
25812589
return;
25822590

25832591
if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
@@ -2588,6 +2596,7 @@ void trace_softirqs_on(unsigned long ip)
25882596
return;
25892597
}
25902598

2599+
current->lockdep_recursion = 1;
25912600
/*
25922601
* We'll do an OFF -> ON transition:
25932602
*/
@@ -2602,6 +2611,7 @@ void trace_softirqs_on(unsigned long ip)
26022611
*/
26032612
if (curr->hardirqs_enabled)
26042613
mark_held_locks(curr, SOFTIRQ);
2614+
current->lockdep_recursion = 0;
26052615
}
26062616

26072617
/*
@@ -2611,7 +2621,7 @@ void trace_softirqs_off(unsigned long ip)
26112621
{
26122622
struct task_struct *curr = current;
26132623

2614-
if (unlikely(!debug_locks))
2624+
if (unlikely(!debug_locks || current->lockdep_recursion))
26152625
return;
26162626

26172627
if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))

‎kernel/printk.c‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ static inline int can_use_console(unsigned int cpu)
782782
static int console_trylock_for_printk(unsigned int cpu)
783783
__releases(&logbuf_lock)
784784
{
785-
int retval = 0;
785+
int retval = 0, wake = 0;
786786

787787
if (console_trylock()) {
788788
retval = 1;
@@ -795,12 +795,14 @@ static int console_trylock_for_printk(unsigned int cpu)
795795
*/
796796
if (!can_use_console(cpu)) {
797797
console_locked = 0;
798-
up(&console_sem);
798+
wake = 1;
799799
retval = 0;
800800
}
801801
}
802802
printk_cpu = UINT_MAX;
803803
spin_unlock(&logbuf_lock);
804+
if (wake)
805+
up(&console_sem);
804806
return retval;
805807
}
806808
static const char recursion_bug_msg [] =
@@ -1242,7 +1244,7 @@ void console_unlock(void)
12421244
{
12431245
unsigned long flags;
12441246
unsigned _con_start, _log_end;
1245-
unsigned wake_klogd = 0;
1247+
unsigned wake_klogd = 0, retry = 0;
12461248

12471249
if (console_suspended) {
12481250
up(&console_sem);
@@ -1251,6 +1253,7 @@ void console_unlock(void)
12511253

12521254
console_may_schedule = 0;
12531255

1256+
again:
12541257
for ( ; ; ) {
12551258
spin_lock_irqsave(&logbuf_lock, flags);
12561259
wake_klogd |= log_start - log_end;
@@ -1271,8 +1274,23 @@ void console_unlock(void)
12711274
if (unlikely(exclusive_console))
12721275
exclusive_console = NULL;
12731276

1277+
spin_unlock(&logbuf_lock);
1278+
12741279
up(&console_sem);
1280+
1281+
/*
1282+
* Someone could have filled up the buffer again, so re-check if there's
1283+
* something to flush. In case we cannot trylock the console_sem again,
1284+
* there's a new owner and the console_unlock() from them will do the
1285+
* flush, no worries.
1286+
*/
1287+
spin_lock(&logbuf_lock);
1288+
if (con_start != log_end)
1289+
retry = 1;
12751290
spin_unlock_irqrestore(&logbuf_lock, flags);
1291+
if (retry && console_trylock())
1292+
goto again;
1293+
12761294
if (wake_klogd)
12771295
wake_up_klogd();
12781296
}

0 commit comments

Comments
 (0)