From: Jan Kara Subject: Re: jbd2_handle and i_data_sem circular locking dependency detected Date: Mon, 4 Feb 2008 17:31:56 +0100 Message-ID: <20080204163156.GC3426@duck.suse.cz> References: <20080204101228.GA1939@skywalker> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Josef Bacik , Theodore Tso , Andreas Dilger , Mingming Cao , "linux-ext4@vger.kernel.org" To: "Aneesh Kumar K.V" Return-path: Received: from styx.suse.cz ([82.119.242.94]:36232 "EHLO duck.suse.cz" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750847AbYBDQb6 (ORCPT ); Mon, 4 Feb 2008 11:31:58 -0500 Content-Disposition: inline In-Reply-To: <20080204101228.GA1939@skywalker> Sender: linux-ext4-owner@vger.kernel.org List-ID: Hi, On Mon 04-02-08 15:42:28, Aneesh Kumar K.V wrote: > This is with the new ext3 -> ext4 migrate code added. The recently added > lockdep for jbd2 helped to find this out. We want to hold the i_data_sem > on the ext3 inode during migration to prevent walking the ext3 inode > when it is being converted to ext4 format. Also we want to avoid > file truncation and new blocks being added while converting to ext4. > Also we dont want to reserve large number of credits for journal. > Any idea how to fix this ? Hmm, while briefly looking at the code - why do you introduce i_data_sem and not use i_alloc_sem which is already in VFS inode? That is aimed exactly at the serialization of truncates, writes and similar users. That doesn't solve problems with lock ordering but I was just wondering... Another problem - ext4_fallocate() has the same lock ordering problem as the migration code and maybe there are others... One (stupid) solution to your problem is to make i_data_sem be always locked before the transaction is started. It could possibly have negative performance impact because you'd have to hold the semaphore for a longer time and thus a writer would block readers for longer time. So one would have to measure how big difference that would make. Another possibility is to start a single transaction for migration and extend it as long as you can (as truncate does it). And when you can't extend any more, you drop the i_data_sem and start a new transaction and acquire the semaphore again. This has the disadvantage that after dropping the semaphore you have to resync your original inode with the temporary one your are building which probably ends up being ugly as night... Hmm, but maybe we could get rid of this - hold i_mutex to protect against all writes (that ranks outside of transaction start so you can hold it for the whole migration time - maybe you even hold it if you are called from the write path...). After dropping i_data_sem you let some readers proceed but writers still wait on i_mutex so the file shouldn't change under you (but I suggest adding some BUG_ONs to verify that the file really doesn't change :). Honza > ======================================================= > [ INFO: possible circular locking dependency detected ] > 2.6.24-rc8 #6 > ------------------------------------------------------- > rm/2401 is trying to acquire lock: > (&ei->i_data_sem){----}, at: [] ext4_get_blocks_wrap+0x21/0x108 > > but task is already holding lock: > (jbd2_handle){--..}, at: [] jbd2_journal_start+0xd2/0xff > > which lock already depends on the new lock. > > > the existing dependency chain (in reverse order) is: > > -> #1 (jbd2_handle){--..}: > [] __lock_acquire+0xa31/0xc1a > [] lock_acquire+0x7a/0x94 > [] jbd2_journal_start+0xf5/0xff > [] ext4_journal_start_sb+0x48/0x4a > [] ext4_ext_migrate+0x7d/0x535 > [] ext4_ioctl+0x528/0x56c > [] do_ioctl+0x50/0x67 > [] vfs_ioctl+0x237/0x24a > [] sys_ioctl+0x31/0x4b > [] sysenter_past_esp+0x5f/0xa5 > [] 0xffffffff > > -> #0 (&ei->i_data_sem){----}: > [] __lock_acquire+0x921/0xc1a > [] lock_acquire+0x7a/0x94 > [] down_read+0x42/0x79 > [] ext4_get_blocks_wrap+0x21/0x108 > [] ext4_getblk+0x62/0x1c4 > [] ext4_find_entry+0x350/0x5b7 > [] ext4_unlink+0x6e/0x1a4 > [] vfs_unlink+0x49/0x89 > [] do_unlinkat+0x96/0x12c > [] sys_unlink+0x10/0x12 > [] sysenter_past_esp+0x5f/0xa5 > [] 0xffffffff > > other info that might help us debug this: > > 3 locks held by rm/2401: > #0: (&type->i_mutex_dir_key#5/1){--..}, at: [] do_unlinkat+0x5e/0x12c > #1: (&sb->s_type->i_mutex_key#8){--..}, at: [] vfs_unlink+0x36/0x89 > #2: (jbd2_handle){--..}, at: [] jbd2_journal_start+0xd2/0xff > > stack backtrace: > Pid: 2401, comm: rm Not tainted 2.6.24-rc8 #6 > [] show_trace_log_lvl+0x1a/0x2f > [] show_trace+0x12/0x14 > [] dump_stack+0x6c/0x72 > [] print_circular_bug_tail+0x5f/0x68 > [] __lock_acquire+0x921/0xc1a > [] lock_acquire+0x7a/0x94 > [] down_read+0x42/0x79 > [] ext4_get_blocks_wrap+0x21/0x108 > [] ext4_getblk+0x62/0x1c4 > [] ext4_find_entry+0x350/0x5b7 > [] ext4_unlink+0x6e/0x1a4 > [] vfs_unlink+0x49/0x89 > [] do_unlinkat+0x96/0x12c > [] sys_unlink+0x10/0x12 > [] sysenter_past_esp+0x5f/0xa5 > -- Jan Kara SUSE Labs, CR