From owner-svn-src-projects@FreeBSD.ORG Sat Apr 26 13:55:04 2014 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 59C0F37F; Sat, 26 Apr 2014 13:55:04 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 3AA8E1DB4; Sat, 26 Apr 2014 13:55:04 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s3QDt4gj062753; Sat, 26 Apr 2014 13:55:04 GMT (envelope-from markm@svn.freebsd.org) Received: (from markm@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s3QDt4ja062752; Sat, 26 Apr 2014 13:55:04 GMT (envelope-from markm@svn.freebsd.org) Message-Id: <201404261355.s3QDt4ja062752@svn.freebsd.org> From: Mark Murray Date: Sat, 26 Apr 2014 13:55:04 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r264971 - projects/random_number_generator/sys/dev/random X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 26 Apr 2014 13:55:04 -0000 Author: markm Date: Sat Apr 26 13:55:03 2014 New Revision: 264971 URL: http://svnweb.freebsd.org/changeset/base/264971 Log: Improve lockless ring buffer. Modified: projects/random_number_generator/sys/dev/random/random_harvestq.c Modified: projects/random_number_generator/sys/dev/random/random_harvestq.c ============================================================================== --- projects/random_number_generator/sys/dev/random/random_harvestq.c Sat Apr 26 13:53:04 2014 (r264970) +++ projects/random_number_generator/sys/dev/random/random_harvestq.c Sat Apr 26 13:55:03 2014 (r264971) @@ -117,7 +117,7 @@ static struct proc *random_kthread_proc; static void random_kthread(void *arg __unused) { - u_int maxloop; + u_int maxloop, ring_out; /* * Process until told to stop. @@ -131,12 +131,16 @@ random_kthread(void *arg __unused) /* Deal with events, if any. Restrict the number we do in one go. */ maxloop = RANDOM_FIFO_MAX; while (entropyfifo.ring_out != entropyfifo.ring_in) { - /* Modifying ring_out here ONLY. */ - entropyfifo.ring_out = (entropyfifo.ring_out + 1)%RANDOM_FIFO_MAX; - harvest_process_event(entropyfifo.ring + entropyfifo.ring_out); + + ring_out = (entropyfifo.ring_out + 1)%RANDOM_FIFO_MAX; + harvest_process_event(entropyfifo.ring + ring_out); + /* Modifying ring_out here ONLY. Sufficient for atomicity? */ + entropyfifo.ring_out = ring_out; + /* The ring may be filled quickly so don't loop forever. */ if (--maxloop) break; + } /* @@ -153,7 +157,7 @@ random_kthread(void *arg __unused) if (random_kthread_control == 1) random_kthread_control = 0; - /* Work done, so don't belabour the issue */ + /* Some work is done, so give the rest of the OS a chance. */ tsleep_sbt(&random_kthread_control, 0, "-", SBT_1S/10, 0, C_PREL(1)); } @@ -356,25 +360,23 @@ random_harvestq_internal(const void *ent return; /* Lock ring_in against multi-thread contention */ - /* XXX: Fix? Go critical instead of locking? */ mtx_lock_spin(&harvest_mtx); ring_in = (entropyfifo.ring_in + 1)%RANDOM_FIFO_MAX; - if (ring_in == entropyfifo.ring_out) { - /* The ring is full; unlock and leave. */ - mtx_unlock_spin(&harvest_mtx); - return; + if (ring_in != entropyfifo.ring_out) { + /* The ring is not full */ + event = entropyfifo.ring + ring_in; + + /* Stash the harvested stuff in the *event buffer */ + count = MIN(count, HARVESTSIZE); + event->he_somecounter = get_cyclecount(); + event->he_size = count; + event->he_bits = bits; + event->he_source = origin; + event->he_destination = harvest_destination[origin]++; + memcpy(event->he_entropy, entropy, count); + memset(event->he_entropy + count, 0, HARVESTSIZE - count); + + entropyfifo.ring_in = ring_in; } - entropyfifo.ring_in = ring_in; - event = entropyfifo.ring + ring_in; mtx_unlock_spin(&harvest_mtx); - - /* Stash the harvested stuff in the *event buffer */ - count = MIN(count, HARVESTSIZE); - event->he_somecounter = get_cyclecount(); - event->he_size = count; - event->he_bits = bits; - event->he_source = origin; - event->he_destination = harvest_destination[origin]++; - memcpy(event->he_entropy, entropy, count); - memset(event->he_entropy + count, 0, HARVESTSIZE - count); }