Received: by 2002:a19:f614:0:0:0:0:0 with SMTP id x20csp63985lfe; Fri, 15 Apr 2022 19:41:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrHrQcaX8oLgHWcj0fjQ8mqNJXnfxku9zNN5uInaHgUl16jaTo1rYu6/29PoaTS7LKlrQD X-Received: by 2002:a17:902:a610:b0:158:9fdd:332a with SMTP id u16-20020a170902a61000b001589fdd332amr1808895plq.94.1650076901544; Fri, 15 Apr 2022 19:41:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650076901; cv=none; d=google.com; s=arc-20160816; b=p2RQ++zFyuR9/52KMPDG+S59fpnaF3LJ2f1rTxYUDmQKQzjIIlKYRozAO5KqVoxY8u g8z/cx/S1xzdZ1odOZLfSBKjdR2hpKPDRh9FvFB+5y05sdHnFranphHytNNV5oBinEa+ RfTot2S2vZeRNg1eaYEbHr+3yHoLojMjOiWzpPjZPU45PEmDYcoNOcHXAZTcO3Xa7CeX vubIsc1/Shnp47ttD8d8yICyxo1G03+Yt5pqn/KswsrAwtQY/y1u1ZMVFFV0sYwnFxOL INhZOz850y2FglPaC7rYrZFZH3Ql1no1Pd4a5uAgbJ8DpG5Mt9soYxkNKhSgj0LZJMHv JcAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=viTO11sutyCOvC0GzO4vXpICt5o/bhVMk/e9YAsUI2s=; b=LYFOyKxYsIQTLWNM6j8UlHsNB7VrJ4VcmWYyeHF5fCAs6om3OYVlIsJWHoAuNI9jfl 8osANsja4UBbPYpWmSGX8kjJjENZerF+lILb0gTGx6448YU3BjNDhUj0MplMaZqX1mNZ vsKxDENbvZUx4/OFNnXVuCIcv+Spb4am9FBF7QuEZXY69McPBIlM2nac3GupB3g6ZEFa 3QpeXcVQHkjsRNXehuupc95QlB7pKBP9k4ZG1jHErLAgcsL+FAwHmiIbdCSI/gQTYiGc SeEGm1bq8le3dUD8lZJ+HhRtpXr78zXwFL3j1GPYZJYLBnee8iIHsp1YOmfO4T+vIz2N ktGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="z/6SAZch"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id a19-20020a63e853000000b003985223c137si3191285pgk.354.2022.04.15.19.41.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 19:41:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="z/6SAZch"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CF1B5260CEB; Fri, 15 Apr 2022 18:49:26 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348059AbiDNOCK (ORCPT + 99 others); Thu, 14 Apr 2022 10:02:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344559AbiDNNci (ORCPT ); Thu, 14 Apr 2022 09:32:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E22A522298; Thu, 14 Apr 2022 06:30:13 -0700 (PDT) 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 dfw.source.kernel.org (Postfix) with ESMTPS id 7EC98619FC; Thu, 14 Apr 2022 13:30:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B2EDC385A1; Thu, 14 Apr 2022 13:30:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649943012; bh=o2qRZxiJMFrC6xd3ha3Dm1E7Iwm3LXPUpxf9+ofkc68=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=z/6SAZch+Q51gB36wuh5meZarSRmRz0+TgI9TGcjroPEHT0E9pUBNkBz9y2qBmKYd TTkzS8StND4oxzbFc3DyEG1Vatk4qA4kvI7g/mdRl0DN9nLaXdow190lOQe5AhR18t zLEV48Mb1EUdjFsRgYptvfcM8B6zCgadQoG5EWNo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Waiman Long , Justin Forbes , "Kirill A . Shutemov" , Ingo Molnar , Rafael Aquini , Andrew Morton , Linus Torvalds Subject: [PATCH 4.19 329/338] mm/sparsemem: fix mem_section will never be NULL gcc 12 warning Date: Thu, 14 Apr 2022 15:13:52 +0200 Message-Id: <20220414110848.254859660@linuxfoundation.org> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20220414110838.883074566@linuxfoundation.org> References: <20220414110838.883074566@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Waiman Long commit a431dbbc540532b7465eae4fc8b56a85a9fc7d17 upstream. 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 CONFIG_SPARSEMEM_EXTREME 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 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 moving the "!mem_section[SECTION_NR_TO_ROOT(nr)]" check up inside the CONFIG_SPARSEMEM_EXTREME block and adding an explicit NR_SECTION_ROOTS check to make sure that there is no out-of-bound array access. Link: https://lkml.kernel.org/r/20220331180246.2746210-1-longman@redhat.com Fixes: 3e347261a80b ("sparsemem extreme implementation") Signed-off-by: Waiman Long Reported-by: Justin Forbes Cc: "Kirill A . Shutemov" Cc: Ingo Molnar Cc: Rafael Aquini Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- include/linux/mmzone.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -1155,13 +1155,16 @@ extern struct mem_section mem_section[NR static inline struct mem_section *__nr_to_section(unsigned long nr) { + unsigned long root = SECTION_NR_TO_ROOT(nr); + + if (unlikely(root >= NR_SECTION_ROOTS)) + return NULL; + #ifdef CONFIG_SPARSEMEM_EXTREME - if (!mem_section) + if (!mem_section || !mem_section[root]) 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]; + return &mem_section[root][nr & SECTION_ROOT_MASK]; } extern int __section_nr(struct mem_section* ms); extern unsigned long usemap_size(void);