linux-bcache.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Overstreet <kent.overstreet@gmail.com>
To: Junhui Tang <tang.junhui@zte.com.cn>
Cc: Nix <nix@esperi.org.uk>, Coly Li <colyli@suse.de>,
	Michael Lyle <mlyle@lyle.org>,
	linux-bcache@vger.kernel.org,
	linux-block <linux-block@vger.kernel.org>
Subject: Re: [PATCH] bcache: lock in btree_flush_write() to avoid races
Date: Wed, 24 Jan 2018 20:58:39 -0500	[thread overview]
Message-ID: <CAC7rs0vW1EAMgM6vbfB-w7firQD2SbHLJcidXPD+oc02b8PZDg@mail.gmail.com> (raw)
In-Reply-To: <1516845029-9491-1-git-send-email-tang.junhui@zte.com.cn>

The only purpose of rcu_read_lock() would be to ensure the object
isn't freed out from under you. That's not an issue here.

Racing with other writers isn't a correctness issue here, and if it
was - just throwing write_lock around where you have it wouldn't be
sufficient, you'd need something much more complicated.

On Wed, Jan 24, 2018 at 8:50 PM,  <tang.junhui@zte.com.cn> wrote:
> From: Tang Junhui <tang.junhui@zte.com.cn>
>
> Hello Kent && Nix
>
>>
>>neither of those locks are needed - rcu_read_lock() isn't needed because we never
>>free struct btree (except at shutdown), and we're not derefing journal there
>
> __bch_btree_node_write() is called in many places, in __bch_btree_node_write(),
> before node writing over, it has changed the write buff index to another, but the
> journal of changed write buff maybe still NULL if no journal writing occurred. But
> luckily we only use the NULL as ZERO to calculate the fifo_idx, the result is a very
> big value, so it does no harm. And since we cannot take mutex under rcu_read_lock,
> we can ignore it.
>
> As to the rcu lock, though the btree is not freed, but we often delete it from one
> list (such as one bucket_hash) and move it to another list (such as btree_cache_freed),
> shouldn't it be protected by rcu locker?
>
>
>>On Wed, Jan 24, 2018 at 5:30 AM, Nikolay Borisov <nborisov@suse.com> wrote:
>>
>>
>>On 24.01.2018 08:54, tang.junhui@zte.com.cn wrote:
>>> From: Tang Junhui <tang.junhui@zte.com.cn>
>>>
>>> In btree_flush_write(), two places need to take a locker to
>>> avoid races:
>>>
>>> Firstly, we need take rcu read locker to protect the bucket_hash
>>> traverse, since hlist_for_each_entry_rcu() must be called under
>>> the protection of rcu read locker.
>>>
>>> Secondly, we need take b->write_lock locker to protect journal
>>> of the btree node, otherwise, the btree node may have been
>>> written, and the journal have been assign to NULL.
>>>
>>> Signed-off-by: Tang Junhui <tang.junhui@zte.com.cn>
>>> ---
>>>  drivers/md/bcache/journal.c | 7 ++++++-
>>>  1 file changed, 6 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/md/bcache/journal.c b/drivers/md/bcache/journal.c
>>> index 02a98dd.505f9f3 100644
>>> --- a/drivers/md/bcache/journal.c
>>> +++ b/drivers/md/bcache/journal.c
>>> @@ -375,7 +375,9 @@ static void btree_flush_write(struct cache_set *c)
>>>  retry:
>>>       best = NULL;
>>>
>>> -     for_each_cached_btree(b, c, i)
>>> +     rcu_read_lock();
>>> +     for_each_cached_btree(b, c, i) {
>>> +             mutex_lock(&b->write_lock);
>>
>
>>You can't sleep in rcu read critical section, yet here you take mutex
>>which can sleep under rcu_read_lock.
> To Nix: Yes, you are write. Good catch.
>
>>
>>>               if (btree_current_write(b)->journal) {
>>>                       if (!best)
>>>                               best = b;
>>> @@ -385,6 +387,9 @@ static void btree_flush_write(struct cache_set *c)
>>>                               best = b;
>>>                       }
>>>               }
>>> +             mutex_unlock(&b->write_lock);
>>> +     }
>>> +     rcu_read_unlock();
>>>
>>>       b = best;
>>>       if (b) {
>
> Thanks,
> Tang Junhui
>

  reply	other threads:[~2018-01-25  1:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-25  1:50 [PATCH] bcache: lock in btree_flush_write() to avoid races tang.junhui
2018-01-25  1:58 ` Kent Overstreet [this message]
  -- strict thread matches above, loose matches on Subject: below --
2018-01-25  2:30 tang.junhui
2018-01-24  9:45 tang.junhui
2018-01-24  9:49 ` Coly Li
2018-01-24  6:54 tang.junhui
2018-01-24  9:13 ` Coly Li
2018-01-24 10:30 ` Nikolay Borisov

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=CAC7rs0vW1EAMgM6vbfB-w7firQD2SbHLJcidXPD+oc02b8PZDg@mail.gmail.com \
    --to=kent.overstreet@gmail.com \
    --cc=colyli@suse.de \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=mlyle@lyle.org \
    --cc=nix@esperi.org.uk \
    --cc=tang.junhui@zte.com.cn \
    /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).