add_early_randomness() is called every time a new rng backend is added
and every time it is set as the current rng provider.
add_early_randomness() is called from functions locking rng_mutex,
and if it hangs all the hw_random framework hangs: we can't read sysfs,
add or remove a backend.
This patch move add_early_randomness() out of the rng_mutex zone.
It only needs the reading_mutex.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
}
/* Returns ERR_PTR(), NULL or refcounted hwrng */
}
/* Returns ERR_PTR(), NULL or refcounted hwrng */
+static struct hwrng *get_current_rng_nolock(void)
+{
+ if (current_rng)
+ kref_get(¤t_rng->ref);
+
+ return current_rng;
+}
+
static struct hwrng *get_current_rng(void)
{
struct hwrng *rng;
static struct hwrng *get_current_rng(void)
{
struct hwrng *rng;
if (mutex_lock_interruptible(&rng_mutex))
return ERR_PTR(-ERESTARTSYS);
if (mutex_lock_interruptible(&rng_mutex))
return ERR_PTR(-ERESTARTSYS);
- rng = current_rng;
- if (rng)
- kref_get(&rng->ref);
+ rng = get_current_rng_nolock();
mutex_unlock(&rng_mutex);
return rng;
mutex_unlock(&rng_mutex);
return rng;
reinit_completion(&rng->cleanup_done);
skip_init:
reinit_completion(&rng->cleanup_done);
skip_init:
- add_early_randomness(rng);
-
current_quality = rng->quality ? : default_quality;
if (current_quality > 1024)
current_quality = 1024;
current_quality = rng->quality ? : default_quality;
if (current_quality > 1024)
current_quality = 1024;
const char *buf, size_t len)
{
int err = -ENODEV;
const char *buf, size_t len)
{
int err = -ENODEV;
+ struct hwrng *rng, *old_rng, *new_rng;
err = mutex_lock_interruptible(&rng_mutex);
if (err)
return -ERESTARTSYS;
err = mutex_lock_interruptible(&rng_mutex);
if (err)
return -ERESTARTSYS;
if (sysfs_streq(buf, "")) {
err = enable_best_rng();
} else {
if (sysfs_streq(buf, "")) {
err = enable_best_rng();
} else {
+ new_rng = get_current_rng_nolock();
mutex_unlock(&rng_mutex);
mutex_unlock(&rng_mutex);
+ if (new_rng) {
+ if (new_rng != old_rng)
+ add_early_randomness(new_rng);
+ put_rng(new_rng);
+ }
+
int hwrng_register(struct hwrng *rng)
{
int err = -EINVAL;
int hwrng_register(struct hwrng *rng)
{
int err = -EINVAL;
- struct hwrng *old_rng, *tmp;
+ struct hwrng *old_rng, *new_rng, *tmp;
struct list_head *rng_list_ptr;
if (!rng->name || (!rng->data_read && !rng->read))
goto out;
mutex_lock(&rng_mutex);
struct list_head *rng_list_ptr;
if (!rng->name || (!rng->data_read && !rng->read))
goto out;
mutex_lock(&rng_mutex);
+
+ old_rng = current_rng;
+ new_rng = NULL;
+
/* Must not register two RNGs with the same name. */
err = -EEXIST;
list_for_each_entry(tmp, &rng_list, list) {
/* Must not register two RNGs with the same name. */
err = -EEXIST;
list_for_each_entry(tmp, &rng_list, list) {
}
list_add_tail(&rng->list, rng_list_ptr);
}
list_add_tail(&rng->list, rng_list_ptr);
err = 0;
if (!old_rng ||
(!cur_rng_set_by_user && rng->quality > old_rng->quality)) {
err = 0;
if (!old_rng ||
(!cur_rng_set_by_user && rng->quality > old_rng->quality)) {
- if (old_rng && !rng->init) {
+ new_rng = rng;
+ kref_get(&new_rng->ref);
+out_unlock:
+ mutex_unlock(&rng_mutex);
+
+ if (new_rng) {
+ if (new_rng != old_rng || !rng->init) {
/*
* Use a new device's input to add some randomness to
* the system. If this rng device isn't going to be
* used right away, its init function hasn't been
/*
* Use a new device's input to add some randomness to
* the system. If this rng device isn't going to be
* used right away, its init function hasn't been
- * called yet; so only use the randomness from devices
- * that don't need an init callback.
+ * called yet by set_current_rng(); so only use the
+ * randomness from devices that don't need an init callback
- add_early_randomness(rng);
+ add_early_randomness(new_rng);
+ }
+ put_rng(new_rng);
-
-out_unlock:
- mutex_unlock(&rng_mutex);
void hwrng_unregister(struct hwrng *rng)
{
void hwrng_unregister(struct hwrng *rng)
{
+ struct hwrng *old_rng, *new_rng;
int err;
mutex_lock(&rng_mutex);
int err;
mutex_lock(&rng_mutex);
list_del(&rng->list);
if (current_rng == rng) {
err = enable_best_rng();
list_del(&rng->list);
if (current_rng == rng) {
err = enable_best_rng();
+ new_rng = get_current_rng_nolock();
if (list_empty(&rng_list)) {
mutex_unlock(&rng_mutex);
if (hwrng_fill)
if (list_empty(&rng_list)) {
mutex_unlock(&rng_mutex);
if (hwrng_fill)
} else
mutex_unlock(&rng_mutex);
} else
mutex_unlock(&rng_mutex);
+ if (new_rng) {
+ if (old_rng != new_rng)
+ add_early_randomness(new_rng);
+ put_rng(new_rng);
+ }
+
wait_for_completion(&rng->cleanup_done);
}
EXPORT_SYMBOL_GPL(hwrng_unregister);
wait_for_completion(&rng->cleanup_done);
}
EXPORT_SYMBOL_GPL(hwrng_unregister);