From: Baolin Wang <baolin.wang@linux.alibaba.com>
To: Usama Arif <usama.arif@linux.dev>,
Andrew Morton <akpm@linux-foundation.org>,
david@fromorbit.com, dgc@kernel.org, qi.zheng@linux.dev,
brauner@kernel.org, cgroups@vger.kernel.org, clm@fb.com,
dsterba@suse.com, hannes@cmpxchg.org, hughd@google.com,
jack@suse.cz, linux-btrfs@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, mhocko@kernel.org, muchun.song@linux.dev,
roman.gushchin@linux.dev, shakeel.butt@linux.dev,
Al Viro <viro@zeniv.linux.org.uk>,
kernel-team@meta.com
Subject: Re: [PATCH v2] fs: push nr_cached_objects memcg gating into individual filesystems
Date: Thu, 16 Jul 2026 09:20:02 +0800 [thread overview]
Message-ID: <09771ca6-2869-4028-98a8-5d6614b48301@linux.alibaba.com> (raw)
In-Reply-To: <20260715103516.2410175-1-usama.arif@linux.dev>
On 7/15/26 6:35 PM, Usama Arif wrote:
> Commit 0baad6f9b997 ("fs/super: skip non-memcg-aware nr_cached_objects
> in memcg slab shrink") added a check in fs/super.c that skipped every
> ->nr_cached_objects() hook whenever the shrinker was invoked for a
> non-root memcg, on the assumption that none of them honour sc->memcg.
>
> That assumption is wrong for XFS, whose inode-reclaim hook is
> intentionally driven from per-memcg contexts to free memcg-charged
> slab. Encoding a blanket "never memcg-aware" policy in fs/super.c
> short-circuits that path.
>
> Push the check down into the callbacks whose counters really are
> irrelevant to per-memcg reclaim - btrfs_nr_cached_objects() and
> shmem_unused_huge_count() - and drop the fs/super.c gate. Each
> filesystem can now lift the restriction independently if its counter
> later grows memcg awareness, without touching fs/super.c.
>
> Introduce mem_cgroup_shrink_is_root() in <linux/memcontrol.h> so the
> callbacks don't open-code "sc->memcg is NULL or root".
>
> Fixes: 0baad6f9b997 ("fs/super: skip non-memcg-aware nr_cached_objects in memcg slab shrink")
> Acked-by: Qi Zheng <qi.zheng@linux.dev>
> Reviewed-by: Jan Kara <jack@suse.cz>
> Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
[snip]
> diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> index e1f46a0016fc..5407e4200460 100644
> --- a/include/linux/memcontrol.h
> +++ b/include/linux/memcontrol.h
> @@ -520,6 +520,22 @@ static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
> return (memcg == root_mem_cgroup);
> }
>
> +/**
> + * mem_cgroup_shrink_is_root - is this a global or root-memcg shrink invocation?
> + * @sc: shrink_control describing the current shrinker call
> + *
> + * Returns true when @sc represents a global reclaim shrink (sc->memcg == NULL)
> + * or a root-memcg shrink, i.e. not a per-memcg iteration of
> + * shrink_slab_memcg(). Filesystems whose ->nr_cached_objects()/
> + * ->free_cached_objects() implementations operate on filesystem-global state
> + * and do not honour sc->memcg can use this to early-return 0 in per-memcg
> + * contexts.
> + */
> +static inline bool mem_cgroup_shrink_is_root(struct shrink_control *sc)
> +{
> + return !sc->memcg || mem_cgroup_is_root(sc->memcg);
> +}
> +
> static inline bool obj_cgroup_is_root(const struct obj_cgroup *objcg)
> {
> return objcg->is_root;
> @@ -1071,6 +1087,11 @@ static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
> return true;
> }
>
> +static inline bool mem_cgroup_shrink_is_root(struct shrink_control *sc)
> +{
> + return true;
> +}
> +
> static inline bool obj_cgroup_is_root(const struct obj_cgroup *objcg)
> {
> return true;
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 5789a0f5a346..dc8cd4f563f4 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -846,6 +846,16 @@ static long shmem_unused_huge_count(struct super_block *sb,
> struct shrink_control *sc)
> {
> struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
> +
> + /*
> + * The per-superblock shrinklist is filesystem-global and does not
> + * honour sc->memcg, so it is only meaningful on the global (kswapd or
> + * root direct reclaim) shrink path. Skip the per-memcg iterations of
> + * shrink_slab_memcg() to avoid queueing duplicate global work.
> + */
> + if (!mem_cgroup_shrink_is_root(sc))
> + return 0;
> +
> return READ_ONCE(sbinfo->shrinklist_len);
> }
> #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
For shmem part, LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
next prev parent reply other threads:[~2026-07-16 1:20 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-15 10:35 [PATCH v2] fs: push nr_cached_objects memcg gating into individual filesystems Usama Arif
2026-07-16 1:20 ` Baolin Wang [this message]
2026-07-20 10:42 ` David Sterba
2026-07-30 8:58 ` Qi Zheng
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=09771ca6-2869-4028-98a8-5d6614b48301@linux.alibaba.com \
--to=baolin.wang@linux.alibaba.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=clm@fb.com \
--cc=david@fromorbit.com \
--cc=dgc@kernel.org \
--cc=dsterba@suse.com \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=kernel-team@meta.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=qi.zheng@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=usama.arif@linux.dev \
--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