linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Zhouyi Zhou <zhouzhouyi@gmail.com>
To: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: "akpm@linux-foundation.org" <akpm@linux-foundation.org>,
	"nathan@kernel.org" <nathan@kernel.org>,
	 "ndesaulniers@google.com" <ndesaulniers@google.com>,
	"trix@redhat.com" <trix@redhat.com>,
	 "linux-mm@kvack.org" <linux-mm@kvack.org>,
	 "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	 "llvm@lists.linux.dev" <llvm@lists.linux.dev>
Subject: Re: [PATCH linux-next] mm: fix used but uninitialized variable
Date: Wed, 24 Aug 2022 16:10:12 +0800	[thread overview]
Message-ID: <CAABZP2ybddBG1KVTemcg4PvHsObVfP7Z+UMUkMCpZSBjBwFKbg@mail.gmail.com> (raw)
In-Reply-To: <66f0770d-83ee-34cb-2dea-b8d957613c7f@csgroup.eu>

On Wed, Aug 24, 2022 at 2:49 PM Christophe Leroy
<christophe.leroy@csgroup.eu> wrote:
>
>
>
> Le 24/08/2022 à 08:41, Zhouyi Zhou a écrit :
> > Thank Christophe for reviewing my patch
> >
> > On Wed, Aug 24, 2022 at 2:10 PM Christophe Leroy
> > <christophe.leroy@csgroup.eu> wrote:
> >>
> >>
> >>
> >> Le 24/08/2022 à 07:56, Zhouyi Zhou a écrit :
> >>> 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 <zhouzhouyi@gmail.com>
> >>> ---
> >>> Dear mm Developers:
> >>>
> >>> When I build kernel using "make CC=clang-14"
> >>> the compiler complains following:
> >>>
> >>> CC      mm/pagewalk.o
> >>> mm/pagewalk.c:318:12: error: variable 'err' is used uninitialized whenever '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;
> >>>                          ^
> >>>                           = 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 assign 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 = huge_page_mask(h);
> >>>        unsigned long sz = huge_page_size(h);
> >>>        const struct mm_walk_ops *ops = walk->ops;
> >>> +     int err = 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. Because
> > 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 = 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 = next) {
> >>> -             int err;
> >>
> >> Another solution would be to add an explicit else, setting err = 0 in
> >> 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.org/T/
>
> >
> > Thanks
> > Zhouyi
Many thanks
Zhouyi
> >>
> >>>                pte_t *pte = huge_pte_offset(walk->mm, addr & hmask, sz);
> >>>
> >>>                next = hugetlb_entry_end(h, addr, end);


      reply	other threads:[~2022-08-24  8:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-24  5:56 Zhouyi Zhou
2022-08-24  6:10 ` Christophe Leroy
2022-08-24  6:41   ` Zhouyi Zhou
2022-08-24  6:49     ` Christophe Leroy
2022-08-24  8:10       ` Zhouyi Zhou [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAABZP2ybddBG1KVTemcg4PvHsObVfP7Z+UMUkMCpZSBjBwFKbg@mail.gmail.com \
    --to=zhouzhouyi@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=christophe.leroy@csgroup.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=llvm@lists.linux.dev \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=trix@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox