Merge tag 'v4.5-rc5' into devel
[deliverable/linux.git] / drivers / char / hw_random / core.c
CommitLineData
844dd05f
MB
1/*
2 Added support for the AMD Geode LX RNG
3 (c) Copyright 2004-2005 Advanced Micro Devices, Inc.
4
5 derived from
6
7 Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
8 (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
9
10 derived from
11
12 Hardware driver for the AMD 768 Random Number Generator (RNG)
13 (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
14
15 derived from
16
17 Hardware driver for Intel i810 Random Number Generator (RNG)
18 Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
19 Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
20
21 Added generic RNG API
eb032b98 22 Copyright 2006 Michael Buesch <m@bues.ch>
844dd05f
MB
23 Copyright 2005 (c) MontaVista Software, Inc.
24
25 Please read Documentation/hw_random.txt for details on use.
26
27 ----------------------------------------------------------
28 This software may be used and distributed according to the terms
29 of the GNU General Public License, incorporated herein by reference.
30
31 */
32
33
34#include <linux/device.h>
35#include <linux/hw_random.h>
36#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/fs.h>
914e2637 39#include <linux/sched.h>
844dd05f 40#include <linux/miscdevice.h>
be4000bc 41#include <linux/kthread.h>
844dd05f 42#include <linux/delay.h>
f7f154f1 43#include <linux/slab.h>
d9e79726 44#include <linux/random.h>
3a2c0ba5 45#include <linux/err.h>
844dd05f
MB
46#include <asm/uaccess.h>
47
48
49#define RNG_MODULE_NAME "hw_random"
50#define PFX RNG_MODULE_NAME ": "
51#define RNG_MISCDEV_MINOR 183 /* official */
52
53
54static struct hwrng *current_rng;
be4000bc 55static struct task_struct *hwrng_fill;
844dd05f 56static LIST_HEAD(rng_list);
9372b35e 57/* Protects rng_list and current_rng */
844dd05f 58static DEFINE_MUTEX(rng_mutex);
9372b35e
RR
59/* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
60static DEFINE_MUTEX(reading_mutex);
9996508b 61static int data_avail;
be4000bc 62static u8 *rng_buffer, *rng_fillbuf;
0f734e6e
TD
63static unsigned short current_quality;
64static unsigned short default_quality; /* = 0; default to "off" */
be4000bc
TD
65
66module_param(current_quality, ushort, 0644);
67MODULE_PARM_DESC(current_quality,
68 "current hwrng entropy estimation per mill");
0f734e6e
TD
69module_param(default_quality, ushort, 0644);
70MODULE_PARM_DESC(default_quality,
71 "default entropy content of hwrng per mill");
be4000bc 72
ff77c150 73static void drop_current_rng(void);
90ac41bd 74static int hwrng_init(struct hwrng *rng);
be4000bc 75static void start_khwrngd(void);
f7f154f1 76
d3cc7996
AS
77static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
78 int wait);
79
f7f154f1
RR
80static size_t rng_buffer_size(void)
81{
82 return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
83}
844dd05f 84
d3cc7996
AS
85static void add_early_randomness(struct hwrng *rng)
86{
87 unsigned char bytes[16];
88 int bytes_read;
89
9372b35e 90 mutex_lock(&reading_mutex);
d3cc7996 91 bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
9372b35e 92 mutex_unlock(&reading_mutex);
d3cc7996
AS
93 if (bytes_read > 0)
94 add_device_randomness(bytes, bytes_read);
95}
96
3a2c0ba5
RR
97static inline void cleanup_rng(struct kref *kref)
98{
99 struct hwrng *rng = container_of(kref, struct hwrng, ref);
100
101 if (rng->cleanup)
102 rng->cleanup(rng);
a027f30d 103
77584ee5 104 complete(&rng->cleanup_done);
3a2c0ba5
RR
105}
106
90ac41bd 107static int set_current_rng(struct hwrng *rng)
3a2c0ba5 108{
90ac41bd
HX
109 int err;
110
3a2c0ba5 111 BUG_ON(!mutex_is_locked(&rng_mutex));
90ac41bd
HX
112
113 err = hwrng_init(rng);
114 if (err)
115 return err;
116
ff77c150 117 drop_current_rng();
3a2c0ba5 118 current_rng = rng;
90ac41bd
HX
119
120 return 0;
3a2c0ba5
RR
121}
122
123static void drop_current_rng(void)
124{
125 BUG_ON(!mutex_is_locked(&rng_mutex));
126 if (!current_rng)
127 return;
128
129 /* decrease last reference for triggering the cleanup */
130 kref_put(&current_rng->ref, cleanup_rng);
131 current_rng = NULL;
132}
133
134/* Returns ERR_PTR(), NULL or refcounted hwrng */
135static struct hwrng *get_current_rng(void)
136{
137 struct hwrng *rng;
138
139 if (mutex_lock_interruptible(&rng_mutex))
140 return ERR_PTR(-ERESTARTSYS);
141
142 rng = current_rng;
143 if (rng)
144 kref_get(&rng->ref);
145
146 mutex_unlock(&rng_mutex);
147 return rng;
148}
149
150static void put_rng(struct hwrng *rng)
151{
152 /*
153 * Hold rng_mutex here so we serialize in case they set_current_rng
154 * on rng again immediately.
155 */
156 mutex_lock(&rng_mutex);
157 if (rng)
158 kref_put(&rng->ref, cleanup_rng);
159 mutex_unlock(&rng_mutex);
160}
161
90ac41bd 162static int hwrng_init(struct hwrng *rng)
844dd05f 163{
15b66cd5
HX
164 if (kref_get_unless_zero(&rng->ref))
165 goto skip_init;
166
d3cc7996
AS
167 if (rng->init) {
168 int ret;
169
170 ret = rng->init(rng);
171 if (ret)
172 return ret;
173 }
15b66cd5
HX
174
175 kref_init(&rng->ref);
176 reinit_completion(&rng->cleanup_done);
177
178skip_init:
d3cc7996 179 add_early_randomness(rng);
be4000bc 180
0f734e6e 181 current_quality = rng->quality ? : default_quality;
506bf0c0
KP
182 if (current_quality > 1024)
183 current_quality = 1024;
0f734e6e
TD
184
185 if (current_quality == 0 && hwrng_fill)
186 kthread_stop(hwrng_fill);
be4000bc
TD
187 if (current_quality > 0 && !hwrng_fill)
188 start_khwrngd();
189
d3cc7996 190 return 0;
844dd05f
MB
191}
192
844dd05f
MB
193static int rng_dev_open(struct inode *inode, struct file *filp)
194{
195 /* enforce read-only access to this chrdev */
196 if ((filp->f_mode & FMODE_READ) == 0)
197 return -EINVAL;
198 if (filp->f_mode & FMODE_WRITE)
199 return -EINVAL;
200 return 0;
201}
202
9996508b
IM
203static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
204 int wait) {
205 int present;
206
9372b35e 207 BUG_ON(!mutex_is_locked(&reading_mutex));
9996508b
IM
208 if (rng->read)
209 return rng->read(rng, (void *)buffer, size, wait);
210
211 if (rng->data_present)
212 present = rng->data_present(rng, wait);
213 else
214 present = 1;
215
216 if (present)
217 return rng->data_read(rng, (u32 *)buffer);
218
219 return 0;
220}
221
844dd05f
MB
222static ssize_t rng_dev_read(struct file *filp, char __user *buf,
223 size_t size, loff_t *offp)
224{
844dd05f 225 ssize_t ret = 0;
984e976f 226 int err = 0;
9996508b 227 int bytes_read, len;
3a2c0ba5 228 struct hwrng *rng;
844dd05f
MB
229
230 while (size) {
3a2c0ba5
RR
231 rng = get_current_rng();
232 if (IS_ERR(rng)) {
233 err = PTR_ERR(rng);
844dd05f 234 goto out;
9996508b 235 }
3a2c0ba5 236 if (!rng) {
844dd05f 237 err = -ENODEV;
3a2c0ba5 238 goto out;
844dd05f 239 }
984e976f 240
1ab87298
JS
241 if (mutex_lock_interruptible(&reading_mutex)) {
242 err = -ERESTARTSYS;
243 goto out_put;
244 }
9996508b 245 if (!data_avail) {
3a2c0ba5 246 bytes_read = rng_get_data(rng, rng_buffer,
f7f154f1 247 rng_buffer_size(),
9996508b
IM
248 !(filp->f_flags & O_NONBLOCK));
249 if (bytes_read < 0) {
250 err = bytes_read;
9372b35e 251 goto out_unlock_reading;
9996508b
IM
252 }
253 data_avail = bytes_read;
893f1128 254 }
844dd05f 255
9996508b
IM
256 if (!data_avail) {
257 if (filp->f_flags & O_NONBLOCK) {
258 err = -EAGAIN;
9372b35e 259 goto out_unlock_reading;
9996508b
IM
260 }
261 } else {
262 len = data_avail;
263 if (len > size)
264 len = size;
265
266 data_avail -= len;
267
268 if (copy_to_user(buf + ret, rng_buffer + data_avail,
269 len)) {
270 err = -EFAULT;
9372b35e 271 goto out_unlock_reading;
9996508b
IM
272 }
273
274 size -= len;
275 ret += len;
844dd05f
MB
276 }
277
9372b35e 278 mutex_unlock(&reading_mutex);
3a2c0ba5 279 put_rng(rng);
9996508b 280
844dd05f
MB
281 if (need_resched())
282 schedule_timeout_interruptible(1);
9996508b
IM
283
284 if (signal_pending(current)) {
285 err = -ERESTARTSYS;
844dd05f 286 goto out;
9996508b 287 }
844dd05f
MB
288 }
289out:
290 return ret ? : err;
3a2c0ba5 291
9372b35e
RR
292out_unlock_reading:
293 mutex_unlock(&reading_mutex);
1ab87298 294out_put:
3a2c0ba5
RR
295 put_rng(rng);
296 goto out;
844dd05f
MB
297}
298
299
62322d25 300static const struct file_operations rng_chrdev_ops = {
844dd05f
MB
301 .owner = THIS_MODULE,
302 .open = rng_dev_open,
303 .read = rng_dev_read,
6038f373 304 .llseek = noop_llseek,
844dd05f
MB
305};
306
0daa7a0a
TI
307static const struct attribute_group *rng_dev_groups[];
308
844dd05f
MB
309static struct miscdevice rng_miscdev = {
310 .minor = RNG_MISCDEV_MINOR,
311 .name = RNG_MODULE_NAME,
e454cea2 312 .nodename = "hwrng",
844dd05f 313 .fops = &rng_chrdev_ops,
0daa7a0a 314 .groups = rng_dev_groups,
844dd05f
MB
315};
316
317
94fbcded
GKH
318static ssize_t hwrng_attr_current_store(struct device *dev,
319 struct device_attribute *attr,
844dd05f
MB
320 const char *buf, size_t len)
321{
322 int err;
323 struct hwrng *rng;
324
325 err = mutex_lock_interruptible(&rng_mutex);
326 if (err)
327 return -ERESTARTSYS;
328 err = -ENODEV;
329 list_for_each_entry(rng, &rng_list, list) {
d9a53b01 330 if (sysfs_streq(rng->name, buf)) {
844dd05f 331 err = 0;
90ac41bd
HX
332 if (rng != current_rng)
333 err = set_current_rng(rng);
844dd05f
MB
334 break;
335 }
336 }
337 mutex_unlock(&rng_mutex);
338
339 return err ? : len;
340}
341
94fbcded
GKH
342static ssize_t hwrng_attr_current_show(struct device *dev,
343 struct device_attribute *attr,
844dd05f
MB
344 char *buf)
345{
844dd05f 346 ssize_t ret;
3a2c0ba5 347 struct hwrng *rng;
844dd05f 348
3a2c0ba5
RR
349 rng = get_current_rng();
350 if (IS_ERR(rng))
351 return PTR_ERR(rng);
352
353 ret = snprintf(buf, PAGE_SIZE, "%s\n", rng ? rng->name : "none");
354 put_rng(rng);
844dd05f
MB
355
356 return ret;
357}
358
94fbcded
GKH
359static ssize_t hwrng_attr_available_show(struct device *dev,
360 struct device_attribute *attr,
844dd05f
MB
361 char *buf)
362{
363 int err;
844dd05f
MB
364 struct hwrng *rng;
365
366 err = mutex_lock_interruptible(&rng_mutex);
367 if (err)
368 return -ERESTARTSYS;
369 buf[0] = '\0';
370 list_for_each_entry(rng, &rng_list, list) {
61daf055
RS
371 strlcat(buf, rng->name, PAGE_SIZE);
372 strlcat(buf, " ", PAGE_SIZE);
844dd05f 373 }
61daf055 374 strlcat(buf, "\n", PAGE_SIZE);
844dd05f
MB
375 mutex_unlock(&rng_mutex);
376
61daf055 377 return strlen(buf);
844dd05f
MB
378}
379
94fbcded
GKH
380static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
381 hwrng_attr_current_show,
382 hwrng_attr_current_store);
383static DEVICE_ATTR(rng_available, S_IRUGO,
384 hwrng_attr_available_show,
385 NULL);
844dd05f 386
0daa7a0a
TI
387static struct attribute *rng_dev_attrs[] = {
388 &dev_attr_rng_current.attr,
389 &dev_attr_rng_available.attr,
390 NULL
391};
392
393ATTRIBUTE_GROUPS(rng_dev);
844dd05f 394
ac3a497f 395static void __exit unregister_miscdev(void)
844dd05f 396{
b844eba2 397 misc_deregister(&rng_miscdev);
844dd05f
MB
398}
399
ac3a497f 400static int __init register_miscdev(void)
844dd05f 401{
0daa7a0a 402 return misc_register(&rng_miscdev);
844dd05f
MB
403}
404
be4000bc
TD
405static int hwrng_fillfn(void *unused)
406{
407 long rc;
408
409 while (!kthread_should_stop()) {
3a2c0ba5
RR
410 struct hwrng *rng;
411
412 rng = get_current_rng();
413 if (IS_ERR(rng) || !rng)
be4000bc 414 break;
9372b35e 415 mutex_lock(&reading_mutex);
3a2c0ba5 416 rc = rng_get_data(rng, rng_fillbuf,
be4000bc 417 rng_buffer_size(), 1);
9372b35e 418 mutex_unlock(&reading_mutex);
3a2c0ba5 419 put_rng(rng);
be4000bc
TD
420 if (rc <= 0) {
421 pr_warn("hwrng: no data available\n");
422 msleep_interruptible(10000);
423 continue;
424 }
9372b35e 425 /* Outside lock, sure, but y'know: randomness. */
be4000bc 426 add_hwgenerator_randomness((void *)rng_fillbuf, rc,
e02b8765 427 rc * current_quality * 8 >> 10);
be4000bc 428 }
9dda727d 429 hwrng_fill = NULL;
be4000bc
TD
430 return 0;
431}
432
433static void start_khwrngd(void)
434{
435 hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
17fb874d 436 if (IS_ERR(hwrng_fill)) {
be4000bc
TD
437 pr_err("hwrng_fill thread creation failed");
438 hwrng_fill = NULL;
439 }
440}
441
844dd05f
MB
442int hwrng_register(struct hwrng *rng)
443{
844dd05f
MB
444 int err = -EINVAL;
445 struct hwrng *old_rng, *tmp;
446
447 if (rng->name == NULL ||
9996508b 448 (rng->data_read == NULL && rng->read == NULL))
844dd05f
MB
449 goto out;
450
451 mutex_lock(&rng_mutex);
452
f7f154f1
RR
453 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
454 err = -ENOMEM;
455 if (!rng_buffer) {
456 rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
457 if (!rng_buffer)
458 goto out_unlock;
459 }
be4000bc
TD
460 if (!rng_fillbuf) {
461 rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
462 if (!rng_fillbuf) {
463 kfree(rng_buffer);
464 goto out_unlock;
465 }
466 }
f7f154f1 467
844dd05f
MB
468 /* Must not register two RNGs with the same name. */
469 err = -EEXIST;
470 list_for_each_entry(tmp, &rng_list, list) {
471 if (strcmp(tmp->name, rng->name) == 0)
472 goto out_unlock;
473 }
474
15b66cd5
HX
475 init_completion(&rng->cleanup_done);
476 complete(&rng->cleanup_done);
477
844dd05f 478 old_rng = current_rng;
ebbbfa24 479 err = 0;
844dd05f 480 if (!old_rng) {
90ac41bd 481 err = set_current_rng(rng);
844dd05f
MB
482 if (err)
483 goto out_unlock;
844dd05f 484 }
844dd05f 485 list_add_tail(&rng->list, &rng_list);
d9e79726 486
d3cc7996
AS
487 if (old_rng && !rng->init) {
488 /*
489 * Use a new device's input to add some randomness to
490 * the system. If this rng device isn't going to be
491 * used right away, its init function hasn't been
492 * called yet; so only use the randomness from devices
493 * that don't need an init callback.
494 */
495 add_early_randomness(rng);
496 }
497
844dd05f
MB
498out_unlock:
499 mutex_unlock(&rng_mutex);
500out:
501 return err;
502}
503EXPORT_SYMBOL_GPL(hwrng_register);
504
b844eba2 505void hwrng_unregister(struct hwrng *rng)
844dd05f 506{
844dd05f
MB
507 mutex_lock(&rng_mutex);
508
509 list_del(&rng->list);
510 if (current_rng == rng) {
3a2c0ba5
RR
511 drop_current_rng();
512 if (!list_empty(&rng_list)) {
513 struct hwrng *tail;
514
515 tail = list_entry(rng_list.prev, struct hwrng, list);
516
90ac41bd 517 set_current_rng(tail);
844dd05f
MB
518 }
519 }
3a2c0ba5 520
be4000bc 521 if (list_empty(&rng_list)) {
1dacb395 522 mutex_unlock(&rng_mutex);
be4000bc
TD
523 if (hwrng_fill)
524 kthread_stop(hwrng_fill);
1dacb395
AK
525 } else
526 mutex_unlock(&rng_mutex);
a027f30d 527
77584ee5 528 wait_for_completion(&rng->cleanup_done);
844dd05f 529}
b844eba2 530EXPORT_SYMBOL_GPL(hwrng_unregister);
844dd05f 531
4d9b519c
DT
532static void devm_hwrng_release(struct device *dev, void *res)
533{
534 hwrng_unregister(*(struct hwrng **)res);
535}
536
537static int devm_hwrng_match(struct device *dev, void *res, void *data)
538{
539 struct hwrng **r = res;
540
541 if (WARN_ON(!r || !*r))
542 return 0;
543
544 return *r == data;
545}
546
547int devm_hwrng_register(struct device *dev, struct hwrng *rng)
548{
549 struct hwrng **ptr;
550 int error;
551
552 ptr = devres_alloc(devm_hwrng_release, sizeof(*ptr), GFP_KERNEL);
553 if (!ptr)
554 return -ENOMEM;
555
556 error = hwrng_register(rng);
557 if (error) {
558 devres_free(ptr);
559 return error;
560 }
561
562 *ptr = rng;
563 devres_add(dev, ptr);
564 return 0;
565}
566EXPORT_SYMBOL_GPL(devm_hwrng_register);
567
568void devm_hwrng_unregister(struct device *dev, struct hwrng *rng)
569{
570 devres_release(dev, devm_hwrng_release, devm_hwrng_match, rng);
571}
572EXPORT_SYMBOL_GPL(devm_hwrng_unregister);
573
ac3a497f
HX
574static int __init hwrng_modinit(void)
575{
576 return register_miscdev();
577}
578
579static void __exit hwrng_modexit(void)
b7d44d94
ST
580{
581 mutex_lock(&rng_mutex);
582 BUG_ON(current_rng);
583 kfree(rng_buffer);
be4000bc 584 kfree(rng_fillbuf);
b7d44d94 585 mutex_unlock(&rng_mutex);
ac3a497f
HX
586
587 unregister_miscdev();
b7d44d94
ST
588}
589
ac3a497f
HX
590module_init(hwrng_modinit);
591module_exit(hwrng_modexit);
844dd05f
MB
592
593MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
594MODULE_LICENSE("GPL");
This page took 0.790153 seconds and 5 git commands to generate.