Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2952824pxb; Mon, 18 Apr 2022 11:50:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzCO73s1PF1Sr3cbHH5/A9r6uRIxyxCPVSetKilFRp/ub6kPubh9K+2BnKrX4i1HY3cMcx X-Received: by 2002:a63:c49:0:b0:3aa:1ad6:2c9f with SMTP id 9-20020a630c49000000b003aa1ad62c9fmr2453607pgm.360.1650307820689; Mon, 18 Apr 2022 11:50:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650307820; cv=none; d=google.com; s=arc-20160816; b=PJAAZrJOGLNYJY9eEJmGopVDz455vRkYAH6jFXSO7F6l3nBamZEwG2V5J/3HfndBjH bJglR8Q6BCL2m9IE51zDgs1YmB9pvwYbSAnqHK5RlUbX3nExsjqY7m7EzkWQs/GwMOZ0 GtBlraGIFlGtVdnHHDeuOkp61qYrO9z+23G97alu6wHG5RMh7G+djXw/AF3cPE3UMfDx 4r8Qz8KozQ/gHwvzg4GyIaoAhjU/7NWUqDQXQ5x6rO1hfDVKDvOYNX6iXlTdhzKsMtvo hpXTjOYhk2Pqyt+DiB0fpQuJ9R1ZuzID5Jn5Bqo0+s8jK2VB5eQ6caeQmBP7n1PRyslx /T2A== 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=TbmMTW7jL2WzrkOFv+fFIxhf7sY0RNvxV61dgQy1CiE=; b=XlBc6Ejy7otdhxAUg9umokmc10IPaPkn3D7dZQGZPVDmQgKgLafl/Fz8GwiGW9Eoc+ mxxid9gE4AEEkpZMNEALrnRAaOX1By6R6qcmQhXrdhj5raUMlSDPTLDAQGa2ipareRH+ tDVz+n3hdFkM8yq8J7qQjs5GxaV7xH8HxGdPyUob+699CqDktRRcb9gu6WpQz2G4nOd7 AW1dga3FhUi9gEJL2A/iusxyDMG4BfV7HFpYXDnPvMxFUEJg3FdwxLSvlZgGkn3PsCzq FJ3digbDOTYFErPeFkPNWCyrrUTBLZPvKC5HFd8JHQz71cMTuhNx2Yrip0fsFVWST1bh sMlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=g1CICoKl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k5-20020a170902c40500b00153b2d16636si10932665plk.574.2022.04.18.11.50.04; Mon, 18 Apr 2022 11:50:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=g1CICoKl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244240AbiDROFq (ORCPT + 99 others); Mon, 18 Apr 2022 10:05:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244926AbiDRNsh (ORCPT ); Mon, 18 Apr 2022 09:48:37 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 764CE338B8; Mon, 18 Apr 2022 06:01:17 -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 D831B603E0; Mon, 18 Apr 2022 13:01:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EBCE1C385A7; Mon, 18 Apr 2022 13:01:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286876; bh=m2wwDRKEUb1FSsfsa8zID9Em893Bo+8xBvdGNWAui70=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g1CICoKlpggb6chhetbOUQxXPyCO1P3PTX5P0kzSYKBf+suvVik4iyHvbaAb/a2la iKTm9czw5MWbwIbG85RyHxX5/hNTLlUCDf3Wnyh1CLv2aYlPJ7ZBtqOibVb6WV4IXI 4VBlGEG6VxvCR356uUQlsmG1eZVKYhyqEUaKg34Q= 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.14 256/284] mm/sparsemem: fix mem_section will never be NULL gcc 12 warning Date: Mon, 18 Apr 2022 14:13:57 +0200 Message-Id: <20220418121219.686110669@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@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=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 @@ -1160,13 +1160,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);