All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Jones <drjones@redhat.com>
To: Alexander Gordeev <agordeev@redhat.com>
Cc: kvm@vger.kernel.org, Thomas Huth <thuth@redhat.com>
Subject: Re: [kvm-unit-tests PATCH v4 04/12] pci: Rework pci_bar_addr()
Date: Tue, 7 Jun 2016 09:03:18 +0200	[thread overview]
Message-ID: <20160607070318.fxwprxsme46oc6ys@hawk.localdomain> (raw)
In-Reply-To: <20160607063854.GA21204@dhcp-27-118.brq.redhat.com>

On Tue, Jun 07, 2016 at 08:38:55AM +0200, Alexander Gordeev wrote:
> On Mon, Jun 06, 2016 at 04:12:03PM +0200, Andrew Jones wrote:
> > > +phys_addr_t pci_bar_size(pcidevaddr_t dev, int bar_num)
> > >  {
> > >  	uint32_t bar = pci_config_readl(dev, PCI_BASE_ADDRESS_0 + bar_num * 4);
> > > +	phys_addr_t size = (int32_t)pci_bar_size32(dev, bar_num);
> > > +	phys_addr_t mask = (int32_t)pci_bar_mask(bar);
> > >  
> > > -	if (bar & PCI_BASE_ADDRESS_SPACE_IO)
> > > -		return bar & PCI_BASE_ADDRESS_IO_MASK;
> > > -	else
> > > -		return bar & PCI_BASE_ADDRESS_MEM_MASK;
> > > +	if (pci_bar_is64(dev, bar_num)) {
> > > +		uint32_t size_high = pci_bar_size32(dev, bar_num + 1);
> > > +		size = ((phys_addr_t)size_high << 32) | (uint32_t)size;
> > > +	}
> > > +
> > > +	return (~(size & mask)) + 1;
> > 
> > All this casting of size and mask is pointless. Please rework it
> > similar to what I did above.
> 
> This is the most compact and straight variant I was able to come up with:
> 
> 	uint32_t bar = pci_bar_get(dev, bar_num);
> 	phys_addr_t size = (int32_t)pci_bar_size32(dev, bar_num);

But this is wrong. If your 32-bit size was 0x80000000, then you now
say it's 0xffffffff80000000.

> 	phys_addr_t mask = (int32_t)pci_bar_mask(bar);

It might be OK to do that here, on a mask, but even if it is, then I
don't like it, because it's too subtle (like I said for the casting
in pci_bar_addr in my last reply). I don't like that it requires us to
know that masking bit 31 in a 32-bit mask means we also want to mask
63..32. That should at least be in a comment somewhere.

> 
> 	if (pci_bar_is64(dev, bar_num))
> 		size |= (phys_addr_t)pci_bar_size32(dev, bar_num + 1) << 32;
> 
> 	return (~(size & mask)) + 1;
> 
> The casting is needed to avoid putting explicitly all 1s into higher
> bits of size and/or mask. Otherwise (~(size & mask)) + 1 expression would
> not bring correct results. I really struggle to make something better
> readable.

It's not just about readability, it's about correctness. You shouldn't
use 32-bit sizes/masks with 64-bit data this way. Like I said in the
last reply, you should rework it like I showed you, operate on the
32-bit data with the 32-bit mask/size, and then eventually construct
64-bit data.

Thanks,
drew

  reply	other threads:[~2016-06-07  7:03 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-06 12:46 [kvm-unit-tests PATCH v4 00/12] PCI bus support Alexander Gordeev
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 01/12] pci: Fix coding style in generic PCI files Alexander Gordeev
2016-06-06 13:22   ` Andrew Jones
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 02/12] pci: x86: Rename pci_config_read() to pci_config_readl() Alexander Gordeev
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 03/12] pci: x86: Add remaining PCI configuration space accessors Alexander Gordeev
2016-06-07  6:48   ` Thomas Huth
2016-06-08  6:21     ` Alexander Gordeev
2016-06-08 10:08       ` Andrew Jones
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 04/12] pci: Rework pci_bar_addr() Alexander Gordeev
2016-06-06 14:12   ` Andrew Jones
2016-06-07  6:38     ` Alexander Gordeev
2016-06-07  7:03       ` Andrew Jones [this message]
2016-06-07 10:33         ` Alexander Gordeev
2016-06-07 11:23           ` Alexander Gordeev
2016-06-07 14:10             ` Andrew Jones
2016-06-07 14:08           ` Andrew Jones
2016-06-07 20:00             ` Alexander Gordeev
2016-06-08 11:59               ` Andrew Jones
2016-06-09 20:41                 ` Alexander Gordeev
2016-06-10  7:14                   ` Andrew Jones
2016-06-07  6:55     ` Alexander Gordeev
2016-06-10 18:56     ` Alexander Gordeev
2016-06-12 13:41       ` Andrew Jones
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 05/12] pci: Factor out pci_bar_get() Alexander Gordeev
2016-06-06 15:19   ` Andrew Jones
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 06/12] pci: Add pci_bar_set_addr() Alexander Gordeev
2016-06-06 15:38   ` Andrew Jones
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 07/12] pci: Add pci_dev_exists() Alexander Gordeev
2016-06-06 15:40   ` Andrew Jones
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 08/12] pci: Add pci_print() Alexander Gordeev
2016-06-06 15:48   ` Andrew Jones
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 09/12] pci: Add generic ECAM host support Alexander Gordeev
2016-06-06 16:27   ` Andrew Jones
2016-06-08  6:36     ` Alexander Gordeev
2016-06-11 20:10     ` Alexander Gordeev
2016-06-12 13:42       ` Andrew Jones
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 10/12] arm/arm64: pci: Add PCI bus operation test Alexander Gordeev
2016-06-06 16:39   ` Andrew Jones
2016-06-08  6:53     ` Alexander Gordeev
2016-06-08 12:08       ` Andrew Jones
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 11/12] pci: Add pci-testdev PCI bus test device Alexander Gordeev
2016-06-06 17:00   ` Andrew Jones
2016-06-06 12:46 ` [kvm-unit-tests PATCH v4 12/12] arm/arm64: pci: Add pci-testdev PCI device operation test Alexander Gordeev
2016-06-06 17:04   ` Andrew Jones
2016-06-06 17:11 ` [kvm-unit-tests PATCH v4 00/12] PCI bus support Andrew Jones
2016-06-21  7:02   ` Alexander Gordeev
2016-06-27 12:59     ` Alexander Gordeev
2016-06-27 13:46       ` Andrew Jones
2016-06-28 10:54   ` Alexander Gordeev
2016-06-28 11:18     ` Andrew Jones
2016-06-28 11:28       ` Alexander Gordeev
2016-06-28 11:32         ` Andrew Jones
2016-06-28 11:56           ` Alexander Gordeev
2016-06-28 12:38             ` Andrew Jones
2016-06-28 13:04               ` Alexander Gordeev

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=20160607070318.fxwprxsme46oc6ys@hawk.localdomain \
    --to=drjones@redhat.com \
    --cc=agordeev@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=thuth@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 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.