linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Price <price@MIT.EDU>
To: "Theodore Ts'o" <tytso@MIT.EDU>
Cc: linux-kernel@vger.kernel.org, Jiri Kosina <jkosina@suse.cz>,
	"H. Peter Anvin" <hpa@zytor.com>
Subject: [PATCH 9/9] random: simplify accounting code
Date: Wed, 13 Nov 2013 03:08:41 -0500	[thread overview]
Message-ID: <20131113080841.GW16018@ringworld.MIT.EDU> (raw)
In-Reply-To: <cover.1384329436.git.price@mit.edu>

With this we handle "reserved" in just one place.  As a bonus the
code becomes less nested, and the "wakeup_write" flag variable
becomes unnecessary.  The variable "flags" was already unused.

This code behaves identically to the previous version except in
two pathological cases that don't occur.  If the argument "nbytes"
is already less than "min", then we didn't previously enforce
"min".  If r->limit is false while "reserved" is nonzero, then we
previously applied "reserved" in checking whether we had enough
bits, even though we don't apply it to actually limit how many we
take.  The callers of account() never exercise either of these cases.

Before the previous commit, it was possible for "nbytes" to be less
than "min" if userspace chose a pathological configuration, but no
longer.

Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Jiri Kosina <jkosina@suse.cz>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Signed-off-by: Greg Price <price@mit.edu>
---
 drivers/char/random.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 865c8db..87b41b0 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -968,8 +968,6 @@ static void push_to_pool(struct work_struct *work)
 static size_t account(struct entropy_store *r, size_t nbytes, int min,
 		      int reserved)
 {
-	unsigned long flags;
-	int wakeup_write = 0;
 	int have_bytes;
 	int entropy_count, orig;
 	size_t ibytes;
@@ -981,24 +979,19 @@ retry:
 	entropy_count = orig = ACCESS_ONCE(r->entropy_count);
 	have_bytes = entropy_count >> (ENTROPY_SHIFT + 3);
 	ibytes = nbytes;
-	if (have_bytes < min + reserved) {
+	/* If limited, never pull more than available */
+	if (r->limit)
+		ibytes = min_t(size_t, ibytes, have_bytes - reserved);
+	if (ibytes < min)
 		ibytes = 0;
-	} else {
-		/* If limited, never pull more than available */
-		if (r->limit)
-			ibytes = min_t(size_t, ibytes, have_bytes - reserved);
-		entropy_count = max_t(int, 0,
-			    entropy_count - (ibytes << (ENTROPY_SHIFT + 3)));
-		if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
-			goto retry;
-
-		if ((r->entropy_count >> ENTROPY_SHIFT)
-		    < random_write_wakeup_thresh)
-			wakeup_write = 1;
-	}
+	entropy_count = max_t(int, 0,
+			      entropy_count - (ibytes << (ENTROPY_SHIFT + 3)));
+	if (ibytes && cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
+		goto retry;
 
 	trace_debit_entropy(r->name, 8 * ibytes);
-	if (wakeup_write) {
+	if (ibytes &&
+	    (r->entropy_count >> ENTROPY_SHIFT) < random_write_wakeup_thresh) {
 		wake_up_interruptible(&random_write_wait);
 		kill_fasync(&fasync, SIGIO, POLL_OUT);
 	}
-- 
1.8.3.2

  parent reply	other threads:[~2013-11-13  8:09 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-13  8:07 [PATCH v2 0/9] random: code cleanups Greg Price
2013-11-13  8:07 ` [PATCH 1/9] random: fix typos / spelling errors in comments Greg Price
2013-11-29 19:56   ` Theodore Ts'o
2013-11-13  8:07 ` [PATCH 2/9] random: fix comment on proc_do_uuid Greg Price
2013-11-29 19:59   ` Theodore Ts'o
2013-11-13  8:07 ` [PATCH 3/9] random: fix description of get_random_bytes Greg Price
2013-11-29 20:00   ` Theodore Ts'o
2013-11-13  8:07 ` [PATCH 4/9] random: simplify loop in random_read Greg Price
2013-11-29 20:09   ` Theodore Ts'o
2013-11-30  5:30     ` Greg Price
2013-11-13  8:08 ` [PATCH 5/9] random: fix comment on "account" Greg Price
2013-11-29 20:53   ` Theodore Ts'o
2013-11-13  8:08 ` [PATCH 6/9] random: simplify accounting logic Greg Price
2013-11-30  1:04   ` Theodore Ts'o
2013-11-13  8:08 ` [PATCH 7/9] random: forget lock in lockless accounting Greg Price
2013-11-30  1:10   ` Theodore Ts'o
2013-11-13  8:08 ` [PATCH 8/9] random: tighten bound on random_read_wakeup_thresh Greg Price
2013-12-06  0:31   ` Theodore Ts'o
2013-11-13  8:08 ` Greg Price [this message]
2013-12-06  1:13   ` [PATCH 9/9] random: simplify accounting code Theodore Ts'o
2013-12-06 18:25     ` Greg Price
2013-12-07 14:48       ` Theodore Ts'o

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20131113080841.GW16018@ringworld.MIT.EDU \
    --to=price@mit.edu \
    --cc=hpa@zytor.com \
    --cc=jkosina@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tytso@MIT.EDU \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).