[PATCH] Add irq protection in the percpu-counters cpu-hotplug-callback path

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Gautham R Shenoy
Date: Sunday, October 14, 2007 - 11:18 pm

Hi Andrew,

While running regular cpu-offline tests on 2.6.23-mm1, I 
hit the following lockdep warning.

It was triggered because some of the per-cpu counters and thus 
their locks are accessed from IRQ context. 
This can cause a deadlock if it interrupts a cpu-offline thread which 
is transferring a dead-cpu's counts to the global counter.

Please find the patch for the same below. Tested on i386.

Thanks and Regards
gautham.

=====================Warning! ===========================================

[root@llm43]# ./all_hotplug_once

CPU 1 is now offline

=================================
[ INFO: inconsistent lock state ]
2.6.23-mm1 #3
---------------------------------
inconsistent {in-softirq-W} -> {softirq-on-W} usage.
sh/7103 [HC0[0]:SC0[0]:HE1:SE1] takes:
 (&percpu_counter_irqsafe){-+..}, at: [<c028e296>] percpu_counter_hotcpu_callback+0x22/0x67
{in-softirq-W} state was registered at:
  [<c014126f>] __lock_acquire+0x40d/0xb4a
  [<c0141966>] __lock_acquire+0xb04/0xb4a
  [<c0141a0b>] lock_acquire+0x5f/0x79
  [<c028e4b5>] __percpu_counter_add+0x62/0xad
  [<c04d5e81>] _spin_lock+0x21/0x2c
  [<c028e4b5>] __percpu_counter_add+0x62/0xad
  [<c028e4b5>] __percpu_counter_add+0x62/0xad
  [<c01531af>] test_clear_page_writeback+0x88/0xc5
  [<c014d35e>] end_page_writeback+0x20/0x3c
  [<c0188757>] end_buffer_async_write+0x133/0x181
  [<c0141966>] __lock_acquire+0xb04/0xb4a
  [<c0187eb4>] end_bio_bh_io_sync+0x21/0x29
  [<c0187e93>] end_bio_bh_io_sync+0x0/0x29
  [<c0189345>] bio_endio+0x27/0x29
  [<c04358f8>] dec_pending+0x17d/0x199
  [<c0435a13>] clone_endio+0x73/0x9f
  [<c04359a0>] clone_endio+0x0/0x9f
  [<c0189345>] bio_endio+0x27/0x29
  [<c027ba83>] __end_that_request_first+0x150/0x2c0
  [<c034a161>] scsi_end_request+0x1d/0xab
  [<c014f5ed>] mempool_free+0x63/0x67
  [<c034ac22>] scsi_io_completion+0x108/0x2c7
  [<c027e03b>] blk_done_softirq+0x51/0x5c
  [<c012b291>] __do_softirq+0x68/0xdb
  [<c012b33a>] do_softirq+0x36/0x51
  [<c012b4bf>] irq_exit+0x43/0x4e
  [<c0106f60>] do_IRQ+0x73/0x83
  [<c0105902>] common_interrupt+0x2e/0x34
  [<c01600d8>] add_to_swap+0x23/0x66
  [<c01031b4>] mwait_idle_with_hints+0x3b/0x3f
  [<c01033a8>] mwait_idle+0x0/0xf
  [<c01034d1>] cpu_idle+0x9a/0xc7
  [<ffffffff>] 0xffffffff
irq event stamp: 4007
hardirqs last  enabled at (4007): [<c04d4d9c>] __mutex_lock_slowpath+0x21d/0x241
hardirqs last disabled at (4006): [<c04d4bda>] __mutex_lock_slowpath+0x5b/0x241
softirqs last  enabled at (2130): [<c0135ab7>] __rcu_offline_cpu+0x2f/0x5a
softirqs last disabled at (2128): [<c04d5e94>] _spin_lock_bh+0x8/0x31

other info that might help us debug this:
6 locks held by sh/7103:
 #0:  (&buffer->mutex){--..}, at: [<c019f414>] sysfs_write_file+0x22/0xdb
 #1:  (cpu_add_remove_lock){--..}, at: [<c01450fd>] cpu_down+0x13/0x36
 #2:  (sched_hotcpu_mutex){--..}, at: [<c01220db>] migration_call+0x26/0x36a
 #3:  (cache_chain_mutex){--..}, at: [<c0168289>] cpuup_callback+0x28/0x1f9
 #4:  (workqueue_mutex){--..}, at: [<c013456d>] workqueue_cpu_callback+0x26/0xca
 #5:  (percpu_counters_lock){--..}, at: [<c028e287>] percpu_counter_hotcpu_callback+0x13/0x67

