From: Greg Price <price@MIT.EDU>
To: "Theodore Ts'o" <tytso@MIT.EDU>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 4/9] random: simplify loop in random_read
Date: Wed, 13 Nov 2013 03:07:40 -0500 [thread overview]
Message-ID: <20131113080740.GR16018@ringworld.MIT.EDU> (raw)
In-Reply-To: <cover.1384329436.git.price@mit.edu>
The loop condition never changes until just before a break, so we
might as well write it as a constant. Also since v2.6.33-rc7~40^2~2
("random: drop weird m_time/a_time manipulation") we don't do
anything after the loop finishes, so the 'break's might as well
return directly. Some other simplifications.
The behavior should be identical except that I've changed a debug
message.
Cc: "Theodore Ts'o" <tytso@mit.edu>
Signed-off-by: Greg Price <price@mit.edu>
---
drivers/char/random.c | 57 ++++++++++++++++-----------------------------------
1 file changed, 18 insertions(+), 39 deletions(-)
diff --git a/drivers/char/random.c b/drivers/char/random.c
index 6d3627d..cf1357b 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1289,53 +1289,32 @@ void rand_initialize_disk(struct gendisk *disk)
static ssize_t
random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
- ssize_t n, retval = 0, count = 0;
+ ssize_t n;
if (nbytes == 0)
return 0;
- while (nbytes > 0) {
- n = nbytes;
- if (n > SEC_XFER_SIZE)
- n = SEC_XFER_SIZE;
-
- n = extract_entropy_user(&blocking_pool, buf, n);
-
- if (n < 0) {
- retval = n;
- break;
- }
-
+ nbytes = min_t(size_t, nbytes, SEC_XFER_SIZE);
+ while (1) {
+ n = extract_entropy_user(&blocking_pool, buf, nbytes);
+ if (n < 0)
+ return n;
trace_random_read(n*8, (nbytes-n)*8,
ENTROPY_BITS(&blocking_pool),
ENTROPY_BITS(&input_pool));
-
- if (n == 0) {
- if (file->f_flags & O_NONBLOCK) {
- retval = -EAGAIN;
- break;
- }
-
- wait_event_interruptible(random_read_wait,
- ENTROPY_BITS(&input_pool) >=
- random_read_wakeup_thresh);
-
- if (signal_pending(current)) {
- retval = -ERESTARTSYS;
- break;
- }
-
- continue;
- }
-
- count += n;
- buf += n;
- nbytes -= n;
- break; /* This break makes the device work */
- /* like a named pipe */
+ if (n > 0)
+ return n;
+ /* Pool is (near) empty. Maybe wait and retry. */
+
+ if (file->f_flags & O_NONBLOCK)
+ return -EAGAIN;
+
+ wait_event_interruptible(random_read_wait,
+ ENTROPY_BITS(&input_pool) >=
+ random_read_wakeup_thresh);
+ if (signal_pending(current))
+ return -ERESTARTSYS;
}
-
- return (count ? count : retval);
}
static ssize_t
--
1.8.3.2
next prev parent reply other threads:[~2013-11-13 8:08 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 ` Greg Price [this message]
2013-11-29 20:09 ` [PATCH 4/9] random: simplify loop in random_read 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 ` [PATCH 9/9] random: simplify accounting code Greg Price
2013-12-06 1:13 ` 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=20131113080740.GR16018@ringworld.MIT.EDU \
--to=price@mit.edu \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.