From owner-svn-src-head@freebsd.org Wed Jul 15 10:24:40 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id ED1283621E3; Wed, 15 Jul 2020 10:24:40 +0000 (UTC) (envelope-from mjg@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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4B6D5h4PD9z48d0; Wed, 15 Jul 2020 10:24:40 +0000 (UTC) (envelope-from mjg@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 4F2E015D0F; Wed, 15 Jul 2020 10:24:40 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 06FAOeK4007945; Wed, 15 Jul 2020 10:24:40 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 06FAOePk007944; Wed, 15 Jul 2020 10:24:40 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202007151024.06FAOePk007944@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Wed, 15 Jul 2020 10:24:40 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r363215 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 363215 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jul 2020 10:24:41 -0000 Author: mjg Date: Wed Jul 15 10:24:39 2020 New Revision: 363215 URL: https://svnweb.freebsd.org/changeset/base/363215 Log: poll: factor fd lookup out of scan and rescan Modified: head/sys/kern/sys_generic.c Modified: head/sys/kern/sys_generic.c ============================================================================== --- head/sys/kern/sys_generic.c Wed Jul 15 10:24:04 2020 (r363214) +++ head/sys/kern/sys_generic.c Wed Jul 15 10:24:39 2020 (r363215) @@ -1476,7 +1476,50 @@ sys_ppoll(struct thread *td, struct ppoll_args *uap) return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); } +#ifdef CAPABILITIES static int +poll_fget(struct filedesc *fdp, int fd, struct file **fpp) +{ + const struct filedescent *fde; + const struct fdescenttbl *fdt; + const cap_rights_t *haverights; + struct file *fp; + int error; + + if (__predict_false(fd >= fdp->fd_nfiles)) + return (EBADF); + + fdt = fdp->fd_files; + fde = &fdt->fdt_ofiles[fd]; + fp = fde->fde_file; + if (__predict_false(fp == NULL)) + return (EBADF); + haverights = cap_rights_fde_inline(fde); + error = cap_check_inline(haverights, &cap_event_rights); + if (__predict_false(error != 0)) + return (EBADF); + *fpp = fp; + return (0); +} +#else +static int +poll_fget(struct filedesc *fdp, int fd, struct file **fpp) +{ + struct file *fp; + + if (__predict_false(fd >= fdp->fd_nfiles)) + return (EBADF); + + fp = fdp->fd_ofiles[fd].fde_file; + if (__predict_false(fp == NULL)) + return (EBADF); + + *fpp = fp; + return (0); +} +#endif + +static int pollrescan(struct thread *td) { struct seltd *stp; @@ -1499,19 +1542,11 @@ pollrescan(struct thread *td) /* If the selinfo wasn't cleared the event didn't fire. */ if (si != NULL) continue; - fp = fdp->fd_ofiles[fd->fd].fde_file; -#ifdef CAPABILITIES - if (fp == NULL || - cap_check(cap_rights(fdp, fd->fd), &cap_event_rights) != 0) -#else - if (fp == NULL) -#endif - { + if (poll_fget(fdp, fd->fd, &fp) != 0) { fd->revents = POLLNVAL; n++; continue; } - /* * Note: backend also returns POLLHUP and * POLLERR if appropriate. @@ -1550,47 +1585,39 @@ pollout(struct thread *td, struct pollfd *fds, struct static int pollscan(struct thread *td, struct pollfd *fds, u_int nfd) { - struct filedesc *fdp = td->td_proc->p_fd; + struct filedesc *fdp; struct file *fp; - int i, n = 0; + int i, n; + n = 0; + fdp = td->td_proc->p_fd; FILEDESC_SLOCK(fdp); for (i = 0; i < nfd; i++, fds++) { - if (fds->fd >= fdp->fd_nfiles) { + if (fds->fd < 0) { + fds->revents = 0; + continue; + } + if (poll_fget(fdp, fds->fd, &fp) != 0) { fds->revents = POLLNVAL; n++; - } else if (fds->fd < 0) { - fds->revents = 0; - } else { - fp = fdp->fd_ofiles[fds->fd].fde_file; -#ifdef CAPABILITIES - if (fp == NULL || - cap_check(cap_rights(fdp, fds->fd), &cap_event_rights) != 0) -#else - if (fp == NULL) -#endif - { - fds->revents = POLLNVAL; - n++; - } else { - /* - * Note: backend also returns POLLHUP and - * POLLERR if appropriate. - */ - selfdalloc(td, fds); - fds->revents = fo_poll(fp, fds->events, - td->td_ucred, td); - /* - * POSIX requires POLLOUT to be never - * set simultaneously with POLLHUP. - */ - if ((fds->revents & POLLHUP) != 0) - fds->revents &= ~POLLOUT; - - if (fds->revents != 0) - n++; - } + continue; } + /* + * Note: backend also returns POLLHUP and + * POLLERR if appropriate. + */ + selfdalloc(td, fds); + fds->revents = fo_poll(fp, fds->events, + td->td_ucred, td); + /* + * POSIX requires POLLOUT to be never + * set simultaneously with POLLHUP. + */ + if ((fds->revents & POLLHUP) != 0) + fds->revents &= ~POLLOUT; + + if (fds->revents != 0) + n++; } FILEDESC_SUNLOCK(fdp); td->td_retval[0] = n;