stack backtrace:
 [<c013febd>] print_usage_bug+0x101/0x10b
 [<c01406fd>] mark_lock+0x249/0x3f0
 [<c01412d6>] __lock_acquire+0x474/0xb4a
 [<c0141a0b>] lock_acquire+0x5f/0x79
 [<c028e296>] percpu_counter_hotcpu_callback+0x22/0x67
 [<c04d5e81>] _spin_lock+0x21/0x2c
 [<c028e296>] percpu_counter_hotcpu_callback+0x22/0x67
 [<c028e296>] percpu_counter_hotcpu_callback+0x22/0x67
 [<c04d7e3d>] notifier_call_chain+0x2a/0x47
 [<c013aece>] raw_notifier_call_chain+0x9/0xc
 [<c014503d>] _cpu_down+0x174/0x221
 [<c014510f>] cpu_down+0x25/0x36
 [<c02e7a66>] store_online+0x24/0x56
 [<c02e7a42>] store_online+0x0/0x56
 [<c02e5132>] sysdev_store+0x1e/0x22
 [<c019f499>] sysfs_write_file+0xa7/0xdb
 [<c019f3f2>] sysfs_write_file+0x0/0xdb
 [<c016b882>] vfs_write+0x83/0xf6
 [<c016bde3>] sys_write+0x3c/0x63
 [<c0104e8e>] sysenter_past_esp+0x5f/0x99
 =======================

---> 

From: Gautham R Shenoy <ego@in.ibm.com>

Some of the per-cpu counters and thus their locks
are accessed from IRQ contexts. This can cause a deadlock
if it interrupts a cpu-offline thread which is transferring 
a dead-cpu's counts to the global counter.

Add appropriate IRQ protection in the cpu-hotplug callback path.

Signed-off-by: Gautham R Shenoy <ego@in.ibm.com>

---
 lib/percpu_counter.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Index: linux-2.6.23/lib/percpu_counter.c
===================================================================
--- linux-2.6.23.orig/lib/percpu_counter.c
+++ linux-2.6.23/lib/percpu_counter.c
@@ -124,12 +124,13 @@ static int __cpuinit percpu_counter_hotc
 	mutex_lock(&percpu_counters_lock);
 	list_for_each_entry(fbc, &percpu_counters, list) {
 		s32 *pcount;
+		unsigned long flags;
 
-		spin_lock(&fbc->lock);
+		spin_lock_irqsave(&fbc->lock, flags);
 		pcount = per_cpu_ptr(fbc->counters, cpu);
 		fbc->count += *pcount;
 		*pcount = 0;
-		spin_unlock(&fbc->lock);
+		spin_unlock_irqrestore(&fbc->lock, flags);
 	}
 	mutex_unlock(&percpu_counters_lock);
 	return NOTIFY_OK;
