From owner-svn-src-head@FreeBSD.ORG Thu Jan 26 03:03:09 2012 Return-Path: Delivered-To: svn-src-head@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3FF7D1065670; Thu, 26 Jan 2012 03:03:09 +0000 (UTC) (envelope-from ache@vniz.net) Received: from vniz.net (vniz.net [194.87.13.69]) by mx1.freebsd.org (Postfix) with ESMTP id BA4A68FC12; Thu, 26 Jan 2012 03:03:08 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by vniz.net (8.14.5/8.14.5) with ESMTP id q0Q336GT080556; Thu, 26 Jan 2012 07:03:07 +0400 (MSK) (envelope-from ache@vniz.net) Received: (from ache@localhost) by localhost (8.14.5/8.14.5/Submit) id q0Q33680080555; Thu, 26 Jan 2012 07:03:06 +0400 (MSK) (envelope-from ache) Date: Thu, 26 Jan 2012 07:03:05 +0400 From: Andrey Chernov To: Mark Murray , David Schultz Message-ID: <20120126030305.GA80493@vniz.net> Mail-Followup-To: Andrey Chernov , Mark Murray , David Schultz , src-committers@FreeBSD.ORG, svn-src-all@FreeBSD.ORG, svn-src-head@FreeBSD.ORG References: <20120118061943.GA80874@vniz.net> <20120120055823.GA28177@vniz.net> <20120120215649.GA40016@vniz.net> <20120122185545.GA11874@vniz.net> <20120125140237.GA74896@vniz.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Cc: svn-src-head@FreeBSD.ORG, svn-src-all@FreeBSD.ORG, src-committers@FreeBSD.ORG Subject: Re: svn commit: r230230 - head/sys/dev/random X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 Jan 2012 03:03:09 -0000 On Wed, Jan 25, 2012 at 07:16:41PM +0000, Mark Murray wrote: > I thought you were going to do this as a function? It would be > slightly neater to do it that way. > > Looks good! Are you sure this needs no locking or volatile > variables? Now with function, volatile, atomic and even enum: --- sys/libkern.h.old 2012-01-16 07:15:12.000000000 +0400 +++ sys/libkern.h 2012-01-26 06:01:51.000000000 +0400 @@ -72,6 +72,8 @@ static __inline quad_t qabs(quad_t a) { /* Prototypes for non-quad routines. */ struct malloc_type; +enum arc4_is { ARC4_ENTR_NONE, ARC4_ENTR_HAVE, ARC4_ENTR_DONE }; +void arc4rand_iniseed_state(enum arc4_is state); uint32_t arc4random(void); void arc4rand(void *ptr, u_int len, int reseed); int bcmp(const void *, const void *, size_t); --- dev/random/randomdev_soft.c.old 2011-03-02 01:42:19.000000000 +0300 +++ dev/random/randomdev_soft.c 2012-01-26 06:04:05.000000000 +0400 @@ -366,6 +366,7 @@ random_yarrow_unblock(void) selwakeuppri(&random_systat.rsel, PUSER); wakeup(&random_systat); } + arc4rand_iniseed_state(ARC4_ENTR_HAVE); } static int --- libkern/arc4random.c.old 2008-08-08 01:51:09.000000000 +0400 +++ libkern/arc4random.c 2012-01-26 06:01:07.000000000 +0400 @@ -24,6 +24,7 @@ __FBSDID("$FreeBSD: src/sys/libkern/arc4 #define ARC4_RESEED_SECONDS 300 #define ARC4_KEYBYTES (256 / 8) +static volatile enum arc4_is iniseed_state = ARC4_ENTR_NONE; static u_int8_t arc4_i, arc4_j; static int arc4_numruns = 0; static u_int8_t arc4_sbox[256]; @@ -74,6 +75,7 @@ arc4_randomstir (void) /* Reset for next reseed cycle. */ arc4_t_reseed = tv_now.tv_sec + ARC4_RESEED_SECONDS; arc4_numruns = 0; + arc4rand_iniseed_state(ARC4_ENTR_DONE); /* * Throw away the first N words of output, as suggested in the @@ -103,6 +105,24 @@ arc4_init(void) SYSINIT(arc4_init, SI_SUB_LOCK, SI_ORDER_ANY, arc4_init, NULL); +void +arc4rand_iniseed_state(enum arc4_is state) +{ + switch (state) { + case ARC4_ENTR_NONE: + atomic_store_rel_int(&iniseed_state, state); + break; + case ARC4_ENTR_HAVE: + if (iniseed_state == ARC4_ENTR_NONE) + atomic_store_rel_int(&iniseed_state, state); + break; + case ARC4_ENTR_DONE: + if (iniseed_state == ARC4_ENTR_HAVE) + atomic_store_rel_int(&iniseed_state, state); + break; + } +} + /* * Generate a random byte. */ @@ -130,7 +150,7 @@ arc4rand(void *ptr, u_int len, int resee struct timeval tv; getmicrouptime(&tv); - if (reseed || + if (reseed || iniseed_state == ARC4_ENTR_HAVE || (arc4_numruns > ARC4_RESEED_BYTES) || (tv.tv_sec > arc4_t_reseed)) arc4_randomstir(); -- http://ache.vniz.net/