Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp322929rwe; Wed, 24 Aug 2022 01:21:31 -0700 (PDT) X-Google-Smtp-Source: AA6agR6e20AQv9/0zKsDgss36vKfpY29w1C8izy0FvB3KStVpOTq5NWTGKm6wO3IT+54zMrqpDdq X-Received: by 2002:a17:906:e9b:b0:730:a6a1:9fc9 with SMTP id p27-20020a1709060e9b00b00730a6a19fc9mr2142414ejf.601.1661329291654; Wed, 24 Aug 2022 01:21:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661329291; cv=none; d=google.com; s=arc-20160816; b=IOc73wIXZ/43RATaUESwTABFyBOlCTu1+zLzx/JzBFwZ6x5LHDEtVivi/D3mV2qJyW ATT6z2v/iQiTiExqNrJsGKHchUfgiFyJh/LMfCi8SY1zTZI9SRDgrmpAzIaGXopZgI6B 8+ML1Vfoly9847XF+ykyh58ppscZSkJb+3qUYccNA2DhlZBPC7f7wRnp+48aHRKIm55R m3Iy9zphnJZrCRYPKEncScdssS2iMjGS5GxLr6MKRsYkFlQNZtDGcG7BXZbcTvJV5kuq JZ40T0heTV1wJniUu1yeQ2ac3tdxfI+1QUCUk6/iN+4xpXZ2EUmAw2hi6ZgEpOn8ZTuh yogg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=9/eyKaPfuteIwHEnAMNI1rBDayjT1OLnm5QHb0/XnpM=; b=i8yzqDtxF7rT5tq2GCpJCEb505ATuxbk/Rc9pfUCNUl68SUSjd7MxClmWAJBe+NRxw olj+rsvKE9qBw50Peqx1clC7A1cLsDP1gyx3SkI4ghNF/YQBINN15BRgMyXx7LXdp4pT VKUXqo6Ae8rTdAoZrEmwVdRBs6X3jaPq2oM1Tr195OLEOI5z1wDZKBgaAfA62uUy95PF dTPV+3fq0cjooiP/W2Od2d6ej2hYi8u/98XA9CVOdVkE9BJhSVA3m3kFyr1YMLYpss4b c0Zo5nIfCSVV2etUErHI86CmYbRkZFoddyoNioA3CzWTBiNf/kKB6AkvArRFIv4e265W ox1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=cPxFHkHR; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ne31-20020a1709077b9f00b0073d62f62c9csi1887204ejc.217.2022.08.24.01.21.05; Wed, 24 Aug 2022 01:21:31 -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=@gmail.com header.s=20210112 header.b=cPxFHkHR; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235092AbiHXIK2 (ORCPT + 99 others); Wed, 24 Aug 2022 04:10:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235679AbiHXIK0 (ORCPT ); Wed, 24 Aug 2022 04:10:26 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D33E857C7 for ; Wed, 24 Aug 2022 01:10:24 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id bf22so16296735pjb.4 for ; Wed, 24 Aug 2022 01:10:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc; bh=9/eyKaPfuteIwHEnAMNI1rBDayjT1OLnm5QHb0/XnpM=; b=cPxFHkHRNoi1QBAXIZO2yurpDn3w+3l34l7pBPurq8rCROhaUjmamifjcijzDGua/m YRIcg6wAzqTPTsp320ufVQc0Kb8NnQtL8yx25moUm5SswdFzVU9kYjWoc5jiCIF3uM2r 3DyVOi1J8LAaxEvuVcahNa7DE7YdictCKevdI2TcRCvNDOUrIbX47U+KCKlVtPTWh9X6 F6N3hXERQBk9dAvoiTRiFLR02mSYLOKIvrv9rnQst77WFHPvwKGYFm7utnSPnackgNwb LmYnZiPxuBIGZVPiSGBTcZINkXsugVVl2OktUjldWgZtHgmsxdOzfAyVEOkEYQRcr6GR ZcqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc; bh=9/eyKaPfuteIwHEnAMNI1rBDayjT1OLnm5QHb0/XnpM=; b=SIG0cpZPpxljk1OhdxYk9iecKv1N5JJ2bEB/qVVfRyR3Ngi1f2Gpf7ASAYvrg8nSXs GwwJrxzpBz56Wsknu4zFQoO/MUCumIDXuJuAXzbAbanFKqzjKYWx4zmoyfNkRxyZRqEd WtcuPEGwNCsgNt8N1bq+cP6GjQ79ClmuxLd5QSSxu5yQ6gngg9HsJaNO2VZNtVOPhkZZ lEJ2AyIj0lR2a3FAxJo1w0wCk5hYOpyhRK7Ia+aBJvclsyx1hTDjJETogTZWjFPKcBHg YjoWL40OCLHJ/SgkjNuR89TYYgMcH/a0/T25SNRpKJGmpNS8u4NXqIPf0nP9XmXZccxe yJcQ== X-Gm-Message-State: ACgBeo1WjbyRuq9J9qHEuN83QeRJqoCs6KuZBPveMnK0QxjEaNg1Ct1I yc7sCwkxuYykQHMw5H6bs0TqWPkg0XHquDn1oW4= X-Received: by 2002:a17:90b:4a05:b0:1f5:62d5:4155 with SMTP id kk5-20020a17090b4a0500b001f562d54155mr7417749pjb.6.1661328623952; Wed, 24 Aug 2022 01:10:23 -0700 (PDT) MIME-Version: 1.0 References: <20220824055637.10676-1-zhouzhouyi@gmail.com> <8d8bfc81-b584-20f4-65ae-d626f019cdf4@csgroup.eu> <66f0770d-83ee-34cb-2dea-b8d957613c7f@csgroup.eu> In-Reply-To: <66f0770d-83ee-34cb-2dea-b8d957613c7f@csgroup.eu> From: Zhouyi Zhou Date: Wed, 24 Aug 2022 16:10:12 +0800 Message-ID: Subject: Re: [PATCH linux-next] mm: fix used but uninitialized variable To: Christophe Leroy Cc: "akpm@linux-foundation.org" , "nathan@kernel.org" , "ndesaulniers@google.com" , "trix@redhat.com" , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , "llvm@lists.linux.dev" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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 On Wed, Aug 24, 2022 at 2:49 PM Christophe Leroy wrote: > > > > Le 24/08/2022 =C3=A0 08:41, Zhouyi Zhou a =C3=A9crit : > > Thank Christophe for reviewing my patch > > > > On Wed, Aug 24, 2022 at 2:10 PM Christophe Leroy > > wrote: > >> > >> > >> > >> Le 24/08/2022 =C3=A0 07:56, Zhouyi Zhou a =C3=A9crit : > >>> In function walk_hugetlb_range, the local variable err may > >>> be used uninitialzed when: > >>> ops->pte_hole in side of "else if (ops->pte_hole)" is false. > >>> > >>> Signed-off-by: Zhouyi Zhou > >>> --- > >>> Dear mm Developers: > >>> > >>> When I build kernel using "make CC=3Dclang-14" > >>> the compiler complains following: > >>> > >>> CC mm/pagewalk.o > >>> mm/pagewalk.c:318:12: error: variable 'err' is used uninitialized whe= never 'if' condition is false [-Werror,-Wsometimes-uninitialized] > >>> else if (ops->pte_hole) > >>> ^~~~~~~~~~~~~ > >>> mm/pagewalk.c:321:7: note: uninitialized use occurs here > >>> if (err) > >>> ^~~ > >>> mm/pagewalk.c:318:8: note: remove the 'if' if its condition is always= true > >>> else if (ops->pte_hole) > >>> ^~~~~~~~~~~~~~~~~~ > >>> mm/pagewalk.c:311:10: note: initialize the variable 'err' to silence = this warning > >>> int err; > >>> ^ > >>> =3D 0 > >>> 1 error generated. > >>> make[1]: *** [scripts/Makefile.build:250: mm/pagewalk.o] Error 1 > >>> make: *** [Makefile:2006: mm] Error 2 > >>> > >>> I initialize that variable outside of the for loop because we can ass= ign 0 to err > >>> only once in this function. > >>> > >>> After my fix, I can compile the kernel. > >>> > >>> Many Thanks > >>> Zhouyi > >>> -- > >>> mm/pagewalk.c | 2 +- > >>> 1 file changed, 1 insertion(+), 1 deletion(-) > >>> > >>> diff --git a/mm/pagewalk.c b/mm/pagewalk.c > >>> index 54b2a1beeeb3..b6eb330e8ecd 100644 > >>> --- a/mm/pagewalk.c > >>> +++ b/mm/pagewalk.c > >>> @@ -306,9 +306,9 @@ static int walk_hugetlb_range(unsigned long addr,= unsigned long end, > >>> unsigned long hmask =3D huge_page_mask(h); > >>> unsigned long sz =3D huge_page_size(h); > >>> const struct mm_walk_ops *ops =3D walk->ops; > >>> + int err =3D 0; > >> > >> Why do you move it back outside of the for loop allthough it is > >> exclusively used inside the loop ? > > I move it outside of the for loop for performance consideration. Becaus= e > > if we initialize err inside, there will be an assignment statement > > every iteration. > > I think GCC is smart enough to do the assignment only when necessary, > maybe have a look at the generated assembly in order to confirm. Thank Christophe for your guidance, I disassembled the GCC generated code, it optimized away int err =3D 0 on both X86 and PowerPC platforms. The disassembled instructions are quite long, so I prefer not to paste in my email unless someone asks me to do so ;-) > > >> > >>> > >>> for (; addr < end; addr =3D next) { > >>> - int err; > >> > >> Another solution would be to add an explicit else, setting err =3D 0 i= n > >> the if/else if sequence. > > Thank Christophe for your valuable advice, I am going to prepare a 2nd = version > Thank Christophe for your reminder, I only subscribed to the RCU mailing list currently, so I submitted duplicated work. I should be more devoted to the Linux community. I am sorry. > Don't spend too much time on that, there are already other people > looking at it, see > https://lore.kernel.org/linux-mm/20220823153055.2517764-1-nathan@kernel.o= rg/T/ > > > > > Thanks > > Zhouyi Many thanks Zhouyi > >> > >>> pte_t *pte =3D huge_pte_offset(walk->mm, addr & hmask,= sz); > >>> > >>> next =3D hugetlb_entry_end(h, addr, end);