From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: dsterba@suse.cz, Qu Wenruo <wqu@suse.com>, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 0/3] btrfs: fixes for relocation to avoid KASAN reports
Date: Thu, 12 Dec 2019 08:39:43 +0800 [thread overview]
Message-ID: <74a07fa4-ca35-57ee-2cd9-586a8db04712@gmx.com> (raw)
In-Reply-To: <20191211153429.GO3929@twin.jikos.cz>
[-- Attachment #1.1: Type: text/plain, Size: 2780 bytes --]
On 2019/12/11 下午11:34, David Sterba wrote:
> On Wed, Dec 11, 2019 at 01:00:01PM +0800, Qu Wenruo wrote:
>> Due to commit d2311e698578 ("btrfs: relocation: Delay reloc tree
>> deletion after merge_reloc_roots"), reloc tree lifespan is extended.
>>
>> Although we always set root->reloc_root to NULL before we drop the reloc
>> tree, but that's not multi-core safe since we have no proper memory
>> barrier to ensure other cores can see the same root->reloc_root.
>>
>> The proper root fix should be some proper root refcount, and make
>> btrfs_drop_snapshot() to wait for all other root owner to release the
>> root before dropping it.
>
> This would block cleaning deleted subvolumes, no? We can skip the dead
> tree (and add it back to the list) in that can and not wait. The
> cleaner thread is able to process the list repeatedly.
What I mean is:
- For consumer (reading root->reloc_root)
spin_lock(&root->reloc_lock);
if (!root->reloc_root) {
spin_unlock(&root->reloc_lock);
return NULL
}
refcount_inc(&root->reloc_root->refcount);
return(root->reloc_root);
spin_unlock(&root->reloc_lock);
And of cource, release it after grabbing reloc_root.
- For cleaner
grab reloc_root just like consumer.
retry:
wait_event(refcount_read(&root->reloc_root->ref_count) == 1);
spin_lock(&root->reloc_lock);
if (&root->reloc_root->ref_count != 1){
spin_unlock(); goto retry;
}
root->reloc_root = NULL;
spin_unlock(&root->reloc_lock);
/* Now we're the only owner, delete the root */
>
>> But for now, let's just check the DEAD_RELOC_ROOT bit before accessing
>> root->reloc_root.
>
> Ok, the bit is safe way to sync that as long as the correct order of
> setting/clearing is done. The ops are atomic wrt to the value itself
> but need barriers around as they're simple atomic ops (not RMW,
> according to Documentation/atomic_bitops.txt) and there's no outer
> synchronization.
>
> Check:
>
> smp_mb__before_atomic();
> if (test_bit() ...)
> return;
>
> Set:
>
> set_bit()
> smp_mb__after_atomic();
> (delete reloc_root)
> reloc_root = NULL
>
> Clearing of the bit is done when there are not potential other users so
> that part does not need the barrier (I think).
>
> The checking part could use a helper so we don't have barriers scattered
> around code.
>
I'm still not confident enough for the "reloc_root = NULL" assignment
and "reloc_root == NULL" test.
But since the set_bit()/test_bit() is safe, and it happens before we
modify reloc_root, it's safer and is what we used in this quick fix.
Still, I'm really looking forward to Josef's root refcount work, that
should be the real fix for all the problems.
Thanks,
Qu
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
next prev parent reply other threads:[~2019-12-12 0:39 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-11 5:00 [PATCH 0/3] btrfs: fixes for relocation to avoid KASAN reports Qu Wenruo
2019-12-11 5:00 ` [PATCH 1/3] btrfs: relocation: Fix a KASAN use-after-free bug due to extended reloc tree lifespan Qu Wenruo
2019-12-11 14:53 ` Josef Bacik
2019-12-11 5:00 ` [PATCH 2/3] btrfs: relocation: Fix KASAN report on create_reloc_tree due to extended reloc tree lifepsan Qu Wenruo
2019-12-11 14:55 ` Josef Bacik
2019-12-11 15:15 ` David Sterba
2019-12-11 5:00 ` [PATCH 3/3] btrfs: relocation: Fix a KASAN report on btrfs_reloc_pre_snapshot() due to extended reloc root lifespan Qu Wenruo
2019-12-11 14:55 ` Josef Bacik
2019-12-11 15:34 ` [PATCH 0/3] btrfs: fixes for relocation to avoid KASAN reports David Sterba
2019-12-12 0:39 ` Qu Wenruo [this message]
2019-12-12 14:28 ` David Sterba
2020-01-03 15:52 ` David Sterba
2020-01-03 16:15 ` David Sterba
2020-01-04 9:37 ` Qu Wenruo
2020-01-04 13:18 ` Qu Wenruo
2020-01-06 7:04 ` Qu Wenruo
2020-01-06 18:23 ` David Sterba
2020-01-04 1:32 ` Qu Wenruo
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=74a07fa4-ca35-57ee-2cd9-586a8db04712@gmx.com \
--to=quwenruo.btrfs@gmx.com \
--cc=dsterba@suse.cz \
--cc=linux-btrfs@vger.kernel.org \
--cc=wqu@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