Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp2726110pxy; Mon, 3 May 2021 06:45:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZt/HGEa314U+bGOdpz3pWm2zPO6Cri4gHRv70+XrGY838NqdEliJiJruON13hOBN1R2iO X-Received: by 2002:a17:906:2bd3:: with SMTP id n19mr200991ejg.210.1620049527911; Mon, 03 May 2021 06:45:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620049527; cv=none; d=google.com; s=arc-20160816; b=yPlmZ6LVKavvwIO779JZAk423osnwZqLwW+VjO5s1LxgbKYOlX7RP9Dg5VcuCcwvFz rXBlx6T7pBvVgMQRYY1o6N7xXqOq57YBmi3PPYq78eWjMxuOm13tjrANuytNaWP+GqbN 6fOYit2C5564gEFK8nr/SxgteLEG134G1DhMFwLjj9tYq2peUoaGdVutYwaR8g8JoJWz xOGCnZTwZLtMRpCcgnpAqijq8/ODSUNwrcpB1J2qAtBgcsO0ayjNc21jcR8rCRLXZsXn hQHv63J2gA9lAzG8G6OVN5wcU5OGfK3V5+6Q3Bg7UYH8ennt0LVIh1QOh2DwNBuRpimi zJoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:subject:from :references:cc:to; bh=PO60IJAUtXvBTDa9rndGiCLnvxo4G94x9Fs7TgbdAms=; b=AG1ADGzMKt1Ivez2cayrAPDSAlTI1brRFDI1GMBluClQjartu+YVDQMY05iJ5PN05j cYVgvlQpYeJnlhQa+sepCpYR9Ab95mSxvALnjbhnON6WYIQ392y1J5jqZUo40CUp0LgB ZcGXYxxctsaCWlaP10RtfFVPuzcc5De/fdSDDIa0+umSrd5T76dpXSG0LAgHbqqdHnGw XVF8QTUkdwjb4DkrdRjdRMN5fbvl/5qF7P2qR5FR5gLvpSYxq1fIOXmVuR+089YBXhCL FN/pWoZbYekLKvMG0BgsSGpGo+4ECgXFNDi/j+X5IDM61Sq7o2mWSnI2SCFuBIDyofRv KVDA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w3si8833039edr.107.2021.05.03.06.45.04; Mon, 03 May 2021 06:45:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233406AbhECMXZ (ORCPT + 99 others); Mon, 3 May 2021 08:23:25 -0400 Received: from mx2.suse.de ([195.135.220.15]:42624 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230414AbhECMXY (ORCPT ); Mon, 3 May 2021 08:23:24 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id A8C04B172; Mon, 3 May 2021 12:22:27 +0000 (UTC) To: Waiman Long , Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Shakeel Butt Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org References: <20210502180755.445-1-longman@redhat.com> <20210502180755.445-2-longman@redhat.com> From: Vlastimil Babka Subject: Re: [PATCH 2/2] mm: memcg/slab: Don't create unfreeable slab Message-ID: <699e5ac8-9044-d664-f73f-778fe72fd09b@suse.cz> Date: Mon, 3 May 2021 14:22:26 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <20210502180755.445-2-longman@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > --- > 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) { > /* >