From patchwork Mon Aug 7 09:54:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wangkai X-Patchwork-Id: 9884869 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 6DEB160364 for ; Mon, 7 Aug 2017 09:43:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6271B285A6 for ; Mon, 7 Aug 2017 09:43:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 55B4C2860C; Mon, 7 Aug 2017 09:43:42 +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=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 A60BD285A6 for ; Mon, 7 Aug 2017 09:43:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752606AbdHGJnk (ORCPT ); Mon, 7 Aug 2017 05:43:40 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:2562 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752367AbdHGJnk (ORCPT ); Mon, 7 Aug 2017 05:43:40 -0400 Received: from 172.30.72.58 (EHLO DGGEMS406-HUB.china.huawei.com) ([172.30.72.58]) by dggrg05-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id DES32973; Mon, 07 Aug 2017 17:43:38 +0800 (CST) Received: from localhost.localdomain (10.107.56.235) by smtp.huawei.com (10.3.19.206) with Microsoft SMTP Server (TLS) id 14.3.301.0; Mon, 7 Aug 2017 17:43:29 +0800 From: Wangkai To: , CC: , Subject: =?UTF-8?q?=5BPATCH=5D=20fs/dcache=3A=20dentries=20should=20free=20after=20files=20unlinked=20or=20directories=20removed?= Date: Mon, 7 Aug 2017 17:54:33 +0800 Message-ID: <1502099673-31620-1-git-send-email-wangkai86@huawei.com> X-Mailer: git-send-email 1.7.2.5 MIME-Version: 1.0 X-Originating-IP: [10.107.56.235] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.5988364A.0090, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: e43190f6276b2df21734a951e984fba3 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 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 --- 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