From: Vlastimil Babka <vbabka@suse.cz>
To: Waiman Long <longman@redhat.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Vladimir Davydov <vdavydov.dev@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
Christoph Lameter <cl@linux.com>,
Pekka Enberg <penberg@kernel.org>,
David Rientjes <rientjes@google.com>,
Joonsoo Kim <iamjoonsoo.kim@lge.com>,
Roman Gushchin <guro@fb.com>, Shakeel Butt <shakeelb@google.com>
Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org,
linux-mm@kvack.org
Subject: Re: [PATCH 2/2] mm: memcg/slab: Don't create unfreeable slab
Date: Mon, 3 May 2021 14:22:26 +0200 [thread overview]
Message-ID: <699e5ac8-9044-d664-f73f-778fe72fd09b@suse.cz> (raw)
In-Reply-To: <20210502180755.445-2-longman@redhat.com>
On 5/2/21 8:07 PM, Waiman Long wrote:
> The obj_cgroup array (memcg_data) embedded in the page structure is
> allocated at the first instance an accounted memory allocation happens.
> With the right size object, it is possible that the allocated obj_cgroup
> array comes from the same slab that requires memory accounting. If this
> happens, the slab will never become empty again as there is at least one
> object left (the obj_cgroup array) in the slab.
>
> With instructmentation code added to detect this situation, I got 76
> hits on the kmalloc-192 slab when booting up a test kernel on a VM.
> So this can really happen.
>
> To avoid the creation of these unfreeable slabs, a check is added to
> memcg_alloc_page_obj_cgroups() to detect that and double the size
> of the array in case it happens to make sure that it comes from a
> different kmemcache.
>
> This change, however, does not completely eliminate the presence
> of unfreeable slabs which can still happen if a circular obj_cgroup
> array dependency is formed.
Hm this looks like only a half fix then.
I'm afraid the proper fix is for kmemcg to create own set of caches for the
arrays. It would also solve the recursive kfree() issue.
> Fixes: 286e04b8ed7a ("mm: memcg/slab: allocate obj_cgroups for non-root slab pages")
> Signed-off-by: Waiman Long <longman@redhat.com>
> ---
> mm/memcontrol.c | 14 +++++++++++++-
> 1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index b0695d3aa530..44852ac048c3 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2876,12 +2876,24 @@ int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
> */
> objects = max(objs_per_slab_page(s, page),
> (int)(sizeof(struct rcu_head)/sizeof(void *)));
> -
> +retry:
> vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp,
> page_to_nid(page));
> if (!vec)
> return -ENOMEM;
>
> + /*
> + * The allocated vector should not come from the same slab.
> + * Otherwise, this slab will never become empty. Double the size
> + * in this case to make sure that the vector comes from a different
> + * kmemcache.
> + */
> + if (unlikely(virt_to_head_page(vec) == page)) {
> + kfree(vec);
> + objects *= 2;
> + goto retry;
> + }
> +
> memcg_data = (unsigned long) vec | MEMCG_DATA_OBJCGS;
> if (new_page) {
> /*
>
next prev parent reply other threads:[~2021-05-03 12:22 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-02 18:07 [PATCH 1/2] mm: memcg/slab: Prevent recursive kfree() loop Waiman Long
2021-05-02 18:07 ` [PATCH 2/2] mm: memcg/slab: Don't create unfreeable slab Waiman Long
2021-05-03 12:22 ` Vlastimil Babka [this message]
2021-05-03 14:20 ` Waiman Long
2021-05-03 15:32 ` Vlastimil Babka
2021-05-03 16:24 ` Shakeel Butt
2021-05-03 17:21 ` Waiman Long
2021-05-03 20:15 ` Waiman Long
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=699e5ac8-9044-d664-f73f-778fe72fd09b@suse.cz \
--to=vbabka@suse.cz \
--cc=akpm@linux-foundation.org \
--cc=cgroups@vger.kernel.org \
--cc=cl@linux.com \
--cc=guro@fb.com \
--cc=hannes@cmpxchg.org \
--cc=iamjoonsoo.kim@lge.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=longman@redhat.com \
--cc=mhocko@kernel.org \
--cc=penberg@kernel.org \
--cc=rientjes@google.com \
--cc=shakeelb@google.com \
--cc=vdavydov.dev@gmail.com \
/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