2 * hw_random/core.c: HWRNG core API
4 * Copyright 2006 Michael Buesch <m@bues.ch>
5 * Copyright 2005 (c) MontaVista Software, Inc.
7 * Please read Documentation/hw_random.txt for details on use.
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
17 #include <linux/hw_random.h>
18 #include <linux/random.h>
19 #include <linux/kernel.h>
20 #include <linux/kthread.h>
21 #include <linux/sched/signal.h>
22 #include <linux/miscdevice.h>
23 #include <linux/module.h>
24 #include <linux/random.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
29 #define RNG_MODULE_NAME "hw_random"
31 static struct hwrng *current_rng;
32 /* the current rng has been explicitly chosen by user via sysfs */
33 static int cur_rng_set_by_user;
34 static struct task_struct *hwrng_fill;
35 /* list of registered rngs, sorted decending by quality */
36 static LIST_HEAD(rng_list);
37 /* Protects rng_list and current_rng */
38 static DEFINE_MUTEX(rng_mutex);
39 /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
40 static DEFINE_MUTEX(reading_mutex);
41 static int data_avail;
42 static u8 *rng_buffer, *rng_fillbuf;
43 static unsigned short current_quality;
44 static unsigned short default_quality; /* = 0; default to "off" */
46 module_param(current_quality, ushort, 0644);
47 MODULE_PARM_DESC(current_quality,
48 "current hwrng entropy estimation per mill");
49 module_param(default_quality, ushort, 0644);
50 MODULE_PARM_DESC(default_quality,
51 "default entropy content of hwrng per mill");
53 static void drop_current_rng(void);
54 static int hwrng_init(struct hwrng *rng);
55 static void start_khwrngd(void);
57 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
60 static size_t rng_buffer_size(void)
62 return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
65 static void add_early_randomness(struct hwrng *rng)
68 size_t size = min_t(size_t, 16, rng_buffer_size());
70 mutex_lock(&reading_mutex);
71 bytes_read = rng_get_data(rng, rng_buffer, size, 0);
72 mutex_unlock(&reading_mutex);
74 add_device_randomness(rng_buffer, bytes_read);
77 static inline void cleanup_rng(struct kref *kref)
79 struct hwrng *rng = container_of(kref, struct hwrng, ref);
84 complete(&rng->cleanup_done);
87 static int set_current_rng(struct hwrng *rng)
91 BUG_ON(!mutex_is_locked(&rng_mutex));
93 err = hwrng_init(rng);
103 static void drop_current_rng(void)
105 BUG_ON(!mutex_is_locked(&rng_mutex));
109 /* decrease last reference for triggering the cleanup */
110 kref_put(¤t_rng->ref, cleanup_rng);
114 /* Returns ERR_PTR(), NULL or refcounted hwrng */
115 static struct hwrng *get_current_rng(void)
119 if (mutex_lock_interruptible(&rng_mutex))
120 return ERR_PTR(-ERESTARTSYS);
126 mutex_unlock(&rng_mutex);
130 static void put_rng(struct hwrng *rng)
133 * Hold rng_mutex here so we serialize in case they set_current_rng
134 * on rng again immediately.
136 mutex_lock(&rng_mutex);
138 kref_put(&rng->ref, cleanup_rng);
139 mutex_unlock(&rng_mutex);
142 static int hwrng_init(struct hwrng *rng)
144 if (kref_get_unless_zero(&rng->ref))
150 ret = rng->init(rng);
155 kref_init(&rng->ref);
156 reinit_completion(&rng->cleanup_done);
159 add_early_randomness(rng);
161 current_quality = rng->quality ? : default_quality;
162 if (current_quality > 1024)
163 current_quality = 1024;
165 if (current_quality == 0 && hwrng_fill)
166 kthread_stop(hwrng_fill);
167 if (current_quality > 0 && !hwrng_fill)
173 static int rng_dev_open(struct inode *inode, struct file *filp)
175 /* enforce read-only access to this chrdev */
176 if ((filp->f_mode & FMODE_READ) == 0)
178 if (filp->f_mode & FMODE_WRITE)
183 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
187 BUG_ON(!mutex_is_locked(&reading_mutex));
189 return rng->read(rng, (void *)buffer, size, wait);
191 if (rng->data_present)
192 present = rng->data_present(rng, wait);
197 return rng->data_read(rng, (u32 *)buffer);
202 static ssize_t rng_dev_read(struct file *filp, char __user *buf,
203 size_t size, loff_t *offp)
211 rng = get_current_rng();
221 if (mutex_lock_interruptible(&reading_mutex)) {
226 bytes_read = rng_get_data(rng, rng_buffer,
228 !(filp->f_flags & O_NONBLOCK));
229 if (bytes_read < 0) {
231 goto out_unlock_reading;
233 data_avail = bytes_read;
237 if (filp->f_flags & O_NONBLOCK) {
239 goto out_unlock_reading;
248 if (copy_to_user(buf + ret, rng_buffer + data_avail,
251 goto out_unlock_reading;
258 mutex_unlock(&reading_mutex);
262 schedule_timeout_interruptible(1);
264 if (signal_pending(current)) {
273 mutex_unlock(&reading_mutex);
279 static const struct file_operations rng_chrdev_ops = {
280 .owner = THIS_MODULE,
281 .open = rng_dev_open,
282 .read = rng_dev_read,
283 .llseek = noop_llseek,
286 static const struct attribute_group *rng_dev_groups[];
288 static struct miscdevice rng_miscdev = {
289 .minor = HWRNG_MINOR,
290 .name = RNG_MODULE_NAME,
292 .fops = &rng_chrdev_ops,
293 .groups = rng_dev_groups,
296 static int enable_best_rng(void)
300 BUG_ON(!mutex_is_locked(&rng_mutex));
302 /* rng_list is sorted by quality, use the best (=first) one */
303 if (!list_empty(&rng_list)) {
304 struct hwrng *new_rng;
306 new_rng = list_entry(rng_list.next, struct hwrng, list);
307 ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
309 cur_rng_set_by_user = 0;
312 cur_rng_set_by_user = 0;
319 static ssize_t hwrng_attr_current_store(struct device *dev,
320 struct device_attribute *attr,
321 const char *buf, size_t len)
326 err = mutex_lock_interruptible(&rng_mutex);
330 if (sysfs_streq(buf, "")) {
331 err = enable_best_rng();
333 list_for_each_entry(rng, &rng_list, list) {
334 if (sysfs_streq(rng->name, buf)) {
335 cur_rng_set_by_user = 1;
336 err = set_current_rng(rng);
342 mutex_unlock(&rng_mutex);
347 static ssize_t hwrng_attr_current_show(struct device *dev,
348 struct device_attribute *attr,
354 rng = get_current_rng();
358 ret = snprintf(buf, PAGE_SIZE, "%s\n", rng ? rng->name : "none");
364 static ssize_t hwrng_attr_available_show(struct device *dev,
365 struct device_attribute *attr,
371 err = mutex_lock_interruptible(&rng_mutex);
375 list_for_each_entry(rng, &rng_list, list) {
376 strlcat(buf, rng->name, PAGE_SIZE);
377 strlcat(buf, " ", PAGE_SIZE);
379 strlcat(buf, "\n", PAGE_SIZE);
380 mutex_unlock(&rng_mutex);
385 static ssize_t hwrng_attr_selected_show(struct device *dev,
386 struct device_attribute *attr,
389 return snprintf(buf, PAGE_SIZE, "%d\n", cur_rng_set_by_user);
392 static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
393 hwrng_attr_current_show,
394 hwrng_attr_current_store);
395 static DEVICE_ATTR(rng_available, S_IRUGO,
396 hwrng_attr_available_show,
398 static DEVICE_ATTR(rng_selected, S_IRUGO,
399 hwrng_attr_selected_show,
402 static struct attribute *rng_dev_attrs[] = {
403 &dev_attr_rng_current.attr,
404 &dev_attr_rng_available.attr,
405 &dev_attr_rng_selected.attr,
409 ATTRIBUTE_GROUPS(rng_dev);
411 static void __exit unregister_miscdev(void)
413 misc_deregister(&rng_miscdev);
416 static int __init register_miscdev(void)
418 return misc_register(&rng_miscdev);
421 static int hwrng_fillfn(void *unused)
425 while (!kthread_should_stop()) {
428 rng = get_current_rng();
429 if (IS_ERR(rng) || !rng)
431 mutex_lock(&reading_mutex);
432 rc = rng_get_data(rng, rng_fillbuf,
433 rng_buffer_size(), 1);
434 mutex_unlock(&reading_mutex);
437 pr_warn("hwrng: no data available\n");
438 msleep_interruptible(10000);
441 /* Outside lock, sure, but y'know: randomness. */
442 add_hwgenerator_randomness((void *)rng_fillbuf, rc,
443 rc * current_quality * 8 >> 10);
449 static void start_khwrngd(void)
451 hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
452 if (IS_ERR(hwrng_fill)) {
453 pr_err("hwrng_fill thread creation failed\n");
458 int hwrng_register(struct hwrng *rng)
461 struct hwrng *old_rng, *tmp;
462 struct list_head *rng_list_ptr;
464 if (!rng->name || (!rng->data_read && !rng->read))
467 mutex_lock(&rng_mutex);
468 /* Must not register two RNGs with the same name. */
470 list_for_each_entry(tmp, &rng_list, list) {
471 if (strcmp(tmp->name, rng->name) == 0)
475 init_completion(&rng->cleanup_done);
476 complete(&rng->cleanup_done);
478 /* rng_list is sorted by decreasing quality */
479 list_for_each(rng_list_ptr, &rng_list) {
480 tmp = list_entry(rng_list_ptr, struct hwrng, list);
481 if (tmp->quality < rng->quality)
484 list_add_tail(&rng->list, rng_list_ptr);
486 old_rng = current_rng;
489 (!cur_rng_set_by_user && rng->quality > old_rng->quality)) {
491 * Set new rng as current as the new rng source
492 * provides better entropy quality and was not
493 * chosen by userspace.
495 err = set_current_rng(rng);
500 if (old_rng && !rng->init) {
502 * Use a new device's input to add some randomness to
503 * the system. If this rng device isn't going to be
504 * used right away, its init function hasn't been
505 * called yet; so only use the randomness from devices
506 * that don't need an init callback.
508 add_early_randomness(rng);
512 mutex_unlock(&rng_mutex);
516 EXPORT_SYMBOL_GPL(hwrng_register);
518 void hwrng_unregister(struct hwrng *rng)
522 mutex_lock(&rng_mutex);
524 list_del(&rng->list);
525 if (current_rng == rng) {
526 err = enable_best_rng();
529 cur_rng_set_by_user = 0;
533 if (list_empty(&rng_list)) {
534 mutex_unlock(&rng_mutex);
536 kthread_stop(hwrng_fill);
538 mutex_unlock(&rng_mutex);
540 wait_for_completion(&rng->cleanup_done);
542 EXPORT_SYMBOL_GPL(hwrng_unregister);
544 static void devm_hwrng_release(struct device *dev, void *res)
546 hwrng_unregister(*(struct hwrng **)res);
549 static int devm_hwrng_match(struct device *dev, void *res, void *data)
551 struct hwrng **r = res;
553 if (WARN_ON(!r || !*r))
559 int devm_hwrng_register(struct device *dev, struct hwrng *rng)
564 ptr = devres_alloc(devm_hwrng_release, sizeof(*ptr), GFP_KERNEL);
568 error = hwrng_register(rng);
575 devres_add(dev, ptr);
578 EXPORT_SYMBOL_GPL(devm_hwrng_register);
580 void devm_hwrng_unregister(struct device *dev, struct hwrng *rng)
582 devres_release(dev, devm_hwrng_release, devm_hwrng_match, rng);
584 EXPORT_SYMBOL_GPL(devm_hwrng_unregister);
586 static int __init hwrng_modinit(void)
590 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
591 rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
595 rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
601 ret = register_miscdev();
610 static void __exit hwrng_modexit(void)
612 mutex_lock(&rng_mutex);
616 mutex_unlock(&rng_mutex);
618 unregister_miscdev();
621 module_init(hwrng_modinit);
622 module_exit(hwrng_modexit);
624 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
625 MODULE_LICENSE("GPL");