Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1515313pxy; Mon, 2 Aug 2021 03:44:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzw/jE+ifYTTq4Vbbzx0OtLoWCdBxxGoHDDhtyyrAB3kNXRovFb0Us4gjvXdqT8glFtxAcO X-Received: by 2002:a92:d8cf:: with SMTP id l15mr519746ilo.227.1627901086549; Mon, 02 Aug 2021 03:44:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627901086; cv=none; d=google.com; s=arc-20160816; b=Ctgl1YqD0x5Rs65PXABa+wbvZM3ZgbjTkjPTfe/kUV0Gy23BSWexdyvwIErQGpcri1 By8L9KSERCNFloi7ceq7xxLVuKpchY6O3eov5VgD4EIEFYjCAHV2HGRkxvIcv8SJerFk 3zgA1DuSxQWlLgg91I1KDNvwaY/JjuVAxmpeddSjcPCn7KAWtXppebS6JbmXqwEZI6ES l3UwLJaUnTN3DacDo4iVUfwWKgqiemt/xsDIM3GD7FcnRuWlHCuxnlE1LMmxnCAYeZ2x qK2d0/nZZFPYPCySLDdlrhnLY4YEucJVTTtXAvFHsy/CU+lffKOWN4t+5y5OWc1U4lAv O9IA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=VjdOHRLy4OhFB/nI1Wg/RzSMrcrEU18YM9TrBL5loyk=; b=KqdayfPCGwrsc/gbx80rqtYU0Fmt5Owl2Vtzk69TTTpYmk7+4ACTvoKv5qdD1nguW3 YMZwdYdG9c5CfF2SMPbXEyvXOW80bDEq7VvgCybPWoXFUqHw1Ghtq5znmy5EgQcJl3M5 HV4Fl/hofAMA8hlA3A7kpxmXSVnoPx1ZqAr29QLddLUwwthQn9clQeUZu1VLMyBF+lLP +CSLl1nA7ZSbh1i1hRyvYzDP5ZBAw3DmZjb7kedQ4+ZsYVeyEPacCmeyDuqan1de3dP2 K1LU/1nsC0qjUAfTjBOtL3uWswsv8mvyQR36yB3BR+UkCckhiU8hXGVIlGMiyRkWDQ8O o31A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=uXnGYFv+; 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=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q4si11755138ils.118.2021.08.02.03.44.34; Mon, 02 Aug 2021 03:44:46 -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=@suse.com header.s=susede1 header.b=uXnGYFv+; 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=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233322AbhHBKmx (ORCPT + 99 others); Mon, 2 Aug 2021 06:42:53 -0400 Received: from smtp-out1.suse.de ([195.135.220.28]:57124 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233081AbhHBKmw (ORCPT ); Mon, 2 Aug 2021 06:42:52 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 497A821ED8; Mon, 2 Aug 2021 10:42:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1627900962; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=VjdOHRLy4OhFB/nI1Wg/RzSMrcrEU18YM9TrBL5loyk=; b=uXnGYFv+I4AyENCnJs+dpJ3e4RdX5jc6mcG8FDSMNdW/wQTEBxVUxaa1TSn2wMGP704vHN zTPIhA4mbq3bXmdtbT3kBDE1E1OQpI1WpF7auNV5Cc2yXdBvJekxBhuL3lgFSlxZPQKyKk bW2lha1tGQe28RcD4/okxagmJjVzAjQ= Received: from suse.cz (unknown [10.100.201.86]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id BC0AFA3BAE; Mon, 2 Aug 2021 10:42:41 +0000 (UTC) Date: Mon, 2 Aug 2021 12:42:41 +0200 From: Michal Hocko To: Miaohe Lin Cc: Roman Gushchin , hannes@cmpxchg.org, vdavydov.dev@gmail.com, akpm@linux-foundation.org, shakeelb@google.com, willy@infradead.org, alexs@kernel.org, richard.weiyang@gmail.com, songmuchun@bytedance.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org Subject: Re: [PATCH 4/5] mm, memcg: avoid possible NULL pointer dereferencing in mem_cgroup_init() Message-ID: References: <20210729125755.16871-1-linmiaohe@huawei.com> <20210729125755.16871-5-linmiaohe@huawei.com> <47daf062-f510-edb3-6ec7-f8e7615ad8a0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <47daf062-f510-edb3-6ec7-f8e7615ad8a0@huawei.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon 02-08-21 18:00:10, Miaohe Lin wrote: > On 2021/8/2 14:43, Michal Hocko wrote: > > On Sat 31-07-21 10:05:51, Miaohe Lin wrote: > >> On 2021/7/30 14:44, Michal Hocko wrote: > >>> On Thu 29-07-21 20:12:43, Roman Gushchin wrote: > >>>> On Thu, Jul 29, 2021 at 08:57:54PM +0800, Miaohe Lin wrote: > >>>>> rtpn might be NULL in very rare case. We have better to check it before > >>>>> dereferencing it. Since memcg can live with NULL rb_tree_per_node in > >>>>> soft_limit_tree, warn this case and continue. > >>>>> > >>>>> Signed-off-by: Miaohe Lin > >>>>> --- > >>>>> mm/memcontrol.c | 2 ++ > >>>>> 1 file changed, 2 insertions(+) > >>>>> > >>>>> diff --git a/mm/memcontrol.c b/mm/memcontrol.c > >>>>> index 5b4592d1e0f2..70a32174e7c4 100644 > >>>>> --- a/mm/memcontrol.c > >>>>> +++ b/mm/memcontrol.c > >>>>> @@ -7109,6 +7109,8 @@ static int __init mem_cgroup_init(void) > >>>>> rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, > >>>>> node_online(node) ? node : NUMA_NO_NODE); > >>>>> > >>>>> + if (WARN_ON_ONCE(!rtpn)) > >>>>> + continue; > >>>> > >>>> I also really doubt that it makes any sense to continue in this case. > >>>> If this allocations fails (at the very beginning of the system's life, it's an __init function), > >>>> something is terribly wrong and panic'ing on a NULL-pointer dereference sounds like > >>>> a perfect choice. > >>> > >>> Moreover this is 24B allocation during early boot. Kernel will OOM and > >>> panic when not being able to find any victim. I do not think we need to > >> > >> Agree with you. But IMO it may not be a good idea to leave the rtpn without NULL check. We should defend > >> it though it could hardly happen. But I'm not insist on this check. I will drop this patch if you insist. > > > > It is not that I would insist. I just do not see any point in the code > > churn. This check is not going to ever trigger and there is nothing you > > can do to recover anyway so crashing the kernel is likely the only > > choice left. > > > > I hope I get the point now. What you mean is nothing we can do to recover and panic'ing on a > NULL-pointer dereference is a perfect choice ? Should we declare that we leave the rtpn without > NULL check on purpose like below ? > > Many thanks. > > @@ -7109,8 +7109,12 @@ static int __init mem_cgroup_init(void) > rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, > node_online(node) ? node : NUMA_NO_NODE); > > - if (WARN_ON_ONCE(!rtpn)) > - continue; > + /* > + * If this allocation fails (at the very beginning of the > + * system's life, it's an __init function), something is > + * terribly wrong and panic'ing on a NULL-pointer > + * dereference sounds like a perfect choice. > + */ I am not really sure this is really worth it. Really we do not really want to have similar comments all over the early init code, do we? > rtpn->rb_root = RB_ROOT; > rtpn->rb_rightmost = NULL; > spin_lock_init(&rtpn->lock); -- Michal Hocko SUSE Labs