2017-12-18 01:41:56

by NeilBrown

[permalink] [raw]
Subject: [PATCH SERIES 4: 0/4] staging: lustre: use standard prng

Lustre has its own internal PRNG code.
This adds nothing of value to the Linux standard prng code,
so switch over to using the standard interfaces.
This adds a few callers to add_device_randomness(), which
helps everyone, and removes unnecessary code.

Thanks,
NeilBrown


---

NeilBrown (4):
staging: lustre: replace cfs_rand() with prandom_u32_max()
staging: lustre: replace cfs_srand() calls with add_device_randomness().
staging: lustre: replace cfs_get_random_bytes calls with get_random_byte()
staging: lustre: libcfs: remove prng


.../staging/lustre/include/linux/libcfs/libcfs.h | 10 -
drivers/staging/lustre/lnet/libcfs/Makefile | 2
drivers/staging/lustre/lnet/libcfs/fail.c | 2
drivers/staging/lustre/lnet/libcfs/prng.c | 137 --------------------
drivers/staging/lustre/lnet/lnet/net_fault.c | 38 +++---
drivers/staging/lustre/lnet/lnet/router.c | 19 +--
drivers/staging/lustre/lustre/include/obd_class.h | 2
drivers/staging/lustre/lustre/llite/super25.c | 17 +-
drivers/staging/lustre/lustre/mgc/mgc_request.c | 4 -
.../lustre/lustre/obdclass/lustre_handles.c | 9 -
drivers/staging/lustre/lustre/ptlrpc/client.c | 2
11 files changed, 42 insertions(+), 200 deletions(-)
delete mode 100644 drivers/staging/lustre/lnet/libcfs/prng.c

--
Signature


2017-12-18 01:42:05

by NeilBrown

[permalink] [raw]
Subject: [PATCH 1/4] staging: lustre: replace cfs_rand() with prandom_u32_max()

All occurrences of
cfs_rand() % X
are replaced with
prandom_u32_max(X)

cfs_rand() is a simple Linear Congruential PRNG. prandom_u32_max()
is at least as random, is seeded with more randomness, and uses
cpu-local state to avoid cross-cpu issues.

This is the first step is discarding the libcfs prng with
the standard linux prng.

Signed-off-by: NeilBrown <[email protected]>
---
drivers/staging/lustre/lnet/libcfs/fail.c | 2 +
drivers/staging/lustre/lnet/lnet/net_fault.c | 38 ++++++++++++-----------
drivers/staging/lustre/lnet/lnet/router.c | 4 +-
drivers/staging/lustre/lustre/mgc/mgc_request.c | 4 +-
4 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/drivers/staging/lustre/lnet/libcfs/fail.c b/drivers/staging/lustre/lnet/libcfs/fail.c
index 5d501beeb622..39439b303d65 100644
--- a/drivers/staging/lustre/lnet/libcfs/fail.c
+++ b/drivers/staging/lustre/lnet/libcfs/fail.c
@@ -61,7 +61,7 @@ int __cfs_fail_check_set(u32 id, u32 value, int set)

/* Fail 1/cfs_fail_val times */
if (cfs_fail_loc & CFS_FAIL_RAND) {
- if (cfs_fail_val < 2 || cfs_rand() % cfs_fail_val > 0)
+ if (cfs_fail_val < 2 || prandom_u32_max(cfs_fail_val) > 0)
return 0;
}

diff --git a/drivers/staging/lustre/lnet/lnet/net_fault.c b/drivers/staging/lustre/lnet/lnet/net_fault.c
index 0318e64c413f..e3468cef273b 100644
--- a/drivers/staging/lustre/lnet/lnet/net_fault.c
+++ b/drivers/staging/lustre/lnet/lnet/net_fault.c
@@ -170,10 +170,10 @@ lnet_drop_rule_add(struct lnet_fault_attr *attr)
rule->dr_attr = *attr;
if (attr->u.drop.da_interval) {
rule->dr_time_base = cfs_time_shift(attr->u.drop.da_interval);
- rule->dr_drop_time = cfs_time_shift(cfs_rand() %
- attr->u.drop.da_interval);
+ rule->dr_drop_time = cfs_time_shift(
+ prandom_u32_max(attr->u.drop.da_interval));
} else {
- rule->dr_drop_at = cfs_rand() % attr->u.drop.da_rate;
+ rule->dr_drop_at = prandom_u32_max(attr->u.drop.da_rate);
}

