From: Rusty Russell <rusty@rustcorp.com.au>
To: Amos Kong <akong@redhat.com>, virtualization@lists.linux-foundation.org
Cc: herbert@gondor.apana.org.au, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, m@bues.ch, mpm@selenic.com,
amit.shah@redhat.com, mb@bu3sch.de
Subject: Re: [PATCH v4 3/6] hw_random: use reference counts on each struct hwrng.
Date: Wed, 12 Nov 2014 14:11:23 +1030 [thread overview]
Message-ID: <87zjbxm6fw.fsf@rustcorp.com.au> (raw)
In-Reply-To: <1415030186-18303-4-git-send-email-akong@redhat.com>
Amos Kong <akong@redhat.com> writes:
> From: Rusty Russell <rusty@rustcorp.com.au>
>
> current_rng holds one reference, and we bump it every time we want
> to do a read from it.
>
> This means we only hold the rng_mutex to grab or drop a reference,
> so accessing /sys/devices/virtual/misc/hw_random/rng_current doesn't
> block on read of /dev/hwrng.
>
> Using a kref is overkill (we're always under the rng_mutex), but
> a standard pattern.
>
> This also solves the problem that the hwrng_fillfn thread was
> accessing current_rng without a lock, which could change (eg. to NULL)
> underneath it.
>
> v4: decrease last reference for triggering the cleanup
This doesn't make any sense:
> +static void drop_current_rng(void)
> +{
> + struct hwrng *rng = current_rng;
> +
> + BUG_ON(!mutex_is_locked(&rng_mutex));
> + if (!current_rng)
> + return;
> +
> + /* release current_rng reference */
> + kref_put(¤t_rng->ref, cleanup_rng);
> + current_rng = NULL;
> +
> + /* decrease last reference for triggering the cleanup */
> + kref_put(&rng->ref, cleanup_rng);
> +}
Why would it drop the refcount twice? This doesn't make sense.
Hmm, because you added kref_init, which initializes the reference count
to 1, you created this bug.
Leave out the kref_init, and let it naturally be 0 (until, and if, it
becomes current_rng). Add a comment if you want.
Thanks,
Rusty.
next prev parent reply other threads:[~2014-11-12 3:41 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-03 15:56 [PATCH v4 0/6] fix hw_random stuck Amos Kong
2014-11-03 15:56 ` [PATCH v4 1/6] hw_random: place mutex around read functions and buffers Amos Kong
2014-11-03 15:56 ` [PATCH v4 2/6] hw_random: move some code out mutex_lock for avoiding underlying deadlock Amos Kong
2014-11-03 15:56 ` [PATCH v4 3/6] hw_random: use reference counts on each struct hwrng Amos Kong
2014-11-12 3:41 ` Rusty Russell [this message]
2014-11-17 15:20 ` Amos Kong
2014-11-03 15:56 ` [PATCH v4 4/6] hw_random: fix unregister race Amos Kong
2014-11-10 13:47 ` Herbert Xu
2014-11-12 4:47 ` Herbert Xu
2014-11-12 4:03 ` Rusty Russell
[not found] ` <87y4rhm5fv.fsf@rustcorp.com.au>
2014-11-25 7:42 ` Amos Kong
2014-12-06 3:51 ` Amos Kong
2014-11-03 15:56 ` [PATCH v4 5/6] hw_random: don't double-check old_rng Amos Kong
2014-11-03 15:56 ` [PATCH v4 6/6] hw_random: don't init list element we're about to add to list Amos Kong
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=87zjbxm6fw.fsf@rustcorp.com.au \
--to=rusty@rustcorp.com.au \
--cc=akong@redhat.com \
--cc=amit.shah@redhat.com \
--cc=herbert@gondor.apana.org.au \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=m@bues.ch \
--cc=mb@bu3sch.de \
--cc=mpm@selenic.com \
--cc=virtualization@lists.linux-foundation.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).