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/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/miscdevice.h>
21 #include <linux/module.h>
22 #include <linux/random.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
27 #define RNG_MODULE_NAME "hw_random"
29 static struct hwrng
*current_rng
;
30 static struct task_struct
*hwrng_fill
;
31 static LIST_HEAD(rng_list
);
32 /* Protects rng_list and current_rng */
33 static DEFINE_MUTEX(rng_mutex
);
34 /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
35 static DEFINE_MUTEX(reading_mutex
);
36 static int data_avail
;
37 static u8
*rng_buffer
, *rng_fillbuf
;
38 static unsigned short current_quality
;
39 static unsigned short default_quality
; /* = 0; default to "off" */
41 module_param(current_quality
, ushort
, 0644);
42 MODULE_PARM_DESC(current_quality
,
43 "current hwrng entropy estimation per mill");
44 module_param(default_quality
, ushort
, 0644);
45 MODULE_PARM_DESC(default_quality
,
46 "default entropy content of hwrng per mill");
48 static void drop_current_rng(void);
49 static int hwrng_init(struct hwrng
*rng
);
50 static void start_khwrngd(void);
52 static inline int rng_get_data(struct hwrng
*rng
, u8
*buffer
, size_t size
,
55 static size_t rng_buffer_size(void)
57 return SMP_CACHE_BYTES
< 32 ? 32 : SMP_CACHE_BYTES
;
60 static void add_early_randomness(struct hwrng
*rng
)
63 size_t size
= min_t(size_t, 16, rng_buffer_size());
65 mutex_lock(&reading_mutex
);
66 bytes_read
= rng_get_data(rng
, rng_buffer
, size
, 1);
67 mutex_unlock(&reading_mutex
);
69 add_device_randomness(rng_buffer
, bytes_read
);
70 memset(rng_buffer
, 0, size
);
73 static inline void cleanup_rng(struct kref
*kref
)
75 struct hwrng
*rng
= container_of(kref
, struct hwrng
, ref
);
80 complete(&rng
->cleanup_done
);
83 static int set_current_rng(struct hwrng
*rng
)
87 BUG_ON(!mutex_is_locked(&rng_mutex
));
89 err
= hwrng_init(rng
);
99 static void drop_current_rng(void)
101 BUG_ON(!mutex_is_locked(&rng_mutex
));
105 /* decrease last reference for triggering the cleanup */
106 kref_put(¤t_rng
->ref
, cleanup_rng
);
110 /* Returns ERR_PTR(), NULL or refcounted hwrng */
111 static struct hwrng
*get_current_rng(void)
115 if (mutex_lock_interruptible(&rng_mutex
))
116 return ERR_PTR(-ERESTARTSYS
);
122 mutex_unlock(&rng_mutex
);
126 static void put_rng(struct hwrng
*rng
)
129 * Hold rng_mutex here so we serialize in case they set_current_rng
130 * on rng again immediately.
132 mutex_lock(&rng_mutex
);
134 kref_put(&rng
->ref
, cleanup_rng
);
135 mutex_unlock(&rng_mutex
);
138 static int hwrng_init(struct hwrng
*rng
)
140 if (kref_get_unless_zero(&rng
->ref
))
146 ret
= rng
->init(rng
);
151 kref_init(&rng
->ref
);
152 reinit_completion(&rng
->cleanup_done
);
155 add_early_randomness(rng
);
157 current_quality
= rng
->quality
? : default_quality
;
158 if (current_quality
> 1024)
159 current_quality
= 1024;
161 if (current_quality
== 0 && hwrng_fill
)
162 kthread_stop(hwrng_fill
);
163 if (current_quality
> 0 && !hwrng_fill
)
169 static int rng_dev_open(struct inode
*inode
, struct file
*filp
)
171 /* enforce read-only access to this chrdev */
172 if ((filp
->f_mode
& FMODE_READ
) == 0)
174 if (filp
->f_mode
& FMODE_WRITE
)
179 static inline int rng_get_data(struct hwrng
*rng
, u8
*buffer
, size_t size
,
183 BUG_ON(!mutex_is_locked(&reading_mutex
));
185 return rng
->read(rng
, (void *)buffer
, size
, wait
);
187 if (rng
->data_present
)
188 present
= rng
->data_present(rng
, wait
);
193 return rng
->data_read(rng
, (u32
*)buffer
);
198 static ssize_t
rng_dev_read(struct file
*filp
, char __user
*buf
,
199 size_t size
, loff_t
*offp
)
207 rng
= get_current_rng();
217 if (mutex_lock_interruptible(&reading_mutex
)) {
222 bytes_read
= rng_get_data(rng
, rng_buffer
,
224 !(filp
->f_flags
& O_NONBLOCK
));
225 if (bytes_read
< 0) {
227 goto out_unlock_reading
;
229 data_avail
= bytes_read
;
233 if (filp
->f_flags
& O_NONBLOCK
) {
235 goto out_unlock_reading
;
244 if (copy_to_user(buf
+ ret
, rng_buffer
+ data_avail
,
247 goto out_unlock_reading
;
254 mutex_unlock(&reading_mutex
);
258 schedule_timeout_interruptible(1);
260 if (signal_pending(current
)) {
266 memset(rng_buffer
, 0, rng_buffer_size());
270 mutex_unlock(&reading_mutex
);
276 static const struct file_operations rng_chrdev_ops
= {
277 .owner
= THIS_MODULE
,
278 .open
= rng_dev_open
,
279 .read
= rng_dev_read
,
280 .llseek
= noop_llseek
,
283 static const struct attribute_group
*rng_dev_groups
[];
285 static struct miscdevice rng_miscdev
= {
286 .minor
= HWRNG_MINOR
,
287 .name
= RNG_MODULE_NAME
,
289 .fops
= &rng_chrdev_ops
,
290 .groups
= rng_dev_groups
,
293 static ssize_t
hwrng_attr_current_store(struct device
*dev
,
294 struct device_attribute
*attr
,
295 const char *buf
, size_t len
)
300 err
= mutex_lock_interruptible(&rng_mutex
);
304 list_for_each_entry(rng
, &rng_list
, list
) {
305 if (sysfs_streq(rng
->name
, buf
)) {
307 if (rng
!= current_rng
)
308 err
= set_current_rng(rng
);
312 mutex_unlock(&rng_mutex
);
317 static ssize_t
hwrng_attr_current_show(struct device
*dev
,
318 struct device_attribute
*attr
,
324 rng
= get_current_rng();
328 ret
= snprintf(buf
, PAGE_SIZE
, "%s\n", rng
? rng
->name
: "none");
334 static ssize_t
hwrng_attr_available_show(struct device
*dev
,
335 struct device_attribute
*attr
,
341 err
= mutex_lock_interruptible(&rng_mutex
);
345 list_for_each_entry(rng
, &rng_list
, list
) {
346 strlcat(buf
, rng
->name
, PAGE_SIZE
);
347 strlcat(buf
, " ", PAGE_SIZE
);
349 strlcat(buf
, "\n", PAGE_SIZE
);
350 mutex_unlock(&rng_mutex
);
355 static DEVICE_ATTR(rng_current
, S_IRUGO
| S_IWUSR
,
356 hwrng_attr_current_show
,
357 hwrng_attr_current_store
);
358 static DEVICE_ATTR(rng_available
, S_IRUGO
,
359 hwrng_attr_available_show
,
362 static struct attribute
*rng_dev_attrs
[] = {
363 &dev_attr_rng_current
.attr
,
364 &dev_attr_rng_available
.attr
,
368 ATTRIBUTE_GROUPS(rng_dev
);
370 static void __exit
unregister_miscdev(void)
372 misc_deregister(&rng_miscdev
);
375 static int __init
register_miscdev(void)
377 return misc_register(&rng_miscdev
);
380 static int hwrng_fillfn(void *unused
)
384 while (!kthread_should_stop()) {
387 rng
= get_current_rng();
388 if (IS_ERR(rng
) || !rng
)
390 mutex_lock(&reading_mutex
);
391 rc
= rng_get_data(rng
, rng_fillbuf
,
392 rng_buffer_size(), 1);
393 mutex_unlock(&reading_mutex
);
396 pr_warn("hwrng: no data available\n");
397 msleep_interruptible(10000);
400 /* Outside lock, sure, but y'know: randomness. */
401 add_hwgenerator_randomness((void *)rng_fillbuf
, rc
,
402 rc
* current_quality
* 8 >> 10);
403 memset(rng_fillbuf
, 0, rng_buffer_size());
409 static void start_khwrngd(void)
411 hwrng_fill
= kthread_run(hwrng_fillfn
, NULL
, "hwrng");
412 if (IS_ERR(hwrng_fill
)) {
413 pr_err("hwrng_fill thread creation failed");
418 int hwrng_register(struct hwrng
*rng
)
421 struct hwrng
*old_rng
, *tmp
;
423 if (!rng
->name
|| (!rng
->data_read
&& !rng
->read
))
426 mutex_lock(&rng_mutex
);
427 /* Must not register two RNGs with the same name. */
429 list_for_each_entry(tmp
, &rng_list
, list
) {
430 if (strcmp(tmp
->name
, rng
->name
) == 0)
434 init_completion(&rng
->cleanup_done
);
435 complete(&rng
->cleanup_done
);
437 old_rng
= current_rng
;
440 err
= set_current_rng(rng
);
444 list_add_tail(&rng
->list
, &rng_list
);
446 if (old_rng
&& !rng
->init
) {
448 * Use a new device's input to add some randomness to
449 * the system. If this rng device isn't going to be
450 * used right away, its init function hasn't been
451 * called yet; so only use the randomness from devices
452 * that don't need an init callback.
454 add_early_randomness(rng
);
458 mutex_unlock(&rng_mutex
);
462 EXPORT_SYMBOL_GPL(hwrng_register
);
464 void hwrng_unregister(struct hwrng
*rng
)
466 mutex_lock(&rng_mutex
);
468 list_del(&rng
->list
);
469 if (current_rng
== rng
) {
471 if (!list_empty(&rng_list
)) {
474 tail
= list_entry(rng_list
.prev
, struct hwrng
, list
);
476 set_current_rng(tail
);
480 if (list_empty(&rng_list
)) {
481 mutex_unlock(&rng_mutex
);
483 kthread_stop(hwrng_fill
);
485 mutex_unlock(&rng_mutex
);
487 wait_for_completion(&rng
->cleanup_done
);
489 EXPORT_SYMBOL_GPL(hwrng_unregister
);
491 static void devm_hwrng_release(struct device
*dev
, void *res
)
493 hwrng_unregister(*(struct hwrng
**)res
);
496 static int devm_hwrng_match(struct device
*dev
, void *res
, void *data
)
498 struct hwrng
**r
= res
;
500 if (WARN_ON(!r
|| !*r
))
506 int devm_hwrng_register(struct device
*dev
, struct hwrng
*rng
)
511 ptr
= devres_alloc(devm_hwrng_release
, sizeof(*ptr
), GFP_KERNEL
);
515 error
= hwrng_register(rng
);
522 devres_add(dev
, ptr
);
525 EXPORT_SYMBOL_GPL(devm_hwrng_register
);
527 void devm_hwrng_unregister(struct device
*dev
, struct hwrng
*rng
)
529 devres_release(dev
, devm_hwrng_release
, devm_hwrng_match
, rng
);
531 EXPORT_SYMBOL_GPL(devm_hwrng_unregister
);
533 static int __init
hwrng_modinit(void)
537 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
538 rng_buffer
= kmalloc(rng_buffer_size(), GFP_KERNEL
);
542 rng_fillbuf
= kmalloc(rng_buffer_size(), GFP_KERNEL
);
548 ret
= register_miscdev();
557 static void __exit
hwrng_modexit(void)
559 mutex_lock(&rng_mutex
);
563 mutex_unlock(&rng_mutex
);
565 unregister_miscdev();
568 module_init(hwrng_modinit
);
569 module_exit(hwrng_modexit
);
571 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
572 MODULE_LICENSE("GPL");