From nobody Fri Jun 17 19:31:55 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id D8D58853256; Fri, 17 Jun 2022 19:31:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4LPq181Km0z3KCY; Fri, 17 Jun 2022 19:31:55 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655494316; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LzqtyVof7ZII13ggPBfNICCLx/52WcZF40xC7xdzeMw=; b=WMW3d3zdMr4/TNmdZSIagaTR3snNyvlyPrgZ4Qm63/Qdlupi7UjVdeRbQprHe8qPpphK+K QAeGWoDxBeFBddMQFHggszEeNPs4efMHc/MhjSIgMHKvIcHsIXnULHrBvOnshnrjwMucNV +Vwm5EIhXTs7xvSyULOgAeJ5FvtAB2AthG6HBwDRGxzVE2lt6EdfedrjytIdb1rLqRv4Na OpHbhg0LU0cb9AhZk6dTAAY3o9MyONEz6xaU5TRZPNYSi02Ja8NLGVxnVhKsyQRkZmex3Q eVPxPBcQ3Jsnk1K958biNFOxO7ThWo1M9Qv5MR5jn+VZJCM8wf52qQj7WxCuJA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D2FBF25565; Fri, 17 Jun 2022 19:31:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 25HJVt8F007787; Fri, 17 Jun 2022 19:31:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 25HJVtWf007786; Fri, 17 Jun 2022 19:31:55 GMT (envelope-from git) Date: Fri, 17 Jun 2022 19:31:55 GMT Message-Id: <202206171931.25HJVtWf007786@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dmitry Chagin Subject: git: 3ae9b75f6bad - stable/13 - Split kern_poll() on two counterparts. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dchagin X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 3ae9b75f6bad2456e4b2cd4a5d6734d4737d5af4 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655494316; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LzqtyVof7ZII13ggPBfNICCLx/52WcZF40xC7xdzeMw=; b=qZfT201fg4uuIB3UnA9cMAfgtLBt+jxjJGTica9eqUCuFJBbEo+gJQq+VGmboVxg+T815s RgxDUXA62aY+k0Rt66HINbQK1fFjMJXZKQmbZTKUd/heg3swP6Pp0b6V47DwPJ+4CxU10g VTMWkPpopwiu/5Q2FP1f3EUIiIztrFeymQSCStE1v2J5iIfN659apUeLQzWz79gIlWZM/m j5PYn/hf5AiJB8XR6ISTF+oSO0la3AF7ryIBNSvT6Q4Gy+YQZval520ZTVFY26GRDB3ohi 2yZT9j2Mpn+zCNVNCoNOFgUU1UTavxVWJU9sG4+K1QwloGx0t86Zcv4gp7KeeA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1655494316; a=rsa-sha256; cv=none; b=lE6pt+mqIhKtBXBECwe7MLoo9I1wRidh1zLxfOObs+TWq/c5LVrMA3cTB7tYqlJ6ea4Cg1 reLWqDmSlGOlqI3Vr3zoxaQehakJFZclpNmStQik8MVgg1tFFqydyn0GA4se7TBEusR49W nLt2Uy44YlRZJ9kDoSBxGf9RwSIp5gzr6VJcgWKbi54TE/D2LW420uI09nQAPhnenS28fb GhRfOkMs9C+0Kx1/Y0uEFnJV4URFGMa766wGj4i7w3f2aHIAaRCL90NKQQfaHBS+3IB46D gcvGC4AO68jtE8oB1b2BlfF65iYni7OIgu7FTPspyclEv1NdWGsb/wtNXzghnw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=3ae9b75f6bad2456e4b2cd4a5d6734d4737d5af4 commit 3ae9b75f6bad2456e4b2cd4a5d6734d4737d5af4 Author: Dmitry Chagin AuthorDate: 2021-06-10 12:11:25 +0000 Commit: Dmitry Chagin CommitDate: 2022-06-17 19:30:17 +0000 Split kern_poll() on two counterparts. The kern_poll_kfds() operates on clear kernel data, kfds points to an array in the kernel, while kern_poll() operates on user supplied pollfd. Move nfds check to kern_poll_maxfds(). No functional changes, it's for future use in the Linux emulation layer. Reviewd by: kib Differential Revision: https://reviews.freebsd.org/D30690 MFC after: 2 weeks (cherry picked from commit e884512ad143952f0dbacad631487ce28363fd08) --- sys/kern/sys_generic.c | 83 +++++++++++++++++++++++++++++--------------------- sys/sys/syscallsubr.h | 3 ++ 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c index 0fcb93545e86..541c9f910df1 100644 --- a/sys/kern/sys_generic.c +++ b/sys/kern/sys_generic.c @@ -1417,12 +1417,13 @@ sys_poll(struct thread *td, struct poll_args *uap) return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL)); } +/* + * kfds points to an array in the kernel. + */ int -kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds, +kern_poll_kfds(struct thread *td, struct pollfd *kfds, u_int nfds, struct timespec *tsp, sigset_t *uset) { - struct pollfd *kfds; - struct pollfd stackfds[32]; sbintime_t sbt, precision, tmp; time_t over; struct timespec ts; @@ -1453,28 +1454,11 @@ kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds, } else sbt = -1; - /* - * This is kinda bogus. We have fd limits, but that is not - * really related to the size of the pollfd array. Make sure - * we let the process use at least FD_SETSIZE entries and at - * least enough for the system-wide limits. We want to be reasonably - * safe, but not overly restrictive. - */ - if (nfds > maxfilesperproc && nfds > FD_SETSIZE) - return (EINVAL); - if (nfds > nitems(stackfds)) - kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK); - else - kfds = stackfds; - error = copyin(ufds, kfds, nfds * sizeof(*kfds)); - if (error) - goto done; - if (uset != NULL) { error = kern_sigprocmask(td, SIG_SETMASK, uset, &td->td_oldsigmask, 0); if (error) - goto done; + return (error); td->td_pflags |= TDP_OLDMASK; /* * Make sure that ast() is called on return to @@ -1501,20 +1485,11 @@ kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds, } seltdclear(td); -done: /* poll is not restarted after signals... */ if (error == ERESTART) error = EINTR; if (error == EWOULDBLOCK) error = 0; - if (error == 0) { - error = pollout(td, kfds, ufds, nfds); - if (error) - goto out; - } -out: - if (nfds > nitems(stackfds)) - free(kfds, M_TEMP); return (error); } @@ -1539,12 +1514,52 @@ sys_ppoll(struct thread *td, struct ppoll_args *uap) ssp = &set; } else ssp = NULL; + return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); +} + +/* + * ufds points to an array in user space. + */ +int +kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds, + struct timespec *tsp, sigset_t *set) +{ + struct pollfd *kfds; + struct pollfd stackfds[32]; + int error; + + if (kern_poll_maxfds(nfds)) + return (EINVAL); + if (nfds > nitems(stackfds)) + kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK); + else + kfds = stackfds; + error = copyin(ufds, kfds, nfds * sizeof(*kfds)); + if (error != 0) + goto out; + + error = kern_poll_kfds(td, kfds, nfds, tsp, set); + if (error == 0) + error = pollout(td, kfds, ufds, nfds); + +out: + if (nfds > nitems(stackfds)) + free(kfds, M_TEMP); + return (error); +} + +bool +kern_poll_maxfds(u_int nfds) +{ + /* - * fds is still a pointer to user space. kern_poll() will - * take care of copyin that array to the kernel space. + * This is kinda bogus. We have fd limits, but that is not + * really related to the size of the pollfd array. Make sure + * we let the process use at least FD_SETSIZE entries and at + * least enough for the system-wide limits. We want to be reasonably + * safe, but not overly restrictive. */ - - return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); + return (nfds > maxfilesperproc && nfds > FD_SETSIZE); } static int diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h index b155de1e08bf..09eae475cb2c 100644 --- a/sys/sys/syscallsubr.h +++ b/sys/sys/syscallsubr.h @@ -229,6 +229,9 @@ int kern_pipe(struct thread *td, int fildes[2], int flags, struct filecaps *fcaps1, struct filecaps *fcaps2); int kern_poll(struct thread *td, struct pollfd *fds, u_int nfds, struct timespec *tsp, sigset_t *uset); +int kern_poll_kfds(struct thread *td, struct pollfd *fds, u_int nfds, + struct timespec *tsp, sigset_t *uset); +bool kern_poll_maxfds(u_int nfds); int kern_posix_error(struct thread *td, int error); int kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len, int advice);