[PATCH 4/7] lib/random32.c: Use instead of hand-rolling it

George Spelvin
2014-06-07T08:25:21Z

The functions exist for a reason; the manual byte-at-a-time code
is unnecessarily slow (and bloated).

Signed-off-by: George Spelvin <linux@horizon.com>
---
lib/random32.c | 23 +++++++++++------------
1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/lib/random32.c b/lib/random32.c
index e8f3557b..eee60100 100644
--- a/lib/random32.c
+++ b/lib/random32.c
@@ -37,6 +37,7 @@
#include <linux/jiffies.h>
#include <linux/random.h>
#include <linux/sched.h>
+#include <asm/unaligned.h>

#ifdef CONFIG_RANDOM32_SELFTEST
static void __init prandom_state_selftest(void);
@@ -97,25 +98,23 @@ EXPORT_SYMBOL(prandom_u32);
*/
void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes)
{
- unsigned char *p = buf;
+ u8 *p = buf;
int i;

- for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) {
- u32 random = prandom_u32_state(state);
- int j;
+ for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32))
+ put_unaligned_le32(prandom_u32_state(state), p+i);

- for (j = 0; j < sizeof(u32); j++) {
- p[i + j] = random;
- random >>= BITS_PER_BYTE;
- }
- }
if (i < bytes) {
u32 random = prandom_u32_state(state);

- for (; i < bytes; i++) {
- p[i] = random;
- random >>= BITS_PER_BYTE;
+ if (bytes & 2) {
+ put_unaligned_le16((u16)random, p+i);
+ if ((bytes & 1) == 0)
+ return;
+ i += 2;
+ random >>= 16;
}
+ p[i] = (u8)random;
}
}
EXPORT_SYMBOL(prandom_bytes_state);
--
2.0.0