[PATCH v4 2/6] fs/dcache: Track & report number of negative dentries

From: Waiman Long
Date: Mon Sep 18 2017 - 14:22:30 EST


The current dentry number tracking code doesn't distinguish between
positive & negative dentries. It just reports the total number of
dentries in the LRU lists.

As excessive number of negative dentries can have an impact on system
performance, it will be wise to track the number of positive and
negative dentries separately.

This patch adds tracking for the total number of negative dentries in
the system LRU lists and reports it in the /proc/sys/fs/dentry-state
file. The number of positive dentries in the LRU lists can be found
by subtracting the number of negative dentries from the total.

Signed-off-by: Waiman Long <longman@xxxxxxxxxx>
---
fs/dcache.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
include/linux/dcache.h | 7 ++++---
2 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/fs/dcache.c b/fs/dcache.c
index 8979dd8..e13668c 100644
--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -132,6 +132,7 @@ struct dentry_stat_t dentry_stat = {

static DEFINE_PER_CPU(long, nr_dentry);
static DEFINE_PER_CPU(long, nr_dentry_unused);
+static DEFINE_PER_CPU(long, nr_dentry_neg);

#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)

@@ -165,11 +166,22 @@ static long get_nr_dentry_unused(void)
return sum < 0 ? 0 : sum;
}

+static long get_nr_dentry_neg(void)
+{
+ int i;
+ long sum = 0;
+
+ for_each_possible_cpu(i)
+ sum += per_cpu(nr_dentry_neg, i);
+ return sum < 0 ? 0 : sum;
+}
+
int proc_nr_dentry(struct ctl_table *table, int write, void __user *buffer,
size_t *lenp, loff_t *ppos)
{
dentry_stat.nr_dentry = get_nr_dentry();
dentry_stat.nr_unused = get_nr_dentry_unused();
+ dentry_stat.nr_negative = get_nr_dentry_neg();
return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
}
#endif
@@ -227,6 +239,28 @@ static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char

#endif

+static inline void __neg_dentry_dec(struct dentry *dentry)
+{
+ this_cpu_dec(nr_dentry_neg);
+}
+
+static inline void neg_dentry_dec(struct dentry *dentry)
+{
+ if (unlikely(d_is_negative(dentry)))
+ __neg_dentry_dec(dentry);
+}
+
+static inline void __neg_dentry_inc(struct dentry *dentry)
+{
+ this_cpu_inc(nr_dentry_neg);
+}
+
+static inline void neg_dentry_inc(struct dentry *dentry)
+{
+ if (unlikely(d_is_negative(dentry)))
+ __neg_dentry_inc(dentry);
+}
+
static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
{
/*
@@ -329,6 +363,8 @@ static inline void __d_clear_type_and_inode(struct dentry *dentry)
flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
WRITE_ONCE(dentry->d_flags, flags);
dentry->d_inode = NULL;
+ if (dentry->d_flags & DCACHE_LRU_LIST)
+ __neg_dentry_inc(dentry);
}

static void dentry_free(struct dentry *dentry)
@@ -396,6 +432,7 @@ static void d_lru_add(struct dentry *dentry)
dentry->d_flags |= DCACHE_LRU_LIST;
this_cpu_inc(nr_dentry_unused);
WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
+ neg_dentry_inc(dentry);
}

static void d_lru_del(struct dentry *dentry)
@@ -404,6 +441,7 @@ static void d_lru_del(struct dentry *dentry)
dentry->d_flags &= ~DCACHE_LRU_LIST;
this_cpu_dec(nr_dentry_unused);
WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
+ neg_dentry_dec(dentry);
}

static void d_shrink_del(struct dentry *dentry)
@@ -434,6 +472,7 @@ static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
dentry->d_flags &= ~DCACHE_LRU_LIST;
this_cpu_dec(nr_dentry_unused);
list_lru_isolate(lru, &dentry->d_lru);
+ neg_dentry_dec(dentry);
}

static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
@@ -442,6 +481,7 @@ static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
dentry->d_flags |= DCACHE_SHRINK_LIST;
list_lru_isolate_move(lru, &dentry->d_lru, list);
+ neg_dentry_dec(dentry);
}

/*
@@ -1820,6 +1860,11 @@ static void __d_instantiate(struct dentry *dentry, struct inode *inode)
WARN_ON(d_in_lookup(dentry));

spin_lock(&dentry->d_lock);
+ /*
+ * Decrement negative dentry count if it was in the LRU list.
+ */
+ if (dentry->d_flags & DCACHE_LRU_LIST)
+ __neg_dentry_dec(dentry);
hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
raw_write_seqcount_begin(&dentry->d_seq);
__d_set_inode_and_type(dentry, inode, add_flags);
diff --git a/include/linux/dcache.h b/include/linux/dcache.h
index ed1a7cf..c8dcbd3 100644
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -63,9 +63,10 @@ struct qstr {
struct dentry_stat_t {
long nr_dentry;
long nr_unused;
- long age_limit; /* age in seconds */
- long want_pages; /* pages requested by system */
- long dummy[2];
+ long age_limit; /* age in seconds */
+ long want_pages; /* pages requested by system */
+ long nr_negative; /* # of negative dentries */
+ long dummy;
};
extern struct dentry_stat_t dentry_stat;

--
1.8.3.1