Skip to content

Commit 10a515d

Browse files
Harald Freudenbergerherbertx
authored andcommitted
hwrng: remember rng chosen by user
When a user chooses a rng source via sysfs attribute this rng should be sticky, even when other sources with better quality to register. This patch introduces a simple way to remember the user's choice. This is reflected by a new sysfs attribute file 'rng_selected' which shows if the current rng has been chosen by userspace. The new attribute file shows '1' for user selected rng and '0' otherwise. Signed-off-by: Harald Freudenberger <[email protected]> Reviewed-by: PrasannaKumar Muralidharan <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 2bbb698 commit 10a515d

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

drivers/char/hw_random/core.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#define RNG_MODULE_NAME "hw_random"
2929

3030
static struct hwrng *current_rng;
31+
/* the current rng has been explicitly chosen by user via sysfs */
32+
static int cur_rng_set_by_user;
3133
static struct task_struct *hwrng_fill;
3234
/* list of registered rngs, sorted decending by quality */
3335
static LIST_HEAD(rng_list);
@@ -304,6 +306,7 @@ static ssize_t hwrng_attr_current_store(struct device *dev,
304306
list_for_each_entry(rng, &rng_list, list) {
305307
if (sysfs_streq(rng->name, buf)) {
306308
err = 0;
309+
cur_rng_set_by_user = 1;
307310
if (rng != current_rng)
308311
err = set_current_rng(rng);
309312
break;
@@ -352,16 +355,27 @@ static ssize_t hwrng_attr_available_show(struct device *dev,
352355
return strlen(buf);
353356
}
354357

358+
static ssize_t hwrng_attr_selected_show(struct device *dev,
359+
struct device_attribute *attr,
360+
char *buf)
361+
{
362+
return snprintf(buf, PAGE_SIZE, "%d\n", cur_rng_set_by_user);
363+
}
364+
355365
static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
356366
hwrng_attr_current_show,
357367
hwrng_attr_current_store);
358368
static DEVICE_ATTR(rng_available, S_IRUGO,
359369
hwrng_attr_available_show,
360370
NULL);
371+
static DEVICE_ATTR(rng_selected, S_IRUGO,
372+
hwrng_attr_selected_show,
373+
NULL);
361374

362375
static struct attribute *rng_dev_attrs[] = {
363376
&dev_attr_rng_current.attr,
364377
&dev_attr_rng_available.attr,
378+
&dev_attr_rng_selected.attr,
365379
NULL
366380
};
367381

@@ -444,10 +458,12 @@ int hwrng_register(struct hwrng *rng)
444458

445459
old_rng = current_rng;
446460
err = 0;
447-
if (!old_rng || (rng->quality > old_rng->quality)) {
461+
if (!old_rng ||
462+
(!cur_rng_set_by_user && rng->quality > old_rng->quality)) {
448463
/*
449464
* Set new rng as current as the new rng source
450-
* provides better entropy quality.
465+
* provides better entropy quality and was not
466+
* chosen by userspace.
451467
*/
452468
err = set_current_rng(rng);
453469
if (err)
@@ -479,6 +495,7 @@ void hwrng_unregister(struct hwrng *rng)
479495
list_del(&rng->list);
480496
if (current_rng == rng) {
481497
drop_current_rng();
498+
cur_rng_set_by_user = 0;
482499
/* rng_list is sorted by quality, use the best (=first) one */
483500
if (!list_empty(&rng_list)) {
484501
struct hwrng *new_rng;

0 commit comments

Comments
 (0)