]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - drivers/char/hw_random/core.c
hwrng: core - remove unused PFX macro
[mirror_ubuntu-focal-kernel.git] / drivers / char / hw_random / core.c
1 /*
2 * hw_random/core.c: HWRNG core API
3 *
4 * Copyright 2006 Michael Buesch <m@bues.ch>
5 * Copyright 2005 (c) MontaVista Software, Inc.
6 *
7 * Please read Documentation/hw_random.txt for details on use.
8 *
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
11 */
12
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/fs.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>
26
27 #define RNG_MODULE_NAME "hw_random"
28
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" */
40
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");
47
48 static void drop_current_rng(void);
49 static int hwrng_init(struct hwrng *rng);
50 static void start_khwrngd(void);
51
52 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
53 int wait);
54
55 static size_t rng_buffer_size(void)
56 {
57 return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
58 }
59
60 static void add_early_randomness(struct hwrng *rng)
61 {
62 int bytes_read;
63 size_t size = min_t(size_t, 16, rng_buffer_size());
64
65 mutex_lock(&reading_mutex);
66 bytes_read = rng_get_data(rng, rng_buffer, size, 1);
67 mutex_unlock(&reading_mutex);
68 if (bytes_read > 0)
69 add_device_randomness(rng_buffer, bytes_read);
70 memset(rng_buffer, 0, size);
71 }
72
73 static inline void cleanup_rng(struct kref *kref)
74 {
75 struct hwrng *rng = container_of(kref, struct hwrng, ref);
76
77 if (rng->cleanup)
78 rng->cleanup(rng);
79
80 complete(&rng->cleanup_done);
81 }
82
83 static int set_current_rng(struct hwrng *rng)
84 {
85 int err;
86
87 BUG_ON(!mutex_is_locked(&rng_mutex));
88
89 err = hwrng_init(rng);
90 if (err)
91 return err;
92
93 drop_current_rng();
94 current_rng = rng;
95
96 return 0;
97 }
98
99 static void drop_current_rng(void)
100 {
101 BUG_ON(!mutex_is_locked(&rng_mutex));
102 if (!current_rng)
103 return;
104
105 /* decrease last reference for triggering the cleanup */
106 kref_put(&current_rng->ref, cleanup_rng);
107 current_rng = NULL;
108 }
109
110 /* Returns ERR_PTR(), NULL or refcounted hwrng */
111 static struct hwrng *get_current_rng(void)
112 {
113 struct hwrng *rng;
114
115 if (mutex_lock_interruptible(&rng_mutex))
116 return ERR_PTR(-ERESTARTSYS);
117
118 rng = current_rng;
119 if (rng)
120 kref_get(&rng->ref);
121
122 mutex_unlock(&rng_mutex);
123 return rng;
124 }
125
126 static void put_rng(struct hwrng *rng)
127 {
128 /*
129 * Hold rng_mutex here so we serialize in case they set_current_rng
130 * on rng again immediately.
131 */
132 mutex_lock(&rng_mutex);
133 if (rng)
134 kref_put(&rng->ref, cleanup_rng);
135 mutex_unlock(&rng_mutex);
136 }
137
138 static int hwrng_init(struct hwrng *rng)
139 {
140 if (kref_get_unless_zero(&rng->ref))
141 goto skip_init;
142
143 if (rng->init) {
144 int ret;
145
146 ret = rng->init(rng);
147 if (ret)
148 return ret;
149 }
150
151 kref_init(&rng->ref);
152 reinit_completion(&rng->cleanup_done);
153
154 skip_init:
155 add_early_randomness(rng);
156
157 current_quality = rng->quality ? : default_quality;
158 if (current_quality > 1024)
159 current_quality = 1024;
160
161 if (current_quality == 0 && hwrng_fill)
162 kthread_stop(hwrng_fill);
163 if (current_quality > 0 && !hwrng_fill)
164 start_khwrngd();
165
166 return 0;
167 }
168
169 static int rng_dev_open(struct inode *inode, struct file *filp)
170 {
171 /* enforce read-only access to this chrdev */
172 if ((filp->f_mode & FMODE_READ) == 0)
173 return -EINVAL;
174 if (filp->f_mode & FMODE_WRITE)
175 return -EINVAL;
176 return 0;
177 }
178
179 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
180 int wait) {
181 int present;
182
183 BUG_ON(!mutex_is_locked(&reading_mutex));
184 if (rng->read)
185 return rng->read(rng, (void *)buffer, size, wait);
186
187 if (rng->data_present)
188 present = rng->data_present(rng, wait);
189 else
190 present = 1;
191
192 if (present)
193 return rng->data_read(rng, (u32 *)buffer);
194
195 return 0;
196 }
197
198 static ssize_t rng_dev_read(struct file *filp, char __user *buf,
199 size_t size, loff_t *offp)
200 {
201 ssize_t ret = 0;
202 int err = 0;
203 int bytes_read, len;
204 struct hwrng *rng;
205
206 while (size) {
207 rng = get_current_rng();
208 if (IS_ERR(rng)) {
209 err = PTR_ERR(rng);
210 goto out;
211 }
212 if (!rng) {
213 err = -ENODEV;
214 goto out;
215 }
216
217 if (mutex_lock_interruptible(&reading_mutex)) {
218 err = -ERESTARTSYS;
219 goto out_put;
220 }
221 if (!data_avail) {
222 bytes_read = rng_get_data(rng, rng_buffer,
223 rng_buffer_size(),
224 !(filp->f_flags & O_NONBLOCK));
225 if (bytes_read < 0) {
226 err = bytes_read;
227 goto out_unlock_reading;
228 }
229 data_avail = bytes_read;
230 }
231
232 if (!data_avail) {
233 if (filp->f_flags & O_NONBLOCK) {
234 err = -EAGAIN;
235 goto out_unlock_reading;
236 }
237 } else {
238 len = data_avail;
239 if (len > size)
240 len = size;
241
242 data_avail -= len;
243
244 if (copy_to_user(buf + ret, rng_buffer + data_avail,
245 len)) {
246 err = -EFAULT;
247 goto out_unlock_reading;
248 }
249
250 size -= len;
251 ret += len;
252 }
253
254 mutex_unlock(&reading_mutex);
255 put_rng(rng);
256
257 if (need_resched())
258 schedule_timeout_interruptible(1);
259
260 if (signal_pending(current)) {
261 err = -ERESTARTSYS;
262 goto out;
263 }
264 }
265 out:
266 memset(rng_buffer, 0, rng_buffer_size());
267 return ret ? : err;
268
269 out_unlock_reading:
270 mutex_unlock(&reading_mutex);
271 out_put:
272 put_rng(rng);
273 goto out;
274 }
275
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,
281 };
282
283 static const struct attribute_group *rng_dev_groups[];
284
285 static struct miscdevice rng_miscdev = {
286 .minor = HWRNG_MINOR,
287 .name = RNG_MODULE_NAME,
288 .nodename = "hwrng",
289 .fops = &rng_chrdev_ops,
290 .groups = rng_dev_groups,
291 };
292
293 static ssize_t hwrng_attr_current_store(struct device *dev,
294 struct device_attribute *attr,
295 const char *buf, size_t len)
296 {
297 int err;
298 struct hwrng *rng;
299
300 err = mutex_lock_interruptible(&rng_mutex);
301 if (err)
302 return -ERESTARTSYS;
303 err = -ENODEV;
304 list_for_each_entry(rng, &rng_list, list) {
305 if (sysfs_streq(rng->name, buf)) {
306 err = 0;
307 if (rng != current_rng)
308 err = set_current_rng(rng);
309 break;
310 }
311 }
312 mutex_unlock(&rng_mutex);
313
314 return err ? : len;
315 }
316
317 static ssize_t hwrng_attr_current_show(struct device *dev,
318 struct device_attribute *attr,
319 char *buf)
320 {
321 ssize_t ret;
322 struct hwrng *rng;
323
324 rng = get_current_rng();
325 if (IS_ERR(rng))
326 return PTR_ERR(rng);
327
328 ret = snprintf(buf, PAGE_SIZE, "%s\n", rng ? rng->name : "none");
329 put_rng(rng);
330
331 return ret;
332 }
333
334 static ssize_t hwrng_attr_available_show(struct device *dev,
335 struct device_attribute *attr,
336 char *buf)
337 {
338 int err;
339 struct hwrng *rng;
340
341 err = mutex_lock_interruptible(&rng_mutex);
342 if (err)
343 return -ERESTARTSYS;
344 buf[0] = '\0';
345 list_for_each_entry(rng, &rng_list, list) {
346 strlcat(buf, rng->name, PAGE_SIZE);
347 strlcat(buf, " ", PAGE_SIZE);
348 }
349 strlcat(buf, "\n", PAGE_SIZE);
350 mutex_unlock(&rng_mutex);
351
352 return strlen(buf);
353 }
354
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,
360 NULL);
361
362 static struct attribute *rng_dev_attrs[] = {
363 &dev_attr_rng_current.attr,
364 &dev_attr_rng_available.attr,
365 NULL
366 };
367
368 ATTRIBUTE_GROUPS(rng_dev);
369
370 static void __exit unregister_miscdev(void)
371 {
372 misc_deregister(&rng_miscdev);
373 }
374
375 static int __init register_miscdev(void)
376 {
377 return misc_register(&rng_miscdev);
378 }
379
380 static int hwrng_fillfn(void *unused)
381 {
382 long rc;
383
384 while (!kthread_should_stop()) {
385 struct hwrng *rng;
386
387 rng = get_current_rng();
388 if (IS_ERR(rng) || !rng)
389 break;
390 mutex_lock(&reading_mutex);
391 rc = rng_get_data(rng, rng_fillbuf,
392 rng_buffer_size(), 1);
393 mutex_unlock(&reading_mutex);
394 put_rng(rng);
395 if (rc <= 0) {
396 pr_warn("hwrng: no data available\n");
397 msleep_interruptible(10000);
398 continue;
399 }
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());
404 }
405 hwrng_fill = NULL;
406 return 0;
407 }
408
409 static void start_khwrngd(void)
410 {
411 hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
412 if (IS_ERR(hwrng_fill)) {
413 pr_err("hwrng_fill thread creation failed");
414 hwrng_fill = NULL;
415 }
416 }
417
418 int hwrng_register(struct hwrng *rng)
419 {
420 int err = -EINVAL;
421 struct hwrng *old_rng, *tmp;
422
423 if (!rng->name || (!rng->data_read && !rng->read))
424 goto out;
425
426 mutex_lock(&rng_mutex);
427 /* Must not register two RNGs with the same name. */
428 err = -EEXIST;
429 list_for_each_entry(tmp, &rng_list, list) {
430 if (strcmp(tmp->name, rng->name) == 0)
431 goto out_unlock;
432 }
433
434 init_completion(&rng->cleanup_done);
435 complete(&rng->cleanup_done);
436
437 old_rng = current_rng;
438 err = 0;
439 if (!old_rng) {
440 err = set_current_rng(rng);
441 if (err)
442 goto out_unlock;
443 }
444 list_add_tail(&rng->list, &rng_list);
445
446 if (old_rng && !rng->init) {
447 /*
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.
453 */
454 add_early_randomness(rng);
455 }
456
457 out_unlock:
458 mutex_unlock(&rng_mutex);
459 out:
460 return err;
461 }
462 EXPORT_SYMBOL_GPL(hwrng_register);
463
464 void hwrng_unregister(struct hwrng *rng)
465 {
466 mutex_lock(&rng_mutex);
467
468 list_del(&rng->list);
469 if (current_rng == rng) {
470 drop_current_rng();
471 if (!list_empty(&rng_list)) {
472 struct hwrng *tail;
473
474 tail = list_entry(rng_list.prev, struct hwrng, list);
475
476 set_current_rng(tail);
477 }
478 }
479
480 if (list_empty(&rng_list)) {
481 mutex_unlock(&rng_mutex);
482 if (hwrng_fill)
483 kthread_stop(hwrng_fill);
484 } else
485 mutex_unlock(&rng_mutex);
486
487 wait_for_completion(&rng->cleanup_done);
488 }
489 EXPORT_SYMBOL_GPL(hwrng_unregister);
490
491 static void devm_hwrng_release(struct device *dev, void *res)
492 {
493 hwrng_unregister(*(struct hwrng **)res);
494 }
495
496 static int devm_hwrng_match(struct device *dev, void *res, void *data)
497 {
498 struct hwrng **r = res;
499
500 if (WARN_ON(!r || !*r))
501 return 0;
502
503 return *r == data;
504 }
505
506 int devm_hwrng_register(struct device *dev, struct hwrng *rng)
507 {
508 struct hwrng **ptr;
509 int error;
510
511 ptr = devres_alloc(devm_hwrng_release, sizeof(*ptr), GFP_KERNEL);
512 if (!ptr)
513 return -ENOMEM;
514
515 error = hwrng_register(rng);
516 if (error) {
517 devres_free(ptr);
518 return error;
519 }
520
521 *ptr = rng;
522 devres_add(dev, ptr);
523 return 0;
524 }
525 EXPORT_SYMBOL_GPL(devm_hwrng_register);
526
527 void devm_hwrng_unregister(struct device *dev, struct hwrng *rng)
528 {
529 devres_release(dev, devm_hwrng_release, devm_hwrng_match, rng);
530 }
531 EXPORT_SYMBOL_GPL(devm_hwrng_unregister);
532
533 static int __init hwrng_modinit(void)
534 {
535 int ret = -ENOMEM;
536
537 /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
538 rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
539 if (!rng_buffer)
540 return -ENOMEM;
541
542 rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
543 if (!rng_fillbuf) {
544 kfree(rng_buffer);
545 return -ENOMEM;
546 }
547
548 ret = register_miscdev();
549 if (ret) {
550 kfree(rng_fillbuf);
551 kfree(rng_buffer);
552 }
553
554 return ret;
555 }
556
557 static void __exit hwrng_modexit(void)
558 {
559 mutex_lock(&rng_mutex);
560 BUG_ON(current_rng);
561 kfree(rng_buffer);
562 kfree(rng_fillbuf);
563 mutex_unlock(&rng_mutex);
564
565 unregister_miscdev();
566 }
567
568 module_init(hwrng_modinit);
569 module_exit(hwrng_modexit);
570
571 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
572 MODULE_LICENSE("GPL");