Return-Path: Received: from mail.linuxfoundation.org ([140.211.169.12]:52510 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755198AbeDWOnZ (ORCPT ); Mon, 23 Apr 2018 10:43:25 -0400 Date: Mon, 23 Apr 2018 16:43:19 +0200 From: Greg KH To: Greg Thelen Cc: natechancellor@gmail.com, stable@vger.kernel.org, Johannes Weiner , Tejun Heo , npiggin@gmail.com, Andrew Morton , Linus Torvalds Subject: Re: [PATCH 4.14/4.16 v2] writeback: safer lock nesting Message-ID: <20180423144319.GB8290@kroah.com> References: <20180422111514.16886-1-natechancellor@gmail.com> <20180422122432.GA3974@kroah.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: stable-owner@vger.kernel.org List-ID: On Sun, Apr 22, 2018 at 04:13:10PM +0000, Greg Thelen wrote: > On Sun, Apr 22, 2018 at 5:24 AM Greg Kroah-Hartman < > gregkh@linuxfoundation.org> wrote: > > > On Sun, Apr 22, 2018 at 04:15:12AM -0700, Nathan Chancellor wrote: > > > From: Greg Thelen > > > > > > commit 2e898e4c0a3897ccd434adac5abb8330194f527b upstream. > > > > > > lock_page_memcg()/unlock_page_memcg() use spin_lock_irqsave/restore() if > > > the page's memcg is undergoing move accounting, which occurs when a > > > process leaves its memcg for a new one that has > > > memory.move_charge_at_immigrate set. > > > > > > unlocked_inode_to_wb_begin,end() use spin_lock_irq/spin_unlock_irq() if > > > the given inode is switching writeback domains. Switches occur when > > > enough writes are issued from a new domain. > > > > > > This existing pattern is thus suspicious: > > > lock_page_memcg(page); > > > unlocked_inode_to_wb_begin(inode, &locked); > > > ... > > > unlocked_inode_to_wb_end(inode, locked); > > > unlock_page_memcg(page); > > > > > > If both inode switch and process memcg migration are both in-flight then > > > unlocked_inode_to_wb_end() will unconditionally enable interrupts while > > > still holding the lock_page_memcg() irq spinlock. This suggests the > > > possibility of deadlock if an interrupt occurs before > unlock_page_memcg(). > > > > > > truncate > > > __cancel_dirty_page > > > lock_page_memcg > > > unlocked_inode_to_wb_begin > > > unlocked_inode_to_wb_end > > > > > > > > > end_page_writeback > > > test_clear_page_writeback > > > lock_page_memcg > > > > > > unlock_page_memcg > > > > > > Due to configuration limitations this deadlock is not currently possible > > > because we don't mix cgroup writeback (a cgroupv2 feature) and > > > memory.move_charge_at_immigrate (a cgroupv1 feature). > > > > > > If the kernel is hacked to always claim inode switching and memcg > > > moving_account, then this script triggers lockup in less than a minute: > > > > > > cd /mnt/cgroup/memory > > > mkdir a b > > > echo 1 > a/memory.move_charge_at_immigrate > > > echo 1 > b/memory.move_charge_at_immigrate > > > ( > > > echo $BASHPID > a/cgroup.procs > > > while true; do > > > dd if=/dev/zero of=/mnt/big bs=1M count=256 > > > done > > > ) & > > > while true; do > > > sync > > > done & > > > sleep 1h & > > > SLEEP=$! > > > while true; do > > > echo $SLEEP > a/cgroup.procs > > > echo $SLEEP > b/cgroup.procs > > > done > > > > > > The deadlock does not seem possible, so it's debatable if there's any > > > reason to modify the kernel. I suggest we should to prevent future > > > surprises. And Wang Long said "this deadlock occurs three times in our > > > environment", so there's more reason to apply this, even to stable. > > > Stable 4.4 has minor conflicts applying this patch. For a clean 4.4 > patch > > > see "[PATCH for-4.4] writeback: safer lock nesting" > > > https://lkml.org/lkml/2018/4/11/146 > > > > > > Wang Long said "this deadlock occurs three times in our environment" > > > > > > [gthelen@google.com: v4] > > > Link: > http://lkml.kernel.org/r/20180411084653.254724-1-gthelen@google.com > > > [akpm@linux-foundation.org: comment tweaks, struct initialization > simplification] > > > Change-Id: Ibb773e8045852978f6207074491d262f1b3fb613 > > > Link: > http://lkml.kernel.org/r/20180410005908.167976-1-gthelen@google.com > > > Fixes: 682aa8e1a6a1 ("writeback: implement unlocked_inode_to_wb > transaction and use it for stat updates") > > > Signed-off-by: Greg Thelen > > > Reported-by: Wang Long > > > Acked-by: Wang Long > > > Acked-by: Michal Hocko > > > Reviewed-by: Andrew Morton > > > Cc: Johannes Weiner > > > Cc: Tejun Heo > > > Cc: Nicholas Piggin > > > Cc: [v4.2+] > > > Signed-off-by: Andrew Morton > > > Signed-off-by: Linus Torvalds > > > [natechancellor: Adjust context due to lack of b93b016313b3b] > > > Signed-off-by: Nathan Chancellor > > > Thanks for all of these, now queued up. > > > greg k-h > > I reviewed the 4.4, 4.9, 4.14, 4.16 queued stable backports for > ("writeback: safer lock nesting"). They all look good. Thanks. > > I don't know if it's customary to add an author's reviewed-by to > non-trivial backports. > If useful, here you go: > > Reviewed-by: Greg Thelen No need, but thanks for the review! greg k-h