kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Xu <peterx@redhat.com>
To: Sean Christopherson <sean.j.christopherson@intel.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>,
	linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
	Nitesh Narayan Lal <nitesh@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [PATCH v2 3/3] KVM: X86: Fixup kvm_apic_match_dest() dest_mode parameter
Date: Mon, 2 Dec 2019 13:59:35 -0500	[thread overview]
Message-ID: <20191202185935.GB10882@xz-x1> (raw)
In-Reply-To: <20191202173152.GB4063@linux.intel.com>

On Mon, Dec 02, 2019 at 09:31:52AM -0800, Sean Christopherson wrote:
> On Mon, Dec 02, 2019 at 10:18:00AM +0100, Vitaly Kuznetsov wrote:
> > Peter Xu <peterx@redhat.com> writes:
> > 
> > > The problem is the same as the previous patch on that we've got too
> > > many ways to define a dest_mode, but logically we should only pass in
> > > APIC_DEST_* macros for this helper.
> > 
> > Using 'the previous patch' in changelog is OK until it comes to
> > backporting as the order can change. I'd suggest to spell out "KVM: X86:
> > Use APIC_DEST_* macros properly" explicitly.
> 
> Even that is bad practice IMO.  Unless there is an explicit dependency on
> a previous patch, which does not seem to be the case here, the changelog
> should fully describe and justify the patch without referencing a previous
> patch/commit.
> 
> Case in point, I haven't looked at the previous patch yet and have no idea
> why *this* patch is needed or what it's trying to accomplish.

I'll improve both commit messages.

> 
> > >
> > > To make it easier, simply define dest_mode of kvm_apic_match_dest() to
> > > be a boolean to make it right while we can avoid to touch the callers.
> > >
> > > Signed-off-by: Peter Xu <peterx@redhat.com>
> > > ---
> > >  arch/x86/kvm/lapic.c | 5 +++--
> > >  arch/x86/kvm/lapic.h | 2 +-
> > >  2 files changed, 4 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> > > index cf9177b4a07f..80732892c709 100644
> > > --- a/arch/x86/kvm/lapic.c
> > > +++ b/arch/x86/kvm/lapic.c
> > > @@ -791,8 +791,9 @@ static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id,
> > >  	return dest_id;
> > >  }
> > >  
> > > +/* Set dest_mode to true for logical mode, false for physical mode */
> > >  bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
> > > -			   int short_hand, unsigned int dest, int dest_mode)
> > > +			   int short_hand, unsigned int dest, bool dest_mode)
> > >  {
> > >  	struct kvm_lapic *target = vcpu->arch.apic;
> > >  	u32 mda = kvm_apic_mda(vcpu, dest, source, target);
> > > @@ -800,7 +801,7 @@ bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
> > >  	ASSERT(target);
> > >  	switch (short_hand) {
> > >  	case APIC_DEST_NOSHORT:
> > > -		if (dest_mode == APIC_DEST_PHYSICAL)
> > > +		if (dest_mode == false)
> > 
> > I must admit this seriously harm the readability of the code for
> > me. Just look at the 
> > 
> >  if (dest_mode == false)
> > 
> > line without a context and try to say what's being checked. I can't.
> > 
> > I see to solutions:
> > 1) Adhere to the APIC_DEST_PHYSICAL/APIC_DEST_LOGICAL (basically - just
> > check against "dest_mode == APIC_DEST_LOGICAL" in the else branch)
> > 2) Rename the dest_mode parameter to 'dest_mode_is_phys' or something
> > like that.
> 
> For #2, it should be "logical" instead of "phys" as APIC_DEST_PHYSICAL is
> the zero value.
> 
> There's also a third option:
> 
>   3) Add a WARN_ON_ONCE and fix the IO APIC callers, e.g.:
> 
> 	WARN_ON_ONCE(dest_mode != APIC_DEST_PHYSICAL ||
> 		     dest_mode != APIC_DEST_LOGICAL);
> 
> 	if (dest_mode == APIC_DEST_PHYSICAL)
> 		return kvm_apic_match_physical_addr(target, mda);
> 	else
> 		return kvm_apic_match_logical_addr(target, mda);
> 
> Part of me likes the simplicity of #2, but on the other hand I don't like
> the inconsistency with respect to @short_hand and @dest, which take in
> "full" values.  E.g. @short_hand would also be problematic for a caller
> that uses a bitfield.

IMHO the best way is that we should always use a boolean for dest mode
internally because it's always a true or false flag, and we only
convert it to other forms when needed (e.g. when applying that bit to
an IOAPIC entry).  But here I think I'll go with the 3rd option to
avoid code churns (I think it's also what Vitaly suggested as the 1st
option).

> 
> Side topic, the I/O APIC callers should explicitly pass APIC_DEST_NOSHORT
> instead of 0.

I'll fix that too.

(I also missed suggested-by/reported-by for Vitaly)

Thank you both for your reviews,

-- 
Peter Xu


      reply	other threads:[~2019-12-02 18:59 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-29 16:32 [PATCH v2 0/3] KVM: X86: Cleanups on dest_mode and headers Peter Xu
2019-11-29 16:32 ` [PATCH v2 1/3] KVM: X86: Some cleanups in ioapic.h/lapic.h Peter Xu
2019-12-02  9:27   ` Vitaly Kuznetsov
2019-12-02 17:47     ` Sean Christopherson
2019-12-02 19:13       ` Peter Xu
2019-11-29 16:32 ` [PATCH v2 2/3] KVM: X86: Use APIC_DEST_* macros properly Peter Xu
2019-11-29 16:32 ` [PATCH v2 3/3] KVM: X86: Fixup kvm_apic_match_dest() dest_mode parameter Peter Xu
2019-12-02  9:18   ` Vitaly Kuznetsov
2019-12-02 17:31     ` Sean Christopherson
2019-12-02 18:59       ` Peter Xu [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=20191202185935.GB10882@xz-x1 \
    --to=peterx@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nitesh@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=sean.j.christopherson@intel.com \
    --cc=vkuznets@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;
as well as URLs for NNTP newsgroup(s).