From: Nikolay Borisov <nborisov@suse.com>
To: dsterba@suse.cz, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 6/7] btrfs: Replace open-coded maths with DIV_ROUND_UP
Date: Thu, 3 Jan 2019 17:33:26 +0200 [thread overview]
Message-ID: <488fa8c9-4d3c-4d1f-c4a1-8e0d45f0cd14@suse.com> (raw)
In-Reply-To: <20190103144432.GH23615@twin.jikos.cz>
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
Since delalloc_to_write is used in a context where we care about the
number of pages written I think using DIV_ROUND_UP is correct and it
fixes an off-by-one bug, no ?
Let's take extent_write_locked_range, it's called only from
submit_compressed_extents with the range for the async (compressed) extent:
Say we have an extent which is 2k in ram size and starts at 1m offset.
We'll have:
start = 1048576
end = 1048576 + 2048 - 1 = 1050623
nr_pages in this case should be 1, with DIV_ROUND_UP:
nr_pages = 2047 + 4096 -1 / 4096 = 1 (1,4995 actually but due to integer
math we don't care about floating point part)
with old formula:
nr_pages = (2047 + 4096) / 4096 = 1 (1,4998 but ditto as above).
>
> end - start + PAGE_SIZE = end + 1 - start + PAGE_SIZE - 1
>
> Check eg. writepage_delalloc how it sets up page_end.
>
> The correct use in DIV_ROUND_UP needs +1 adjustment.
>
next prev parent reply other threads:[~2019-01-03 15:33 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-03 8:49 [PATCH 0/7] More misc fixes Nikolay Borisov
2019-01-03 8:49 ` [PATCH 1/7] btrfs: Remove inode argument from async_cow_submit Nikolay Borisov
2019-01-05 6:02 ` Anand Jain
2019-01-07 10:12 ` Johannes Thumshirn
2019-01-03 8:50 ` [PATCH 2/7] btrfs: Remove isize local variable in compress_file_range Nikolay Borisov
2019-01-05 6:17 ` Anand Jain
2019-01-07 10:17 ` Johannes Thumshirn
2019-01-07 17:41 ` David Sterba
2019-01-03 8:50 ` [PATCH 3/7] btrfs: Use ihold instead of igrab in cow_file_range_async Nikolay Borisov
2019-01-04 15:29 ` David Sterba
2019-01-03 8:50 ` [PATCH 4/7] btrfs: Remove WARN_ON in btrfs_alloc_delalloc_work Nikolay Borisov
2019-01-04 15:30 ` David Sterba
2019-01-07 10:19 ` Johannes Thumshirn
2019-01-03 8:50 ` [PATCH 5/7] btrfs: Document logic in async_cow_submit Nikolay Borisov
2019-01-03 8:50 ` [PATCH 6/7] btrfs: Replace open-coded maths with DIV_ROUND_UP Nikolay Borisov
2019-01-03 14:44 ` David Sterba
2019-01-03 15:33 ` Nikolay Borisov [this message]
2019-01-07 15:29 ` David Sterba
2019-01-03 8:50 ` [PATCH 7/7] btrfs: Refactor shrink_delalloc Nikolay Borisov
2019-01-04 15:35 ` David Sterba
2019-01-07 17:58 ` David Sterba
2019-01-07 17:59 ` [PATCH 0/7] More misc fixes David Sterba
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=488fa8c9-4d3c-4d1f-c4a1-8e0d45f0cd14@suse.com \
--to=nborisov@suse.com \
--cc=dsterba@suse.cz \
--cc=linux-btrfs@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).