]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/char/hw_random/core.c
hwrng: use reference counts on each struct hwrng.
[mirror_ubuntu-artful-kernel.git] / drivers / char / hw_random / core.c
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
22 Copyright 2006 Michael Buesch <m@bues.ch>
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>
39 #include <linux/sched.h>
40 #include <linux/miscdevice.h>
41 #include <linux/kthread.h>
42 #include <linux/delay.h>
43 #include <linux/slab.h>
44 #include <linux/random.h>
45 #include <linux/err.h>
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
54 static struct hwrng *current_rng;
55 static struct task_struct *hwrng_fill;
56 static LIST_HEAD(rng_list);
57 /* Protects rng_list and current_rng */
58 static DEFINE_MUTEX(rng_mutex);
59 /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
60 static DEFINE_MUTEX(reading_mutex);
61 static int data_avail;
62 static u8 *rng_buffer, *rng_fillbuf;
63 static unsigned short current_quality;
64 static unsigned short default_quality; /* = 0; default to "off" */
65
66 module_param(current_quality, ushort, 0644);
67 MODULE_PARM_DESC(current_quality,
68 "current hwrng entropy estimation per mill");
69 module_param(default_quality, ushort, 0644);
70 MODULE_PARM_DESC(default_quality,
71 "default entropy content of hwrng per mill");
72
73 static void start_khwrngd(void);
74
75 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
76 int wait);
77
78 static size_t rng_buffer_size(void)
79 {
80 return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
81 }
82
83 static void add_early_randomness(struct hwrng *rng)
84 {
85 unsigned char bytes[16];
86 int bytes_read;
87
88 mutex_lock(&reading_mutex);
89 bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
90 mutex_unlock(&reading_mutex);
91 if (bytes_read > 0)
92 add_device_randomness(bytes, bytes_read);
93 }
94
95 static 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
103 static 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
110 static 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 */
122 static 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
137 static 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
149 static inline int hwrng_init(struct hwrng *rng)
150 {
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);
159
160 current_quality = rng->quality ? : default_quality;
161 current_quality &= 1023;
162
163 if (current_quality == 0 && hwrng_fill)
164 kthread_stop(hwrng_fill);
165 if (current_quality > 0 && !hwrng_fill)
166 start_khwrngd();
167
168 return 0;
169 }
170
171 static 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
181 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
182 int wait) {
183 int present;
184
185 BUG_ON(!mutex_is_locked(&reading_mutex));
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
200 static ssize_t rng_dev_read(struct file *filp, char __user *buf,
201 size_t size, loff_t *offp)
202 {
203 ssize_t ret = 0;
204 int err = 0;
205 int bytes_read, len;
206 struct hwrng *rng;
207
208 while (size) {
209 rng = get_current_rng();
210 if (IS_ERR(rng)) {
211 err = PTR_ERR(rng);
212 goto out;
213 }
214 if (!rng) {
215 err = -ENODEV;
216 goto out;
217 }
218
219 mutex_lock(&reading_mutex);
220 if (!data_avail) {
221 bytes_read = rng_get_data(rng, rng_buffer,
222 rng_buffer_size(),
223 !(filp->f_flags & O_NONBLOCK));
224 if (bytes_read < 0) {
225 err = bytes_read;
226 goto out_unlock_reading;
227 }
228 data_avail = bytes_read;
229 }
230
231 if (!data_avail) {
232 if (filp->f_flags & O_NONBLOCK) {
233 err = -EAGAIN;
234 goto out_unlock_reading;
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;
246 goto out_unlock_reading;
247 }
248
249 size -= len;
250 ret += len;
251 }
252
253 mutex_unlock(&reading_mutex);
254 put_rng(rng);
255
256 if (need_resched())
257 schedule_timeout_interruptible(1);
258
259 if (signal_pending(current)) {
260 err = -ERESTARTSYS;
261 goto out;
262 }
263 }
264 out:
265 return ret ? : err;
266
267 out_unlock_reading:
268 mutex_unlock(&reading_mutex);
269 put_rng(rng);
270 goto out;
271 }
272
273
274 static const struct file_operations rng_chrdev_ops = {
275 .owner = THIS_MODULE,
276 .open = rng_dev_open,
277 .read = rng_dev_read,
278 .llseek = noop_llseek,
279 };
280
281 static struct miscdevice rng_miscdev = {
282 .minor = RNG_MISCDEV_MINOR,
283 .name = RNG_MODULE_NAME,
284 .nodename = "hwrng",
285 .fops = &rng_chrdev_ops,
286 };
287
288
289 static ssize_t hwrng_attr_current_store(struct device *dev,
290 struct device_attribute *attr,
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;
309 drop_current_rng();
310 set_current_rng(rng);
311 err = 0;
312 break;
313 }
314 }
315 mutex_unlock(&rng_mutex);
316
317 return err ? : len;
318 }
319
320 static ssize_t hwrng_attr_current_show(struct device *dev,
321 struct device_attribute *attr,
322 char *buf)
323 {
324 ssize_t ret;
325 struct hwrng *rng;
326
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);
333
334 return ret;
335 }
336
337 static ssize_t hwrng_attr_available_show(struct device *dev,
338 struct device_attribute *attr,
339 char *buf)
340 {
341 int err;
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) {
349 strlcat(buf, rng->name, PAGE_SIZE);
350 strlcat(buf, " ", PAGE_SIZE);
351 }
352 strlcat(buf, "\n", PAGE_SIZE);
353 mutex_unlock(&rng_mutex);
354
355 return strlen(buf);
356 }
357
358 static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
359 hwrng_attr_current_show,
360 hwrng_attr_current_store);
361 static DEVICE_ATTR(rng_available, S_IRUGO,
362 hwrng_attr_available_show,
363 NULL);
364
365
366 static void unregister_miscdev(void)
367 {
368 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
369 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
370 misc_deregister(&rng_miscdev);
371 }
372
373 static int register_miscdev(void)
374 {
375 int err;
376
377 err = misc_register(&rng_miscdev);
378 if (err)
379 goto out;
380 err = device_create_file(rng_miscdev.this_device,
381 &dev_attr_rng_current);
382 if (err)
383 goto err_misc_dereg;
384 err = device_create_file(rng_miscdev.this_device,
385 &dev_attr_rng_available);
386 if (err)
387 goto err_remove_current;
388 out:
389 return err;
390
391 err_remove_current:
392 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
393 err_misc_dereg:
394 misc_deregister(&rng_miscdev);
395 goto out;
396 }
397
398 static int hwrng_fillfn(void *unused)
399 {
400 long rc;
401
402 while (!kthread_should_stop()) {
403 struct hwrng *rng;
404
405 rng = get_current_rng();
406 if (IS_ERR(rng) || !rng)
407 break;
408 mutex_lock(&reading_mutex);
409 rc = rng_get_data(rng, rng_fillbuf,
410 rng_buffer_size(), 1);
411 mutex_unlock(&reading_mutex);
412 put_rng(rng);
413 if (rc <= 0) {
414 pr_warn("hwrng: no data available\n");
415 msleep_interruptible(10000);
416 continue;
417 }
418 /* Outside lock, sure, but y'know: randomness. */
419 add_hwgenerator_randomness((void *)rng_fillbuf, rc,
420 rc * current_quality * 8 >> 10);
421 }
422 hwrng_fill = NULL;
423 return 0;
424 }
425
426 static 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
435 int hwrng_register(struct hwrng *rng)
436 {
437 int err = -EINVAL;
438 struct hwrng *old_rng, *tmp;
439
440 if (rng->name == NULL ||
441 (rng->data_read == NULL && rng->read == NULL))
442 goto out;
443
444 mutex_lock(&rng_mutex);
445
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 }
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 }
460
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
468 old_rng = current_rng;
469 if (!old_rng) {
470 err = hwrng_init(rng);
471 if (err)
472 goto out_unlock;
473 set_current_rng(rng);
474 }
475 err = 0;
476 if (!old_rng) {
477 err = register_miscdev();
478 if (err) {
479 drop_current_rng();
480 goto out_unlock;
481 }
482 }
483 INIT_LIST_HEAD(&rng->list);
484 list_add_tail(&rng->list, &rng_list);
485
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
497 out_unlock:
498 mutex_unlock(&rng_mutex);
499 out:
500 return err;
501 }
502 EXPORT_SYMBOL_GPL(hwrng_register);
503
504 void hwrng_unregister(struct hwrng *rng)
505 {
506 mutex_lock(&rng_mutex);
507
508 list_del(&rng->list);
509 if (current_rng == rng) {
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);
518 }
519 }
520
521 if (list_empty(&rng_list)) {
522 mutex_unlock(&rng_mutex);
523 unregister_miscdev();
524 if (hwrng_fill)
525 kthread_stop(hwrng_fill);
526 } else
527 mutex_unlock(&rng_mutex);
528 }
529 EXPORT_SYMBOL_GPL(hwrng_unregister);
530
531 static void __exit hwrng_exit(void)
532 {
533 mutex_lock(&rng_mutex);
534 BUG_ON(current_rng);
535 kfree(rng_buffer);
536 kfree(rng_fillbuf);
537 mutex_unlock(&rng_mutex);
538 }
539
540 module_exit(hwrng_exit);
541
542 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
543 MODULE_LICENSE("GPL");