linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wangkai <wangkai86@huawei.com>
To: <linux-fsdevel@vger.kernel.org>, <viro@zeniv.linux.org.uk>
Cc: <wangkai86@huawei.com>, <renjinyong@huawei.com>
Subject: [PATCH] fs/dcache: dentries should free after files unlinked or directories removed
Date: Mon, 7 Aug 2017 17:54:33 +0800	[thread overview]
Message-ID: <1502099673-31620-1-git-send-email-wangkai86@huawei.com> (raw)

sometimes, on my server, there were lots of files creating and removing, and I
found that memory usage were high, and I checked, almost all the memory was 
occupied by slab recliamable, the slab struct was dentry, and I checked the 
code of dcache, and found that when a file was deleted the dentry never free, 
unless a memory recliam was triggerd.

I made this patch to mark the dentry as a remove state after file unlinked or
directory removed, and when the dentry’s reference count dec to zero and 
free it, and it worked well on my server base on kernel 4.4.


Signed-off-by: Wangkai <wangkai86@huawei.com>
---
 fs/dcache.c            | 12 +++++++++++-
 fs/namei.c             |  6 ++++++
 include/linux/dcache.h |  2 +-
 3 files changed, 18 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 fs/dcache.c
 mode change 100644 => 100755 fs/namei.c
 mode change 100644 => 100755 include/linux/dcache.h

diff --git a/fs/dcache.c b/fs/dcache.c
old mode 100644
new mode 100755
index f901413..6828463
--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -722,7 +722,8 @@ static inline bool fast_dput(struct dentry *dentry)
 	 */
 	smp_rmb();
 	d_flags = ACCESS_ONCE(dentry->d_flags);
-	d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST | DCACHE_DISCONNECTED;
+	d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST | DCACHE_DISCONNECTED
+		| DCACHE_FILE_REMOVED;
 
 	/* Nothing to do? Dropping the reference was all we needed? */
 	if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
@@ -816,6 +817,15 @@ void dput(struct dentry *dentry)
 	dentry_lru_add(dentry);
 
 	dentry->d_lockref.count--;
+
+	/*
+	 * if file has been declare as removed and reference count is zero
+	 * then we can free the dentry rather than leave it stay in dcache
+	 */
+	if (unlikely(dentry->d_flags & DCACHE_FILE_REMOVED)) {
+		if (dentry->d_lockref.count == 0) 
+			goto kill_it;
+	}
 	spin_unlock(&dentry->d_lock);
 	return;
 
diff --git a/fs/namei.c b/fs/namei.c
old mode 100644
new mode 100755
index ddb6a7c..0ec1478
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -3918,6 +3918,9 @@ static long do_rmdir(int dfd, const char __user *pathname)
 		goto exit3;
 	error = vfs_rmdir(path.dentry->d_inode, dentry);
 exit3:
+	/* after remove the dir set dentry remove flag */
+	if (!error)
+		dentry->d_flags |= DCACHE_FILE_REMOVED;
 	dput(dentry);
 exit2:
 	inode_unlock(path.dentry->d_inode);
@@ -4042,6 +4045,9 @@ static long do_unlinkat(int dfd, const char __user *pathname)
 			goto exit2;
 		error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
 exit2:
+		/* after unlink file set dentry remove flag */
+		if (!error)
+			dentry->d_flags |= DCACHE_FILE_REMOVED;
 		dput(dentry);
 	}
 	inode_unlock(path.dentry->d_inode);
diff --git a/include/linux/dcache.h b/include/linux/dcache.h
old mode 100644
new mode 100755
index aae1cdb..2d65bd6
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -215,7 +215,7 @@ struct dentry_operations {
 #define DCACHE_FALLTHRU			0x01000000 /* Fall through to lower layer */
 #define DCACHE_ENCRYPTED_WITH_KEY	0x02000000 /* dir is encrypted with a valid key */
 #define DCACHE_OP_REAL			0x04000000
-
+#define DCACHE_FILE_REMOVED		0x08000000 /* file or dir has been unlinked or removed */
 #define DCACHE_PAR_LOOKUP		0x10000000 /* being looked up (with parent locked shared) */
 #define DCACHE_DENTRY_CURSOR		0x20000000
 
-- 
2.8.0.GIT

             reply	other threads:[~2017-08-07  9:43 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-07  9:54 Wangkai [this message]
2017-08-25  7:06 ` [PATCH] fs/dcache: dentries should free after files unlinked or directories removed Wangkai (Kevin,C)
2017-08-25 14:47   ` Jan Kara
2017-08-25 15:10     ` Colin Walters
2017-08-25 16:43       ` Linus Torvalds
2017-08-26  6:56     ` Wangkai (Kevin,C)
2017-08-26 16:18       ` Linus Torvalds
2017-08-27 15:05         ` Waiman Long
2017-08-31  7:53           ` Jan Kara
2017-08-31 16:27             ` Waiman Long
2017-09-04 12:59               ` Jan Kara
2017-09-04 15:48                 ` Linus Torvalds
2017-09-04 21:45                 ` Waiman Long
2017-08-28  6:31         ` Wangkai (Kevin,C)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1502099673-31620-1-git-send-email-wangkai86@huawei.com \
    --to=wangkai86@huawei.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=renjinyong@huawei.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).