Skip to content

Commit 984e976

Browse files
kaberherbertx
authored andcommitted
[HWRNG]: move status polling loop to data_present callbacks
Handle waiting for new random within the drivers themselves, this allows to use better suited timeouts for the individual rngs. Signed-off-by: Patrick McHardy <kaber@trash.net> Acked-by: Michael Buesch <mb@bu3sch.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 2407d60 commit 984e976

8 files changed

Lines changed: 73 additions & 38 deletions

File tree

‎drivers/char/hw_random/amd-rng.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <linux/kernel.h>
2929
#include <linux/pci.h>
3030
#include <linux/hw_random.h>
31+
#include <linux/delay.h>
3132
#include <asm/io.h>
3233

3334

@@ -52,11 +53,18 @@ MODULE_DEVICE_TABLE(pci, pci_tbl);
5253
static struct pci_dev *amd_pdev;
5354

5455

55-
static int amd_rng_data_present(struct hwrng *rng)
56+
static int amd_rng_data_present(struct hwrng *rng, int wait)
5657
{
5758
u32 pmbase = (u32)rng->priv;
59+
int data, i;
5860

59-
return !!(inl(pmbase + 0xF4) & 1);
61+
for (i = 0; i < 20; i++) {
62+
data = !!(inl(pmbase + 0xF4) & 1);
63+
if (data || !wait)
64+
break;
65+
udelay(10);
66+
}
67+
return data;
6068
}
6169

6270
static int amd_rng_data_read(struct hwrng *rng, u32 *data)

‎drivers/char/hw_random/core.c‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ static inline void hwrng_cleanup(struct hwrng *rng)
6666
rng->cleanup(rng);
6767
}
6868

69-
static inline int hwrng_data_present(struct hwrng *rng)
69+
static inline int hwrng_data_present(struct hwrng *rng, int wait)
7070
{
7171
if (!rng->data_present)
7272
return 1;
73-
return rng->data_present(rng);
73+
return rng->data_present(rng, wait);
7474
}
7575

7676
static inline int hwrng_data_read(struct hwrng *rng, u32 *data)
@@ -94,8 +94,7 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
9494
{
9595
u32 data;
9696
ssize_t ret = 0;
97-
int i, err = 0;
98-
int data_present;
97+
int err = 0;
9998
int bytes_read;
10099

101100
while (size) {
@@ -107,21 +106,10 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
107106
err = -ENODEV;
108107
goto out;
109108
}
110-
if (filp->f_flags & O_NONBLOCK) {
111-
data_present = hwrng_data_present(current_rng);
112-
} else {
113-
/* Some RNG require some time between data_reads to gather
114-
* new entropy. Poll it.
115-
*/
116-
for (i = 0; i < 20; i++) {
117-
data_present = hwrng_data_present(current_rng);
118-
if (data_present)
119-
break;
120-
udelay(10);
121-
}
122-
}
109+
123110
bytes_read = 0;
124-
if (data_present)
111+
if (hwrng_data_present(current_rng,
112+
!(filp->f_flags & O_NONBLOCK)))
125113
bytes_read = hwrng_data_read(current_rng, &data);
126114
mutex_unlock(&rng_mutex);
127115

‎drivers/char/hw_random/geode-rng.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <linux/kernel.h>
2929
#include <linux/pci.h>
3030
#include <linux/hw_random.h>
31+
#include <linux/delay.h>
3132
#include <asm/io.h>
3233

3334

@@ -61,11 +62,18 @@ static int geode_rng_data_read(struct hwrng *rng, u32 *data)
6162
return 4;
6263
}
6364

64-
static int geode_rng_data_present(struct hwrng *rng)
65+
static int geode_rng_data_present(struct hwrng *rng, int wait)
6566
{
6667
void __iomem *mem = (void __iomem *)rng->priv;
68+
int data, i;
6769

68-
return !!(readl(mem + GEODE_RNG_STATUS_REG));
70+
for (i = 0; i < 20; i++) {
71+
data = !!(readl(mem + GEODE_RNG_STATUS_REG));
72+
if (data || !wait)
73+
break;
74+
udelay(10);
75+
}
76+
return data;
6977
}
7078

7179

‎drivers/char/hw_random/intel-rng.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/module.h>
3030
#include <linux/pci.h>
3131
#include <linux/stop_machine.h>
32+
#include <linux/delay.h>
3233
#include <asm/io.h>
3334

3435

@@ -162,11 +163,19 @@ static inline u8 hwstatus_set(void __iomem *mem,
162163
return hwstatus_get(mem);
163164
}
164165

