From nobody Thu May 7 19:50:12 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3AB2BC433EF for ; Thu, 19 May 2022 23:31:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343627AbiESXbv (ORCPT ); Thu, 19 May 2022 19:31:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343572AbiESXbo (ORCPT ); Thu, 19 May 2022 19:31:44 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54A211E2 for ; Thu, 19 May 2022 16:31:43 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id c22so6344141pgu.2 for ; Thu, 19 May 2022 16:31:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+RsdF7f3Mu7C4iTNaFgCSBpFtuFSFFwFqZJwoYMIW08=; b=38WRsJCRa5OV3AzSmTbrHeWA6PJ0dfYCWIHx2YwHfFtJFNW5ZvWubIZYJ2QIuxDk2U SW/K2pe+MOHCBng/hrMTxqJviOAukrrvzSZqy7yy4oZ/IbPYUjvFs1IkSlTpB2RThWum A64yDtUcQJy/Pa+nS2woHbUXax0Z9ctiERQxIda027NDBjJ04Sg7YGT5DqSG796kE+hA tk9/CZ6I7stL449QGJqh+I4WwXm30znKGNRyBW874c9ogw7dLZnvLtVEXCynvTojlgdN X3ihNqx1dlPthU0zu2VUTdxg5xz0op7Nfb9vAAGhdL2E3SAXec8p+5zmL9to/CRiL0TS 6+4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+RsdF7f3Mu7C4iTNaFgCSBpFtuFSFFwFqZJwoYMIW08=; b=iGjPd0PWlZjjxx45CTfeI8sMXQUVrk8ygPSTdcKwy+jdnXyr+GSvViMSx+CN0MMvgK CmU60In8nLgidoWyv4gjmU+f/5bM+eVAzNSTzGxb4BwycB6BeFJ9Jaa7GbUU37o8LW0s ayVthp8LuYsK5l/b/dPDfB2DaGOusqYMCbUXUfeByuisPSmVYJKi/oAh/AVaI0vrG1Iq /vV129d79P+MPqNPxeXXBtnt5G4Dotm0bhHIt6aHGLcyNICHMDL0DZxx3RspC5EVeqg+ lWxLIzP2dK/QNsvmAeiHjKwyCqgd4J2gmIOiWt7HntkE7P4fPL2XmCt/EcKelXK/aN0U JOpg== X-Gm-Message-State: AOAM5330N3fZm27GCYilMVcFeJll6WAoLIjH/Go43L+xY29A560ssiYy zdgQq919c0SYyWWzS0p0BwvgLA== X-Google-Smtp-Source: ABdhPJyYP6ih5NyMtAPGAWdZgmyLn86jOwpdpSEhTsVI6Cc2M1xnKVrEIyviusL5JGr0twuSd+0qkg== X-Received: by 2002:a63:6886:0:b0:3c5:11f4:f055 with SMTP id d128-20020a636886000000b003c511f4f055mr6040567pgc.44.1653003102215; Thu, 19 May 2022 16:31:42 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id f16-20020a170902ab9000b0016191b843e2sm4331249plr.235.2022.05.19.16.31.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 16:31:41 -0700 (PDT) From: Jens Axboe To: tytso@mit.edu, Jason@zx2c4.com Cc: hch@lst.de, linux-kernel@vger.kernel.org, Jens Axboe Subject: [PATCH 1/2] random: convert to using fops->read_iter() Date: Thu, 19 May 2022 17:31:36 -0600 Message-Id: <20220519233137.20415-2-axboe@kernel.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220519233137.20415-1-axboe@kernel.dk> References: <20220519233137.20415-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is a pre-requisite to writing up splice() again for the random and urandom drivers. Signed-off-by: Jens Axboe --- drivers/char/random.c | 44 ++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 0958fa91a964..d000fe6fbb5a 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -397,11 +397,12 @@ void get_random_bytes(void *buf, size_t len) } EXPORT_SYMBOL(get_random_bytes); =20 -static ssize_t get_random_bytes_user(void __user *ubuf, size_t len) +static ssize_t get_random_bytes_user(struct iov_iter *to) { - size_t block_len, left, ret =3D 0; + size_t block_len, ret =3D 0; u32 chacha_state[CHACHA_STATE_WORDS]; u8 output[CHACHA_BLOCK_SIZE]; + size_t len =3D iov_iter_count(to); =20 if (!len) return 0; @@ -418,25 +419,23 @@ static ssize_t get_random_bytes_user(void __user *ubu= f, size_t len) * the user directly. */ if (len <=3D CHACHA_KEY_SIZE) { - ret =3D len - copy_to_user(ubuf, &chacha_state[4], len); + ret =3D copy_to_iter(&chacha_state[4], len, to); goto out_zero_chacha; } =20 for (;;) { + size_t copied; + chacha20_block(chacha_state, output); if (unlikely(chacha_state[12] =3D=3D 0)) ++chacha_state[13]; =20 block_len =3D min_t(size_t, len, CHACHA_BLOCK_SIZE); - left =3D copy_to_user(ubuf, output, block_len); - if (left) { - ret +=3D block_len - left; + copied =3D copy_to_iter(output, block_len, to); + ret +=3D copied; + if (copied !=3D block_len) break; - } - - ubuf +=3D block_len; - ret +=3D block_len; - len -=3D block_len; + len -=3D copied; if (!len) break; =20 @@ -1248,6 +1247,9 @@ static void __cold try_to_generate_entropy(void) =20 SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int,= flags) { + struct iovec iov =3D { .iov_base =3D ubuf }; + struct iov_iter iter; + if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)) return -EINVAL; =20 @@ -1270,7 +1272,9 @@ SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_= t, len, unsigned int, flags if (unlikely(ret)) return ret; } - return get_random_bytes_user(ubuf, len); + iov.iov_len =3D len; + iov_iter_init(&iter, READ, &iov, 1, len); + return get_random_bytes_user(&iter); } =20 static __poll_t random_poll(struct file *file, poll_table *wait) @@ -1314,8 +1318,7 @@ static ssize_t random_write(struct file *file, const = char __user *ubuf, return (ssize_t)len; } =20 -static ssize_t urandom_read(struct file *file, char __user *ubuf, - size_t len, loff_t *ppos) +static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *to) { static int maxwarn =3D 10; =20 @@ -1332,22 +1335,21 @@ static ssize_t urandom_read(struct file *file, char= __user *ubuf, else if (ratelimit_disable || __ratelimit(&urandom_warning)) { --maxwarn; pr_notice("%s: uninitialized urandom read (%zd bytes read)\n", - current->comm, len); + current->comm, iov_iter_count(to)); } } =20 - return get_random_bytes_user(ubuf, len); + return get_random_bytes_user(to); } =20 -static ssize_t random_read(struct file *file, char __user *ubuf, - size_t len, loff_t *ppos) +static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *to) { int ret; =20 ret =3D wait_for_random_bytes(); if (ret !=3D 0) return ret; - return get_random_bytes_user(ubuf, len); + return get_random_bytes_user(to); } =20 static long random_ioctl(struct file *f, unsigned int cmd, unsigned long a= rg) @@ -1409,7 +1411,7 @@ static int random_fasync(int fd, struct file *filp, i= nt on) } =20 const struct file_operations random_fops =3D { - .read =3D random_read, + .read_iter =3D random_read_iter, .write =3D random_write, .poll =3D random_poll, .unlocked_ioctl =3D random_ioctl, @@ -1419,7 +1421,7 @@ const struct file_operations random_fops =3D { }; =20 const struct file_operations urandom_fops =3D { - .read =3D urandom_read, + .read_iter =3D urandom_read_iter, .write =3D random_write, .unlocked_ioctl =3D random_ioctl, .compat_ioctl =3D compat_ptr_ioctl, --=20 2.35.1 From nobody Thu May 7 19:50:12 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B5E6C433EF for ; Thu, 19 May 2022 23:32:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239481AbiESXb6 (ORCPT ); Thu, 19 May 2022 19:31:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343612AbiESXbq (ORCPT ); Thu, 19 May 2022 19:31:46 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E481BC5C for ; Thu, 19 May 2022 16:31:43 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id c22so6344165pgu.2 for ; Thu, 19 May 2022 16:31:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wDZJpQEck2IEnCwvWe0nT2u43d++wRfT3gBaO1c2DlI=; b=e/rCiNwyO+tDaNdaLHEHZ4+HbKYmU0DRLFcvh2OI8F0y49U9SXKLToWn4BllXgf5wS qPKqOHf5PUXNB5NVozFPy/LqFRJCGkhUHRosKk+LerGluLPqxTRxhkZ2DOUbSjs85e3j uIdyyN+3AiSKI/xjzAbn16IbVmqZe5ZDuynY3uyspICNXceH8xH5IdxaTc4RIoOzgUt9 +gy+F2W+dT4nhjLbg9FBEe08aLRb5rgAP7dxfNm1cUWBysIHGbW1XlOv41M3QFd9GxD/ JtcB8lzj3RxldCHXhXIKBtLxtilpKdbyvcX3rljFuMh7Ea4vth/wQ4L9U7tYkOzcYWq7 ll7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wDZJpQEck2IEnCwvWe0nT2u43d++wRfT3gBaO1c2DlI=; b=OerGltAaeNeEQ7W4BM0R8tE48ab5cs084zORYh6+ve8AnjD6nttcB1k5tX0FmdvHBC 4EyFYOeuhRpOYsizNyHMYWq/WPCE0u10dzWuIHvnplsfpHh+LD9+t6EuywjufYyCTT32 iqfTi8O+gVkomJq8C73eg1TQ5xlGm80Kj53zqKNeMH54PE8a3Qw12kfafUoFFgn9uKke ElVHemUZe0r+mb5YUjBZDooJztWg1n9Jq7A6sFiP4tEGLvfjDitMAEI3IRMdyWdLMKMh Kq71YWmIiE5jIINoIzX5b/OsJilspqd1l/n6o59Rtvzr9P8Tfa7uI+44sLvVxJOt58AP m2Iw== X-Gm-Message-State: AOAM530pGF0O9YoT9qenhiBIaJ0jYfi6mfLBsBn+bBJm41bK0Iz8lxYj KCbIZsX1qCZz2ZwDfxh1dDIOGA== X-Google-Smtp-Source: ABdhPJyfwFJjO+PKaBVKYIj6Mk7nJVzmva2Bi1w3gN26hsXBgXQSTe+RbM7aeW4f1dPobqHbuHzuLw== X-Received: by 2002:a65:4d48:0:b0:3f3:936a:7c33 with SMTP id j8-20020a654d48000000b003f3936a7c33mr6025235pgt.217.1653003103377; Thu, 19 May 2022 16:31:43 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id f16-20020a170902ab9000b0016191b843e2sm4331249plr.235.2022.05.19.16.31.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 May 2022 16:31:42 -0700 (PDT) From: Jens Axboe To: tytso@mit.edu, Jason@zx2c4.com Cc: hch@lst.de, linux-kernel@vger.kernel.org, Jens Axboe Subject: [PATCH 2/2] random: wire up fops->splice_read_iter() Date: Thu, 19 May 2022 17:31:37 -0600 Message-Id: <20220519233137.20415-3-axboe@kernel.dk> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220519233137.20415-1-axboe@kernel.dk> References: <20220519233137.20415-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Now that random/urandom is using read_iter, we can wire it up to using the generic splice read handler. Fixes: 36e2c7421f02 ("fs: don't allow splice read/write without explicit op= s") Signed-off-by: Jens Axboe --- drivers/char/random.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/char/random.c b/drivers/char/random.c index d000fe6fbb5a..41ca5966aa4f 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1418,6 +1418,7 @@ const struct file_operations random_fops =3D { .compat_ioctl =3D compat_ptr_ioctl, .fasync =3D random_fasync, .llseek =3D noop_llseek, + .splice_read =3D generic_file_splice_read, }; =20 const struct file_operations urandom_fops =3D { @@ -1427,6 +1428,7 @@ const struct file_operations urandom_fops =3D { .compat_ioctl =3D compat_ptr_ioctl, .fasync =3D random_fasync, .llseek =3D noop_llseek, + .splice_read =3D generic_file_splice_read, }; =20 =20 --=20 2.35.1