core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. unsigned char bytes[16];
  67. int bytes_read;
  68. mutex_lock(&reading_mutex);
  69. bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
  70. mutex_unlock(&reading_mutex);
  71. if (bytes_read > 0)
  72. add_device_randomness(bytes, 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. mutex_lock(&reading_mutex);
  188. if (!data_avail) {
  189. bytes_read = rng_get_data(rng, rng_buffer,
  190. rng_buffer_size(),
  191. !(filp->f_flags & O_NONBLOCK));
  192. if (bytes_read < 0) {
  193. err = bytes_read;
  194. goto out_unlock_reading;
  195. }
  196. data_avail = bytes_read;
  197. }
  198. if (!data_avail) {
  199. if (filp->f_flags & O_NONBLOCK) {
  200. err = -EAGAIN;
  201. goto out_unlock_reading;
  202. }
  203. } else {
  204. len = data_avail;
  205. if (len > size)
  206. len = size;
  207. data_avail -= len;
  208. if (copy_to_user(buf + ret, rng_buffer + data_avail,
  209. len)) {
  210. err = -EFAULT;
  211. goto out_unlock_reading;
  212. }
  213. size -= len;
  214. ret += len;
  215. }
  216. mutex_unlock(&reading_mutex);
  217. put_rng(rng);
  218. if (need_resched())
  219. schedule_timeout_interruptible(1);
  220. if (signal_pending(current)) {
  221. err = -ERESTARTSYS;
  222. goto out;
  223. }
  224. }
  225. out:
  226. return ret ? : err;
  227. out_unlock_reading:
  228. mutex_unlock(&reading_mutex);
  229. put_rng(rng);
  230. goto out;
  231. }
  232. static const struct file_operations rng_chrdev_ops = {
  233. .owner = THIS_MODULE,
  234. .open = rng_dev_open,
  235. .read = rng_dev_read,
  236. .llseek = noop_llseek,
  237. };
  238. static const struct attribute_group *rng_dev_groups[];
  239. static struct miscdevice rng_miscdev = {
  240. .minor = RNG_MISCDEV_MINOR,
  241. .name = RNG_MODULE_NAME,
  242. .nodename = "hwrng",
  243. .fops = &rng_chrdev_ops,
  244. .groups = rng_dev_groups,
  245. };
  246. static ssize_t hwrng_attr_current_store(struct device *dev,
  247. struct device_attribute *attr,
  248. const char *buf, size_t len)
  249. {
  250. int err;
  251. struct hwrng *rng;
  252. err = mutex_lock_interruptible(&rng_mutex);
  253. if (err)
  254. return -ERESTARTSYS;
  255. err = -ENODEV;
  256. list_for_each_entry(rng, &rng_list, list) {
  257. if (strcmp(rng->name, buf) == 0) {
  258. err = 0;
  259. if (rng != current_rng)
  260. err = set_current_rng(rng);
  261. break;
  262. }
  263. }
  264. mutex_unlock(&rng_mutex);
  265. return err ? : len;
  266. }
  267. static ssize_t hwrng_attr_current_show(struct device *dev,
  268. struct device_attribute *attr,
  269. char *buf)
  270. {
  271. ssize_t ret;
  272. struct hwrng *rng;
  273. rng = get_current_rng();
  274. if (IS_ERR(rng))
  275. return PTR_ERR(rng);
  276. ret = snprintf(buf, PAGE_SIZE, "%s\n", rng ? rng->name : "none");
  277. put_rng(rng);
  278. return ret;
  279. }
  280. static ssize_t hwrng_attr_available_show(struct device *dev,
  281. struct device_attribute *attr,
  282. char *buf)
  283. {
  284. int err;
  285. struct hwrng *rng;
  286. err = mutex_lock_interruptible(&rng_mutex);
  287. if (err)
  288. return -ERESTARTSYS;
  289. buf[0] = '\0';
  290. list_for_each_entry(rng, &rng_list, list) {
  291. strlcat(buf, rng->name, PAGE_SIZE);
  292. strlcat(buf, " ", PAGE_SIZE);
  293. }
  294. strlcat(buf, "\n", PAGE_SIZE);
  295. mutex_unlock(&rng_mutex);
  296. return strlen(buf);
  297. }
  298. static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
  299. hwrng_attr_current_show,
  300. hwrng_attr_current_store);
  301. static DEVICE_ATTR(rng_available, S_IRUGO,
  302. hwrng_attr_available_show,
  303. NULL);
  304. static struct attribute *rng_dev_attrs[] = {
  305. &dev_attr_rng_current.attr,
  306. &dev_attr_rng_available.attr,
  307. NULL
  308. };
  309. ATTRIBUTE_GROUPS(rng_dev);
  310. static void __exit unregister_miscdev(void)
  311. {
  312. misc_deregister(&rng_miscdev);
  313. }
  314. static int __init register_miscdev(void)
  315. {
  316. return misc_register(&rng_miscdev);
  317. }
  318. static int hwrng_fillfn(void *unused)
  319. {
  320. long rc;
  321. while (!kthread_should_stop()) {
  322. struct hwrng *rng;
  323. rng = get_current_rng();
  324. if (IS_ERR(rng) || !rng)
  325. break;
  326. mutex_lock(&reading_mutex);
  327. rc = rng_get_data(rng, rng_fillbuf,
  328. rng_buffer_size(), 1);
  329. mutex_unlock(&reading_mutex);
  330. put_rng(rng);
  331. if (rc <= 0) {
  332. pr_warn("hwrng: no data available\n");
  333. msleep_interruptible(10000);
  334. continue;
  335. }
  336. /* Outside lock, sure, but y'know: randomness. */
  337. add_hwgenerator_randomness((void *)rng_fillbuf, rc,
  338. rc * current_quality * 8 >> 10);
  339. }
  340. hwrng_fill = NULL;
  341. return 0;
  342. }
  343. static void start_khwrngd(void)
  344. {
  345. hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
  346. if (IS_ERR(hwrng_fill)) {
  347. pr_err("hwrng_fill thread creation failed");
  348. hwrng_fill = NULL;
  349. }
  350. }
  351. int hwrng_register(struct hwrng *rng)
  352. {
  353. int err = -EINVAL;
  354. struct hwrng *old_rng, *tmp;
  355. if (rng->name == NULL ||
  356. (rng->data_read == NULL && rng->read == NULL))
  357. goto out;
  358. mutex_lock(&rng_mutex);
  359. /* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
  360. err = -ENOMEM;
  361. if (!rng_buffer) {
  362. rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
  363. if (!rng_buffer)
  364. goto out_unlock;
  365. }
  366. if (!rng_fillbuf) {
  367. rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
  368. if (!rng_fillbuf) {
  369. kfree(rng_buffer);
  370. goto out_unlock;
  371. }
  372. }
  373. /* Must not register two RNGs with the same name. */
  374. err = -EEXIST;
  375. list_for_each_entry(tmp, &rng_list, list) {
  376. if (strcmp(tmp->name, rng->name) == 0)
  377. goto out_unlock;
  378. }
  379. init_completion(&rng->cleanup_done);
  380. complete(&rng->cleanup_done);
  381. old_rng = current_rng;
  382. err = 0;
  383. if (!old_rng) {
  384. err = set_current_rng(rng);
  385. if (err)
  386. goto out_unlock;
  387. }
  388. list_add_tail(&rng->list, &rng_list);
  389. if (old_rng && !rng->init) {
  390. /*
  391. * Use a new device's input to add some randomness to
  392. * the system. If this rng device isn't going to be
  393. * used right away, its init function hasn't been
  394. * called yet; so only use the randomness from devices
  395. * that don't need an init callback.
  396. */
  397. add_early_randomness(rng);
  398. }
  399. out_unlock:
  400. mutex_unlock(&rng_mutex);
  401. out:
  402. return err;
  403. }
  404. EXPORT_SYMBOL_GPL(hwrng_register);
  405. void hwrng_unregister(struct hwrng *rng)
  406. {
  407. mutex_lock(&rng_mutex);
  408. list_del(&rng->list);
  409. if (current_rng == rng) {
  410. drop_current_rng();
  411. if (!list_empty(&rng_list)) {
  412. struct hwrng *tail;
  413. tail = list_entry(rng_list.prev, struct hwrng, list);
  414. set_current_rng(tail);
  415. }
  416. }
  417. if (list_empty(&rng_list)) {
  418. mutex_unlock(&rng_mutex);
  419. if (hwrng_fill)
  420. kthread_stop(hwrng_fill);
  421. } else
  422. mutex_unlock(&rng_mutex);
  423. wait_for_completion(&rng->cleanup_done);
  424. }
  425. EXPORT_SYMBOL_GPL(hwrng_unregister);
  426. static void devm_hwrng_release(struct device *dev, void *res)
  427. {
  428. hwrng_unregister(*(struct hwrng **)res);
  429. }
  430. static int devm_hwrng_match(struct device *dev, void *res, void *data)
  431. {
  432. struct hwrng **r = res;
  433. if (WARN_ON(!r || !*r))
  434. return 0;
  435. return *r == data;
  436. }
  437. int devm_hwrng_register(struct device *dev, struct hwrng *rng)
  438. {
  439. struct hwrng **ptr;
  440. int error;
  441. ptr = devres_alloc(devm_hwrng_release, sizeof(*ptr), GFP_KERNEL);
  442. if (!ptr)
  443. return -ENOMEM;
  444. error = hwrng_register(rng);
  445. if (error) {
  446. devres_free(ptr);
  447. return error;
  448. }
  449. *ptr = rng;
  450. devres_add(dev, ptr);
  451. return 0;
  452. }
  453. EXPORT_SYMBOL_GPL(devm_hwrng_register);
  454. void devm_hwrng_unregister(struct device *dev, struct hwrng *rng)
  455. {
  456. devres_release(dev, devm_hwrng_release, devm_hwrng_match, rng);
  457. }
  458. EXPORT_SYMBOL_GPL(devm_hwrng_unregister);
  459. static int __init hwrng_modinit(void)
  460. {
  461. return register_miscdev();
  462. }
  463. static void __exit hwrng_modexit(void)
  464. {
  465. mutex_lock(&rng_mutex);
  466. BUG_ON(current_rng);
  467. kfree(rng_buffer);
  468. kfree(rng_fillbuf);
  469. mutex_unlock(&rng_mutex);
  470. unregister_miscdev();
  471. }
  472. module_init(hwrng_modinit);
  473. module_exit(hwrng_modexit);
  474. MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
  475. MODULE_LICENSE("GPL");