165-
static int intel_rng_data_present(struct hwrng *rng)
166+
static int intel_rng_data_present(struct hwrng *rng, int wait)
166167
{
167168
void __iomem *mem = (void __iomem *)rng->priv;
168-
169-
return !!(readb(mem + INTEL_RNG_STATUS) & INTEL_RNG_DATA_PRESENT);
169+
int data, i;
170+
171+
for (i = 0; i < 20; i++) {
172+
data = !!(readb(mem + INTEL_RNG_STATUS) &
173+
INTEL_RNG_DATA_PRESENT);
174+
if (data || !wait)
175+
break;
176+
udelay(10);
177+
}
178+
return data;
170179
}
171180

172181
static int intel_rng_data_read(struct hwrng *rng, u32 *data)

‎drivers/char/hw_random/omap-rng.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/err.h>
3030
#include <linux/platform_device.h>
3131
#include <linux/hw_random.h>
32+
#include <linux/delay.h>
3233

3334
#include <asm/io.h>
3435

@@ -65,9 +66,17 @@ static void omap_rng_write_reg(int reg, u32 val)
6566
}
6667

6768
/* REVISIT: Does the status bit really work on 16xx? */
68-
static int omap_rng_data_present(struct hwrng *rng)
69+
static int omap_rng_data_present(struct hwrng *rng, int wait)
6970
{
70-
return omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
71+
int data, i;
72+
73+
for (i = 0; i < 20; i++) {
74+
data = omap_rng_read_reg(RNG_STAT_REG) ? 0 : 1;
75+
if (data || !wait)
76+
break;
77+
udelay(10);
78+
}
79+
return data;
7180
}
7281

7382
static int omap_rng_data_read(struct hwrng *rng, u32 *data)

‎drivers/char/hw_random/pasemi-rng.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/kernel.h>
2424
#include <linux/platform_device.h>
2525
#include <linux/hw_random.h>
26+
#include <linux/delay.h>
2627
#include <asm/of_platform.h>
2728
#include <asm/io.h>
2829

@@ -44,9 +45,16 @@
4445
static int pasemi_rng_data_present(struct hwrng *rng)
4546
{
4647
void __iomem *rng_regs = (void __iomem *)rng->priv;
47-
48-
return (in_le32(rng_regs + SDCRNG_CTL_REG)
49-
& SDCRNG_CTL_FVLD_M) ? 1 : 0;
48+
int data, i;
49+
50+
for (i = 0; i < 20; i++) {
51+
data = (in_le32(rng_regs + SDCRNG_CTL_REG)
52+
& SDCRNG_CTL_FVLD_M) ? 1 : 0;
53+
if (data || !wait)
54+
break;
55+
udelay(10);
56+
}
57+
return data;
5058
}
5159

5260
static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)

‎drivers/char/hw_random/via-rng.c‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/module.h>
2828
#include <linux/kernel.h>
2929
#include <linux/hw_random.h>
30+
#include <linux/delay.h>
3031
#include <asm/io.h>
3132
#include <asm/msr.h>
3233
#include <asm/cpufeature.h>
@@ -77,10 +78,11 @@ static inline u32 xstore(u32 *addr, u32 edx_in)
7778
return eax_out;
7879
}
7980

80-
static int via_rng_data_present(struct hwrng *rng)
81+
static int via_rng_data_present(struct hwrng *rng, int wait)
8182
{
8283
u32 bytes_out;
8384
u32 *via_rng_datum = (u32 *)(&rng->priv);
85+
int i;
8486

8587
/* We choose the recommended 1-byte-per-instruction RNG rate,
8688
* for greater randomness at the expense of speed. Larger
@@ -95,12 +97,15 @@ static int via_rng_data_present(struct hwrng *rng)
9597
* completes.
9698
*/
9799

98-
*via_rng_datum = 0; /* paranoia, not really necessary */
99-
bytes_out = xstore(via_rng_datum, VIA_RNG_CHUNK_1);
100-
bytes_out &= VIA_XSTORE_CNT_MASK;
101-
if (bytes_out == 0)
102-
return 0;
103-
return 1;
100+
for (i = 0; i < 20; i++) {
101+
*via_rng_datum = 0; /* paranoia, not really necessary */
102+
bytes_out = xstore(via_rng_datum, VIA_RNG_CHUNK_1);
103+
bytes_out &= VIA_XSTORE_CNT_MASK;
104+
if (bytes_out || !wait)
105+
break;
106+
udelay(10);
107+
}
108+
return bytes_out ? 1 : 0;
104109
}
105110

106111
static int via_rng_data_read(struct hwrng *rng, u32 *data)

‎include/linux/hw_random.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct hwrng {
3333
const char *name;
3434
int (*init)(struct hwrng *rng);
3535
void (*cleanup)(struct hwrng *rng);
36-
int (*data_present)(struct hwrng *rng);
36+
int (*data_present)(struct hwrng *rng, int wait);
3737
int (*data_read)(struct hwrng *rng, u32 *data);
3838
unsigned long priv;
3939

0 commit comments

Comments
 (0)