Re: [PATCH 6/7] btrfs: Replace open-coded maths with DIV_ROUND_UP

David Sterba
2019-01-07T15:30:30Z

On Thu, Jan 03, 2019 at 05:33:26PM +0200, Nikolay Borisov wrote:
>
>
> On 3.01.19 г. 16:44 ч., David Sterba wrote:
> > On Thu, Jan 03, 2019 at 10:50:04AM +0200, Nikolay Borisov wrote:
> >> In a couple of places it's required to calculate the number of pages
> >> given a start and end offsets. Currently this is opencoded, unify the
> >> code base by replacing all such sites with the DIV_ROUND_UP macro. Also,
> >> current open-coded sites were buggy in that they were adding
> >> 'PAGE_SIZE', rather than 'PAGE_SIZE - 1'.
> >
> > Didn't you find it strange that it's so consistently wrong? After a
> > closer inspection, you'd find that the end of the range is inclusive. So
> > the math is correct and your patch introduces a bug.
>
> But since we are talking about number of pages, why does the range need
> to be inclusive. Indeed, let's take delalloc_to_write in
> writepage_delalloc. Say we have a 1mb range, 0-1m - that's 256 pages
> right so with DIV_ROUND_UP we'll have:
>
> (1048576 + 4096 - 1) / 4096 = 256
>
> with the old version we'll have:
>
> 1048576 + 4096 / 4096 = 257

No, it's not 1048576 but 1048575. The end of the range is inclusive, ie.
the value in end is the end of the interval. Not one byte after.

It's even written in the comment in writepage_delalloc just above the
line where you switch to DIV_ROUND_UP.