From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A16EC433F5 for ; Thu, 3 Feb 2022 23:12:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0AB36B0071; Thu, 3 Feb 2022 18:12:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BB2F6B0072; Thu, 3 Feb 2022 18:12:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 881F36B0074; Thu, 3 Feb 2022 18:12:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0107.hostedemail.com [216.40.44.107]) by kanga.kvack.org (Postfix) with ESMTP id 76C036B0071 for ; Thu, 3 Feb 2022 18:12:01 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 21D9682AEF1C for ; Thu, 3 Feb 2022 23:12:01 +0000 (UTC) X-FDA: 79103018442.15.BE4E8F8 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf17.hostedemail.com (Postfix) with ESMTP id B640840002 for ; Thu, 3 Feb 2022 23:12:00 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 37182B835E8; Thu, 3 Feb 2022 23:11:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AAE8BC340E8; Thu, 3 Feb 2022 23:11:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1643929917; bh=y5nr/++kCTv/1eYPXjSUtMA7ZAyuHZosRKaaT4urjdQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=o1k8u9wnw7NWbcegn4kKmVKvXftbtsYgvGcVbdhl90catqBDYZn/HC8M7K7F8Z/81 k6dPihoih1b5Osg73DZIwTlkO3gpPS1Ok3K5RexrrpYv6PGQ7zNfKuHyEAQEK0ZOXA x8ZXeA83ZDcxastoUUmmY0TiJnlEafJ5MEuZOl98= Date: Thu, 3 Feb 2022 15:11:57 -0800 From: Andrew Morton To: Waiman Long Cc: "Kirill A. Shutemov" , Ingo Molnar , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Justin Forbes , Rafael Aquini Subject: Re: [PATCH v2] mm/sparsemem: Fix 'mem_section' will never be NULL gcc 12 warning Message-Id: <20220203151157.659f382c76056d883fa80ec6@linux-foundation.org> In-Reply-To: <20220202003550.698768-1-longman@redhat.com> References: <20220202003550.698768-1-longman@redhat.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B640840002 X-Stat-Signature: yp1ft7u3nr6zx8w9tyeiwhk8pne16wpu X-Rspam-User: nil Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=o1k8u9wn; spf=pass (imf17.hostedemail.com: domain of akpm@linux-foundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-HE-Tag: 1643929920-594665 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, 1 Feb 2022 19:35:50 -0500 Waiman Long wrote: > The gcc 12 compiler reports a "'mem_section' will never be NULL" > warning on the following code: > > static inline struct mem_section *__nr_to_section(unsigned long nr) > { > #ifdef CONFIG_SPARSEMEM_EXTREME > if (!mem_section) > return NULL; > #endif > if (!mem_section[SECTION_NR_TO_ROOT(nr)]) > return NULL; > : > > It happens with both CONFIG_SPARSEMEM_EXTREME on and off. The mem_section > definition is > > #ifdef CONFIG_SPARSEMEM_EXTREME > extern struct mem_section **mem_section; > #else > extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]; > #endif > > In the CONFIG_SPARSEMEM_EXTREME case, mem_section obviously cannot > be NULL, but *mem_section can be if memory hasn't been allocated for > the dynamic mem_section[] array yet. In the !CONFIG_SPARSEMEM_EXTREME > case, mem_section is a static 2-dimensional array and so the check > "!mem_section[SECTION_NR_TO_ROOT(nr)]" doesn't make sense. > > Fix this warning by checking for "!*mem_section" instead of > "!mem_section" and moving the "!mem_section[SECTION_NR_TO_ROOT(nr)]" > check up inside the CONFIG_SPARSEMEM_EXTREME block. > > ... > > --- a/include/linux/mmzone.h > +++ b/include/linux/mmzone.h > @@ -1390,11 +1390,9 @@ static inline unsigned long *section_to_usemap(struct mem_section *ms) > static inline struct mem_section *__nr_to_section(unsigned long nr) > { > #ifdef CONFIG_SPARSEMEM_EXTREME > - if (!mem_section) > + if (!*mem_section || !mem_section[SECTION_NR_TO_ROOT(nr)]) > return NULL; > #endif > - if (!mem_section[SECTION_NR_TO_ROOT(nr)]) > - return NULL; > return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK]; > } > extern size_t mem_section_usage_size(void); What does the v1->v2 change do? --- a/include/linux/mmzone.h~mm-sparsemem-fix-mem_section-will-never-be-null-gcc-12-warning-v2 +++ a/include/linux/mmzone.h @@ -1390,11 +1390,9 @@ static inline unsigned long *section_to_ static inline struct mem_section *__nr_to_section(unsigned long nr) { #ifdef CONFIG_SPARSEMEM_EXTREME - if (!*mem_section) + if (!*mem_section || !mem_section[SECTION_NR_TO_ROOT(nr)]) return NULL; #endif - if (!mem_section[SECTION_NR_TO_ROOT(nr)]) - return NULL; return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK]; } extern size_t mem_section_usage_size(void); _