core.c 12 KB

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