From: Amos Kong <akong@redhat.com>
To: Rusty Russell <rusty@rustcorp.com.au>
Cc: m@bues.ch, kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
virtualization@lists.linux-foundation.org, mb@bu3sch.de,
mpm@selenic.com, amit.shah@redhat.com,
herbert@gondor.apana.org.au
Subject: Re: [PATCH v4 3/6] hw_random: use reference counts on each struct hwrng.
Date: Mon, 17 Nov 2014 23:20:53 +0800 [thread overview]
Message-ID: <20141117152053.GB12501@air.redhat.com> (raw)
In-Reply-To: <87zjbxm6fw.fsf@rustcorp.com.au>
[-- Attachment #1.1: Type: text/plain, Size: 2049 bytes --]
On Wed, Nov 12, 2014 at 02:11:23PM +1030, Rusty Russell wrote:
> 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.
I saw some kernel code uses kref_* helper functions, the reference
conter is initialized to 1. Some code didn't use the helper functions
to increase/decrease the reference counter. So I will drop kref_init()
and the second kref_put().
> Leave out the kref_init, and let it naturally be 0 (until, and if, it
> becomes current_rng). Add a comment if you want.
OK, thanks.
> Thanks,
> Rusty.
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Amos.
[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]
[-- Attachment #2: Type: text/plain, Size: 183 bytes --]
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
next prev parent reply other threads:[~2014-11-17 15:20 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
2014-11-17 15:20 ` Amos Kong [this message]
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
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=20141117152053.GB12501@air.redhat.com \
--to=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=rusty@rustcorp.com.au \
--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