hwrng: use reference counts on each struct hwrng.
[linux-block.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
TD
72
73static void start_khwrngd(void);
f7f154f1 74
d3cc7996
AS
75static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
76 int wait);
77
f7f154f1
RR
78static size_t rng_buffer_size(void)
79{
80 return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
81}
844dd05f 82
d3cc7996
AS
83static void add_early_randomness(struct hwrng *rng)
84{
85 unsigned char bytes[16];
86 int bytes_read;
87
9372b35e 88 mutex_lock(&reading_mutex);
d3cc7996 89 bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
9372b35e 90 mutex_unlock(&reading_mutex);
d3cc7996
AS
91 if (bytes_read > 0)
92 add_device_randomness(bytes, bytes_read);
93}
94
3a2c0ba5
RR
95static inline void cleanup_rng(struct kref *kref)
96{
97 struct hwrng *rng = container_of(kref, struct hwrng, ref);
98
99 if (rng->cleanup)
100 rng->cleanup(rng);
101}
102
103static void set_current_rng(struct hwrng *rng)
104{
105 BUG_ON(!mutex_is_locked(&rng_mutex));
106 kref_get(&rng->ref);
107 current_rng = rng;
108}
109
110static void drop_current_rng(void)
111{
112 BUG_ON(!mutex_is_locked(&rng_mutex));
113 if (!current_rng)
114 return;
115
116 /* decrease last reference for triggering the cleanup */
117 kref_put(&current_rng->ref, cleanup_rng);
118 current_rng = NULL;
119}
120
121/* Returns ERR_PTR(), NULL or refcounted hwrng */
122static struct hwrng *get_current_rng(void)
123{
124 struct hwrng *rng;
125
126 if (mutex_lock_interruptible(&rng_mutex))
127 return ERR_PTR(-ERESTARTSYS);
128
129 rng = current_rng;
130 if (rng)
131 kref_get(&rng->ref);
132
133 mutex_unlock(&rng_mutex);
134 return rng;
135}
136
137static void put_rng(struct hwrng *rng)
138{
139 /*
140 * Hold rng_mutex here so we serialize in case they set_current_rng
141 * on rng again immediately.
142 */
143 mutex_lock(&rng_mutex);
144 if (rng)
145 kref_put(&rng->ref, cleanup_rng);
146 mutex_unlock(&rng_mutex);
147}
148
844dd05f
MB
149static inline int hwrng_init(struct hwrng *rng)
150{
d3cc7996
AS
151 if (rng->init) {
152 int ret;
153
154 ret = rng->init(rng);
155 if (ret)
156 return ret;
157 }
158 add_early_randomness(rng);
be4000bc 159
0f734e6e
TD
160 current_quality = rng->quality ? : default_quality;
161 current_quality &= 1023;
162
163 if (current_quality == 0 && hwrng_fill)
164 kthread_stop(hwrng_fill);
be4000bc
TD
165 if (current_quality > 0 && !hwrng_fill)
166 start_khwrngd();
167
d3cc7996 168 return 0;
844dd05f
MB
169}
170
844dd05f
MB
171static int rng_dev_open(struct inode *inode, struct file *filp)
172{
173 /* enforce read-only access to this chrdev */
174 if ((filp->f_mode & FMODE_READ) == 0)
175 return -EINVAL;
176 if (filp->f_mode & FMODE_WRITE)
177 return -EINVAL;
178 return 0;
179}
180
9996508b
IM
181static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
182 int wait) {
183 int present;
184
9372b35e 185 BUG_ON(!mutex_is_locked(&reading_mutex));
9996508b
IM
186 if (rng->read)
187 return rng->read(rng, (void *)buffer, size, wait);
188
189 if (rng->data_present)
190 present = rng->data_present(rng, wait);
191 else
192 present = 1;
193
194 if (present)
195 return rng->data_read(rng, (u32 *)buffer);
196
197 return 0;
198}
199
844dd05f
MB
200static ssize_t rng_dev_read(struct file *filp, char __user *buf,
201 size_t size, loff_t *offp)
202{
844dd05f 203 ssize_t ret = 0;
984e976f 204 int err = 0;
9996508b 205 int bytes_read, len;
3a2c0ba5 206 struct hwrng *rng;
844dd05f
MB
207
208 while (size) {
3a2c0ba5
RR
209 rng = get_current_rng();
210 if (IS_ERR(rng)) {
211 err = PTR_ERR(rng);
844dd05f 212 goto out;
9996508b 213 }
3a2c0ba5 214 if (!rng) {
844dd05f 215 err = -ENODEV;
3a2c0ba5 216 goto out;
844dd05f 217 }
984e976f 218
9372b35e 219 mutex_lock(&reading_mutex);
9996508b 220 if (!data_avail) {
3a2c0ba5 221 bytes_read = rng_get_data(rng, rng_buffer,
f7f154f1 222 rng_buffer_size(),
9996508b
IM
223 !(filp->f_flags & O_NONBLOCK));
224 if (bytes_read < 0) {
225 err = bytes_read;
9372b35e 226 goto out_unlock_reading;
9996508b
IM
227 }
228 data_avail = bytes_read;
893f1128 229 }
844dd05f 230
9996508b
IM
231 if (!data_avail) {
232 if (filp->f_flags & O_NONBLOCK) {
233 err = -EAGAIN;
9372b35e 234 goto out_unlock_reading;
9996508b
IM
235 }
236 } else {
237 len = data_avail;
238 if (len > size)
239 len = size;
240
241 data_avail -= len;
242
243 if (copy_to_user(buf + ret, rng_buffer + data_avail,
244 len)) {
245 err = -EFAULT;
9372b35e 246 goto out_unlock_reading;
9996508b
IM
247 }
248
249 size -= len;
250 ret += len;
844dd05f
MB
251 }
252
9372b35e 253 mutex_unlock(&reading_mutex);
3a2c0ba5 254 put_rng(rng);
9996508b 255
844dd05f
MB
256 if (need_resched())
257 schedule_timeout_interruptible(1);
9996508b
IM
258
259 if (signal_pending(current)) {
260 err = -ERESTARTSYS;
844dd05f 261 goto out;
9996508b 262 }
844dd05f
MB
263 }
264out:
265 return ret ? : err;
3a2c0ba5 266
9372b35e
RR
267out_unlock_reading:
268 mutex_unlock(&reading_mutex);
3a2c0ba5
RR
269 put_rng(rng);
270 goto out;
844dd05f
MB
271}
272
273
62322d25 274static const struct file_operations rng_chrdev_ops = {
844dd05f
MB
275 .owner = THIS_MODULE,
276 .open = rng_dev_open,
277 .read = rng_dev_read,
6038f373 278 .llseek = noop_llseek,
844dd05f
MB
279};
280
281static struct miscdevice rng_miscdev = {
282 .minor = RNG_MISCDEV_MINOR,
283 .name = RNG_MODULE_NAME,
e454cea2 284 .nodename = "hwrng",
844dd05f
MB
285 .fops = &rng_chrdev_ops,
286};
287
288
94fbcded
GKH
289static ssize_t hwrng_attr_current_store(struct device *dev,
290 struct device_attribute *attr,
844dd05f
MB
291 const char *buf, size_t len)
292{
293 int err;
294 struct hwrng *rng;
295
296 err = mutex_lock_interruptible(&rng_mutex);
297 if (err)
298 return -ERESTARTSYS;
299 err = -ENODEV;
300 list_for_each_entry(rng, &rng_list, list) {
301 if (strcmp(rng->name, buf) == 0) {
302 if (rng == current_rng) {
303 err = 0;
304 break;
305 }
306 err = hwrng_init(rng);
307 if (err)
308 break;
3a2c0ba5
RR
309 drop_current_rng();
310 set_current_rng(rng);
844dd05f
MB
311 err = 0;
312 break;
313 }
314 }
315 mutex_unlock(&rng_mutex);
316
317 return err ? : len;
318}
319
94fbcded
GKH
320static ssize_t hwrng_attr_current_show(struct device *dev,
321 struct device_attribute *attr,
844dd05f
MB
322 char *buf)
323{
844dd05f 324 ssize_t ret;
3a2c0ba5 325 struct hwrng *rng;
844dd05f 326
3a2c0ba5
RR
327 rng = get_current_rng();
328 if (IS_ERR(rng))
329 return PTR_ERR(rng);
330
331 ret = snprintf(buf, PAGE_SIZE, "%s\n", rng ? rng->name : "none");
332 put_rng(rng);
844dd05f
MB
333
334 return ret;
335}
336
94fbcded
GKH
337static ssize_t hwrng_attr_available_show(struct device *dev,
338 struct device_attribute *attr,
844dd05f
MB
339 char *buf)
340{
341 int err;
844dd05f
MB
342 struct hwrng *rng;
343
344 err = mutex_lock_interruptible(&rng_mutex);
345 if (err)
346 return -ERESTARTSYS;
347 buf[0] = '\0';
348 list_for_each_entry(rng, &rng_list, list) {
61daf055
RS
349 strlcat(buf, rng->name, PAGE_SIZE);
350 strlcat(buf, " ", PAGE_SIZE);
844dd05f 351 }
61daf055 352 strlcat(buf, "\n", PAGE_SIZE);
844dd05f
MB
353 mutex_unlock(&rng_mutex);
354
61daf055 355 return strlen(buf);
844dd05f
MB
356}
357
94fbcded
GKH
358static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
359 hwrng_attr_current_show,
360 hwrng_attr_current_store);
361static DEVICE_ATTR(rng_available, S_IRUGO,
362 hwrng_attr_available_show,
363 NULL);
844dd05f
MB
364
365
b844eba2 366static void unregister_miscdev(void)
844dd05f 367{
94fbcded
GKH
368 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
369 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
b844eba2 370 misc_deregister(&rng_miscdev);
844dd05f
MB
371}
372
373static int register_miscdev(void)
374{
375 int err;
376
377 err = misc_register(&rng_miscdev);
378 if (err)
379 goto out;
94fbcded
GKH
380 err = device_create_file(rng_miscdev.this_device,
381 &dev_attr_rng_current);
844dd05f
MB
382 if (err)
383 goto err_misc_dereg;
94fbcded
GKH
384 err = device_create_file(rng_miscdev.this_device,
385 &dev_attr_rng_available);
844dd05f
MB
386 if (err)
387 goto err_remove_current;
388out:
389 return err;
390
391err_remove_current:
94fbcded 392 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
844dd05f
MB
393err_misc_dereg:
394 misc_deregister(&rng_miscdev);
395 goto out;
396}
397
be4000bc
TD
398static int hwrng_fillfn(void *unused)
399{
400 long rc;
401
402 while (!kthread_should_stop()) {
3a2c0ba5
RR
403 struct hwrng *rng;
404
405 rng = get_current_rng();
406 if (IS_ERR(rng) || !rng)
be4000bc 407 break;
9372b35e 408 mutex_lock(&reading_mutex);
3a2c0ba5 409 rc = rng_get_data(rng, rng_fillbuf,
be4000bc 410 rng_buffer_size(), 1);
9372b35e 411 mutex_unlock(&reading_mutex);
3a2c0ba5 412 put_rng(rng);
be4000bc
TD
413 if (rc <= 0) {
414 pr_warn("hwrng: no data available\n");
415 msleep_interruptible(10000);
416 continue;
417 }
9372b35e 418 /* Outside lock, sure, but y'know: randomness. */
be4000bc 419 add_hwgenerator_randomness((void *)rng_fillbuf, rc,
e02b8765 420 rc * current_quality * 8 >> 10);
be4000bc 421 }
9dda727d 422 hwrng_fill = NULL;
be4000bc
TD
423 return 0;
424}
425
426static void start_khwrngd(void)
427{
428 hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
429 if (hwrng_fill == ERR_PTR(-ENOMEM)) {
430 pr_err("hwrng_fill thread creation failed");
431 hwrng_fill = NULL;
432 }
433}
434
844dd05f
MB
435int hwrng_register(struct hwrng *rng)
436{
844dd05f
MB
437 int err = -EINVAL;
438 struct hwrng *old_rng, *tmp;
439
440 if (rng->name == NULL ||
9996508b 441 (rng->data_read == NULL && rng->read == NULL))
844dd05f
MB
442 goto out;
443
444 mutex_lock(&rng_mutex);
445
f7f154f1
RR
446 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
447 err = -ENOMEM;
448 if (!rng_buffer) {
449 rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
450 if (!rng_buffer)
451 goto out_unlock;
452 }
be4000bc
TD
453 if (!rng_fillbuf) {
454 rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
455 if (!rng_fillbuf) {
456 kfree(rng_buffer);
457 goto out_unlock;
458 }
459 }
f7f154f1 460
844dd05f
MB
461 /* Must not register two RNGs with the same name. */
462 err = -EEXIST;
463 list_for_each_entry(tmp, &rng_list, list) {
464 if (strcmp(tmp->name, rng->name) == 0)
465 goto out_unlock;
466 }
467
844dd05f
MB
468 old_rng = current_rng;
469 if (!old_rng) {
470 err = hwrng_init(rng);
471 if (err)
472 goto out_unlock;
3a2c0ba5 473 set_current_rng(rng);
844dd05f
MB
474 }
475 err = 0;
d167b6e1 476 if (!old_rng) {
844dd05f
MB
477 err = register_miscdev();
478 if (err) {
3a2c0ba5 479 drop_current_rng();
844dd05f
MB
480 goto out_unlock;
481 }
482 }
483 INIT_LIST_HEAD(&rng->list);
484 list_add_tail(&rng->list, &rng_list);
d9e79726 485
d3cc7996
AS
486 if (old_rng && !rng->init) {
487 /*
488 * Use a new device's input to add some randomness to
489 * the system. If this rng device isn't going to be
490 * used right away, its init function hasn't been
491 * called yet; so only use the randomness from devices
492 * that don't need an init callback.
493 */
494 add_early_randomness(rng);
495 }
496
844dd05f
MB
497out_unlock:
498 mutex_unlock(&rng_mutex);
499out:
500 return err;
501}
502EXPORT_SYMBOL_GPL(hwrng_register);
503
b844eba2 504void hwrng_unregister(struct hwrng *rng)
844dd05f 505{
844dd05f
MB
506 mutex_lock(&rng_mutex);
507
508 list_del(&rng->list);
509 if (current_rng == rng) {
3a2c0ba5
RR
510 drop_current_rng();
511 if (!list_empty(&rng_list)) {
512 struct hwrng *tail;
513
514 tail = list_entry(rng_list.prev, struct hwrng, list);
515
516 if (hwrng_init(tail) == 0)
517 set_current_rng(tail);
844dd05f
MB
518 }
519 }
3a2c0ba5 520
be4000bc 521 if (list_empty(&rng_list)) {
1dacb395 522 mutex_unlock(&rng_mutex);
b844eba2 523 unregister_miscdev();
be4000bc
TD
524 if (hwrng_fill)
525 kthread_stop(hwrng_fill);
1dacb395
AK
526 } else
527 mutex_unlock(&rng_mutex);
844dd05f 528}
b844eba2 529EXPORT_SYMBOL_GPL(hwrng_unregister);
844dd05f 530
b7d44d94
ST
531static void __exit hwrng_exit(void)
532{
533 mutex_lock(&rng_mutex);
534 BUG_ON(current_rng);
535 kfree(rng_buffer);
be4000bc 536 kfree(rng_fillbuf);
b7d44d94
ST
537 mutex_unlock(&rng_mutex);
538}
539
540module_exit(hwrng_exit);
844dd05f
MB
541
542MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
543MODULE_LICENSE("GPL");