linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs/dcache: dentries should free after files unlinked or directories removed
@ 2017-08-07  9:54 Wangkai
  2017-08-25  7:06 ` Wangkai (Kevin,C)
  0 siblings, 1 reply; 14+ messages in thread
From: Wangkai @ 2017-08-07  9:54 UTC (permalink / raw)
  To: linux-fsdevel, viro; +Cc: wangkai86, renjinyong

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

^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2017-09-04 21:45 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-08-07  9:54 [PATCH] fs/dcache: dentries should free after files unlinked or directories removed Wangkai
2017-08-25  7:06 ` 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)

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).