hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/char/hw_random/core.c
....@@ -4,7 +4,7 @@
44 * Copyright 2006 Michael Buesch <m@bues.ch>
55 * Copyright 2005 (c) MontaVista Software, Inc.
66 *
7
- * Please read Documentation/hw_random.txt for details on use.
7
+ * Please read Documentation/admin-guide/hw_random.rst for details on use.
88 *
99 * This software may be used and distributed according to the terms
1010 * of the GNU General Public License, incorporated herein by reference.
....@@ -15,6 +15,7 @@
1515 #include <linux/err.h>
1616 #include <linux/fs.h>
1717 #include <linux/hw_random.h>
18
+#include <linux/random.h>
1819 #include <linux/kernel.h>
1920 #include <linux/kthread.h>
2021 #include <linux/sched/signal.h>
....@@ -44,10 +45,10 @@
4445
4546 module_param(current_quality, ushort, 0644);
4647 MODULE_PARM_DESC(current_quality,
47
- "current hwrng entropy estimation per mill");
48
+ "current hwrng entropy estimation per 1024 bits of input");
4849 module_param(default_quality, ushort, 0644);
4950 MODULE_PARM_DESC(default_quality,
50
- "default entropy content of hwrng per mill");
51
+ "default entropy content of hwrng per 1024 bits of input");
5152
5253 static void drop_current_rng(void);
5354 static int hwrng_init(struct hwrng *rng);
....@@ -111,6 +112,14 @@
111112 }
112113
113114 /* Returns ERR_PTR(), NULL or refcounted hwrng */
115
+static struct hwrng *get_current_rng_nolock(void)
116
+{
117
+ if (current_rng)
118
+ kref_get(&current_rng->ref);
119
+
120
+ return current_rng;
121
+}
122
+
114123 static struct hwrng *get_current_rng(void)
115124 {
116125 struct hwrng *rng;
....@@ -118,9 +127,7 @@
118127 if (mutex_lock_interruptible(&rng_mutex))
119128 return ERR_PTR(-ERESTARTSYS);
120129
121
- rng = current_rng;
122
- if (rng)
123
- kref_get(&rng->ref);
130
+ rng = get_current_rng_nolock();
124131
125132 mutex_unlock(&rng_mutex);
126133 return rng;
....@@ -155,8 +162,6 @@
155162 reinit_completion(&rng->cleanup_done);
156163
157164 skip_init:
158
- add_early_randomness(rng);
159
-
160165 current_quality = rng->quality ? : default_quality;
161166 if (current_quality > 1024)
162167 current_quality = 1024;
....@@ -320,12 +325,13 @@
320325 const char *buf, size_t len)
321326 {
322327 int err = -ENODEV;
323
- struct hwrng *rng;
328
+ struct hwrng *rng, *old_rng, *new_rng;
324329
325330 err = mutex_lock_interruptible(&rng_mutex);
326331 if (err)
327332 return -ERESTARTSYS;
328333
334
+ old_rng = current_rng;
329335 if (sysfs_streq(buf, "")) {
330336 err = enable_best_rng();
331337 } else {
....@@ -337,8 +343,14 @@
337343 }
338344 }
339345 }
340
-
346
+ new_rng = get_current_rng_nolock();
341347 mutex_unlock(&rng_mutex);
348
+
349
+ if (new_rng) {
350
+ if (new_rng != old_rng)
351
+ add_early_randomness(new_rng);
352
+ put_rng(new_rng);
353
+ }
342354
343355 return err ? : len;
344356 }
....@@ -457,13 +469,15 @@
457469 int hwrng_register(struct hwrng *rng)
458470 {
459471 int err = -EINVAL;
460
- struct hwrng *old_rng, *tmp;
472
+ struct hwrng *tmp;
461473 struct list_head *rng_list_ptr;
474
+ bool is_new_current = false;
462475
463476 if (!rng->name || (!rng->data_read && !rng->read))
464477 goto out;
465478
466479 mutex_lock(&rng_mutex);
480
+
467481 /* Must not register two RNGs with the same name. */
468482 err = -EEXIST;
469483 list_for_each_entry(tmp, &rng_list, list) {
....@@ -482,10 +496,8 @@
482496 }
483497 list_add_tail(&rng->list, rng_list_ptr);
484498
485
- old_rng = current_rng;
486
- err = 0;
487
- if (!old_rng ||
488
- (!cur_rng_set_by_user && rng->quality > old_rng->quality)) {
499
+ if (!current_rng ||
500
+ (!cur_rng_set_by_user && rng->quality > current_rng->quality)) {
489501 /*
490502 * Set new rng as current as the new rng source
491503 * provides better entropy quality and was not
....@@ -494,19 +506,26 @@
494506 err = set_current_rng(rng);
495507 if (err)
496508 goto out_unlock;
509
+ /* to use current_rng in add_early_randomness() we need
510
+ * to take a ref
511
+ */
512
+ is_new_current = true;
513
+ kref_get(&rng->ref);
497514 }
498
-
499
- if (old_rng && !rng->init) {
515
+ mutex_unlock(&rng_mutex);
516
+ if (is_new_current || !rng->init) {
500517 /*
501518 * Use a new device's input to add some randomness to
502519 * the system. If this rng device isn't going to be
503520 * used right away, its init function hasn't been
504
- * called yet; so only use the randomness from devices
505
- * that don't need an init callback.
521
+ * called yet by set_current_rng(); so only use the
522
+ * randomness from devices that don't need an init callback
506523 */
507524 add_early_randomness(rng);
508525 }
509
-
526
+ if (is_new_current)
527
+ put_rng(rng);
528
+ return 0;
510529 out_unlock:
511530 mutex_unlock(&rng_mutex);
512531 out:
....@@ -516,10 +535,12 @@
516535
517536 void hwrng_unregister(struct hwrng *rng)
518537 {
538
+ struct hwrng *old_rng, *new_rng;
519539 int err;
520540
521541 mutex_lock(&rng_mutex);
522542
543
+ old_rng = current_rng;
523544 list_del(&rng->list);
524545 if (current_rng == rng) {
525546 err = enable_best_rng();
....@@ -529,12 +550,19 @@
529550 }
530551 }
531552
553
+ new_rng = get_current_rng_nolock();
532554 if (list_empty(&rng_list)) {
533555 mutex_unlock(&rng_mutex);
534556 if (hwrng_fill)
535557 kthread_stop(hwrng_fill);
536558 } else
537559 mutex_unlock(&rng_mutex);
560
+
561
+ if (new_rng) {
562
+ if (old_rng != new_rng)
563
+ add_early_randomness(new_rng);
564
+ put_rng(new_rng);
565
+ }
538566
539567 wait_for_completion(&rng->cleanup_done);
540568 }
....@@ -584,7 +612,7 @@
584612
585613 static int __init hwrng_modinit(void)
586614 {
587
- int ret = -ENOMEM;
615
+ int ret;
588616
589617 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
590618 rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
....@@ -617,11 +645,7 @@
617645 unregister_miscdev();
618646 }
619647
620
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
621
-rootfs_initcall(hwrng_modinit);
622
-#else
623648 module_init(hwrng_modinit);
624
-#endif
625649 module_exit(hwrng_modexit);
626650
627651 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");