hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
kernel/drivers/char/hw_random/virtio-rng.c
....@@ -4,6 +4,7 @@
44 * Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
55 */
66
7
+#include <asm/barrier.h>
78 #include <linux/err.h>
89 #include <linux/hw_random.h>
910 #include <linux/scatterlist.h>
....@@ -18,71 +19,111 @@
1819 struct virtrng_info {
1920 struct hwrng hwrng;
2021 struct virtqueue *vq;
21
- struct completion have_data;
2222 char name[25];
23
- unsigned int data_avail;
2423 int index;
25
- bool busy;
2624 bool hwrng_register_done;
2725 bool hwrng_removed;
26
+ /* data transfer */
27
+ struct completion have_data;
28
+ unsigned int data_avail;
29
+ unsigned int data_idx;
30
+ /* minimal size returned by rng_buffer_size() */
31
+#if SMP_CACHE_BYTES < 32
32
+ u8 data[32];
33
+#else
34
+ u8 data[SMP_CACHE_BYTES];
35
+#endif
2836 };
2937
3038 static void random_recv_done(struct virtqueue *vq)
3139 {
3240 struct virtrng_info *vi = vq->vdev->priv;
41
+ unsigned int len;
3342
3443 /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
35
- if (!virtqueue_get_buf(vi->vq, &vi->data_avail))
44
+ if (!virtqueue_get_buf(vi->vq, &len))
3645 return;
3746
47
+ smp_store_release(&vi->data_avail, len);
3848 complete(&vi->have_data);
3949 }
4050
41
-/* The host will fill any buffer we give it with sweet, sweet randomness. */
42
-static void register_buffer(struct virtrng_info *vi, u8 *buf, size_t size)
51
+static void request_entropy(struct virtrng_info *vi)
4352 {
4453 struct scatterlist sg;
4554
46
- sg_init_one(&sg, buf, size);
55
+ reinit_completion(&vi->have_data);
56
+ vi->data_idx = 0;
57
+
58
+ sg_init_one(&sg, vi->data, sizeof(vi->data));
4759
4860 /* There should always be room for one buffer. */
49
- virtqueue_add_inbuf(vi->vq, &sg, 1, buf, GFP_KERNEL);
61
+ virtqueue_add_inbuf(vi->vq, &sg, 1, vi->data, GFP_KERNEL);
5062
5163 virtqueue_kick(vi->vq);
64
+}
65
+
66
+static unsigned int copy_data(struct virtrng_info *vi, void *buf,
67
+ unsigned int size)
68
+{
69
+ size = min_t(unsigned int, size, vi->data_avail);
70
+ memcpy(buf, vi->data + vi->data_idx, size);
71
+ vi->data_idx += size;
72
+ vi->data_avail -= size;
73
+ if (vi->data_avail == 0)
74
+ request_entropy(vi);
75
+ return size;
5276 }
5377
5478 static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
5579 {
5680 int ret;
5781 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
82
+ unsigned int chunk;
83
+ size_t read;
5884
5985 if (vi->hwrng_removed)
6086 return -ENODEV;
6187
62
- if (!vi->busy) {
63
- vi->busy = true;
64
- reinit_completion(&vi->have_data);
65
- register_buffer(vi, buf, size);
88
+ read = 0;
89
+
90
+ /* copy available data */
91
+ if (smp_load_acquire(&vi->data_avail)) {
92
+ chunk = copy_data(vi, buf, size);
93
+ size -= chunk;
94
+ read += chunk;
6695 }
6796
6897 if (!wait)
69
- return 0;
98
+ return read;
7099
71
- ret = wait_for_completion_killable(&vi->have_data);
72
- if (ret < 0)
73
- return ret;
100
+ /* We have already copied available entropy,
101
+ * so either size is 0 or data_avail is 0
102
+ */
103
+ while (size != 0) {
104
+ /* data_avail is 0 but a request is pending */
105
+ ret = wait_for_completion_killable(&vi->have_data);
106
+ if (ret < 0)
107
+ return ret;
108
+ /* if vi->data_avail is 0, we have been interrupted
109
+ * by a cleanup, but buffer stays in the queue
110
+ */
111
+ if (vi->data_avail == 0)
112
+ return read;
74113
75
- vi->busy = false;
114
+ chunk = copy_data(vi, buf + read, size);
115
+ size -= chunk;
116
+ read += chunk;
117
+ }
76118
77
- return vi->data_avail;
119
+ return read;
78120 }
79121
80122 static void virtio_cleanup(struct hwrng *rng)
81123 {
82124 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
83125
84
- if (vi->busy)
85
- wait_for_completion(&vi->have_data);
126
+ complete(&vi->have_data);
86127 }
87128
88129 static int probe_common(struct virtio_device *vdev)
....@@ -118,6 +159,9 @@
118159 goto err_find;
119160 }
120161
162
+ /* we always have a pending entropy request */
163
+ request_entropy(vi);
164
+
121165 return 0;
122166
123167 err_find:
....@@ -133,9 +177,9 @@
133177
134178 vi->hwrng_removed = true;
135179 vi->data_avail = 0;
180
+ vi->data_idx = 0;
136181 complete(&vi->have_data);
137182 vdev->config->reset(vdev);
138
- vi->busy = false;
139183 if (vi->hwrng_register_done)
140184 hwrng_unregister(&vi->hwrng);
141185 vdev->config->del_vqs(vdev);