public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Is iommu_num_pages() broken ?
@ 2012-08-05  0:16 Benjamin Herrenschmidt
  2012-08-05  6:39 ` Andreas Schwab
  0 siblings, 1 reply; 3+ messages in thread
From: Benjamin Herrenschmidt @ 2012-08-05  0:16 UTC (permalink / raw)
  To: Joerg Roedel; +Cc: Anton Blanchard, FUJITA Tomonori, linux-kernel

Hi folks !

I stumbled upon this today:

static inline unsigned long iommu_num_pages(unsigned long addr,
					    unsigned long len,
					    unsigned long io_page_size)
{
	unsigned long size = (addr & (io_page_size - 1)) + len;

	return DIV_ROUND_UP(size, io_page_size);
}
 

That doesn't look right to me...

The powerpc iommu code at least uses that with an addr which may not be
page aligned (ie, result of sg_virt() which include the offset).

The above code will align the start before adding the len which is wrong
and will result in potentially missing a page or am I missing
something ?

Shouldn't it be something like

static inline unsigned long iommu_num_pages(unsigned long addr,
					    unsigned long len,
					    unsigned long io_page_size)
{
	unsigned long start = addr & (io_page_size - 1);
	unsigned long end = addr + len;

	return DIV_ROUND_UP(end - start, io_page_size);
}

?

Cheers,
Ben.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Is iommu_num_pages() broken ?
  2012-08-05  0:16 Is iommu_num_pages() broken ? Benjamin Herrenschmidt
@ 2012-08-05  6:39 ` Andreas Schwab
  2012-08-05  6:49   ` Benjamin Herrenschmidt
  0 siblings, 1 reply; 3+ messages in thread
From: Andreas Schwab @ 2012-08-05  6:39 UTC (permalink / raw)
  To: Benjamin Herrenschmidt
  Cc: Joerg Roedel, Anton Blanchard, FUJITA Tomonori, linux-kernel

Benjamin Herrenschmidt <benh@kernel.crashing.org> writes:

> static inline unsigned long iommu_num_pages(unsigned long addr,
> 					    unsigned long len,
> 					    unsigned long io_page_size)
> {
> 	unsigned long size = (addr & (io_page_size - 1)) + len;
>
> 	return DIV_ROUND_UP(size, io_page_size);
> }
>  
>
> That doesn't look right to me...
>
> The powerpc iommu code at least uses that with an addr which may not be
> page aligned (ie, result of sg_virt() which include the offset).
>
> The above code will align the start before adding the len which is wrong

addr & (io_page_size - 1) computes the offset into the page pointed to
by addr.  Looks right to me.

addr & ~(io_page_size - 1) would round addr down to the start of the
page.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Is iommu_num_pages() broken ?
  2012-08-05  6:39 ` Andreas Schwab
@ 2012-08-05  6:49   ` Benjamin Herrenschmidt
  0 siblings, 0 replies; 3+ messages in thread
From: Benjamin Herrenschmidt @ 2012-08-05  6:49 UTC (permalink / raw)
  To: Andreas Schwab
  Cc: Joerg Roedel, Anton Blanchard, FUJITA Tomonori, linux-kernel

On Sun, 2012-08-05 at 08:39 +0200, Andreas Schwab wrote:
> addr & (io_page_size - 1) computes the offset into the page pointed to
> by addr.  Looks right to me.
> 
> addr & ~(io_page_size - 1) would round addr down to the start of the
> page.

Ah that's right, for some reason I'm so used to seeing ~ before such
constructs that my brain must have imagined it :-)

Sorry for the noise. Next time I'll have coffee first then re-check ...

Cheers,
Ben.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-08-05  6:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-05  0:16 Is iommu_num_pages() broken ? Benjamin Herrenschmidt
2012-08-05  6:39 ` Andreas Schwab
2012-08-05  6:49   ` Benjamin Herrenschmidt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox