Merge tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
authorLinus Torvalds <torvalds@linux-foundation.org>
Wed, 4 Apr 2018 21:31:53 +0000 (14:31 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 4 Apr 2018 21:31:53 +0000 (14:31 -0700)
Pull /dev/random updates from Ted Ts'o:
 "A few random (cough, cough) cleanups for the /dev/random driver"

* tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/random:
  drivers/char/random.c: remove unused dont_count_entropy
  random: optimize add_interrupt_randomness
  random: always fill buffer in get_random_bytes_wait
  random: use a tighter cap in credit_entropy_bits_safe()

drivers/char/random.c
include/linux/random.h

index e5b3d3ba46604f7c0bb5612f3da867ea16884d20..e027e7fa1472b6612a50b597a9c3b13bf94939a8 100644 (file)
@@ -709,7 +709,8 @@ retry:
                }
 
                /* should we wake readers? */
-               if (entropy_bits >= random_read_wakeup_bits) {
+               if (entropy_bits >= random_read_wakeup_bits &&
+                   wq_has_sleeper(&random_read_wait)) {
                        wake_up_interruptible(&random_read_wait);
                        kill_fasync(&fasync, SIGIO, POLL_IN);
                }
@@ -732,7 +733,7 @@ retry:
 
 static int credit_entropy_bits_safe(struct entropy_store *r, int nbits)
 {
-       const int nbits_max = (int)(~0U >> (ENTROPY_SHIFT + 1));
+       const int nbits_max = r->poolinfo->poolwords * 32;
 
        if (nbits < 0)
                return -EINVAL;
@@ -963,7 +964,6 @@ static ssize_t extract_crng_user(void __user *buf, size_t nbytes)
 struct timer_rand_state {
        cycles_t last_time;
        long last_delta, last_delta2;
-       unsigned dont_count_entropy:1;
 };
 
 #define INIT_TIMER_RAND_STATE { INITIAL_JIFFIES, };
@@ -1029,35 +1029,33 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
         * We take into account the first, second and third-order deltas
         * in order to make our estimate.
         */
+       delta = sample.jiffies - state->last_time;
+       state->last_time = sample.jiffies;
+
+       delta2 = delta - state->last_delta;
+       state->last_delta = delta;
+
+       delta3 = delta2 - state->last_delta2;
+       state->last_delta2 = delta2;
+
+       if (delta < 0)
+               delta = -delta;
+       if (delta2 < 0)
+               delta2 = -delta2;
+       if (delta3 < 0)
+               delta3 = -delta3;
+       if (delta > delta2)
+               delta = delta2;
+       if (delta > delta3)
+               delta = delta3;
 
-       if (!state->dont_count_entropy) {
-               delta = sample.jiffies - state->last_time;
-               state->last_time = sample.jiffies;
-
-               delta2 = delta - state->last_delta;
-               state->last_delta = delta;
-
-               delta3 = delta2 - state->last_delta2;
-               state->last_delta2 = delta2;
-
-               if (delta < 0)
-                       delta = -delta;
-               if (delta2 < 0)
-                       delta2 = -delta2;
-               if (delta3 < 0)
-                       delta3 = -delta3;
-               if (delta > delta2)
-                       delta = delta2;
-               if (delta > delta3)
-                       delta = delta3;
+       /*
+        * delta is now minimum absolute delta.
+        * Round down by 1 bit on general principles,
+        * and limit entropy entimate to 12 bits.
+        */
+       credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
 
-               /*
-                * delta is now minimum absolute delta.
-                * Round down by 1 bit on general principles,
-                * and limit entropy entimate to 12 bits.
-                */
-               credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
-       }
        preempt_enable();
 }
 
index 4024f7d9c77dfe848de46ee0ea3afea57ad69ec1..2ddf13b4281e14fa0906648f4e0bec0546ac5f29 100644 (file)
@@ -85,10 +85,8 @@ static inline unsigned long get_random_canary(void)
 static inline int get_random_bytes_wait(void *buf, int nbytes)
 {
        int ret = wait_for_random_bytes();
-       if (unlikely(ret))
-               return ret;
        get_random_bytes(buf, nbytes);
-       return 0;
+       return ret;
 }
 
 #define declare_get_random_var_wait(var) \