From de5eb5614565319e3a19fd6063fa2f695c154cbc Mon Sep 17 00:00:00 2001 From: green Date: Sat, 10 Dec 2005 10:19:26 +0000 Subject: [PATCH] b=9794 r=adilger This patch uses a fast, high-quality PRNG internal to Lustre instead of the glibc PRNG to avoid perturbing the glibc PRNG sequence for liblustre clients. It also uses this PRNG for some uses in the lustre core code. Introduces ll_rand() and ll_srand() with usage similar to rand(3) and srand(3) --- lustre/ChangeLog | 7 +++++ lustre/include/linux/lustre_lib.h | 11 +++---- lustre/liblustre/lutil.c | 47 ++++++++++++++++------------- lustre/llite/super.c | 6 +++- lustre/llite/super25.c | 5 +++- lustre/lov/lov_qos.c | 2 +- lustre/mds/mds_fs.c | 2 +- lustre/obdclass/Makefile.in | 2 +- lustre/obdclass/autoMakefile.am | 3 +- lustre/obdclass/llog_test.c | 2 +- lustre/obdclass/prng.c | 63 +++++++++++++++++++++++++++++++++++++++ lustre/obdecho/echo_client.c | 2 +- 12 files changed, 116 insertions(+), 36 deletions(-) create mode 100644 lustre/obdclass/prng.c diff --git a/lustre/ChangeLog b/lustre/ChangeLog index b6c5bb9..3e04199 100644 --- a/lustre/ChangeLog +++ b/lustre/ChangeLog @@ -412,6 +412,13 @@ Description: 'bad disk LOV MAGIC: 0x00000000' error when chown'ing files without objects Details : Make mds_get_md to recognise empty md case and set lmm size to 0. +Severity : minor +Bugzilla : 9794 +Description: Liblustre uses system PRNG disturbing its usage by user application +Details : Introduce internal to lustre fast and high-quality PRNG for + lustre usage and make liblustre and some other places in generic + lustre code to use it. + ------------------------------------------------------------------------------ diff --git a/lustre/include/linux/lustre_lib.h b/lustre/include/linux/lustre_lib.h index 74b99091..3282ac8 100644 --- a/lustre/include/linux/lustre_lib.h +++ b/lustre/include/linux/lustre_lib.h @@ -51,6 +51,10 @@ #endif #endif +/* prng.c */ +unsigned int ll_rand(void); /* returns a random 32-bit integer */ +void ll_srand(unsigned int, unsigned int); /* seed the generator */ + /* target.c */ struct ptlrpc_request; struct recovd_data; @@ -473,13 +477,6 @@ static inline void obd_ioctl_freedata(char *buf, int len) #define POISON_BULK 0 -static inline int ll_insecure_random_int(void) -{ - struct timeval t; - do_gettimeofday(&t); - return (int)(t.tv_usec); -} - /* * l_wait_event is a flexible sleeping function, permitting simple caller * configuration of interrupt and timeout sensitivity along with actions to diff --git a/lustre/liblustre/lutil.c b/lustre/liblustre/lutil.c index bbb788b..65d92da 100644 --- a/lustre/liblustre/lutil.c +++ b/lustre/liblustre/lutil.c @@ -77,9 +77,6 @@ void *inter_module_get(char *arg) /* * random number generator stuff */ -#ifdef LIBLUSTRE_USE_URANDOM -static int _rand_dev_fd = -1; -#endif #ifdef HAVE_GETHOSTBYNAME static int get_ipv4_addr() @@ -107,49 +104,57 @@ static int get_ipv4_addr() void liblustre_init_random() { - int seed; + int _rand_dev_fd; + int seed[2]; struct timeval tv; #ifdef LIBLUSTRE_USE_URANDOM _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY); if (_rand_dev_fd >= 0) { if (syscall(SYS_read, _rand_dev_fd, - &seed, sizeof(int)) == sizeof(int)) { - srand(seed); + &seed, sizeof(seed)) == sizeof(seed)) { + ll_srand(seed[0], seed[1]); return; } syscall(SYS_close, _rand_dev_fd); - _rand_dev_fd = -1; } #endif /* LIBLUSTRE_USE_URANDOM */ #ifdef HAVE_GETHOSTBYNAME - seed = get_ipv4_addr(); + seed[0] = get_ipv4_addr(); #else - seed = _my_pnid; + seed[0] = _my_pnid; #endif gettimeofday(&tv, NULL); - srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(seed)); + ll_srand(tv.tv_usec | __swab32(getpid()), tv.tv_sec|__swab32(seed[0])); } void get_random_bytes(void *buf, int size) { - char *p = buf; + int *p = buf; + int rem; LASSERT(size >= 0); -#ifdef LIBLUSTRE_USE_URANDOM - if (_rand_dev_fd >= 0) { - if (syscall(SYS_read, _rand_dev_fd, buf, size) == size) - return; - syscall(SYS_close, _rand_dev_fd); - _rand_dev_fd = -1; + rem = min((unsigned long)buf & (sizeof(int) - 1), size); + if (rem) { + int val = ll_rand(); + memcpy(buf, &val, rem); + p = buf + rem; + size -= rem; } -#endif - while (size--) - *p++ = rand(); + while (size >= sizeof(int)) { + *p = ll_rand(); + size -= sizeof(int); + p++; + } + buf = p; + if (size) { + int val = ll_rand(); + memcpy(buf, &val, size); + } } - + static void init_capability(int *res) { #ifdef HAVE_LIBCAP diff --git a/lustre/llite/super.c b/lustre/llite/super.c index c96621e..1635f56 100644 --- a/lustre/llite/super.c +++ b/lustre/llite/super.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include "llite_internal.h" @@ -91,7 +92,7 @@ static struct file_system_type lustre_fs_type = { static int __init init_lustre_lite(void) { - int rc; + int rc, seed[2]; printk(KERN_INFO "Lustre: Lustre Lite Client File System; " "info@clusterfs.com\n"); @@ -115,6 +116,9 @@ static int __init init_lustre_lite(void) ll_unregister_cache(&ll_cache_definition); } + get_random_bytes(seed, sizeof(seed)); + ll_srand(seed[0], seed[1]); + return rc; } diff --git a/lustre/llite/super25.c b/lustre/llite/super25.c index 8fc9af4..d84f1b3 100644 --- a/lustre/llite/super25.c +++ b/lustre/llite/super25.c @@ -128,7 +128,7 @@ struct file_system_type lustre_fs_type = { static int __init init_lustre_lite(void) { - int rc; + int rc, seed[2]; printk(KERN_INFO "Lustre: Lustre Lite Client File System; " "info@clusterfs.com\n"); rc = ll_init_inodecache(); @@ -156,6 +156,9 @@ static int __init init_lustre_lite(void) ll_unregister_cache(&ll_cache_definition); } + get_random_bytes(seed, sizeof(seed)); + ll_srand(seed[0], seed[1]); + return rc; } diff --git a/lustre/lov/lov_qos.c b/lustre/lov/lov_qos.c index 3a42f88..90a6491 100644 --- a/lustre/lov/lov_qos.c +++ b/lustre/lov/lov_qos.c @@ -131,7 +131,7 @@ int qos_prep_create(struct lov_obd *lov, struct lov_request_set *set, int newea) if (newea || lsm->lsm_oinfo[0].loi_ost_idx >= ost_count) { if (--ost_start_count <= 0) { - ost_start_idx = ll_insecure_random_int(); + ost_start_idx = ll_rand(); ost_start_count = (LOV_CREATE_RESEED_MIN / max(ost_active_count, 1U) + LOV_CREATE_RESEED_MULT) * max(ost_active_count, 1U); diff --git a/lustre/mds/mds_fs.c b/lustre/mds/mds_fs.c index 5d4bc8c..78062c1 100644 --- a/lustre/mds/mds_fs.c +++ b/lustre/mds/mds_fs.c @@ -617,7 +617,7 @@ int mds_obd_create(struct obd_export *exp, struct obdo *oa, { struct mds_obd *mds = &exp->exp_obd->u.mds; struct inode *parent_inode = mds->mds_objects_dir->d_inode; - unsigned int tmpname = ll_insecure_random_int(); + unsigned int tmpname = ll_rand(); struct file *filp; struct dentry *new_child; struct lvfs_run_ctxt saved; diff --git a/lustre/obdclass/Makefile.in b/lustre/obdclass/Makefile.in index f761599..37c8b14 100644 --- a/lustre/obdclass/Makefile.in +++ b/lustre/obdclass/Makefile.in @@ -4,7 +4,7 @@ obdclass-objs := llog.o llog_cat.o llog_lvfs.o llog_obd.o llog_swab.o obdclass-objs += class_obd.o obdclass-objs += debug.o genops.o sysctl.o uuid.o llog_ioctl.o obdclass-objs += lprocfs_status.o lustre_handles.o lustre_peer.o -obdclass-objs += statfs_pack.o obdo.o obd_config.o +obdclass-objs += statfs_pack.o obdo.o obd_config.o prng.o ifeq ($(PATCHLEVEL),6) llog_test-objs := llog-test.o diff --git a/lustre/obdclass/autoMakefile.am b/lustre/obdclass/autoMakefile.am index 0cdce91..163596e 100644 --- a/lustre/obdclass/autoMakefile.am +++ b/lustre/obdclass/autoMakefile.am @@ -4,7 +4,8 @@ noinst_LIBRARIES = liblustreclass.a liblustreclass_a_SOURCES = class_obd.c debug.c genops.c statfs_pack.c uuid.c liblustreclass_a_SOURCES += lustre_handles.c lustre_peer.c lprocfs_status.c liblustreclass_a_SOURCES += obdo.c obd_config.c llog.c llog_obd.c llog_cat.c -liblustreclass_a_SOURCES += llog_lvfs.c llog_swab.c #llog_ioctl.c rbtree.c +liblustreclass_a_SOURCES += llog_lvfs.c llog_swab.c +liblustreclass_a_SOURCES += prng.c #llog_ioctl.c rbtree.c liblustreclass_a_CPPFLAGS = $(LLCPPFLAGS) -DLUSTRE_VERSION=\"32\" -DBUILD_VERSION=\"1\" liblustreclass_a_CFLAGS = $(LLCFLAGS) diff --git a/lustre/obdclass/llog_test.c b/lustre/obdclass/llog_test.c index 0e2018a..b8a21d0 100644 --- a/lustre/obdclass/llog_test.c +++ b/lustre/obdclass/llog_test.c @@ -656,7 +656,7 @@ static int llog_test_setup(struct obd_device *obd, obd_count len, void *buf) if (rc) RETURN(rc); - llog_test_rand = ll_insecure_random_int(); + llog_test_rand = ll_rand(); rc = llog_run_tests(obd); if (rc) diff --git a/lustre/obdclass/prng.c b/lustre/obdclass/prng.c new file mode 100644 index 0000000..31775f4 --- /dev/null +++ b/lustre/obdclass/prng.c @@ -0,0 +1,63 @@ +/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*- + * vim:expandtab:shiftwidth=8:tabstop=8: + * + * This file is part of the Lustre file system, http://www.lustre.org + * Lustre is a trademark of Cluster File Systems, Inc. + * + * 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 + ******************************************************************/ +#ifndef EXPORT_SYMTAB +# define EXPORT_SYMTAB +#endif + +#ifdef __KERNEL__ +#include +#else +#include +#endif + +/* +From: George Marsaglia +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; +unsigned int ll_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(ll_rand); + +void ll_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(ll_srand); diff --git a/lustre/obdecho/echo_client.c b/lustre/obdecho/echo_client.c index 9f57cf5..9eed26e 100644 --- a/lustre/obdecho/echo_client.c +++ b/lustre/obdecho/echo_client.c @@ -202,7 +202,7 @@ static int echo_create_object(struct obd_device *obd, int on_target, if (lsm->lsm_stripe_size == 0) lsm->lsm_stripe_size = PAGE_SIZE; - idx = ll_insecure_random_int(); + idx = ll_rand(); /* setup stripes: indices + default ids if required */ for (i = 0; i < lsm->lsm_stripe_count; i++) { -- 1.8.3.1