static ssize_t get_random_bytes_user(void __user *buf, size_t nbytes)
 {
-       ssize_t ret = 0;
-       size_t len;
+       size_t len, left, ret = 0;
        u32 chacha_state[CHACHA_STATE_WORDS];
        u8 output[CHACHA_BLOCK_SIZE];
 
         * the user directly.
         */
        if (nbytes <= CHACHA_KEY_SIZE) {
-               ret = copy_to_user(buf, &chacha_state[4], nbytes) ? -EFAULT : nbytes;
+               ret = nbytes - copy_to_user(buf, &chacha_state[4], nbytes);
                goto out_zero_chacha;
        }
 
-       do {
+       for (;;) {
                chacha20_block(chacha_state, output);
                if (unlikely(chacha_state[12] == 0))
                        ++chacha_state[13];
 
                len = min_t(size_t, nbytes, CHACHA_BLOCK_SIZE);
-               if (copy_to_user(buf, output, len)) {
-                       ret = -EFAULT;
+               left = copy_to_user(buf, output, len);
+               if (left) {
+                       ret += len - left;
                        break;
                }
 
-               nbytes -= len;
                buf += len;
                ret += len;
+               nbytes -= len;
+               if (!nbytes)
+                       break;
 
                BUILD_BUG_ON(PAGE_SIZE % CHACHA_BLOCK_SIZE != 0);
-               if (!(ret % PAGE_SIZE) && nbytes) {
+               if (ret % PAGE_SIZE == 0) {
                        if (signal_pending(current))
                                break;
                        cond_resched();
                }
-       } while (nbytes);
+       }
 
        memzero_explicit(output, sizeof(output));
 out_zero_chacha:
        memzero_explicit(chacha_state, sizeof(chacha_state));
-       return ret;
+       return ret ? ret : -EFAULT;
 }
 
 /*