From patchwork Tue Nov 1 15:00:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexey Lyashkov X-Patchwork-Id: 9407495 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 9BCF060234 for ; Tue, 1 Nov 2016 15:01:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A816299AC for ; Tue, 1 Nov 2016 15:01:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7C179299AE; Tue, 1 Nov 2016 15:01:04 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham 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 954CB299AC for ; Tue, 1 Nov 2016 15:01:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966653AbcKAPBA (ORCPT ); Tue, 1 Nov 2016 11:01:00 -0400 Received: from mail-lf0-f67.google.com ([209.85.215.67]:33393 "EHLO mail-lf0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S966645AbcKAPA7 (ORCPT ); Tue, 1 Nov 2016 11:00:59 -0400 Received: by mail-lf0-f67.google.com with SMTP id n3so9603816lfn.0 for ; Tue, 01 Nov 2016 08:00:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=H+sCUR66siQtckY3eYlEmuWd4QuPtmuNAptVsIXHMVc=; b=WHn1V4iREaqDE5D0RkLLxJp4OHlIkuoj3ZNZBr9H+jjovVwfSZr+vG51aTlq1I2DP0 OMLob0pCQYE3QwpbcxbJM/wKV1kPaO/Dgov7anPfc4zdT9ycwclOqi/h/R8ESowyPT9g lm3x62hI9OF3LCon+WuV9nWRjD4wrpaO6sfcBoKnZQ+7C72b/iJpPH44n0l5iTYtPHOH a4S0Co+zJF7lZePFLtf+Ym84VIt9nlfM12c0smHlteEq6LHldtjT/l+LWKgvRkHh7C+D L0l9AOzXrLyWVK8GahrY4+xLPY9Yt+9UXrQoasexuKSQBwEpQvc+GtL/m3LAF1y4LDAE X+ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=H+sCUR66siQtckY3eYlEmuWd4QuPtmuNAptVsIXHMVc=; b=nGYeAPdq8hCWomNIu7924jsN+YgbiyklvdOCQDm7JFICSgfamXXZldiTi4e93hNjSE LMFadZajcj570L+tnRaxX88tPrwH7jPCx/6jDENa6WdzMXAJdZfOO4l2pDhoZB0f/twB I5fF109sd6vJ1VtvJZSH4LgFYHi8IFlN9jXloNAo+bHDCY0r7IGLdbF5I+v2DvcK1wzR H+5GgDTVOvYG59TF8zKMkbV0Ul7AtibNZUheGy97RWTQ2nqhrykNGnvEpFFuGcjRv1Ku fgu5fQeS5HS9C4qOF+qqo1tsjcVMLSIxm8uS/Wb03yYskfpedG8IyhWm3WeE9lbgDvJb AZqA== X-Gm-Message-State: ABUngvcejm87oTi0qQFw7HYUKDsqJoj8s7h+rqSFhoXJKKnXHZ0R+i/cwroZDffmOhkltw== X-Received: by 10.25.154.84 with SMTP id c81mr12386130lfe.72.1478012452621; Tue, 01 Nov 2016 08:00:52 -0700 (PDT) Received: from alexeys-macbook-pro.local ([2a00:1a28:155b:2::100f]) by smtp.gmail.com with ESMTPSA id 135sm4148903ljj.32.2016.11.01.08.00.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Nov 2016 08:00:51 -0700 (PDT) Subject: [PATCH v2 1/2] Re introduces a DCACHE_DENTRY_UNHASHED flag From: Alexey Lyashkov To: linux-fsdevel@vger.kernel.org Cc: Andreas Dilger , Jan Kara , Al Viro Date: Tue, 01 Nov 2016 18:00:50 +0300 Message-ID: <147801241940.67124.14554112464218635024.stgit@alexeys-macbook-pro.local> In-Reply-To: <147801228331.67124.8779865197533529488.stgit@alexeys-macbook-pro.local> References: <147801228331.67124.8779865197533529488.stgit@alexeys-macbook-pro.local> User-Agent: StGit/0.17-dirty MIME-Version: 1.0 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 rehash process protected with d_seq and d_lock locks, but VFS have access to the d_hashed field without any locks sometimes. It produce errors with get cwd operations or fsnotify may report an unlink event sometimes. d_seq lock isn’t used to protect due possibility to sleep with holding locks, and d_lock is too hot to check a simple state, lets return a DCACHE_DENTRY_UNHASHED flag to ability to check a unhashed state without locks held. #include #include #include #include #include #include #include void *thread_main(void *unused) { int rc; for (;;) { rc = rename("/tmp/t-a", "/tmp/t-b"); assert(rc == 0); rc = rename("/tmp/t-b", "/tmp/t-a"); assert(rc == 0); } return NULL; } int main(void) { int rc, i; pthread_t ptt; rmdir("/tmp/t-a"); rmdir("/tmp/t-b"); rc = mkdir("/tmp/t-a", 0666); assert(rc == 0); rc = chdir("/tmp/t-a"); assert(rc == 0); rc = pthread_create(&ptt, NULL, thread_main, NULL); assert(rc == 0); for (i=0; ;i++) { char buf[100], *b; b = getcwd(buf, sizeof(buf)); if (b == NULL) { printf("getcwd failed on iter %d with %d\n", i, errno); break; } } return 0; } Reported-by: Andrew Perepechko Signed-off-by: Alexey Lyashkov --- fs/configfs/inode.c | 2 +- fs/dcache.c | 24 ++++++++++++++++-------- fs/nfs/dir.c | 2 +- include/linux/dcache.h | 6 ++++-- 4 files changed, 22 insertions(+), 12 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/fs/configfs/inode.c b/fs/configfs/inode.c index ad718e5..9f531f7 100644 --- a/fs/configfs/inode.c +++ b/fs/configfs/inode.c @@ -248,7 +248,7 @@ void configfs_drop_dentry(struct configfs_dirent * sd, struct dentry * parent) spin_lock(&dentry->d_lock); if (simple_positive(dentry)) { dget_dlock(dentry); - __d_drop(dentry); + _d_drop(dentry); spin_unlock(&dentry->d_lock); simple_unlink(d_inode(parent), dentry); } else diff --git a/fs/dcache.c b/fs/dcache.c index 5c7cc95..0863841 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -438,7 +438,7 @@ static void dentry_lru_add(struct dentry *dentry) */ void __d_drop(struct dentry *dentry) { - if (!d_unhashed(dentry)) { + if (!hlist_bl_unhashed(&dentry->d_hash)) { struct hlist_bl_head *b; /* * Hashed dentries are normally on the dentry hashtable, @@ -458,12 +458,18 @@ void __d_drop(struct dentry *dentry) write_seqcount_invalidate(&dentry->d_seq); } } -EXPORT_SYMBOL(__d_drop); + +void _d_drop(struct dentry *dentry) +{ + dentry->d_flags |= DCACHE_DENTRY_UNHASHED; + __d_drop(dentry); +} +EXPORT_SYMBOL(_d_drop); void d_drop(struct dentry *dentry) { spin_lock(&dentry->d_lock); - __d_drop(dentry); + _d_drop(dentry); spin_unlock(&dentry->d_lock); } EXPORT_SYMBOL(d_drop); @@ -530,7 +536,7 @@ static void __dentry_kill(struct dentry *dentry) d_lru_del(dentry); } /* if it was on the hash then remove it */ - __d_drop(dentry); + _d_drop(dentry); dentry_unlist(dentry, parent); if (parent) spin_unlock(&parent->d_lock); @@ -1486,7 +1492,7 @@ static void check_and_drop(void *_data) struct detach_data *data = _data; if (!data->mountpoint && !data->select.found) - __d_drop(data->select.start); + _d_drop(data->select.start); } /** @@ -1601,7 +1607,7 @@ struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) dentry->d_name.name = dname; dentry->d_lockref.count = 1; - dentry->d_flags = 0; + dentry->d_flags = DCACHE_DENTRY_UNHASHED; spin_lock_init(&dentry->d_lock); seqcount_init(&dentry->d_seq); dentry->d_inode = NULL; @@ -1926,6 +1932,7 @@ static struct dentry *__d_obtain_alias(struct inode *inode, int disconnected) spin_lock(&tmp->d_lock); __d_set_inode_and_type(tmp, inode, add_flags); hlist_add_head(&tmp->d_u.d_alias, &inode->i_dentry); + tmp->d_flags &= ~DCACHE_DENTRY_UNHASHED; hlist_bl_lock(&tmp->d_sb->s_anon); hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon); hlist_bl_unlock(&tmp->d_sb->s_anon); @@ -2331,7 +2338,7 @@ void d_delete(struct dentry * dentry) } if (!d_unhashed(dentry)) - __d_drop(dentry); + _d_drop(dentry); spin_unlock(&dentry->d_lock); @@ -2342,7 +2349,8 @@ EXPORT_SYMBOL(d_delete); static void __d_rehash(struct dentry *entry) { struct hlist_bl_head *b = d_hash(entry->d_name.hash); - BUG_ON(!d_unhashed(entry)); + BUG_ON(!hlist_bl_unhashed(&entry->d_hash)); + entry->d_flags &= ~DCACHE_DENTRY_UNHASHED; hlist_bl_lock(b); hlist_bl_add_head_rcu(&entry->d_hash, b); hlist_bl_unlock(b); diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c index 5f1af4c..a1911bb 100644 --- a/fs/nfs/dir.c +++ b/fs/nfs/dir.c @@ -1891,7 +1891,7 @@ int nfs_unlink(struct inode *dir, struct dentry *dentry) goto out; } if (!d_unhashed(dentry)) { - __d_drop(dentry); + _d_drop(dentry); need_rehash = 1; } spin_unlock(&dentry->d_lock); diff --git a/include/linux/dcache.h b/include/linux/dcache.h index 5beed7b..6b0b3d7 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -213,6 +213,7 @@ struct dentry_operations { #define DCACHE_PAR_LOOKUP 0x10000000 /* being looked up (with parent locked shared) */ #define DCACHE_DENTRY_CURSOR 0x20000000 +#define DCACHE_DENTRY_UNHASHED 0x40000000 /* dentry unhashed from tree with unlink */ extern seqlock_t rename_lock; /* @@ -221,7 +222,7 @@ extern seqlock_t rename_lock; extern void d_instantiate(struct dentry *, struct inode *); extern struct dentry * d_instantiate_unique(struct dentry *, struct inode *); extern int d_instantiate_no_diralias(struct dentry *, struct inode *); -extern void __d_drop(struct dentry *dentry); +extern void _d_drop(struct dentry *dentry); extern void d_drop(struct dentry *dentry); extern void d_delete(struct dentry *); extern void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op); @@ -326,11 +327,12 @@ extern struct dentry *dget_parent(struct dentry *dentry); * @dentry: entry to check * * Returns true if the dentry passed is not currently hashed. + * hlist_bl_unhashed can't be used as race with d_move(). */ static inline int d_unhashed(const struct dentry *dentry) { - return hlist_bl_unhashed(&dentry->d_hash); + return dentry->d_flags & DCACHE_DENTRY_UNHASHED; } static inline int d_unlinked(const struct dentry *dentry)