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 EE31BC433F5 for ; Fri, 4 Feb 2022 03:29:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E68FD6B0072; Thu, 3 Feb 2022 22:29:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E17FA6B0073; Thu, 3 Feb 2022 22:29:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB7F36B0074; Thu, 3 Feb 2022 22:29:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0092.hostedemail.com [216.40.44.92]) by kanga.kvack.org (Postfix) with ESMTP id BA0816B0072 for ; Thu, 3 Feb 2022 22:29:37 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 73E81998FA for ; Fri, 4 Feb 2022 03:29:37 +0000 (UTC) X-FDA: 79103667594.18.DF3169A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf31.hostedemail.com (Postfix) with ESMTP id F293920002 for ; Fri, 4 Feb 2022 03:29:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643945376; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XY2ChDMo+/yGwR1vFiG3NhPQOgcec0xW5lNtn3nVMtM=; b=fLPRvLwo5UkhsTTOkvJyCxVRJNcr4gb65QAdMhEb9hRxf1iVCH/uukOnVwLJbSDiXjk/xi q9xSAB++YkCatIZm51Rsje4BRGFy0SCVNzqCKP/TzdcXKqcKt7w0NdEQCsBtvc/Q8CmkAx SlCDN6jAcGX2BWhq/Dkc84jhsb4BV8Y= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-515-mR7nj8ecM52g-SleUjP3Xw-1; Thu, 03 Feb 2022 22:29:31 -0500 X-MC-Unique: mR7nj8ecM52g-SleUjP3Xw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9A41A814246; Fri, 4 Feb 2022 03:29:29 +0000 (UTC) Received: from [10.22.32.130] (unknown [10.22.32.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3E1475D6BA; Fri, 4 Feb 2022 03:29:24 +0000 (UTC) Message-ID: <9f5a1c2a-b0ce-8ebf-b811-5f27be4439f3@redhat.com> Date: Thu, 3 Feb 2022 22:29:23 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [PATCH v2] mm/sparsemem: Fix 'mem_section' will never be NULL gcc 12 warning Content-Language: en-US To: Andrew Morton Cc: "Kirill A. Shutemov" , Ingo Molnar , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Justin Forbes , Rafael Aquini References: <20220202003550.698768-1-longman@redhat.com> <20220203151157.659f382c76056d883fa80ec6@linux-foundation.org> From: Waiman Long In-Reply-To: <20220203151157.659f382c76056d883fa80ec6@linux-foundation.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: F293920002 X-Stat-Signature: aiwoatmgk3ydkdi36ruifny7651nefx6 Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fLPRvLwo; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf31.hostedemail.com: domain of longman@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=longman@redhat.com X-Rspam-User: nil X-HE-Tag: 1643945376-904690 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 2/3/22 18:11, Andrew Morton wrote: > 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); > _ When !CONFIG_SPARSEMEM_EXTREME, mem_section is really a static 2-D array. Since it is not a table of pointers, mem_section[SECTION_NR_TO_ROOT(nr)] has no real meaning. That is why the compiler is complaining. This check isn't applicable in the !CONFIG_SPARSEMEM_EXTREME case, but it is meaningful for CONFIG_SPARSEMEM_EXTREME. That is why it is pulled into the CONFIG_SPARSEMEM_EXTREME block. Thanks, Longman