Skip to content

Commit

Permalink
dcache: sweep cached negative dentries to the end of list of siblings
Browse files Browse the repository at this point in the history
For disk filesystems result of every negative lookup is cached, content of
directories is usually cached too. Production of negative dentries isn't
limited with disk speed. It's really easy to generate millions of them if
system has enough memory. Negative dentries are linked into siblings list
along with normal positive dentries. Some operations walks dcache tree but
looks only for positive dentries: most important is fsnotify/inotify.

This patch moves negative dentries to the end of list at final dput() and
marks with flag which tells that all following dentries are negative too.
Reverse operation is required before instantiating negative dentry.

Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Signed-off-by: Gautham Ananthakrishna <gautham.ananthakrishna@oracle.com>
  • Loading branch information
koct9i authored and intel-lab-lkp committed Jan 21, 2021
1 parent fecc455 commit 65ea958
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
59 changes: 56 additions & 3 deletions fs/dcache.c
Expand Up @@ -632,6 +632,48 @@ static inline struct dentry *lock_parent(struct dentry *dentry)
return __lock_parent(dentry);
}

/*
* Move cached negative dentry to the tail of parent->d_subdirs.
* This lets walkers skip them all together at first sight.
* Must be called at dput of negative dentry.
*/
static void sweep_negative(struct dentry *dentry)
{
struct dentry *parent;

if (!d_is_tail_negative(dentry)) {
parent = lock_parent(dentry);
if (!parent)
return;

if (!d_count(dentry) && d_is_negative(dentry) &&
!d_is_tail_negative(dentry)) {
dentry->d_flags |= DCACHE_TAIL_NEGATIVE;
list_move_tail(&dentry->d_child, &parent->d_subdirs);
}

spin_unlock(&parent->d_lock);
}
}

/*
* Undo sweep_negative() and move to the head of parent->d_subdirs.
* Must be called before converting negative dentry into positive.
*/
static void recycle_negative(struct dentry *dentry)
{
struct dentry *parent;

spin_lock(&dentry->d_lock);
parent = lock_parent(dentry);
dentry->d_flags &= ~DCACHE_TAIL_NEGATIVE;
if (parent) {
list_move(&dentry->d_child, &parent->d_subdirs);
spin_unlock(&parent->d_lock);
}
spin_unlock(&dentry->d_lock);
}

static inline bool retain_dentry(struct dentry *dentry)
{
WARN_ON(d_in_lookup(dentry));
Expand Down Expand Up @@ -737,7 +779,7 @@ static struct dentry *dentry_kill(struct dentry *dentry)
static inline bool fast_dput(struct dentry *dentry)
{
int ret;
unsigned int d_flags;
unsigned int d_flags, required;

/*
* If we have a d_op->d_delete() operation, we sould not
Expand Down Expand Up @@ -785,6 +827,8 @@ static inline bool fast_dput(struct dentry *dentry)
* a 'delete' op, and it's referenced and already on
* the LRU list.
*
* Cached negative dentry must be swept to the tail.
*
* NOTE! Since we aren't locked, these values are
* not "stable". However, it is sufficient that at
* some point after we dropped the reference the
Expand All @@ -796,10 +840,15 @@ static inline bool fast_dput(struct dentry *dentry)
*/
smp_rmb();
d_flags = READ_ONCE(dentry->d_flags);
d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST | DCACHE_DISCONNECTED;

required = DCACHE_REFERENCED | DCACHE_LRU_LIST |
(d_flags_negative(d_flags) ? DCACHE_TAIL_NEGATIVE : 0);

d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST |
DCACHE_DISCONNECTED | DCACHE_TAIL_NEGATIVE;

/* Nothing to do? Dropping the reference was all we needed? */
if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
if (d_flags == required && !d_unhashed(dentry))
return true;

/*
Expand Down Expand Up @@ -871,6 +920,8 @@ void dput(struct dentry *dentry)
rcu_read_unlock();

if (likely(retain_dentry(dentry))) {
if (d_is_negative(dentry))
sweep_negative(dentry);
spin_unlock(&dentry->d_lock);
return;
}
Expand Down Expand Up @@ -1970,6 +2021,8 @@ void d_instantiate(struct dentry *entry, struct inode * inode)
{
BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
if (inode) {
if (d_is_tail_negative(entry))
recycle_negative(entry);
security_d_instantiate(entry, inode);
spin_lock(&inode->i_lock);
__d_instantiate(entry, inode);
Expand Down
6 changes: 6 additions & 0 deletions include/linux/dcache.h
Expand Up @@ -219,6 +219,7 @@ struct dentry_operations {
#define DCACHE_PAR_LOOKUP 0x10000000 /* being looked up (with parent locked shared) */
#define DCACHE_DENTRY_CURSOR 0x20000000
#define DCACHE_NORCU 0x40000000 /* No RCU delay for freeing */
#define DCACHE_TAIL_NEGATIVE 0x80000000 /* All following siblings are negative */

extern seqlock_t rename_lock;

Expand Down Expand Up @@ -495,6 +496,11 @@ static inline int simple_positive(const struct dentry *dentry)
return d_really_is_positive(dentry) && !d_unhashed(dentry);
}

static inline bool d_is_tail_negative(const struct dentry *dentry)
{
return unlikely(dentry->d_flags & DCACHE_TAIL_NEGATIVE);
}

extern void d_set_fallthru(struct dentry *dentry);

static inline bool d_is_fallthru(const struct dentry *dentry)
Expand Down

0 comments on commit 65ea958

Please sign in to comment.