lnet_net_lock(LNET_LOCK_EX);
@@ -277,10 +277,10 @@ lnet_drop_rule_reset(void)

memset(&rule->dr_stat, 0, sizeof(rule->dr_stat));
if (attr->u.drop.da_rate) {
- rule->dr_drop_at = cfs_rand() % attr->u.drop.da_rate;
+ rule->dr_drop_at = prandom_u32_max(attr->u.drop.da_rate);
} else {
- rule->dr_drop_time = cfs_time_shift(cfs_rand() %
- attr->u.drop.da_interval);
+ rule->dr_drop_time = cfs_time_shift(
+ prandom_u32_max(attr->u.drop.da_interval));
rule->dr_time_base = cfs_time_shift(attr->u.drop.da_interval);
}
spin_unlock(&rule->dr_lock);
@@ -315,8 +315,8 @@ drop_rule_match(struct lnet_drop_rule *rule, lnet_nid_t src,
rule->dr_time_base = now;

rule->dr_drop_time = rule->dr_time_base +
- cfs_time_seconds(cfs_rand() %
- attr->u.drop.da_interval);
+ cfs_time_seconds(
+ prandom_u32_max(attr->u.drop.da_interval));
rule->dr_time_base += cfs_time_seconds(attr->u.drop.da_interval);

CDEBUG(D_NET, "Drop Rule %s->%s: next drop : %lu\n",
@@ -330,7 +330,7 @@ drop_rule_match(struct lnet_drop_rule *rule, lnet_nid_t src,

if (!do_div(rule->dr_stat.fs_count, attr->u.drop.da_rate)) {
rule->dr_drop_at = rule->dr_stat.fs_count +
- cfs_rand() % attr->u.drop.da_rate;
+ prandom_u32_max(attr->u.drop.da_rate);
CDEBUG(D_NET, "Drop Rule %s->%s: next drop: %lu\n",
libcfs_nid2str(attr->fa_src),
libcfs_nid2str(attr->fa_dst), rule->dr_drop_at);
@@ -483,8 +483,9 @@ delay_rule_match(struct lnet_delay_rule *rule, lnet_nid_t src,
rule->dl_time_base = now;

rule->dl_delay_time = rule->dl_time_base +
- cfs_time_seconds(cfs_rand() %
- attr->u.delay.la_interval);
+ cfs_time_seconds(
+ prandom_u32_max(
+ attr->u.delay.la_interval));
rule->dl_time_base += cfs_time_seconds(attr->u.delay.la_interval);

CDEBUG(D_NET, "Delay Rule %s->%s: next delay : %lu\n",
@@ -498,7 +499,7 @@ delay_rule_match(struct lnet_delay_rule *rule, lnet_nid_t src,
/* generate the next random rate sequence */
if (!do_div(rule->dl_stat.fs_count, attr->u.delay.la_rate)) {
rule->dl_delay_at = rule->dl_stat.fs_count +
- cfs_rand() % attr->u.delay.la_rate;
+ prandom_u32_max(attr->u.delay.la_rate);
CDEBUG(D_NET, "Delay Rule %s->%s: next delay: %lu\n",
libcfs_nid2str(attr->fa_src),
libcfs_nid2str(attr->fa_dst), rule->dl_delay_at);
@@ -771,10 +772,10 @@ lnet_delay_rule_add(struct lnet_fault_attr *attr)
rule->dl_attr = *attr;
if (attr->u.delay.la_interval) {
rule->dl_time_base = cfs_time_shift(attr->u.delay.la_interval);
- rule->dl_delay_time = cfs_time_shift(cfs_rand() %
- attr->u.delay.la_interval);
+ rule->dl_delay_time = cfs_time_shift(
+ prandom_u32_max(attr->u.delay.la_interval));
} else {
- rule->dl_delay_at = cfs_rand() % attr->u.delay.la_rate;
+ rule->dl_delay_at = prandom_u32_max(attr->u.delay.la_rate);
}

rule->dl_msg_send = -1;
@@ -920,10 +921,11 @@ lnet_delay_rule_reset(void)

memset(&rule->dl_stat, 0, sizeof(rule->dl_stat));
if (attr->u.delay.la_rate) {
- rule->dl_delay_at = cfs_rand() % attr->u.delay.la_rate;
+ rule->dl_delay_at = prandom_u32_max(attr->u.delay.la_rate);
} else {
- rule->dl_delay_time = cfs_time_shift(cfs_rand() %
- attr->u.delay.la_interval);
+ rule->dl_delay_time =
+ cfs_time_shift(prandom_u32_max(
+ attr->u.delay.la_interval));
rule->dl_time_base = cfs_time_shift(attr->u.delay.la_interval);
}
spin_unlock(&rule->dl_lock);
diff --git a/drivers/staging/lustre/lnet/lnet/router.c b/drivers/staging/lustre/lnet/lnet/router.c
index c40aa79baf5c..e5c9b29e199f 100644
--- a/drivers/staging/lustre/lnet/lnet/router.c
+++ b/drivers/staging/lustre/lnet/lnet/router.c
@@ -277,8 +277,8 @@ lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
len++;
}

- /* len+1 positions to add a new entry, also prevents division by 0 */
- offset = cfs_rand() % (len + 1);
+ /* len+1 positions to add a new entry */
+ offset = prandom_u32_max(len + 1);
list_for_each(e, &rnet->lrn_routes) {
if (!offset)
break;
diff --git a/drivers/staging/lustre/lustre/mgc/mgc_request.c b/drivers/staging/lustre/lustre/mgc/mgc_request.c
index 77fa8fea0249..79ff85feab64 100644
--- a/drivers/staging/lustre/lustre/mgc/mgc_request.c
+++ b/drivers/staging/lustre/lustre/mgc/mgc_request.c
@@ -523,7 +523,7 @@ static void do_requeue(struct config_llog_data *cld)
* in order to not flood the MGS.
*/
#define MGC_TIMEOUT_MIN_SECONDS 5
-#define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
+#define MGC_TIMEOUT_RAND_CENTISEC 500

static int mgc_requeue_thread(void *data)
{
@@ -537,7 +537,7 @@ static int mgc_requeue_thread(void *data)
while (!(rq_state & RQ_STOP)) {
struct l_wait_info lwi;
struct config_llog_data *cld, *cld_prev;
- int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
+ int rand = prandom_u32_max(MGC_TIMEOUT_RAND_CENTISEC);
int to;

/* Any new or requeued lostlocks will change the state */


2017-12-18 01:42:11

by NeilBrown

[permalink] [raw]
Subject: [PATCH 2/4] staging: lustre: replace cfs_srand() calls with add_device_randomness().

The only places that cfs_srand is called, the random bits are
mixed with bits from get_random_bytes(). So it is equally effective
to add entropy to either pool.
So we can replace calls to cfs_srand() with calls that add the
entropy with add_device_randomness(). That function adds time-based
entropy, so we can discard the ktime_get_ts64 calls.

One location in lustre_handles.c only adds timebased
entropy. This cannot improve the entropy provided by get_random_bytes(),
so just discard that call.

Signed-off-by: NeilBrown <[email protected]>
---
drivers/staging/lustre/lnet/lnet/router.c | 15 ++++++---------
drivers/staging/lustre/lustre/llite/super25.c | 17 +++++++----------
.../lustre/lustre/obdclass/lustre_handles.c | 7 -------
3 files changed, 13 insertions(+), 26 deletions(-)

diff --git a/drivers/staging/lustre/lnet/lnet/router.c b/drivers/staging/lustre/lnet/lnet/router.c
index e5c9b29e199f..80a7e8a88acb 100644
--- a/drivers/staging/lustre/lnet/lnet/router.c
+++ b/drivers/staging/lustre/lnet/lnet/router.c
@@ -238,28 +238,25 @@ lnet_find_net_locked(__u32 net)
static void lnet_shuffle_seed(void)
{
static int seeded;
- __u32 lnd_type, seed[2];
- struct timespec64 ts;
struct lnet_ni *ni;

if (seeded)
return;

- cfs_get_random_bytes(seed, sizeof(seed));
-
/*
* Nodes with small feet have little entropy
* the NID for this node gives the most entropy in the low bits
*/
list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
- lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
+ __u32 lnd_type, seed;

- if (lnd_type != LOLND)
- seed[0] ^= (LNET_NIDADDR(ni->ni_nid) | lnd_type);
+ lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
+ if (lnd_type != LOLND) {
+ seed = (LNET_NIDADDR(ni->ni_nid) | lnd_type);
+ add_device_randomness(&seed, sizeof(seed));
+ }
}

- ktime_get_ts64(&ts);
- cfs_srand(ts.tv_sec ^ seed[0], ts.tv_nsec ^ seed[1]);
seeded = 1;
}

diff --git a/drivers/staging/lustre/lustre/llite/super25.c b/drivers/staging/lustre/lustre/llite/super25.c
index 10105339790e..9b0bb3541a84 100644
--- a/drivers/staging/lustre/lustre/llite/super25.c
+++ b/drivers/staging/lustre/lustre/llite/super25.c
@@ -86,8 +86,7 @@ MODULE_ALIAS_FS("lustre");
static int __init lustre_init(void)
{
struct lnet_process_id lnet_id;
- struct timespec64 ts;
- int i, rc, seed[2];
+ int i, rc;

BUILD_BUG_ON(sizeof(LUSTRE_VOLATILE_HDR) !=
LUSTRE_VOLATILE_HDR_LEN + 1);
@@ -126,22 +125,20 @@ static int __init lustre_init(void)
goto out_debugfs;
}

- cfs_get_random_bytes(seed, sizeof(seed));
-
/* Nodes with small feet have little entropy. The NID for this
* node gives the most entropy in the low bits
*/
for (i = 0;; i++) {
+ u32 seed;
+
if (LNetGetId(i, &lnet_id) == -ENOENT)
break;
-
- if (LNET_NETTYP(LNET_NIDNET(lnet_id.nid)) != LOLND)
- seed[0] ^= LNET_NIDADDR(lnet_id.nid);
+ if (LNET_NETTYP(LNET_NIDNET(lnet_id.nid)) != LOLND) {
+ seed = LNET_NIDADDR(lnet_id.nid);
+ add_device_randomness(&seed, sizeof(seed));
+ }
}

- ktime_get_ts64(&ts);
- cfs_srand(ts.tv_sec ^ seed[0], ts.tv_nsec ^ seed[1]);
-
rc = vvp_global_init();
if (rc != 0)
goto out_sysfs;
diff --git a/drivers/staging/lustre/lustre/obdclass/lustre_handles.c b/drivers/staging/lustre/lustre/obdclass/lustre_handles.c
index 71329adc0318..d1b6c2f134d7 100644
--- a/drivers/staging/lustre/lustre/obdclass/lustre_handles.c
+++ b/drivers/staging/lustre/lustre/obdclass/lustre_handles.c
@@ -181,8 +181,6 @@ EXPORT_SYMBOL(class_handle_free_cb);
int class_handle_init(void)
{
struct handle_bucket *bucket;
- struct timespec64 ts;
- int seed[2];

LASSERT(!handle_hash);

@@ -198,11 +196,6 @@ int class_handle_init(void)
spin_lock_init(&bucket->lock);
}

- /** bug 21430: add randomness to the initial base */
- cfs_get_random_bytes(seed, sizeof(seed));
- ktime_get_ts64(&ts);
- cfs_srand(ts.tv_sec ^ seed[0], ts.tv_nsec ^ seed[1]);
-
cfs_get_random_bytes(&handle_base, sizeof(handle_base));
LASSERT(handle_base != 0ULL);



2017-12-18 01:42:17

by NeilBrown

[permalink] [raw]
Subject: [PATCH 3/4] staging: lustre: replace cfs_get_random_bytes calls with get_random_byte()

The cfs_get_random_bytes() interface adds nothing of value
to get_random_byte() (which it uses internally). So just use the
standard interface.

Signed-off-by: NeilBrown <[email protected]>
---
drivers/staging/lustre/lustre/include/obd_class.h | 2 +-
.../lustre/lustre/obdclass/lustre_handles.c | 2 +-
drivers/staging/lustre/lustre/ptlrpc/client.c | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/lustre/lustre/include/obd_class.h b/drivers/staging/lustre/lustre/include/obd_class.h
index 67c535c5aa98..25db7ec6ecd0 100644
--- a/drivers/staging/lustre/lustre/include/obd_class.h
+++ b/drivers/staging/lustre/lustre/include/obd_class.h
@@ -1562,7 +1562,7 @@ int class_procfs_init(void);
int class_procfs_clean(void);

/* prng.c */
-#define ll_generate_random_uuid(uuid_out) cfs_get_random_bytes(uuid_out, sizeof(class_uuid_t))
+#define ll_generate_random_uuid(uuid_out) get_random_bytes(uuid_out, sizeof(class_uuid_t))

/* statfs_pack.c */
struct kstatfs;
diff --git a/drivers/staging/lustre/lustre/obdclass/lustre_handles.c b/drivers/staging/lustre/lustre/obdclass/lustre_handles.c
index d1b6c2f134d7..2d6da2431a09 100644
--- a/drivers/staging/lustre/lustre/obdclass/lustre_handles.c
+++ b/drivers/staging/lustre/lustre/obdclass/lustre_handles.c
@@ -196,7 +196,7 @@ int class_handle_init(void)
spin_lock_init(&bucket->lock);
}

- cfs_get_random_bytes(&handle_base, sizeof(handle_base));
+ get_random_bytes(&handle_base, sizeof(handle_base));
LASSERT(handle_base != 0ULL);

return 0;
diff --git a/drivers/staging/lustre/lustre/ptlrpc/client.c b/drivers/staging/lustre/lustre/ptlrpc/client.c
index 2a9f2f2ebaa8..bac4b2304bad 100644
--- a/drivers/staging/lustre/lustre/ptlrpc/client.c
+++ b/drivers/staging/lustre/lustre/ptlrpc/client.c
@@ -3067,7 +3067,7 @@ void ptlrpc_init_xid(void)

spin_lock_init(&ptlrpc_last_xid_lock);
if (now < YEAR_2004) {
- cfs_get_random_bytes(&ptlrpc_last_xid, sizeof(ptlrpc_last_xid));
+ get_random_bytes(&ptlrpc_last_xid, sizeof(ptlrpc_last_xid));
ptlrpc_last_xid >>= 2;
ptlrpc_last_xid |= (1ULL << 61);
} else {


2017-12-18 01:42:24

by NeilBrown

[permalink] [raw]
Subject: [PATCH 4/4] staging: lustre: libcfs: remove prng

The cfs prng is no longer used, so discard it.

Signed-off-by: NeilBrown <[email protected]>
---
.../staging/lustre/include/linux/libcfs/libcfs.h | 10 -
drivers/staging/lustre/lnet/libcfs/Makefile | 2
drivers/staging/lustre/lnet/libcfs/prng.c | 137 --------------------
3 files changed, 1 insertion(+), 148 deletions(-)
delete mode 100644 drivers/staging/lustre/lnet/libcfs/prng.c

diff --git a/drivers/staging/lustre/include/linux/libcfs/libcfs.h b/drivers/staging/lustre/include/linux/libcfs/libcfs.h
index f2ba83ee5362..ca3472cc952f 100644
--- a/drivers/staging/lustre/include/linux/libcfs/libcfs.h
+++ b/drivers/staging/lustre/include/linux/libcfs/libcfs.h
@@ -73,16 +73,6 @@ sigset_t cfs_block_sigsinv(unsigned long sigs);
void cfs_restore_sigs(sigset_t sigset);
void cfs_clear_sigpending(void);

-/*
- * Random number handling
- */
-
-/* returns a random 32-bit integer */
-unsigned int cfs_rand(void);
-/* seed the generator */
-void cfs_srand(unsigned int seed1, unsigned int seed2);
-void cfs_get_random_bytes(void *buf, int size);
-
struct libcfs_ioctl_handler {
struct list_head item;
int (*handle_ioctl)(unsigned int cmd, struct libcfs_ioctl_hdr *hdr);
diff --git a/drivers/staging/lustre/lnet/libcfs/Makefile b/drivers/staging/lustre/lnet/libcfs/Makefile
index 51b529434332..730f2c675047 100644
--- a/drivers/staging/lustre/lnet/libcfs/Makefile
+++ b/drivers/staging/lustre/lnet/libcfs/Makefile
@@ -15,7 +15,7 @@ libcfs-linux-objs += linux-mem.o
libcfs-linux-objs := $(addprefix linux/,$(libcfs-linux-objs))

libcfs-all-objs := debug.o fail.o module.o tracefile.o \
- libcfs_string.o hash.o prng.o \
+ libcfs_string.o hash.o \
libcfs_cpu.o libcfs_mem.o libcfs_lock.o

libcfs-objs := $(libcfs-linux-objs) $(libcfs-all-objs)
diff --git a/drivers/staging/lustre/lnet/libcfs/prng.c b/drivers/staging/lustre/lnet/libcfs/prng.c
deleted file mode 100644
index f47cf67a92e3..000000000000
--- a/drivers/staging/lustre/lnet/libcfs/prng.c
+++ /dev/null
@@ -1,137 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * GPL HEADER START
- *
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 only,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License version 2 for more details (a copy is included
- * in the LICENSE file that accompanied this code).
- *
- * You should have received a copy of the GNU General Public License
- * version 2 along with this program; If not, see
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * GPL HEADER END
- */
-/*
- * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
- * Use is subject to license terms.
- */
-/*
- * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
- *
- * libcfs/libcfs/prng.c
- *
- * concatenation of following two 16-bit multiply with carry generators
- * x(n)=a*x(n-1)+carry mod 2^16 and y(n)=b*y(n-1)+carry mod 2^16,
- * number and carry packed within the same 32 bit integer.
- * algorithm recommended by Marsaglia
- */
-
-#include <linux/libcfs/libcfs.h>
-
-/*
- * From: George Marsaglia <[email protected]>
- * Newsgroups: sci.math
- * Subject: Re: A RANDOM NUMBER GENERATOR FOR C
- * Date: Tue, 30 Sep 1997 05:29:35 -0700
- *
- * You may replace the two constants 36969 and 18000 by any
- * pair of distinct constants from this list:
- * 18000 18030 18273 18513 18879 19074 19098 19164 19215 19584
- * 19599 19950 20088 20508 20544 20664 20814 20970 21153 21243
- * 21423 21723 21954 22125 22188 22293 22860 22938 22965 22974
- * 23109 23124 23163 23208 23508 23520 23553 23658 23865 24114
- * 24219 24660 24699 24864 24948 25023 25308 25443 26004 26088
- * 26154 26550 26679 26838 27183 27258 27753 27795 27810 27834
- * 27960 28320 28380 28689 28710 28794 28854 28959 28980 29013
- * 29379 29889 30135 30345 30459 30714 30903 30963 31059 31083
- * (or any other 16-bit constants k for which both k*2^16-1
- * and k*2^15-1 are prime)
- */
-
-#define RANDOM_CONST_A 18030
-#define RANDOM_CONST_B 29013
-
-static unsigned int seed_x = 521288629;
-static unsigned int seed_y = 362436069;
-
-/**
- * cfs_rand - creates new seeds
- *
- * First it creates new seeds from the previous seeds. Then it generates a
- * new pseudo random number for use.
- *
- * Returns a pseudo-random 32-bit integer
- */
-unsigned int cfs_rand(void)
-{
- seed_x = RANDOM_CONST_A * (seed_x & 65535) + (seed_x >> 16);
- seed_y = RANDOM_CONST_B * (seed_y & 65535) + (seed_y >> 16);
-
- return ((seed_x << 16) + (seed_y & 65535));
-}
-EXPORT_SYMBOL(cfs_rand);
-
-/**
- * cfs_srand - sets the initial seed
- * @seed1 : (seed_x) should have the most entropy in the low bits of the word
- * @seed2 : (seed_y) should have the most entropy in the high bits of the word
- *
- * Replaces the original seeds with new values. Used to generate a new pseudo
- * random numbers.
- */
-void cfs_srand(unsigned int seed1, unsigned int seed2)
-{
- if (seed1)
- seed_x = seed1; /* use default seeds if parameter is 0 */
- if (seed2)
- seed_y = seed2;
-}
-EXPORT_SYMBOL(cfs_srand);
-
-/**
- * cfs_get_random_bytes - generate a bunch of random numbers
- * @buf : buffer to fill with random numbers
- * @size: size of passed in buffer
- *
- * Fills a buffer with random bytes
- */
-void cfs_get_random_bytes(void *buf, int size)
-{
- int *p = buf;
- int rem, tmp;
-
- LASSERT(size >= 0);
-
- rem = min((int)((unsigned long)buf & (sizeof(int) - 1)), size);
- if (rem) {
- get_random_bytes(&tmp, sizeof(tmp));
- tmp ^= cfs_rand();
- memcpy(buf, &tmp, rem);
- p = buf + rem;
- size -= rem;
- }
-
- while (size >= sizeof(int)) {
- get_random_bytes(&tmp, sizeof(tmp));
- *p = cfs_rand() ^ tmp;
- size -= sizeof(int);
- p++;
- }
- buf = p;
- if (size) {
- get_random_bytes(&tmp, sizeof(tmp));
- tmp ^= cfs_rand();
- memcpy(buf, &tmp, size);
- }
-}
-EXPORT_SYMBOL(cfs_get_random_bytes);


2017-12-19 00:52:10

by Dilger, Andreas

[permalink] [raw]
Subject: Re: [lustre-devel] [PATCH SERIES 4: 0/4] staging: lustre: use standard prng

On Dec 17, 2017, at 18:41, NeilBrown <[email protected]> wrote:
>
> Lustre has its own internal PRNG code.
> This adds nothing of value to the Linux standard prng code,
> so switch over to using the standard interfaces.
> This adds a few callers to add_device_randomness(), which
> helps everyone, and removes unnecessary code.

Neil,
Thanks for the patches. I'll run them through our testing system, but
they look good at first glance.

An interesting anecdote as this code is removed... When it was first
added, we were running Lustre on a single-threaded runtime environment
without any local storage, interrupts, local clock, or h/w RNG (Catamount,
on the ASCI Red Storm https://en.wikipedia.org/wiki/Red_Storm_(computing)
supercomputer) and since there were thousands of nodes booting up and
mounting Lustre, there were often some with identical random number
states/seeds after boot, so we had to fold in the only unique state that
we had on each node - the network address.

That system is long gone, and it is good to clean up this code in a
more portable manner.

Cheers, Andreas


> ---
>
> NeilBrown (4):
> staging: lustre: replace cfs_rand() with prandom_u32_max()
> staging: lustre: replace cfs_srand() calls with add_device_randomness().
> staging: lustre: replace cfs_get_random_bytes calls with get_random_byte()
> staging: lustre: libcfs: remove prng
>
>
> .../staging/lustre/include/linux/libcfs/libcfs.h | 10 -
> drivers/staging/lustre/lnet/libcfs/Makefile | 2
> drivers/staging/lustre/lnet/libcfs/fail.c | 2
> drivers/staging/lustre/lnet/libcfs/prng.c | 137 --------------------
> drivers/staging/lustre/lnet/lnet/net_fault.c | 38 +++---
> drivers/staging/lustre/lnet/lnet/router.c | 19 +--
> drivers/staging/lustre/lustre/include/obd_class.h | 2
> drivers/staging/lustre/lustre/llite/super25.c | 17 +-
> drivers/staging/lustre/lustre/mgc/mgc_request.c | 4 -
> .../lustre/lustre/obdclass/lustre_handles.c | 9 -
> drivers/staging/lustre/lustre/ptlrpc/client.c | 2
> 11 files changed, 42 insertions(+), 200 deletions(-)
> delete mode 100644 drivers/staging/lustre/lnet/libcfs/prng.c
>
> --
> Signature
>
> _______________________________________________
> lustre-devel mailing list
> [email protected]
> http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org

Cheers, Andreas
--
Andreas Dilger
Lustre Principal Architect
Intel Corporation