All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Takuya Yoshikawa <takuya.yoshikawa@gmail.com>
Cc: Takuya Yoshikawa <yoshikawa.takuya@oss.ntt.co.jp>,
	Marcelo Tosatti <mtosatti@redhat.com>,
	avi@redhat.com, kvm@vger.kernel.org
Subject: Re: [PATCH -v3] KVM: x86: lapic: Clean up find_highest_vector() and count_vectors()
Date: Thu, 30 Aug 2012 19:49:23 +0300	[thread overview]
Message-ID: <20120830164923.GA22024@redhat.com> (raw)
In-Reply-To: <20120831010956.000856ead3c6f96dbe2bec46@gmail.com>

On Fri, Aug 31, 2012 at 01:09:56AM +0900, Takuya Yoshikawa wrote:
> On Thu, 30 Aug 2012 16:21:31 +0300
> "Michael S. Tsirkin" <mst@redhat.com> wrote:
>  
> > > +static u32 apic_read_reg(int reg_off, void *bitmap)
> > > +{
> > > +	return *((u32 *)(bitmap + reg_off));
> > > +}
> > > +
> > 
> > Contrast with apic_set_reg which gets apic,
> > add fact that all callers invoke REG_POS and you will
> > see this is a bad API.
> > 
> > I played with some APIs but in the end it's
> > probably better to just open-code this.
> 
> I don't mind open-coding this.
> 
> > As a bonus, open-coding will avoid the need
> > for cast above, which is good: casts make code more
> > fragile.
> 
> But I still don't understand why we can eliminate casting:
> 
>   u32 reg_val;
> 
>   reg_val = *((u32 *)(bitmap + REG_POS(vec)));
>   if (reg_val)
>       return __fls(reg_val) + vec;
> 
> (I'm not sure compilers are allowed to push out the value and
> do multiple references for this code as explained in
> https://lwn.net/Articles/508991/

So you *were* talking about concurrency?
And you expect to solve it somehow without barriers
explicit or implicit?

> )
> 
> 
> If you mean
> 
>   u32 *reg;
> 
>   reg = bitmap + REG_POS(vec);
>   if (*reg)
>       return __fls(*reg) + vec;

yes

> I'm still not confident if this is a good style.
> I rarely see code doing
> 
>   if (*p)
>       __fls(*p);
> 
> This looks like explicite multiple references: I'm not saying
> this will actually be compiled to do multiple references.
> 
> Thanks,
> 	Takuya

It's just weird. Both versions are exactly equivalent in C.
Adding a temporary changes *nothing* so the best readability
wins. And IMHO, a version that does not cast wins hands down.
I did a small test just to give you an example:

[mst@robin ~]$ cat a.c 

int foo(void *bitmap)
{
   unsigned *reg;
 
   reg = bitmap + 4;
   if (*reg)
       return *reg + 1;

   return -1;
}
[mst@robin ~]$ cat b.c 

int foo(void *bitmap)
{
   unsigned reg;
 
   reg = *((unsigned *)(bitmap + 4));
   if (reg)
       return reg + 1;

   return -1;
}

[mst@robin ~]$ gcc -O2 -c a.c
[mst@robin ~]$ gcc -O2 -c b.c


[mst@robin ~]$ objdump -ld a.o

a.o:     file format elf32-i386


Disassembly of section .text:

00000000 <foo>:
foo():
   0:   8b 44 24 04             mov    0x4(%esp),%eax
   4:   8b 50 04                mov    0x4(%eax),%edx
   7:   b8 ff ff ff ff          mov    $0xffffffff,%eax
   c:   8d 4a 01                lea    0x1(%edx),%ecx
   f:   85 d2                   test   %edx,%edx
  11:   0f 45 c1                cmovne %ecx,%eax
  14:   c3                      ret    
[mst@robin ~]$ objdump -ld b.o

b.o:     file format elf32-i386


Disassembly of section .text:

00000000 <foo>:
foo():
   0:   8b 44 24 04             mov    0x4(%esp),%eax
   4:   8b 50 04                mov    0x4(%eax),%edx
   7:   b8 ff ff ff ff          mov    $0xffffffff,%eax
   c:   8d 4a 01                lea    0x1(%edx),%ecx
   f:   85 d2                   test   %edx,%edx
  11:   0f 45 c1                cmovne %ecx,%eax
  14:   c3                      ret    



-- 
MST

  reply	other threads:[~2012-08-30 16:48 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-24  9:15 [PATCH] KVM: x86: lapic: Fix the misuse of likely() in find_highest_vector() Takuya Yoshikawa
2012-08-27 20:25 ` Marcelo Tosatti
2012-08-28  9:57   ` Takuya Yoshikawa
2012-08-29 19:10     ` Marcelo Tosatti
2012-08-29 22:51       ` Michael S. Tsirkin
2012-08-30  1:09         ` Takuya Yoshikawa
2012-08-30  6:37           ` Michael S. Tsirkin
2012-08-30  9:50             ` Takuya Yoshikawa
2012-08-30 10:10               ` Michael S. Tsirkin
2012-08-30 10:24                 ` Takuya Yoshikawa
2012-08-30 10:44                   ` Michael S. Tsirkin
2012-08-30 12:30                     ` [PATCH -v3] KVM: x86: lapic: Clean up find_highest_vector() and count_vectors() Takuya Yoshikawa
2012-08-30 13:21                       ` Michael S. Tsirkin
2012-08-30 16:09                         ` Takuya Yoshikawa
2012-08-30 16:49                           ` Michael S. Tsirkin [this message]
2012-09-05  8:30                             ` Takuya Yoshikawa
2012-09-05  9:26                               ` Michael S. Tsirkin
2012-09-05  9:40                                 ` Takuya Yoshikawa
2012-09-05  9:51                                   ` Michael S. Tsirkin
2012-09-05 10:30                                     ` [PATCH -v4] " Takuya Yoshikawa
2012-09-05 10:58                                       ` Michael S. Tsirkin
2012-09-12 16:39                                       ` Marcelo Tosatti

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=20120830164923.GA22024@redhat.com \
    --to=mst@redhat.com \
    --cc=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=takuya.yoshikawa@gmail.com \
    --cc=yoshikawa.takuya@oss.ntt.co.jp \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.