virtio-rng.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Randomness driver for virtio
  4. * Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
  5. */
  6. #include <asm/barrier.h>
  7. #include <linux/err.h>
  8. #include <linux/hw_random.h>
  9. #include <linux/scatterlist.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/virtio.h>
  12. #include <linux/virtio_rng.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. static DEFINE_IDA(rng_index_ida);
  16. struct virtrng_info {
  17. struct hwrng hwrng;
  18. struct virtqueue *vq;
  19. char name[25];
  20. int index;
  21. bool hwrng_register_done;
  22. bool hwrng_removed;
  23. /* data transfer */
  24. struct completion have_data;
  25. unsigned int data_avail;
  26. unsigned int data_idx;
  27. /* minimal size returned by rng_buffer_size() */
  28. #if SMP_CACHE_BYTES < 32
  29. u8 data[32];
  30. #else
  31. u8 data[SMP_CACHE_BYTES];
  32. #endif
  33. };
  34. static void random_recv_done(struct virtqueue *vq)
  35. {
  36. struct virtrng_info *vi = vq->vdev->priv;
  37. unsigned int len;
  38. /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
  39. if (!virtqueue_get_buf(vi->vq, &len))
  40. return;
  41. smp_store_release(&vi->data_avail, len);
  42. complete(&vi->have_data);
  43. }
  44. static void request_entropy(struct virtrng_info *vi)
  45. {
  46. struct scatterlist sg;
  47. reinit_completion(&vi->have_data);
  48. vi->data_idx = 0;
  49. sg_init_one(&sg, vi->data, sizeof(vi->data));
  50. /* There should always be room for one buffer. */
  51. virtqueue_add_inbuf(vi->vq, &sg, 1, vi->data, GFP_KERNEL);
  52. virtqueue_kick(vi->vq);
  53. }
  54. static unsigned int copy_data(struct virtrng_info *vi, void *buf,
  55. unsigned int size)
  56. {
  57. size = min_t(unsigned int, size, vi->data_avail);
  58. memcpy(buf, vi->data + vi->data_idx, size);
  59. vi->data_idx += size;
  60. vi->data_avail -= size;
  61. if (vi->data_avail == 0)
  62. request_entropy(vi);
  63. return size;
  64. }
  65. static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
  66. {
  67. int ret;
  68. struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
  69. unsigned int chunk;
  70. size_t read;
  71. if (vi->hwrng_removed)
  72. return -ENODEV;
  73. read = 0;
  74. /* copy available data */
  75. if (smp_load_acquire(&vi->data_avail)) {
  76. chunk = copy_data(vi, buf, size);
  77. size -= chunk;
  78. read += chunk;
  79. }
  80. if (!wait)
  81. return read;
  82. /* We have already copied available entropy,
  83. * so either size is 0 or data_avail is 0
  84. */
  85. while (size != 0) {
  86. /* data_avail is 0 but a request is pending */
  87. ret = wait_for_completion_killable(&vi->have_data);
  88. if (ret < 0)
  89. return ret;
  90. /* if vi->data_avail is 0, we have been interrupted
  91. * by a cleanup, but buffer stays in the queue
  92. */
  93. if (vi->data_avail == 0)
  94. return read;
  95. chunk = copy_data(vi, buf + read, size);
  96. size -= chunk;
  97. read += chunk;
  98. }
  99. return read;
  100. }
  101. static void virtio_cleanup(struct hwrng *rng)
  102. {
  103. struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
  104. complete(&vi->have_data);
  105. }
  106. static int probe_common(struct virtio_device *vdev)
  107. {
  108. int err, index;
  109. struct virtrng_info *vi = NULL;
  110. vi = kzalloc(sizeof(struct virtrng_info), GFP_KERNEL);
  111. if (!vi)
  112. return -ENOMEM;
  113. vi->index = index = ida_simple_get(&rng_index_ida, 0, 0, GFP_KERNEL);
  114. if (index < 0) {
  115. err = index;
  116. goto err_ida;
  117. }
  118. sprintf(vi->name, "virtio_rng.%d", index);
  119. init_completion(&vi->have_data);
  120. vi->hwrng = (struct hwrng) {
  121. .read = virtio_read,
  122. .cleanup = virtio_cleanup,
  123. .priv = (unsigned long)vi,
  124. .name = vi->name,
  125. .quality = 1000,
  126. };
  127. vdev->priv = vi;
  128. /* We expect a single virtqueue. */
  129. vi->vq = virtio_find_single_vq(vdev, random_recv_done, "input");
  130. if (IS_ERR(vi->vq)) {
  131. err = PTR_ERR(vi->vq);
  132. goto err_find;
  133. }
  134. virtio_device_ready(vdev);
  135. /* we always have a pending entropy request */
  136. request_entropy(vi);
  137. return 0;
  138. err_find:
  139. ida_simple_remove(&rng_index_ida, index);
  140. err_ida:
  141. kfree(vi);
  142. return err;
  143. }
  144. static void remove_common(struct virtio_device *vdev)
  145. {
  146. struct virtrng_info *vi = vdev->priv;
  147. vi->hwrng_removed = true;
  148. vi->data_avail = 0;
  149. vi->data_idx = 0;
  150. complete(&vi->have_data);
  151. if (vi->hwrng_register_done)
  152. hwrng_unregister(&vi->hwrng);
  153. virtio_reset_device(vdev);
  154. vdev->config->del_vqs(vdev);
  155. ida_simple_remove(&rng_index_ida, vi->index);
  156. kfree(vi);
  157. }
  158. static int virtrng_probe(struct virtio_device *vdev)
  159. {
  160. return probe_common(vdev);
  161. }
  162. static void virtrng_remove(struct virtio_device *vdev)
  163. {
  164. remove_common(vdev);
  165. }
  166. static void virtrng_scan(struct virtio_device *vdev)
  167. {
  168. struct virtrng_info *vi = vdev->priv;
  169. int err;
  170. err = hwrng_register(&vi->hwrng);
  171. if (!err)
  172. vi->hwrng_register_done = true;
  173. }
  174. #ifdef CONFIG_PM_SLEEP
  175. static int virtrng_freeze(struct virtio_device *vdev)
  176. {
  177. remove_common(vdev);
  178. return 0;
  179. }
  180. static int virtrng_restore(struct virtio_device *vdev)
  181. {
  182. int err;
  183. err = probe_common(vdev);
  184. if (!err) {
  185. struct virtrng_info *vi = vdev->priv;
  186. /*
  187. * Set hwrng_removed to ensure that virtio_read()
  188. * does not block waiting for data before the
  189. * registration is complete.
  190. */
  191. vi->hwrng_removed = true;
  192. err = hwrng_register(&vi->hwrng);
  193. if (!err) {
  194. vi->hwrng_register_done = true;
  195. vi->hwrng_removed = false;
  196. }
  197. }
  198. return err;
  199. }
  200. #endif
  201. static const struct virtio_device_id id_table[] = {
  202. { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
  203. { 0 },
  204. };
  205. static struct virtio_driver virtio_rng_driver = {
  206. .driver.name = KBUILD_MODNAME,
  207. .driver.owner = THIS_MODULE,
  208. .id_table = id_table,
  209. .probe = virtrng_probe,
  210. .remove = virtrng_remove,
  211. .scan = virtrng_scan,
  212. #ifdef CONFIG_PM_SLEEP
  213. .freeze = virtrng_freeze,
  214. .restore = virtrng_restore,
  215. #endif
  216. };
  217. module_virtio_driver(virtio_rng_driver);
  218. MODULE_DEVICE_TABLE(virtio, id_table);
  219. MODULE_DESCRIPTION("Virtio random number driver");
  220. MODULE_LICENSE("GPL");