Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp277418ybg; Tue, 28 Jul 2020 05:59:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6anYc1LvfhI2MVVVK6+yv0vi5Ji4rrF9A1CMhgMj9F8JTBou53L4ic+IZHo7NsMqQZ31u X-Received: by 2002:a17:906:7c86:: with SMTP id w6mr14903841ejo.178.1595941179087; Tue, 28 Jul 2020 05:59:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595941179; cv=none; d=google.com; s=arc-20160816; b=dR64SSjhhRY2/Du+ACyQw+X3c+DEpUQz1Lr02kJc/uUesOsATxs08jYWlbgq9BT/vP jphKkc8wM5dnSbd3xV6DOcmENQ7Iz+jlVeb5t1KF9ne03gwt6qO7WlVu3+Garpldhlxs 3MzoL+GRDt7+yJGr7C2KziOTkn+uP5GenoZbLYFmjN2QeS+ummni7Z9H0RK8AQ0b3lrj NrXrWJ9t0gA/6FJlYkjYqgWi/OS9/5mplkNuo1gq3J2zZ6HPMvhdptmCBx3nvmACB9e7 qt0JsZShvaukQ/9drRRDvgLQE3qmPACHbzyfuyX8QszWn6jSQl5OCSkt6EOQa21rOwwY IWow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=jGv59OL3eBSwcPKc8/h9cDYwkm0Ga9KFVIVK1NEWRJE=; b=aXU7WR0XLFxNMhB04b7MuclLNnR6P0sFj2mqfMK2pBELOVyKZNb1NJu6/grisdGWn8 XIUczRcDkJaKZgYOIfvM9ctfKKj0Fvq3d9/VjrE0Ouqt1cjkSCVutlbiYz8q8kPONMMa kEzvDMduW5A1UnpxRREkP7/+VUFeI7ECnl4Fvjz5rgIr3HXpYRVXJal6CuQIV6VS0b0r vioN/7AD7Xb2pgQu9bHPYfglmVS8BcXP3LYMK2DMlHaeLCQGxFD7IUvhNAM5Gg2GKVir 39P4LgPn6djephUNsCG229iNc+Eg5TlUjMmiBdt+1U9cWTVKVTjvar2pIFIGk24PjDnU 30/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=SNQADGVn; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u10si7162149edo.324.2020.07.28.05.59.15; Tue, 28 Jul 2020 05:59:39 -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; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=SNQADGVn; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729794AbgG1M5T (ORCPT + 99 others); Tue, 28 Jul 2020 08:57:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729780AbgG1M5S (ORCPT ); Tue, 28 Jul 2020 08:57:18 -0400 Received: from mail-pj1-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44EC0C061794 for ; Tue, 28 Jul 2020 05:57:18 -0700 (PDT) Received: by mail-pj1-x1043.google.com with SMTP id a9so11533490pjd.3 for ; Tue, 28 Jul 2020 05:57:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jGv59OL3eBSwcPKc8/h9cDYwkm0Ga9KFVIVK1NEWRJE=; b=SNQADGVnE0GT2O1e70IvNnIdtBVC4WTc8bSWAD5HxNOIq9P1xHSKpegGI6WKHj2wz6 1gz19deudrkxNb9cNbrDAXXSOkqEdWfAquJnQu1/sPK7we2vCeOB9tYCa88Czg/oqkd2 a2+OCvZ/gy2eUX8qxOx0YGJ9RROfHCz+duDtMWGV3xV9/Wk/rs8iyqBe8irr2jETNMl3 K5fNojIW+bkRFjYqCrEUTDq1SHAnJ0ONf972MRoy9gaFBxUcy5pv2sEAp2h6Q82ek7AE Q0B+C2WLgtVFYWLO4+DDDDEqCNpw8XCzvbpDXb5h8d8OVGiB2zQ1yZ0rjpZ1aqeG2+qX Oorw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jGv59OL3eBSwcPKc8/h9cDYwkm0Ga9KFVIVK1NEWRJE=; b=Karz/nIazN31XTQlZ+m0wich4dt82tw8nEo/8o3nH7mQBLiSbrFWX2zSrYFsYrElOv EjANBaqP9JNXLWxF1tbKbeXAV29TBGK326+t884eiTnT2GTqyXH6GUqyMG2Mv5fIdVMJ +PAw5ja1DXqhw3ZROeeX3CVkEXCa0wnI0ICWchtYfatzlZ5LMK/C9WzIOXIx7/FdEmqK xqH6jDFwAEGAJ/MFZUfmJEokRYwjb8zVHadAak9TKW1LD/IZHqv17vxN4j9fE2ANuNJU 4bGS+0TahK0QCV6QqAVFNbr5jo9GDk+nig9zHlPaU05Jd+RzvM8SgJ18zFNJqVJIO8IF 7QPA== X-Gm-Message-State: AOAM530VoNmhpjoRSoZZtv6bt+ph80Ka/gIbOtKt0F6jCuwOcD5YYM+v xmdU5VEjdiTbMPaNPT7xcV1+kReaeR83o1tOflT88w== X-Received: by 2002:a17:902:9b97:: with SMTP id y23mr23964341plp.189.1595941037785; Tue, 28 Jul 2020 05:57:17 -0700 (PDT) MIME-Version: 1.0 References: <20200727134914.312934924@linuxfoundation.org> <20200727134918.205538211@linuxfoundation.org> In-Reply-To: <20200727134918.205538211@linuxfoundation.org> From: Muchun Song Date: Tue, 28 Jul 2020 20:56:41 +0800 Message-ID: Subject: Re: [External] [PATCH 4.19 76/86] mm: memcg/slab: fix memory leak at non-root kmem_cache destroy To: Greg Kroah-Hartman Cc: LKML , stable@vger.kernel.org, Andrew Morton , Shakeel Butt , Roman Gushchin , Vlastimil Babka , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Linus Torvalds Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 27, 2020 at 10:12 PM Greg Kroah-Hartman wrote: > > From: Muchun Song > > commit d38a2b7a9c939e6d7329ab92b96559ccebf7b135 upstream. > > If the kmem_cache refcount is greater than one, we should not mark the > root kmem_cache as dying. If we mark the root kmem_cache dying > incorrectly, the non-root kmem_cache can never be destroyed. It > resulted in memory leak when memcg was destroyed. We can use the > following steps to reproduce. > > 1) Use kmem_cache_create() to create a new kmem_cache named A. > 2) Coincidentally, the kmem_cache A is an alias for kmem_cache B, > so the refcount of B is just increased. > 3) Use kmem_cache_destroy() to destroy the kmem_cache A, just > decrease the B's refcount but mark the B as dying. > 4) Create a new memory cgroup and alloc memory from the kmem_cache > B. It leads to create a non-root kmem_cache for allocating memory. > 5) When destroy the memory cgroup created in the step 4), the > non-root kmem_cache can never be destroyed. > > If we repeat steps 4) and 5), this will cause a lot of memory leak. So > only when refcount reach zero, we mark the root kmem_cache as dying. > > Fixes: 92ee383f6daa ("mm: fix race between kmem_cache destroy, create and deactivate") > Signed-off-by: Muchun Song > Signed-off-by: Andrew Morton > Reviewed-by: Shakeel Butt > Acked-by: Roman Gushchin > Cc: Vlastimil Babka > Cc: Christoph Lameter > Cc: Pekka Enberg > Cc: David Rientjes > Cc: Joonsoo Kim > Cc: Shakeel Butt > Cc: > Link: http://lkml.kernel.org/r/20200716165103.83462-1-songmuchun@bytedance.com > Signed-off-by: Linus Torvalds > Signed-off-by: Greg Kroah-Hartman > > --- > mm/slab_common.c | 35 ++++++++++++++++++++++++++++------- > 1 file changed, 28 insertions(+), 7 deletions(-) > > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -310,6 +310,14 @@ int slab_unmergeable(struct kmem_cache * > if (s->refcount < 0) > return 1; > > +#ifdef CONFIG_MEMCG_KMEM > + /* > + * Skip the dying kmem_cache. > + */ > + if (s->memcg_params.dying) > + return 1; > +#endif > + > return 0; > } > > @@ -832,12 +840,15 @@ static int shutdown_memcg_caches(struct > return 0; > } > > -static void flush_memcg_workqueue(struct kmem_cache *s) > +static void memcg_set_kmem_cache_dying(struct kmem_cache *s) > { > mutex_lock(&slab_mutex); > s->memcg_params.dying = true; > mutex_unlock(&slab_mutex); We should remove mutex_lock/unlock(&slab_mutex) here, because we already hold the slab_mutex from kmem_cache_destroy(). > +} > > +static void flush_memcg_workqueue(struct kmem_cache *s) > +{ > /* > * SLUB deactivates the kmem_caches through call_rcu_sched. Make > * sure all registered rcu callbacks have been invoked. > @@ -858,10 +869,6 @@ static inline int shutdown_memcg_caches( > { > return 0; > } > - > -static inline void flush_memcg_workqueue(struct kmem_cache *s) > -{ > -} > #endif /* CONFIG_MEMCG_KMEM */ > > void slab_kmem_cache_release(struct kmem_cache *s) > @@ -879,8 +886,6 @@ void kmem_cache_destroy(struct kmem_cach > if (unlikely(!s)) > return; > > - flush_memcg_workqueue(s); > - > get_online_cpus(); > get_online_mems(); > > @@ -890,6 +895,22 @@ void kmem_cache_destroy(struct kmem_cach > if (s->refcount) > goto out_unlock; > > +#ifdef CONFIG_MEMCG_KMEM > + memcg_set_kmem_cache_dying(s); > + > + mutex_unlock(&slab_mutex); > + > + put_online_mems(); > + put_online_cpus(); > + > + flush_memcg_workqueue(s); > + > + get_online_cpus(); > + get_online_mems(); > + > + mutex_lock(&slab_mutex); > +#endif > + > err = shutdown_memcg_caches(s); > if (!err) > err = shutdown_cache(s); > > -- Yours, Muchun