From patchwork Thu Dec 1 04:18:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexey Lyashkov X-Patchwork-Id: 9455345 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 26BB660585 for ; Thu, 1 Dec 2016 04:31:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 14950284C6 for ; Thu, 1 Dec 2016 04:31:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 05A3C284C9; Thu, 1 Dec 2016 04:31:34 +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.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, 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 42DAE284C6 for ; Thu, 1 Dec 2016 04:31:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756276AbcLAEbb (ORCPT ); Wed, 30 Nov 2016 23:31:31 -0500 Received: from mail-lf0-f67.google.com ([209.85.215.67]:33955 "EHLO mail-lf0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754165AbcLAEbb (ORCPT ); Wed, 30 Nov 2016 23:31:31 -0500 Received: by mail-lf0-f67.google.com with SMTP id o141so17855220lff.1 for ; Wed, 30 Nov 2016 20:31:30 -0800 (PST) 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=4e2nqsvEY7HBYyNgNVnAnCUKfZyFCvoipuCLaLKzzPo=; b=UiSCocvMShXl7fbGLS+Ry4/CkH11fkVQxDUVYvT01mIfoxke55qidpKh3s9xYt+Ysy 819jWSqNkhXh7YGUxL5EZ+vgfpoCrWUrpeMuW3cDkDi9U1lH0wKE2Ild7K1g3CtiO+ro Y1rXHyl/h/mhsVmhDDG80VfS9arUdaTpGgPKWVRjjxOWNh76PpSRzFooSM2DdIP8YK2z pIrgdrsR5zsAiGRuSNNnFhDAVdrpUZZcZ2tYyLDEcMOlJg13Zm3T9M4uwOBO238QHBoL UaeyABkYjK602KpxxwFWp9fIVpseidr72HcTfhtSjeevOgJTg9FgEnLeW/kfi/ewPJeE Bh/w== 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=4e2nqsvEY7HBYyNgNVnAnCUKfZyFCvoipuCLaLKzzPo=; b=XPm/ScCsuB9HcvJO407mgNqCfNDz0z0Mct/XOpiTzrH+vv79/jaR5FmH7GD8s482pw l+14cb7dvPb2VoLiEjtDSfcwVwWS2EKlg1nRaQwjBpPRpESRj1RroMc5r7ptdZtiiRk7 uuXn/MsNzECf3LzSHZke6yxbsehhqm36L28JqDdr8Ha1R4AdGcZvP42PyTCYZXQMH1VE 1XIuRuIdR1Rvf5kHTEWwiQLnbFz7mC7lwb2Bx/govWF+q5PJe7PqNSd0E8orA/yjA7BY VMMSaMVx3HVFeZHL8U8piApB6/+HEQwCDP2TYvuWxLLm/xqUeQk9dyHURL53RXQbRqmC BVBQ== X-Gm-Message-State: AKaTC00cxBgnHJZuwPUOzvVH18+pW1pFsETpdebTlMu8eNRWY72zBBuFqpp7e72CGXJFaQ== X-Received: by 10.25.76.194 with SMTP id z185mr14169368lfa.182.1480565908343; Wed, 30 Nov 2016 20:18:28 -0800 (PST) Received: from alexeys-macbook-pro.local ([2a00:1a28:155b:5::1016]) by smtp.gmail.com with ESMTPSA id c66sm14670453ljd.44.2016.11.30.20.18.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Nov 2016 20:18:27 -0800 (PST) Subject: [PATCH 1/2] Re introduces a DCACHE_DENTRY_UNHASHED flag From: Alexey Lyashkov To: linux-fsdevel@vger.kernel.org Cc: Andreas Dilger , Andrew Perepechko , Jan Kara , Al Viro Date: Thu, 01 Dec 2016 07:18:26 +0300 Message-ID: <148056588886.28852.15338791339456402291.stgit@alexeys-macbook-pro.local> In-Reply-To: <148056480133.28852.8559789719521724209.stgit@alexeys-macbook-pro.local> References: <148056480133.28852.8559789719521724209.stgit@alexeys-macbook-pro.local> User-Agent: StGit/0.17.1-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 --- arch/powerpc/platforms/cell/spufs/inode.c | 2 +- drivers/infiniband/hw/qib/qib_fs.c | 2 +- .../staging/lustre/lustre/llite/llite_internal.h | 2 +- fs/configfs/inode.c | 2 +- fs/dcache.c | 24 +++++++++++++------- fs/nfs/dir.c | 2 +- include/linux/dcache.h | 6 +++-- 7 files changed, 25 insertions(+), 15 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/arch/powerpc/platforms/cell/spufs/inode.c b/arch/powerpc/platforms/cell/spufs/inode.c index 5364d4a..cc8623d 100644 --- a/arch/powerpc/platforms/cell/spufs/inode.c +++ b/arch/powerpc/platforms/cell/spufs/inode.c @@ -168,7 +168,7 @@ static void spufs_prune_dir(struct dentry *dir) 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(dir), dentry); /* XXX: what was dcache_lock protecting here? Other diff --git a/drivers/infiniband/hw/qib/qib_fs.c b/drivers/infiniband/hw/qib/qib_fs.c index f1e66ef..9b4c0e7 100644 --- a/drivers/infiniband/hw/qib/qib_fs.c +++ b/drivers/infiniband/hw/qib/qib_fs.c @@ -440,7 +440,7 @@ static int remove_file(struct dentry *parent, char *name) spin_lock(&tmp->d_lock); if (simple_positive(tmp)) { - __d_drop(tmp); + _d_drop(tmp); spin_unlock(&tmp->d_lock); simple_unlink(d_inode(parent), tmp); } else { diff --git a/drivers/staging/lustre/lustre/llite/llite_internal.h b/drivers/staging/lustre/lustre/llite/llite_internal.h index 4bc5512..f230505 100644 --- a/drivers/staging/lustre/lustre/llite/llite_internal.h +++ b/drivers/staging/lustre/lustre/llite/llite_internal.h @@ -1353,7 +1353,7 @@ static inline void d_lustre_invalidate(struct dentry *dentry, int nested) * it and busy inodes would be reported. */ if (d_count(dentry) == 0 && !(dentry->d_flags & DCACHE_DISCONNECTED)) - __d_drop(dentry); + _d_drop(dentry); spin_unlock(&dentry->d_lock); } 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)