From owner-svn-src-all@freebsd.org Fri Mar 16 18:50:27 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A6303B9; Fri, 16 Mar 2018 18:50:27 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 5A2596EB99; Fri, 16 Mar 2018 18:50:27 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3AF2A278D9; Fri, 16 Mar 2018 18:50:27 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w2GIoRak072912; Fri, 16 Mar 2018 18:50:27 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w2GIoRoE072911; Fri, 16 Mar 2018 18:50:27 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201803161850.w2GIoRoE072911@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Fri, 16 Mar 2018 18:50:27 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r331070 - head/sys/dev/random X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: head/sys/dev/random X-SVN-Commit-Revision: 331070 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Mar 2018 18:50:27 -0000 Author: cem Date: Fri Mar 16 18:50:26 2018 New Revision: 331070 URL: https://svnweb.freebsd.org/changeset/base/331070 Log: random(4): Poll for signals during large reads Occasionally poll for signals during large reads of the /dev/u?random devices. This allows cancellation via SIGINT of accidental invocations of very large reads. (A 2GB /dev/random read, which takes about 10 seconds on my 2017 AMD Zen processor, can be aborted.) I believe this behavior was intended since 2014 (r273997), just not fully implemented. This is motivated by a potential getrandom(2) interface that may not explicitly forbid extremely large reads on 64-bit platforms -- even larger than the 2GB limit imposed on devfs I/O by default. Such reads, if they are to be allowed, should be cancellable by the user or administrator. Reviewed by: delphij Approved by: secteam (delphij) Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D14684 Modified: head/sys/dev/random/randomdev.c Modified: head/sys/dev/random/randomdev.c ============================================================================== --- head/sys/dev/random/randomdev.c Fri Mar 16 18:16:31 2018 (r331069) +++ head/sys/dev/random/randomdev.c Fri Mar 16 18:50:26 2018 (r331070) @@ -130,7 +130,13 @@ READ_RANDOM_UIO(struct uio *uio, bool nonblock) uint8_t *random_buf; int error, spamcount; ssize_t read_len, total_read, c; + /* 16 MiB takes about 0.08 s CPU time on my 2017 AMD Zen CPU */ +#define SIGCHK_PERIOD (16 * 1024 * 1024) + const size_t sigchk_period = SIGCHK_PERIOD; + CTASSERT(SIGCHK_PERIOD % PAGE_SIZE == 0); +#undef SIGCHK_PERIOD + random_buf = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK); p_random_alg_context->ra_pre_read(); error = 0; @@ -167,11 +173,22 @@ READ_RANDOM_UIO(struct uio *uio, bool nonblock) read_len = MIN(read_len, PAGE_SIZE); p_random_alg_context->ra_read(random_buf, read_len); c = MIN(uio->uio_resid, read_len); + /* + * uiomove() may yield the CPU before each 'c' bytes + * (up to PAGE_SIZE) are copied out. + */ error = uiomove(random_buf, c, uio); total_read += c; + /* + * Poll for signals every few MBs to avoid very long + * uninterruptible syscalls. + */ + if (error == 0 && uio->uio_resid != 0 && + total_read % sigchk_period == 0) + error = tsleep_sbt(&random_alg_context, PCATCH, + "randrd", SBT_1NS, 0, C_HARDCLOCK); } - if (total_read != uio->uio_resid && (error == ERESTART || error == EINTR)) - /* Return partial read, not error. */ + if (error == ERESTART || error == EINTR) error = 0; } free(random_buf, M_ENTROPY);