From: Greg Price <price@MIT.EDU>
To: "Theodore Ts'o" <tytso@MIT.EDU>
Cc: linux-kernel@vger.kernel.org, Jiri Kosina <jkosina@suse.cz>
Subject: [PATCH 11/11] random: simplify accounting code
Date: Thu, 7 Nov 2013 18:59:37 -0500 [thread overview]
Message-ID: <20131107235937.GN16018@ringworld.MIT.EDU> (raw)
In-Reply-To: <cover.1383868558.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.
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Jiri Kosina <jkosina@suse.cz>
Signed-off-by: Greg Price <price@mit.edu>
---
drivers/char/random.c | 23 ++++++++---------------
1 file changed, 8 insertions(+), 15 deletions(-)
diff --git a/drivers/char/random.c b/drivers/char/random.c
index 87d3728..3f343ff 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -840,8 +840,6 @@ static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
static size_t account(struct entropy_store *r, size_t nbytes, int min,
int reserved)
{
- unsigned long flags;
- int wakeup_write = 0;
int entropy_count, orig;
BUG_ON(r->entropy_count > r->poolinfo->POOLBITS);
@@ -850,24 +848,19 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
retry:
entropy_count = orig = ACCESS_ONCE(r->entropy_count);
- if (entropy_count / 8 < min + reserved) {
+ /* If limited, never pull more than available */
+ if (r->limit)
+ nbytes = min_t(size_t, nbytes, entropy_count/8 - reserved);
+ if (nbytes < min)
nbytes = 0;
- } else {
- /* If limited, never pull more than available */
- if (r->limit)
- nbytes = min_t(size_t, nbytes, entropy_count/8 - reserved);
- entropy_count = max_t(int, entropy_count - nbytes*8, 0);
- if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
- goto retry;
-
- if (entropy_count < random_write_wakeup_thresh)
- wakeup_write = 1;
- }
+ entropy_count = max_t(int, entropy_count - nbytes*8, 0);
+ if (nbytes && cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
+ goto retry;
DEBUG_ENT("debiting %zu entropy credits from %s%s\n",
nbytes * 8, r->name, r->limit ? "" : " (unlimited)");
- if (wakeup_write) {
+ if (nbytes && entropy_count < random_write_wakeup_thresh) {
wake_up_interruptible(&random_write_wait);
kill_fasync(&fasync, SIGIO, POLL_OUT);
}
--
1.8.3.2
next prev parent reply other threads:[~2013-11-08 0:05 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-07 23:57 [PATCH 00/11] random: code cleanups Greg Price
2013-11-07 23:57 ` [PATCH 01/11] random: fix typos / spelling errors in comments Greg Price
2013-11-07 23:58 ` [PATCH 02/11] random: fix comment on proc_do_uuid Greg Price
2013-11-07 23:58 ` [PATCH 03/11] random: fix description of get_random_bytes Greg Price
2013-11-07 23:58 ` [PATCH 04/11] random: simplify loop in random_read Greg Price
2013-11-07 23:58 ` [PATCH 05/11] random: declare trickle_count unsigned Greg Price
2013-11-07 23:58 ` [PATCH 06/11] random: fix comment on "account" Greg Price
2013-11-07 23:58 ` [PATCH 07/11] random: simplify accounting code slightly Greg Price
2013-11-07 23:59 ` [PATCH 08/11] random: simplify accounting logic Greg Price
2013-11-07 23:59 ` [PATCH 09/11] random: forget lock in lockless accounting Greg Price
2013-11-07 23:59 ` [PATCH 10/11] random: pull 'min' check in accounting to inside lockless update Greg Price
2013-11-07 23:59 ` Greg Price [this message]
2013-11-12 4:24 ` [PATCH 00/11] random: code cleanups Theodore Ts'o
2013-11-12 22:40 ` Greg Price
2013-11-13 3:32 ` Theodore Ts'o
2013-11-13 4:02 ` H. Peter Anvin
2013-11-13 4:37 ` Greg Price
2013-11-13 4:51 ` H. Peter Anvin
2013-11-13 6:06 ` Greg Price
2013-11-13 4:23 ` Greg Price
2013-11-13 6:08 ` Theodore Ts'o
2013-11-13 6:28 ` Greg Price
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=20131107235937.GN16018@ringworld.MIT.EDU \
--to=price@mit.edu \
--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).