From patchwork Thu Feb 22 23:50:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 10236603 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 27D3060224 for ; Thu, 22 Feb 2018 23:51:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1AB5928D34 for ; Thu, 22 Feb 2018 23:51:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0D2002908A; Thu, 22 Feb 2018 23:51:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A754C28D34 for ; Thu, 22 Feb 2018 23:51:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752108AbeBVXvQ (ORCPT ); Thu, 22 Feb 2018 18:51:16 -0500 Received: from Galois.linutronix.de ([146.0.238.70]:40111 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752099AbeBVXvP (ORCPT ); Thu, 22 Feb 2018 18:51:15 -0500 Received: from hsi-kbw-5-158-153-53.hsi19.kabel-badenwuerttemberg.de ([5.158.153.53] helo=linux.lab.linutronix.de.) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1ep0aH-0002Br-Aw; Fri, 23 Feb 2018 00:47:29 +0100 From: John Ogness To: linux-fsdevel@vger.kernel.org Cc: Al Viro , Linus Torvalds , Christoph Hellwig , Thomas Gleixner , Peter Zijlstra , Sebastian Andrzej Siewior , linux-kernel@vger.kernel.org Subject: [PATCH v2 4/6] fs/dcache: Avoid the try_lock loops in dentry_kill() Date: Fri, 23 Feb 2018 00:50:23 +0100 Message-Id: <20180222235025.28662-5-john.ogness@linutronix.de> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180222235025.28662-1-john.ogness@linutronix.de> References: <20180222235025.28662-1-john.ogness@linutronix.de> X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1, SHORTCIRCUIT=-0.0001 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP dentry_kill() holds dentry->d_lock and needs to acquire both dentry->d_inode->i_lock and dentry->d_parent->d_lock. This cannot be done with spin_lock() operations because it's the reverse of the regular lock order. To avoid ABBA deadlocks it is done with two trylock loops. Trylock loops are problematic in two scenarios: 1) PREEMPT_RT converts spinlocks to 'sleeping' spinlocks, which are preemptible. As a consequence the i_lock holder can be preempted by a higher priority task. If that task executes the trylock loop it will do so forever and live lock. 2) In virtual machines trylock loops are problematic as well. The VCPU on which the i_lock holder runs can be scheduled out and a task on a different VCPU can loop for a whole time slice. In the worst case this can lead to starvation. Commits 47be61845c77 ("fs/dcache.c: avoid soft-lockup in dput()") and 046b961b45f9 ("shrink_dentry_list(): take parent's d_lock earlier") are addressing exactly those symptoms. Avoid the trylock loops by using dentry_lock_inode() and lock_parent() which take the locks in the appropriate order. As both functions might drop dentry->lock briefly, this requires rechecking of the dentry content as it might have changed while the lock was dropped. dentry_lock_inode() performs the checks internally, but lock_parent() relies on the caller to perform the checks. Signed-off-by: John Ogness --- fs/dcache.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 16 deletions(-) diff --git a/fs/dcache.c b/fs/dcache.c index bfdf1ff237f2..082361939b84 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -696,27 +696,67 @@ static bool dentry_lock_inode(struct dentry *dentry) static struct dentry *dentry_kill(struct dentry *dentry) __releases(dentry->d_lock) { - struct inode *inode = dentry->d_inode; - struct dentry *parent = NULL; + int saved_count = dentry->d_lockref.count; + struct dentry *parent; + struct inode *inode; - if (inode && unlikely(!spin_trylock(&inode->i_lock))) - goto failed; +again: + inode = dentry->d_inode; - if (!IS_ROOT(dentry)) { - parent = dentry->d_parent; - if (unlikely(!spin_trylock(&parent->d_lock))) { - if (inode) - spin_unlock(&inode->i_lock); - goto failed; - } + /* + * Lock the inode. It will fail if the refcount + * changed while trying to lock the inode. + */ + if (inode && !dentry_lock_inode(dentry)) + goto out_ref_changed; + + parent = lock_parent(dentry); + + /* + * Check refcount because it might have changed + * if d_lock was temporarily dropped. + */ + if (unlikely(dentry->d_lockref.count != saved_count)) { + if (parent) + spin_unlock(&parent->d_lock); + if (inode) + spin_unlock(&inode->i_lock); + goto out_ref_changed; + } + + /* + * d_inode might have changed if d_lock was temporarily + * dropped. If it changed it is necessary to start over + * because a wrong inode (or no inode) lock is held. + */ + if (unlikely(inode != dentry->d_inode)) { + if (parent) + spin_unlock(&parent->d_lock); + if (inode) + spin_unlock(&inode->i_lock); + goto again; } __dentry_kill(dentry); return parent; -failed: +out_ref_changed: + /* + * The refcount was incremented while dentry->d_lock was dropped. + * Just decrement the refcount, unlock, and tell the caller to + * stop the directory walk. + * + * For paranoia reasons check whether the refcount is < 1. If so, + * report the detection and avoid the decrement which would just + * cause a problem in some other place. The warning might be + * helpful to decode the root cause of the refcounting bug. + */ + if (!WARN_ON(dentry->d_lockref.count < 1)) + dentry->d_lockref.count--; + spin_unlock(&dentry->d_lock); - return dentry; /* try again with same dentry */ + + return NULL; } /* @@ -888,10 +928,8 @@ void dput(struct dentry *dentry) kill_it: dentry = dentry_kill(dentry); - if (dentry) { - cond_resched(); + if (dentry) goto repeat; - } } EXPORT_SYMBOL(dput);