History

Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6

Pull crypto fixes from Herbert Xu: "This push fixes a boot hang in virt guests when the virtio RNG is enabled" * git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6: hwrng: virtio - ensure reads happen after successful probe hwrng: fetch randomness only after device init

Linus Torvalds committed 12 years ago

Commit 4e106272 parents f839719 e052dbf2 files · +49 −8Browse files

Unified · Split

2 files
  1. drivers/char/hw_random/core.c +39 −8
  2. drivers/char/hw_random/virtio-rng.c +10 −0
drivers/char/hw_random/core.c+39−8
@@ -55,16 +55,41 @@ static DEFINE_MUTEX(rng_mutex); · 54 unmodified lines
5555 static int data_avail;
5656 static u8 *rng_buffer;
5757
58+static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
59+ int wait);
60+
5861 static size_t rng_buffer_size(void)
5962 {
6063 return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
6164 }
6265
66+static void add_early_randomness(struct hwrng *rng)
67+{
68+ unsigned char bytes[16];
69+ int bytes_read;
70+
71+ /*
72+ * Currently only virtio-rng cannot return data during device
73+ * probe, and that's handled in virtio-rng.c itself. If there
74+ * are more such devices, this call to rng_get_data can be
75+ * made conditional here instead of doing it per-device.
76+ */
77+ bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
78+ if (bytes_read > 0)
79+ add_device_randomness(bytes, bytes_read);
80+}
81+
6382 static inline int hwrng_init(struct hwrng *rng)
6483 {
65− if (!rng->init)
66− return 0;
67− return rng->init(rng);
84+ if (rng->init) {
85+ int ret;
86+
87+ ret = rng->init(rng);
88+ if (ret)
89+ return ret;
90+ }
91+ add_early_randomness(rng);
92+ return 0;
6893 }
6994
7095 static inline void hwrng_cleanup(struct hwrng *rng)
@@ -304,8 +329,6 @@ int hwrng_register(struct hwrng *rng) · 233 unmodified lines
304329 {
305330 int err = -EINVAL;
306331 struct hwrng *old_rng, *tmp;
307− unsigned char bytes[16];
308− int bytes_read;
309332
310333 if (rng->name == NULL ||
311334 (rng->data_read == NULL && rng->read == NULL))
@@ -347,9 +370,17 @@ int hwrng_register(struct hwrng *rng) · 35 unmodified lines
347370 INIT_LIST_HEAD(&rng->list);
348371 list_add_tail(&rng->list, &rng_list);
349372
350− bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
351− if (bytes_read > 0)
352− add_device_randomness(bytes, bytes_read);
373+ if (old_rng && !rng->init) {
374+ /*
375+ * Use a new device's input to add some randomness to
376+ * the system. If this rng device isn't going to be
377+ * used right away, its init function hasn't been
378+ * called yet; so only use the randomness from devices
379+ * that don't need an init callback.
380+ */
381+ add_early_randomness(rng);
382+ }
383+
353384 out_unlock:
354385 mutex_unlock(&rng_mutex);
355386 out:
drivers/char/hw_random/virtio-rng.c+10−0
@@ -38,6 +38,8 @@ struct virtrng_info { · 37 unmodified lines
3838 int index;
3939 };
4040
41+static bool probe_done;
42+
4143 static void random_recv_done(struct virtqueue *vq)
4244 {
4345 struct virtrng_info *vi = vq->vdev->priv;
@@ -67,6 +69,13 @@ static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait) · 23 unmodified lines
6769 int ret;
6870 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
6971
72+ /*
73+ * Don't ask host for data till we're setup. This call can
74+ * happen during hwrng_register(), after commit d9e7972619.
75+ */
76+ if (unlikely(!probe_done))
77+ return 0;
78+
7079 if (!vi->busy) {
7180 vi->busy = true;
7281 init_completion(&vi->have_data);
@@ -137,6 +146,7 @@ static int probe_common(struct virtio_device *vdev) · 64 unmodified lines
137146 return err;
138147 }
139148
149+ probe_done = true;
140150 return 0;
141151 }
142152

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t