-- 
Gautham R Shenoy
Linux Technology Center
IBM India.
"Freedom comes with a price tag of responsibility, which is still a bargain,
because Freedom is priceless!"
-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
2.6.23-mm1, Andrew Morton, (Thu Oct 11, 9:31 pm)
Re: 2.6.23-mm1, KAMEZAWA Hiroyuki, (Thu Oct 11, 10:03 pm)
Re: 2.6.23-mm1, Andrew Morton, (Thu Oct 11, 11:42 pm)
Re: 2.6.23-mm1, Al Viro, (Thu Oct 11, 11:46 pm)
Re: 2.6.23-mm1, Cedric Le Goater, (Thu Oct 11, 11:48 pm)
[PATCH] add missing parenthesis in cfe_writeblk() macro, Mariusz Kozlowski, (Thu Oct 11, 11:51 pm)
Re: 2.6.23-mm1, Andrew Morton, (Fri Oct 12, 12:13 am)
Re: 2.6.23-mm1, KAMEZAWA Hiroyuki, (Fri Oct 12, 12:25 am)
Re: 2.6.23-mm1 - build failure on axonram, Kamalesh Babulal, (Fri Oct 12, 12:44 am)
Re: 2.6.23-mm1, Torsten Kaiser, (Fri Oct 12, 1:31 am)
Re: 2.6.23-mm1, Sam Ravnborg, (Fri Oct 12, 1:36 am)
Re: 2.6.23-mm1, Andrew Morton, (Fri Oct 12, 1:37 am)
Build Failure (Was Re: 2.6.23-mm1), Dhaval Giani, (Fri Oct 12, 2:42 am)
Re: 2.6.23-mm1, Torsten Kaiser, (Fri Oct 12, 5:46 am)
[PATCH net-2.6] uml: hard_header fix, Stephen Hemminger, (Fri Oct 12, 11:06 am)
Re: 2.6.23-mm1, Al Viro, (Fri Oct 12, 12:04 pm)
Re: 2.6.23-mm1 thread exit_group issue, Mathieu Desnoyers, (Fri Oct 12, 12:47 pm)
Re: 2.6.23-mm1 thread exit_group issue, Andrew Morton, (Fri Oct 12, 1:01 pm)
Re: 2.6.23-mm1, Laurent Riffard, (Fri Oct 12, 1:38 pm)
Re: 2.6.23-mm1, Andrew Morton, (Fri Oct 12, 2:00 pm)
Re: 2.6.23-mm1, Rafael J. Wysocki, (Fri Oct 12, 2:32 pm)
Re: 2.6.23-mm1 thread exit_group issue, Andrew Morton, (Fri Oct 12, 6:03 pm)
Re: 2.6.23-mm1 - Build failure on rgmii, Kamalesh Babulal, (Fri Oct 12, 9:35 pm)
Re: 2.6.23-mm1 - build failure with advansys, Kamalesh Babulal, (Fri Oct 12, 9:44 pm)
Re: 2.6.23-mm1 - build failure with advansys, Andrew Morton, (Fri Oct 12, 11:52 pm)
Re: 2.6.23-mm1, Torsten Kaiser, (Sat Oct 13, 1:01 am)
Re: 2.6.23-mm1, Jeff Garzik, (Sat Oct 13, 3:55 am)
Re: 2.6.23-mm1 thread exit_group issue, Oleg Nesterov, (Sat Oct 13, 4:48 am)
Re: 2.6.23-mm1 thread exit_group issue, Oleg Nesterov, (Sat Oct 13, 5:02 am)
Re: 2.6.23-mm1, Torsten Kaiser, (Sat Oct 13, 5:03 am)
Re: 2.6.23-mm1, Jeff Garzik, (Sat Oct 13, 5:19 am)
Re: 2.6.23-mm1, Torsten Kaiser, (Sat Oct 13, 7:32 am)
Re: 2.6.23-mm1, Torsten Kaiser, (Sat Oct 13, 7:40 am)
Re: 2.6.23-mm1, Torsten Kaiser, (Sat Oct 13, 8:13 am)
Re: 2.6.23-mm1, Gabriel C, (Sat Oct 13, 10:12 am)
Re: 2.6.23-mm1 pm_prepare() and _finish() w/ args vs. without, Rafael J. Wysocki, (Sat Oct 13, 10:22 am)
Re: 2.6.23-mm1, Jeff Garzik, (Sat Oct 13, 10:48 am)
Re: 2.6.23-mm1 thread exit_group issue, Andrew Morton, (Sat Oct 13, 10:49 am)
Suspend Broken (Re: 2.6.23-mm1), Dhaval Giani, (Sat Oct 13, 10:58 am)
Re: 2.6.23-mm1, Andrew Morton, (Sat Oct 13, 11:01 am)
Re: 2.6.23-mm1, Torsten Kaiser, (Sat Oct 13, 11:05 am)
Re: 2.6.23-mm1, Gabriel C, (Sat Oct 13, 11:08 am)
Re: 2.6.23-mm1, Andrew Morton, (Sat Oct 13, 11:18 am)
Re: Suspend Broken (Re: 2.6.23-mm1), Rafael J. Wysocki, (Sat Oct 13, 11:33 am)
Re: 2.6.23-mm1, Torsten Kaiser, (Sat Oct 13, 11:35 am)
Re: 2.6.23-mm1, Jeff Garzik, (Sat Oct 13, 11:41 am)
Re: 2.6.23-mm1 pm_prepare() and _finish() w/ args vs. without, Rafael J. Wysocki, (Sat Oct 13, 12:13 pm)
[2.6.23-mm1] CONFIG_LOCALVERSION handling broken, Tilman Schmidt, (Sat Oct 13, 3:11 pm)
Re: 2.6.23-mm1 thread exit_group issue, Mathieu Desnoyers, (Sat Oct 13, 9:04 pm)
Re: Suspend Broken (Re: 2.6.23-mm1), Dhaval Giani, (Sat Oct 13, 9:26 pm)
Re: 2.6.23-mm1, Torsten Kaiser, (Sun Oct 14, 4:54 am)
Re: Suspend Broken (Re: 2.6.23-mm1), Rafael J. Wysocki, (Sun Oct 14, 7:19 am)
Re: 2.6.23-mm1, Andrew Morton, (Sun Oct 14, 11:39 am)
Re: 2.6.23-mm1, Torsten Kaiser, (Sun Oct 14, 12:12 pm)
Re: 2.6.23-mm1, Andrew Morton, (Sun Oct 14, 12:26 pm)
Re: 2.6.23-mm1, Torsten Kaiser, (Sun Oct 14, 12:40 pm)
Re: 2.6.23-mm1 pm_prepare() and _finish() w/ args vs. without, Rafael J. Wysocki, (Sun Oct 14, 1:20 pm)
Re: 2.6.23-mm1, Milan Broz, (Sun Oct 14, 3:03 pm)
Re: 2.6.23-mm1: BUG in reiserfs_delete_xattrs, Laurent Riffard, (Sun Oct 14, 3:34 pm)
[PATCH] Add irq protection in the percpu-counters cpu-hotp ..., Gautham R Shenoy, (Sun Oct 14, 11:18 pm)
Re: 2.6.23-mm1, Jens Axboe, (Sun Oct 14, 11:50 pm)
Re: 2.6.23-mm1: BUG in reiserfs_delete_xattrs, Christoph Hellwig, (Mon Oct 15, 1:40 am)
nfs mmap adventure (was: 2.6.23-mm1), Peter Zijlstra, (Mon Oct 15, 5:28 am)
Re: nfs mmap adventure (was: 2.6.23-mm1), David Howells, (Mon Oct 15, 7:06 am)
Re: nfs mmap adventure (was: 2.6.23-mm1), Trond Myklebust, (Mon Oct 15, 8:43 am)
Re: nfs mmap adventure (was: 2.6.23-mm1), Trond Myklebust, (Mon Oct 15, 8:51 am)
Re: 2.6.23-mm1, Mark Gross, (Mon Oct 15, 9:09 am)
Re: 2.6.23-mm1, Zan Lynx, (Mon Oct 15, 9:13 am)
Re: 2.6.23-mm1, Dave Hansen, (Mon Oct 15, 9:28 am)
Re: nfs mmap adventure (was: 2.6.23-mm1), Peter Zijlstra, (Mon Oct 15, 9:38 am)
Re: 2.6.23-mm1: BUG in reiserfs_delete_xattrs, Jeff Mahoney, (Mon Oct 15, 11:31 am)
Re: 2.6.23-mm1: BUG in reiserfs_delete_xattrs, Laurent Riffard, (Mon Oct 15, 12:51 pm)
Re: 2.6.23-mm1: BUG in reiserfs_delete_xattrs, Laurent Riffard, (Mon Oct 15, 1:06 pm)
Re: 2.6.23-mm1: BUG in reiserfs_delete_xattrs, Jeff Mahoney, (Mon Oct 15, 1:23 pm)
Re: 2.6.23-mm1, Rafael J. Wysocki, (Mon Oct 15, 1:40 pm)
Re: 2.6.23-mm1 pm_prepare() and _finish() w/ args vs. without, Rafael J. Wysocki, (Mon Oct 15, 1:55 pm)
Re: nfs mmap adventure (was: 2.6.23-mm1), David Howells, (Mon Oct 15, 4:27 pm)
Re: nfs mmap adventure (was: 2.6.23-mm1), Nick Piggin, (Mon Oct 15, 6:46 pm)
Re: 2.6.23-mm1 - regression- PowerPC link failure at arch/ ..., Kamalesh Babulal, (Tue Oct 16, 12:18 am)
Re: 2.6.23-mm1 - regression- PowerPC link failure at arch/ ..., Kamalesh Babulal, (Tue Oct 16, 12:44 am)
Re: 2.6.23-mm1, Mark Gross, (Tue Oct 16, 12:58 pm)
Re: 2.6.23-mm1, Rafael J. Wysocki, (Tue Oct 16, 1:28 pm)
Re: 2.6.23-mm1, Mark Gross, (Tue Oct 16, 4:31 pm)
Re: 2.6.23-mm1, KAMEZAWA Hiroyuki, (Wed Oct 17, 12:01 am)
Re: 2.6.23-mm1: BUG in reiserfs_delete_xattrs, Christoph Hellwig, (Wed Oct 17, 1:58 am)
Re: 2.6.23-mm1: BUG in reiserfs_delete_xattrs, Christoph Hellwig, (Wed Oct 17, 1:59 am)
Re: 2.6.23-mm1, Andrew Morton, (Wed Oct 17, 2:02 am)
Re: 2.6.23-mm1, Jiri Kosina, (Wed Oct 17, 2:10 am)
Re: 2.6.23-mm1, KAMEZAWA Hiroyuki, (Wed Oct 17, 2:36 am)
Re: 2.6.23-mm1, Jiri Kosina, (Wed Oct 17, 4:42 am)
Re: 2.6.23-mm1, KAMEZAWA Hiroyuki, (Wed Oct 17, 5:33 am)
Re: 2.6.23-mm1: BUG in reiserfs_delete_xattrs, Jeff Mahoney, (Wed Oct 17, 7:55 am)
Re: 2.6.23-mm1 - list_add corruption in cgroup, Cedric Le Goater, (Wed Oct 17, 8:54 am)
Re: [2.6.23-mm1] CONFIG_LOCALVERSION handling broken, Sam Ravnborg, (Wed Oct 17, 1:27 pm)
Re: [2.6.23-mm1] CONFIG_LOCALVERSION handling broken, Tilman Schmidt, (Wed Oct 17, 4:06 pm)
Re: 2.6.23-mm1 - build failure with advansys, Paul Mackerras, (Wed Oct 17, 5:07 pm)
Re: 2.6.23-mm1 - build failure with advansys, Matthew Wilcox, (Wed Oct 17, 6:48 pm)
Re: 2.6.23-mm1 - list_add corruption in cgroup, Paul Menage, (Thu Oct 18, 8:56 am)
PIE randomization (was Re: 2.6.23-mm1), Jiri Kosina, (Fri Oct 19, 2:07 am)
Re: 2.6.23-mm1, Jiri Kosina, (Fri Oct 19, 2:54 pm)
Re: 2.6.23-mm1 - list_add corruption in cgroup, Paul Menage, (Fri Oct 19, 3:11 pm)
oops in lbmIODone, fails to boot [Re: 2.6.23-mm1], Mattia Dongili, (Fri Oct 19, 9:57 pm)
Re: 2.6.23-mm1 - autofs broken, Rik van Riel, (Fri Oct 19, 10:13 pm)
Re: oops in lbmIODone, fails to boot [Re: 2.6.23-mm1], Andrew Morton, (Fri Oct 19, 10:34 pm)
Re: 2.6.23-mm1 - autofs broken, Andrew Morton, (Fri Oct 19, 10:39 pm)
Re: 2.6.23-mm1 - autofs broken, Rik van Riel, (Fri Oct 19, 10:54 pm)
Re: 2.6.23-mm1 - autofs broken, Rik van Riel, (Fri Oct 19, 10:54 pm)
Re: oops in lbmIODone, fails to boot [Re: 2.6.23-mm1], Dave Kleikamp, (Sat Oct 20, 5:18 am)
Re: 2.6.23-mm1 - autofs broken, Rik van Riel, (Sat Oct 20, 7:56 am)
Re: oops in lbmIODone, fails to boot [Re: 2.6.23-mm1], Mattia Dongili, (Sat Oct 20, 10:44 pm)
mysqld prevents s2ram [Re: 2.6.23-mm1], Mattia Dongili, (Sat Oct 20, 10:58 pm)
Re: mysqld prevents s2ram [Re: 2.6.23-mm1], Mattia Dongili, (Sat Oct 20, 11:28 pm)
Re: 2.6.23-mm1 - regression- PowerPC link failure at arch/ ..., Kamalesh Babulal, (Sat Oct 20, 11:42 pm)
Re: mysqld prevents s2ram [Re: 2.6.23-mm1], Pavel Machek, (Sun Oct 21, 2:58 am)
Re: mysqld prevents s2ram [Re: 2.6.23-mm1], Rafael J. Wysocki, (Sun Oct 21, 4:53 am)
Re: 2.6.23-mm1 - autofs broken, Ian Kent, (Sun Oct 21, 8:45 pm)
Re: 2.6.23-mm1 - autofs broken, Rik van Riel, (Mon Oct 22, 9:46 am)
kernel panic when running tcpdump, Mariusz Kozlowski, (Mon Oct 22, 11:40 am)
Re: kernel panic when running tcpdump, Andrew Morton, (Mon Oct 22, 12:03 pm)
Re: kernel panic when running tcpdump, Mariusz Kozlowski, (Mon Oct 22, 2:16 pm)
Re: 2.6.23-mm1 - autofs broken, Dave Hansen, (Mon Oct 22, 3:03 pm)
Re: 2.6.23-mm1 - regression- PowerPC link failure at arch/ ..., Stephen Rothwell, (Fri Oct 26, 10:05 pm)
Re: [2.6.23-mm1] CONFIG_LOCALVERSION handling broken, Tilman Schmidt, (Sat Oct 27, 8:19 am)
Re: [2.6.23-mm1] CONFIG_LOCALVERSION handling broken, Sam Ravnborg, (Sat Oct 27, 8:28 am)