Return-Path: Received: from out30-133.freemail.mail.aliyun.com ([115.124.30.133]:59373 "EHLO out30-133.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725994AbeK2HOa (ORCPT ); Thu, 29 Nov 2018 02:14:30 -0500 Date: Wed, 28 Nov 2018 12:11:23 -0800 From: Liu Bo To: Jan Kara Cc: linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: Re: [PATCH RFC] Ext4: fix deadlock on dirty pages between fault and writeback Message-ID: <20181128201122.r4sec265cnlxgj2x@US-160370MP2.local> Reply-To: bo.liu@linux.alibaba.com References: <1540858969-75803-1-git-send-email-bo.liu@linux.alibaba.com> <20181127114249.GH16301@quack2.suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181127114249.GH16301@quack2.suse.cz> Sender: linux-ext4-owner@vger.kernel.org List-ID: On Tue, Nov 27, 2018 at 12:42:49PM +0100, Jan Kara wrote: > CCed fsdevel since this may be interesting to other filesystem developers > as well. > > On Tue 30-10-18 08:22:49, Liu Bo wrote: > > mpage_prepare_extent_to_map() tries to build up a large bio to stuff down > > the pipe. But if it needs to wait for a page lock, it needs to make sure > > and send down any pending writes so we don't deadlock with anyone who has > > the page lock and is waiting for writeback of things inside the bio. > > Thanks for report! I agree the current code has a deadlock possibility you > describe. But I think the problem reaches a bit further than what your > patch fixes. The problem is with pages that are unlocked but have > PageWriteback set. Page reclaim may end up waiting for these pages and > thus any memory allocation with __GFP_FS set can block on these. So in our > current setting page writeback must not block on anything that can be held > while doing memory allocation with __GFP_FS set. Page lock is just one of > these possibilities, wait_on_page_writeback() in > mpage_prepare_extent_to_map() is another suspect and there mat be more. Or > to say it differently, if there's lock A and GFP_KERNEL allocation can > happen under lock A, then A cannot be taken by the writeback path. This is > actually pretty subtle deadlock possibility and our current lockdep > instrumentation isn't going to catch this. > Thanks for the nice summary, it's true that a lock A held in both writeback path and memory reclaim can end up with deadlock. Fortunately, by far there're only deadlock reports of page's lock bit and writeback bit in both ext4 and btrfs[1]. I think wait_on_page_writeback() would be OK as it's been protected by page lock. [1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=01d658f2ca3c85c1ffb20b306e30d16197000ce7 > So I see two ways how to fix this properly: > > 1) Change ext4 code to always submit the bio once we have a full page > prepared for writing. This may be relatively simple but has a higher CPU > overhead for bio allocation & freeing (actual IO won't really differ since > the plugging code should take care of merging the submitted bios). XFS > seems to be doing this. Seems that that's the safest way to do it, but as you said there's some tradeoff. (Just took a look at xfs's writepages, xfs also did the page collection if there're adjacent pages in xfs_add_to_ioend(), and since xfs_vm_writepages() is using the generic helper write_cache_pages() which calls lock_page() as well, it's still possible to run into the above kind of deadlock.) > > 2) Change the code to unlock the page only when we submit the bio. This sounds doable but not good IMO, the concern is that page locks can be held for too long, or if we do 2), submitting one bio per page in 1) is also needed. thanks, -liubo > > Honza > > The related lock stack is shown as follows, > > > > task1: > > [] wait_on_page_bit+0x82/0xa0 > > [] shrink_page_list+0x907/0x960 > > [] shrink_inactive_list+0x2c7/0x680 > > [] shrink_node_memcg+0x404/0x830 > > [] shrink_node+0xd8/0x300 > > [] do_try_to_free_pages+0x10d/0x330 > > [] try_to_free_mem_cgroup_pages+0xd5/0x1b0 > > [] try_charge+0x14d/0x720 > > [] memcg_kmem_charge_memcg+0x3c/0xa0 > > [] memcg_kmem_charge+0x7e/0xd0 > > [] __alloc_pages_nodemask+0x178/0x260 > > [] alloc_pages_current+0x95/0x140 > > [] pte_alloc_one+0x17/0x40 > > [] __pte_alloc+0x1e/0x110 > > [] alloc_set_pte+0x5fe/0xc20 > > [] do_fault+0x103/0x970 > > [] handle_mm_fault+0x61e/0xd10 > > [] __do_page_fault+0x252/0x4d0 > > [] do_page_fault+0x30/0x80 > > [] page_fault+0x28/0x30 > > [] 0xffffffffffffffff > > > > task2: > > [] __lock_page+0x86/0xa0 > > [] mpage_prepare_extent_to_map+0x2e7/0x310 [ext4] > > [] ext4_writepages+0x479/0xd60 > > [] do_writepages+0x1e/0x30 > > [] __writeback_single_inode+0x45/0x320 > > [] writeback_sb_inodes+0x272/0x600 > > [] __writeback_inodes_wb+0x92/0xc0 > > [] wb_writeback+0x268/0x300 > > [] wb_workfn+0xb4/0x390 > > [] process_one_work+0x189/0x420 > > [] worker_thread+0x4e/0x4b0 > > [] kthread+0xe6/0x100 > > [] ret_from_fork+0x41/0x50 > > [] 0xffffffffffffffff > > > > task1 is waiting for the PageWriteback bit of the page that task2 has > > collected in mpd->io_submit->io_bio, and tasks2 is waiting for the LOCKED > > bit the page which tasks1 has locked. > > > > It seems that this deadlock only happens when those pages are mapped pages > > so that mpage_prepare_extent_to_map() can have pages queued in io_bio and > > when waiting to lock the subsequent page. > > > > Signed-off-by: Liu Bo > > --- > > > > Only did build test. > > > > fs/ext4/inode.c | 21 ++++++++++++++++++++- > > 1 file changed, 20 insertions(+), 1 deletion(-) > > > > diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c > > index c3d9a42c561e..becbfb292bf0 100644 > > --- a/fs/ext4/inode.c > > +++ b/fs/ext4/inode.c > > @@ -2681,7 +2681,26 @@ static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd) > > if (mpd->map.m_len > 0 && mpd->next_page != page->index) > > goto out; > > > > - lock_page(page); > > + if (!trylock_page(page)) { > > + /* > > + * A rare race may happen between fault and > > + * writeback, > > + * > > + * 1. fault may have raced in and locked this > > + * page ahead of us, and if fault needs to > > + * reclaim memory via shrink_page_list(), it may > > + * also wait on the writeback pages we've > > + * collected in our mpd->io_submit. > > + * > > + * 2. We have to submit mpd->io_submit->io_bio > > + * to let memory reclaim make progress in order > > + * to avoid the deadlock between fault and > > + * ourselves(writeback). > > + */ > > + ext4_io_submit(&mpd->io_submit); > > + lock_page(page); > > + } > > + > > /* > > * If the page is no longer dirty, or its mapping no > > * longer corresponds to inode we are writing (which > > -- > > 1.8.3.1 > > > -- > Jan Kara > SUSE Labs, CR