From: WenRuo Qu <wqu@suse.com>
To: Nikolay Borisov <nborisov@suse.com>,
"linux-btrfs@vger.kernel.org" <linux-btrfs@vger.kernel.org>
Subject: Re: [PATCH 2/5] btrfs: extent-tree: Kill BUG_ON() in __btrfs_free_extent() and do better comment
Date: Wed, 10 Jul 2019 11:00:27 +0000 [thread overview]
Message-ID: <2e81bdc8-f441-a10e-d776-c23e5940c091@suse.com> (raw)
In-Reply-To: <ed5a0fda-0369-e7ba-14f8-4f3b4d04add8@suse.com>
On 2019/7/10 下午6:48, Nikolay Borisov wrote:
>
>
> On 10.07.19 г. 11:02 ч., Qu Wenruo wrote:
>> __btrfs_free_extent() is one of the best cases to show how optimization
>> could make a function hard to read.
>>
>> In fact __btrfs_free_extent() is only doing two major works:
>> 1. Reduce the refs number of an extent backref
>> Either it's an inlined extent backref (inside EXTENT/METADATA item) or
>> a keyed extent backref (SHARED_* item).
>> We only need to locate that backref line, either reduce the number or
>> remove the backref line completely.
>>
>> 2. Update the refs count in EXTENT/METADATA_ITEM
>>
>> But in real world, we do it in a complex but somewhat efficient way.
>> During step 1), we will try to locate the EXTENT/METADATA_ITEM without
>> triggering another btrfs_search_slot() as fast path.
>>
>> Only when we failed to locate that item, we will trigger another
>> btrfs_search_slot() to get that EXTENT/METADATA_ITEM after we
>> updated/deleted the backref line.
>>
>> And we have a lot of restrict check on things like refs_to_drop against
>> extent refs and special case check for single ref extent.
>>
>> All of these results:
>> - 7 BUG_ON()s in a single function
>> Although all these BUG_ON() are doing correct check, they're super
>> easy to get triggered for fuzzed images.
>> It's never a good idea to piss the end user.
>>
>> - Near 300 lines without much useful comments but a lot of hidden
>> conditions
>> I believe even the author needs several minutes to recall what the
>> code is doing
>> Not to mention a lot of BUG_ON() conditions needs to go back tens of
>> lines to find out why.
>>
>> This patch address all these problems by:
>> - Introduce two examples to show what __btrfs_free_extent() is doing
>> One inlined backref case and one keyed case.
>> Should cover most cases.
>>
>> - Kill all BUG_ON()s with proper error message and optional leaf dump
>>
>> - Add comment to show the overall workflow
>>
>> Link: https://bugzilla.kernel.org/show_bug.cgi?id=202819
>> [ The report triggers one BUG_ON() in __btrfs_free_extent() ]
>> Signed-off-by: Qu Wenruo <wqu@suse.com>
>> ---
>
> <snip>
>
>> @@ -6997,19 +7068,24 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
>> if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
>> key.type == BTRFS_EXTENT_ITEM_KEY) {
>> struct btrfs_tree_block_info *bi;
>> - BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
>> + if (unlikely(item_size < sizeof(*ei) + sizeof(*bi))) {
>> + btrfs_crit(info,
>> +"invalid extent item size for key (%llu, %u, %llu) owner %llu, has %u expect >%lu",
> nit: stray '>'
With values filled, it would be:
"invalid extent item size for key (1048575, 168, 4096) owner 7, has 10
expect >33"
Now you'd see it's not a stray '>'.
Thanks,
Qu
>
> <snip>
>
next prev parent reply other threads:[~2019-07-10 11:03 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-10 8:02 [PATCH 0/5] btrfs: Enhanced runtime defence against fuzzed images Qu Wenruo
2019-07-10 8:02 ` [PATCH 1/5] btrfs: extent_io: Do extra check for extent buffer read write functions Qu Wenruo
2019-07-10 10:42 ` Nikolay Borisov
2019-07-10 10:58 ` WenRuo Qu
2019-07-24 16:00 ` David Sterba
2019-07-24 22:54 ` Qu Wenruo
2019-07-25 6:39 ` Nikolay Borisov
2019-07-10 8:02 ` [PATCH 2/5] btrfs: extent-tree: Kill BUG_ON() in __btrfs_free_extent() and do better comment Qu Wenruo
2019-07-10 10:48 ` Nikolay Borisov
2019-07-10 11:00 ` WenRuo Qu [this message]
2019-07-10 8:02 ` [PATCH 3/5] btrfs: Detect unbalanced tree with empty leaf before crashing btree operations Qu Wenruo
2019-07-10 10:54 ` Nikolay Borisov
2019-07-10 8:02 ` [PATCH 4/5] btrfs: extent-tree: Kill the BUG_ON() in insert_inline_extent_backref() Qu Wenruo
2019-07-10 11:12 ` Nikolay Borisov
2019-07-10 8:02 ` [PATCH 5/5] btrfs: ctree: Checking key orders before merged tree blocks Qu Wenruo
2019-07-10 11:19 ` Nikolay Borisov
2019-07-10 12:02 ` Qu Wenruo
2019-07-10 12:12 ` Nikolay Borisov
2019-07-24 16:24 ` 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=2e81bdc8-f441-a10e-d776-c23e5940c091@suse.com \
--to=wqu@suse.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=nborisov@suse.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox