On Fri, Apr 18, 2008 at 8:15 AM, Rusty Russell <[EMAIL PROTECTED]> wrote:
> On Friday 18 April 2008 21:31:20 Andrew Morton wrote:
> > On Fri, 18 Apr 2008 14:43:24 +1000 Rusty Russell <[EMAIL PROTECTED]> wrote:
>
> > > + /* How many pages will this take? */
> > > + npages = 1 + (base + len - 1)/PAGE_SIZE - base/PAGE_SIZE;
> >
> > Brain hurts. I hope you got that right.
>
> I tested it when I wrote it, but just wrote a tester again:
>
> base len npages
> 0 1 1
> 0xfff 1 1
> 0x1000 1 1
> 0 4096 1
> 0x1 4096 2
> 0xfff 4096 2
> 0x1000 4096 1
> 0xfffff000 4096 1
> 0xfffff000 4097 4293918722
Picky, picky :-).
If base were always page aligned, we'd want
npages = (len + PAGE_SIZE - 1) / PAGE_SIZE;
which is simply rounding len up to the next largest page size. So when
base is not page aligned, increase len by the left-over space at the
beginning, and then do the same calculation as above. (ie, pretend
base is page aligned, and instead count the excess at the beginning as
part of len.)
npages = ( (base & PAGE_MASK) + len + PAGE_SIZE - 1) / PAGE_SIZE;
As long as len < PAGE_MASK - PAGE_SIZE, we're safe from overflows.
(The above gives a different answer when len=0, but you guard for that.)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/virtualization