Commit f1782c9b authored by Roman Gushchin's avatar Roman Gushchin Committed by Linus Torvalds

dcache: account external names as indirectly reclaimable memory

I received a report about suspicious growth of unreclaimable slabs on
some machines.  I've found that it happens on machines with low memory
pressure, and these unreclaimable slabs are external names attached to
dentries.

External names are allocated using generic kmalloc() function, so they
are accounted as unreclaimable.  But they are held by dentries, which
are reclaimable, and they will be reclaimed under the memory pressure.

In particular, this breaks MemAvailable calculation, as it doesn't take
unreclaimable slabs into account.  This leads to a silly situation, when
a machine is almost idle, has no memory pressure and therefore has a big
dentry cache.  And the resulting MemAvailable is too low to start a new
workload.

To address the issue, the NR_INDIRECTLY_RECLAIMABLE_BYTES counter is
used to track the amount of memory, consumed by external names.  The
counter is increased in the dentry allocation path, if an external name
structure is allocated; and it's decreased in the dentry freeing path.

To reproduce the problem I've used the following Python script:

  import os

  for iter in range (0, 10000000):
      try:
          name = ("/some_long_name_%d" % iter) + "_" * 220
          os.stat(name)
      except Exception:
          pass

Without this patch:
  $ cat /proc/meminfo | grep MemAvailable
  MemAvailable:    7811688 kB
  $ python indirect.py
  $ cat /proc/meminfo | grep MemAvailable
  MemAvailable:    2753052 kB

With the patch:
  $ cat /proc/meminfo | grep MemAvailable
  MemAvailable:    7809516 kB
  $ python indirect.py
  $ cat /proc/meminfo | grep MemAvailable
  MemAvailable:    7749144 kB

[guro@fb.com: fix indirectly reclaimable memory accounting for CONFIG_SLOB]
  Link: http://lkml.kernel.org/r/20180312194140.19517-1-guro@fb.com
[guro@fb.com: fix indirectly reclaimable memory accounting]
  Link: http://lkml.kernel.org/r/20180313125701.7955-1-guro@fb.com
Link: http://lkml.kernel.org/r/20180305133743.12746-5-guro@fb.comSigned-off-by: default avatarRoman Gushchin <guro@fb.com>
Reviewed-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Mel Gorman <mgorman@techsingularity.net>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 034ebf65
...@@ -257,11 +257,25 @@ static void __d_free(struct rcu_head *head) ...@@ -257,11 +257,25 @@ static void __d_free(struct rcu_head *head)
kmem_cache_free(dentry_cache, dentry); kmem_cache_free(dentry_cache, dentry);
} }
static void __d_free_external_name(struct rcu_head *head)
{
struct external_name *name = container_of(head, struct external_name,
u.head);
mod_node_page_state(page_pgdat(virt_to_page(name)),
NR_INDIRECTLY_RECLAIMABLE_BYTES,
-ksize(name));
kfree(name);
}
static void __d_free_external(struct rcu_head *head) static void __d_free_external(struct rcu_head *head)
{ {
struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu); struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
kfree(external_name(dentry));
kmem_cache_free(dentry_cache, dentry); __d_free_external_name(&external_name(dentry)->u.head);
kmem_cache_free(dentry_cache, dentry);
} }
static inline int dname_external(const struct dentry *dentry) static inline int dname_external(const struct dentry *dentry)
...@@ -291,7 +305,7 @@ void release_dentry_name_snapshot(struct name_snapshot *name) ...@@ -291,7 +305,7 @@ void release_dentry_name_snapshot(struct name_snapshot *name)
struct external_name *p; struct external_name *p;
p = container_of(name->name, struct external_name, name[0]); p = container_of(name->name, struct external_name, name[0]);
if (unlikely(atomic_dec_and_test(&p->u.count))) if (unlikely(atomic_dec_and_test(&p->u.count)))
kfree_rcu(p, u.head); call_rcu(&p->u.head, __d_free_external_name);
} }
} }
EXPORT_SYMBOL(release_dentry_name_snapshot); EXPORT_SYMBOL(release_dentry_name_snapshot);
...@@ -1617,6 +1631,7 @@ EXPORT_SYMBOL(d_invalidate); ...@@ -1617,6 +1631,7 @@ EXPORT_SYMBOL(d_invalidate);
struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
{ {
struct external_name *ext = NULL;
struct dentry *dentry; struct dentry *dentry;
char *dname; char *dname;
int err; int err;
...@@ -1637,14 +1652,14 @@ struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) ...@@ -1637,14 +1652,14 @@ struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
dname = dentry->d_iname; dname = dentry->d_iname;
} else if (name->len > DNAME_INLINE_LEN-1) { } else if (name->len > DNAME_INLINE_LEN-1) {
size_t size = offsetof(struct external_name, name[1]); size_t size = offsetof(struct external_name, name[1]);
struct external_name *p = kmalloc(size + name->len,
GFP_KERNEL_ACCOUNT); ext = kmalloc(size + name->len, GFP_KERNEL_ACCOUNT);
if (!p) { if (!ext) {
kmem_cache_free(dentry_cache, dentry); kmem_cache_free(dentry_cache, dentry);
return NULL; return NULL;
} }
atomic_set(&p->u.count, 1); atomic_set(&ext->u.count, 1);
dname = p->name; dname = ext->name;
} else { } else {
dname = dentry->d_iname; dname = dentry->d_iname;
} }
...@@ -1683,6 +1698,12 @@ struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) ...@@ -1683,6 +1698,12 @@ struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
} }
} }
if (unlikely(ext)) {
pg_data_t *pgdat = page_pgdat(virt_to_page(ext));
mod_node_page_state(pgdat, NR_INDIRECTLY_RECLAIMABLE_BYTES,
ksize(ext));
}
this_cpu_inc(nr_dentry); this_cpu_inc(nr_dentry);
return dentry; return dentry;
...@@ -2770,7 +2791,7 @@ static void copy_name(struct dentry *dentry, struct dentry *target) ...@@ -2770,7 +2791,7 @@ static void copy_name(struct dentry *dentry, struct dentry *target)
dentry->d_name.hash_len = target->d_name.hash_len; dentry->d_name.hash_len = target->d_name.hash_len;
} }
if (old_name && likely(atomic_dec_and_test(&old_name->u.count))) if (old_name && likely(atomic_dec_and_test(&old_name->u.count)))
kfree_rcu(old_name, u.head); call_rcu(&old_name->u.head, __d_free_external_name);
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment