f40a945
                                                                                                                                                                                                                                                               
f40a945
Delivered-To: jwboyer@gmail.com
f40a945
Received: by 10.76.168.104 with SMTP id zv8csp55663oab;
f40a945
        Fri, 30 Aug 2013 15:52:46 -0700 (PDT)
f40a945
X-Received: by 10.68.244.168 with SMTP id xh8mr12419215pbc.3.1377903166373;
f40a945
        Fri, 30 Aug 2013 15:52:46 -0700 (PDT)
f40a945
Return-Path: <linux-kernel-owner@vger.kernel.org>
f40a945
Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67])
f40a945
        by mx.google.com with ESMTP id qc9si280431pac.269.1969.12.31.16.00.00;
f40a945
        Fri, 30 Aug 2013 15:52:46 -0700 (PDT)
f40a945
Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67;
f40a945
Authentication-Results: mx.google.com;
f40a945
       spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mail=linux-kernel-owner@vger.kernel.org;
f40a945
       dkim=neutral (bad format) header.i=@hds.com
f40a945
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
f40a945
	id S1753535Ab3H3WrV (ORCPT <rfc822;georgezhim@gmail.com>
f40a945
	+ 99 others); Fri, 30 Aug 2013 18:47:21 -0400
f40a945
Received: from usindpps04.hds.com ([207.126.252.17]:35636 "EHLO
f40a945
	usindpps04.hds.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
f40a945
	with ESMTP id S1752650Ab3H3WrU (ORCPT
f40a945
	<rfc822;linux-kernel@vger.kernel.org>);
f40a945
	Fri, 30 Aug 2013 18:47:20 -0400
f40a945
DKIM-Signature:	v=1; a=rsa-sha256; c=relaxed/simple; d=hds.com; h=subject : to : from : cc
f40a945
 : date : message-id : mime-version : content-type :
f40a945
 content-transfer-encoding; s=mail1;
f40a945
 bh=VofHN8IMnygn2hbqnFjLmX0PPEPbvpzE377u1RxpGOY=;
f40a945
 b=piW6J78W57qDXBPJJuodWw/tvf0T//JbxKX6sLPvpuaOG2nBLMHzDqUeTYwFEQqUvdmf
f40a945
 ZTkiwsKi0WEku3MKcxJ7veR7wvTZcQ4fGMETFTf1c2J/1JOKpXLnft4ERuW89/FAxw25
f40a945
 wQM1ulsuQ3Cncl0I/sIaqMlaMOtvuQ/C8rsHorp+75eFiL6yx1jU5wMbuti4D/NprIET
f40a945
 3r57cPZ0YCh6sLjvOgjay6mKyktMToyjHPx6X1TWCSWcwes33Popc1hpadxUdFI/0npL
f40a945
 mN3Tttbe7e2RcmkXAZbwg8xj+FwSu3nIRC4G9UpFCsMz518C/AWZj4puwWE6VHZWVvVZ Rg== 
f40a945
Received: from usindmail01.hds.com (usindmail03 [207.126.252.22])
f40a945
	by usindpps04.hds.com (8.14.5/8.14.5) with ESMTP id r7UMlBjr025492;
f40a945
	Fri, 30 Aug 2013 18:47:11 -0400
f40a945
Received: from hds.com (usindnetf5d-vlan47float.corp.hds.com [10.74.73.11])
f40a945
	by usindmail01.hds.com (8.14.1/8.14.1) with ESMTP id r7UMl8SG058466;
f40a945
	Fri, 30 Aug 2013 18:47:10 -0400 (EDT)
f40a945
Subject: [PATCH v2 1/2] elevator: Fix a race in elevator switching and md
f40a945
 device initialization
f40a945
To:	linux-kernel@vger.kernel.org
f40a945
From:	Tomoki Sekiyama <tomoki.sekiyama@hds.com>
f40a945
Cc:	axboe@kernel.dk, tj@kernel.org, seiji.aguchi@hds.com,
f40a945
	vgoyal@redhat.com, majianpeng@gmail.com
f40a945
Date:	Fri, 30 Aug 2013 18:47:07 -0400
f40a945
Message-ID: <20130830224707.21812.63516.stgit@hds.com>
f40a945
User-Agent: StGit/0.16
f40a945
MIME-Version: 1.0
f40a945
Content-Type: text/plain; charset="utf-8"
f40a945
Content-Transfer-Encoding: 7bit
f40a945
X-Proofpoint-SPF-Result: pass
f40a945
X-Proofpoint-SPF-Record: v=spf1 mx ip4:207.126.244.0/26 ip4:207.126.252.0/25 include:mktomail.com
f40a945
 include:cloud.hds.com ~all
f40a945
X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.10.8794,1.0.431,0.0.0000
f40a945
 definitions=2013-08-30_09:2013-08-30,2013-08-30,1970-01-01 signatures=0
f40a945
X-Proofpoint-Spam-Details: rule=notspam policy=outbound_policy score=0 spamscore=0 suspectscore=1
f40a945
 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx
f40a945
 scancount=1 engine=7.0.1-1305240000 definitions=main-1308300162
f40a945
Sender:	linux-kernel-owner@vger.kernel.org
f40a945
Precedence: bulk
f40a945
List-ID: <linux-kernel.vger.kernel.org>
f40a945
X-Mailing-List:	linux-kernel@vger.kernel.org
f40a945
f40a945
The soft lockup below happens at the boot time of the system using dm
f40a945
multipath and the udev rules to switch scheduler.
f40a945
f40a945
[  356.127001] BUG: soft lockup - CPU#3 stuck for 22s! [sh:483]
f40a945
[  356.127001] RIP: 0010:[<ffffffff81072a7d>]  [<ffffffff81072a7d>] lock_timer_base.isra.35+0x1d/0x50
f40a945
...
f40a945
[  356.127001] Call Trace:
f40a945
[  356.127001]  [<ffffffff81073810>] try_to_del_timer_sync+0x20/0x70
f40a945
[  356.127001]  [<ffffffff8118b08a>] ? kmem_cache_alloc_node_trace+0x20a/0x230
f40a945
[  356.127001]  [<ffffffff810738b2>] del_timer_sync+0x52/0x60
f40a945
[  356.127001]  [<ffffffff812ece22>] cfq_exit_queue+0x32/0xf0
f40a945
[  356.127001]  [<ffffffff812c98df>] elevator_exit+0x2f/0x50
f40a945
[  356.127001]  [<ffffffff812c9f21>] elevator_change+0xf1/0x1c0
f40a945
[  356.127001]  [<ffffffff812caa50>] elv_iosched_store+0x20/0x50
f40a945
[  356.127001]  [<ffffffff812d1d09>] queue_attr_store+0x59/0xb0
f40a945
[  356.127001]  [<ffffffff812143f6>] sysfs_write_file+0xc6/0x140
f40a945
[  356.127001]  [<ffffffff811a326d>] vfs_write+0xbd/0x1e0
f40a945
[  356.127001]  [<ffffffff811a3ca9>] SyS_write+0x49/0xa0
f40a945
[  356.127001]  [<ffffffff8164e899>] system_call_fastpath+0x16/0x1b
f40a945
f40a945
This is caused by a race between md device initialization by multipathd and
f40a945
shell script to switch the scheduler using sysfs.
f40a945
f40a945
 - multipathd:
f40a945
   SyS_ioctl -> do_vfs_ioctl -> dm_ctl_ioctl -> ctl_ioctl -> table_load
f40a945
   -> dm_setup_md_queue -> blk_init_allocated_queue -> elevator_init
f40a945
    q->elevator = elevator_alloc(q, e); // not yet initialized
f40a945
f40a945
 - sh -c 'echo deadline > /sys/$DEVPATH/queue/scheduler':
f40a945
   elevator_switch (in the call trace above)
f40a945
    struct elevator_queue *old = q->elevator;
f40a945
    q->elevator = elevator_alloc(q, new_e);
f40a945
    elevator_exit(old);                 // lockup! (*)
f40a945
f40a945
 - multipathd: (cont.)
f40a945
    err = e->ops.elevator_init_fn(q);   // init fails; q->elevator is modified
f40a945
f40a945
(*) When del_timer_sync() is called, lock_timer_base() will loop infinitely
f40a945
while timer->base == NULL. In this case, as timer will never initialized,
f40a945
it results in lockup.
f40a945
f40a945
This patch introduces acquisition of q->sysfs_lock around elevator_init()
f40a945
into blk_init_allocated_queue(), to provide mutual exclusion between
f40a945
initialization of the q->scheduler and switching of the scheduler.
f40a945
f40a945
This should fix this bugzilla:
f40a945
https://bugzilla.redhat.com/show_bug.cgi?id=902012
f40a945
f40a945
Signed-off-by: Tomoki Sekiyama <tomoki.sekiyama@hds.com>
f40a945
---
f40a945
 block/blk-core.c |   10 +++++++++-
f40a945
 block/elevator.c |    6 ++++++
f40a945
 2 files changed, 15 insertions(+), 1 deletion(-)
f40a945
f40a945
diff --git a/block/blk-core.c b/block/blk-core.c
f40a945
index 93a18d1..2f6275f 100644
f40a945
--- a/block/blk-core.c
f40a945
+++ b/block/blk-core.c
f40a945
@@ -739,9 +739,17 @@ blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
f40a945
 
f40a945
 	q->sg_reserved_size = INT_MAX;
f40a945
 
f40a945
+	/* Protect q->elevator from elevator_change */
f40a945
+	mutex_lock(&q->sysfs_lock);
f40a945
+
f40a945
 	/* init elevator */
f40a945
-	if (elevator_init(q, NULL))
f40a945
+	if (elevator_init(q, NULL)) {
f40a945
+		mutex_unlock(&q->sysfs_lock);
f40a945
 		return NULL;
f40a945
+	}
f40a945
+
f40a945
+	mutex_unlock(&q->sysfs_lock);
f40a945
+
f40a945
 	return q;
f40a945
 }
f40a945
 EXPORT_SYMBOL(blk_init_allocated_queue);
f40a945
diff --git a/block/elevator.c b/block/elevator.c
f40a945
index 668394d..02d4390 100644
f40a945
--- a/block/elevator.c
f40a945
+++ b/block/elevator.c
f40a945
@@ -186,6 +186,12 @@ int elevator_init(struct request_queue *q, char *name)
f40a945
 	struct elevator_type *e = NULL;
f40a945
 	int err;
f40a945
 
f40a945
+	/*
f40a945
+	 * q->sysfs_lock must be held to provide mutual exclusion between
f40a945
+	 * elevator_switch() and here.
f40a945
+	 */
f40a945
+	lockdep_assert_held(&q->sysfs_lock);
f40a945
+
f40a945
 	if (unlikely(q->elevator))
f40a945
 		return 0;
f40a945
 
f40a945
f40a945
--
f40a945
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
f40a945
the body of a message to majordomo@vger.kernel.org
f40a945
More majordomo info at  http://vger.kernel.org/majordomo-info.html
f40a945
Please read the FAQ at  http://www.tux.org/lkml/