mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Qian Cai <quic_qiancai@quicinc.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>, <kvm@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] kvm: Avoid shadowing a local in search_memslots()
Date: Tue, 26 Oct 2021 11:57:13 -0400	[thread overview]
Message-ID: <9a9100f8-3e60-2fb7-1200-ec2895ae1b13@quicinc.com> (raw)
In-Reply-To: <YXgib3l+sSwy8Sje@google.com>



On 10/26/21 11:44 AM, Sean Christopherson wrote:
> On Tue, Oct 26, 2021, Qian Cai wrote:
>> It is less error-prone to use a different variable name from the existing
>> one in a wider scope. This is also flagged by GCC (W=2):
>>
>> ./include/linux/kvm_host.h: In function 'search_memslots':
>> ./include/linux/kvm_host.h:1246:7: warning: declaration of 'slot' shadows a previous local [-Wshadow]
>>  1246 |   int slot = start + (end - start) / 2;
>>       |       ^~~~
>> ./include/linux/kvm_host.h:1240:26: note: shadowed declaration is here
>>  1240 |  struct kvm_memory_slot *slot;
>>       |                          ^~~~
>>
> 
> Even though this doesn't need to go to stable, probably worth adding a Fixes: to
> acknowledge that this was a recently introduced mess.
> 
>   Fixes: 0f22af940dc8 ("KVM: Move last_used_slot logic out of search_memslots")
> 
> 
>> Signed-off-by: Qian Cai <quic_qiancai@quicinc.com>
>> ---
>>  include/linux/kvm_host.h | 8 ++++----
>>  1 file changed, 4 insertions(+), 4 deletions(-)
>>
>> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
>> index 60a35d9fe259..1c1a36f658fe 100644
>> --- a/include/linux/kvm_host.h
>> +++ b/include/linux/kvm_host.h
>> @@ -1243,12 +1243,12 @@ search_memslots(struct kvm_memslots *slots, gfn_t gfn, int *index)
>>  		return NULL;
>>  
>>  	while (start < end) {
>> -		int slot = start + (end - start) / 2;
>> +		int new_slot = start + (end - start) / 2;
> 
> new_slot isn't a great name, the integer "slot" isn't directly connected to the
> final memslot and may not be representative of the final memslot's index depending
> on how the binary search resolves.
> 
> Maybe "pivot"?  Or just "tmp"?  I also vote to hoist the declaration out of the
> loop precisely to avoid potential shadows, and to also associate the variable
> with the "start" and "end" variables, e.g.

Yes, I like "pivot" and the rest of the feedback makes sense. I'll send
a v2 soon.

  reply	other threads:[~2021-10-26 15:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-26 15:13 Qian Cai
2021-10-26 15:44 ` Sean Christopherson
2021-10-26 15:57   ` Qian Cai [this message]
2021-10-26 16:14   ` Qian Cai
2021-10-26 16:18     ` Paolo Bonzini
2021-10-26 16:39       ` Sean Christopherson

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=9a9100f8-3e60-2fb7-1200-ec2895ae1b13@quicinc.com \
    --to=quic_qiancai@quicinc.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.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

all inboxes | Powered by JetHome®