From: Waiman Long <longman@redhat.com>
To: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-mm@kvack.org, Alexander Viro <viro@zeniv.linux.org.uk>
Subject: Re: [PATCH RFC 3/8] dcache: sweep cached negative dentries to the end of list of siblings
Date: Fri, 8 May 2020 15:38:59 -0400 [thread overview]
Message-ID: <9b5d75d0-d627-2b53-10eb-a850bace091b@redhat.com> (raw)
In-Reply-To: <158894060021.200862.15936671684100629802.stgit@buzz>
On 5/8/20 8:23 AM, Konstantin Khlebnikov wrote:
> 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>
> ---
> fs/dcache.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++--
> include/linux/dcache.h | 6 +++++
> 2 files changed, 66 insertions(+), 3 deletions(-)
>
> diff --git a/fs/dcache.c b/fs/dcache.c
> index 386f97eaf2ff..743255773cc7 100644
> --- a/fs/dcache.c
> +++ b/fs/dcache.c
> @@ -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);
> + if (parent) {
> + list_move(&dentry->d_child, &parent->d_subdirs);
> + spin_unlock(&parent->d_lock);
> + }
> + dentry->d_flags &= ~DCACHE_TAIL_NEGATIVE;
> + spin_unlock(&dentry->d_lock);
> +}
> +
The name sweep_negative and recycle_negative are not very descriptive of
what the function is doing (especially the later one). I am not good at
naming, but some kind of naming scheme that can clearly show one is
opposite of another will be better.
> static inline bool retain_dentry(struct dentry *dentry)
> {
> WARN_ON(d_in_lookup(dentry));
> @@ -703,6 +745,8 @@ static struct dentry *dentry_kill(struct dentry *dentry)
> spin_unlock(&inode->i_lock);
> if (parent)
> spin_unlock(&parent->d_lock);
> + if (d_is_negative(dentry))
> + sweep_negative(dentry);
We can potentially save an unneeded lock/unlock pair by moving it up
before "if (parent)" and pass in a flag to indicate if the parent lock
is being held.
> spin_unlock(&dentry->d_lock);
> return NULL;
> }
> @@ -718,7 +762,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
> @@ -766,6 +810,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
> @@ -777,10 +823,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;
>
> /*
> @@ -852,6 +903,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;
> }
> @@ -1951,6 +2004,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);
Will it better to recycle_negative() inside __d_instantiate() under the
d_lock. That reduces the number of lock/unlock you need to do and
eliminate the need to change d_instantiate_new().
Cheers,
Longman
next prev parent reply other threads:[~2020-05-08 19:39 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-08 12:23 [PATCH RFC 0/8] dcache: increase poison resistance Konstantin Khlebnikov
2020-05-08 12:23 ` [PATCH RFC 1/8] dcache: show count of hash buckets in sysctl fs.dentry-state Konstantin Khlebnikov
2020-05-08 14:49 ` Waiman Long
2020-05-08 16:16 ` Konstantin Khlebnikov
2020-05-08 19:05 ` Waiman Long
2020-05-08 19:38 ` Konstantin Khlebnikov
2020-05-08 20:00 ` Waiman Long
2020-05-08 20:03 ` Matthew Wilcox
2020-05-08 12:23 ` [PATCH RFC 2/8] selftests: add stress testing tool for dcache Konstantin Khlebnikov
2020-05-13 1:52 ` Dave Chinner
2020-05-08 12:23 ` [PATCH RFC 3/8] dcache: sweep cached negative dentries to the end of list of siblings Konstantin Khlebnikov
2020-05-08 19:38 ` Waiman Long [this message]
2020-05-08 12:23 ` [PATCH RFC 4/8] fsnotify: stop walking child dentries if remaining tail is negative Konstantin Khlebnikov
2020-05-08 12:23 ` [PATCH RFC 5/8] dcache: add action D_WALK_SKIP_SIBLINGS to d_walk() Konstantin Khlebnikov
2020-05-08 12:23 ` [PATCH RFC 6/8] dcache: stop walking siblings if remaining dentries all negative Konstantin Khlebnikov
2020-05-08 12:23 ` [PATCH RFC 7/8] dcache: push releasing dentry lock into sweep_negative Konstantin Khlebnikov
2020-05-08 12:23 ` [PATCH RFC 8/8] dcache: prevent flooding with negative dentries Konstantin Khlebnikov
2020-05-08 14:56 ` Matthew Wilcox
2020-05-08 16:29 ` Konstantin Khlebnikov
2020-05-08 21:07 ` Waiman Long
2020-12-09 23:01 ` [PATCH RFC 0/8] dcache: increase poison resistance Junxiao Bi
[not found] ` <CALYGNiN2F8gcKX+2nKOi1tapquJWfyzUkajWxTqgd9xvd7u1AA@mail.gmail.com>
2020-12-13 18:49 ` Junxiao Bi
[not found] ` <CALYGNiM8Fp=ZV8S6c2L50ne1cGhE30PrT-C=4nfershvfAgP+Q@mail.gmail.com>
2020-12-14 23:10 ` Junxiao Bi
2020-12-16 18:46 ` Junxiao Bi
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=9b5d75d0-d627-2b53-10eb-a850bace091b@redhat.com \
--to=longman@redhat.com \
--cc=khlebnikov@yandex-team.ru \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--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).