diff --git a/lib/libc/sys/select.c b/lib/libc/sys/select.c index 38a3b53cb..52fd2e378 100644 --- a/lib/libc/sys/select.c +++ b/lib/libc/sys/select.c @@ -7,9 +7,9 @@ #include #include -int pselect(int nfds, fd_set *readfds, fd_set *writefds, - fd_set *exceptfds, const struct timespec *timeout, - const sigset_t *sigmask) { +int pselect(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, + fd_set *restrict exceptfds, const struct timespec *restrict timeout, + const sigset_t *restrict sigmask) { int kq; int ret; struct kevent *events; @@ -21,10 +21,15 @@ int pselect(int nfds, fd_set *readfds, fd_set *writefds, return -1; } - kq = kqueue1(O_CLOEXEC); - if (kq < 0) + if (sigmask && sigprocmask(SIG_SETMASK, sigmask, &sigs)) return -1; + kq = kqueue1(O_CLOEXEC); + if (kq < 0) { + ret = -1; + goto restore_sigs; + } + events = malloc(2 * nfds * sizeof(struct kevent)); if (!events) { errno = ENOMEM; @@ -44,9 +49,6 @@ int pselect(int nfds, fd_set *readfds, fd_set *writefds, if (writefds != NULL) FD_ZERO(writefds); - if (sigmask && sigprocmask(SIG_SETMASK, sigmask, &sigs)) - return -1; - ret = kevent(kq, events, nevents, events, nevents, timeout); if (ret == -1) goto end; @@ -65,10 +67,11 @@ int pselect(int nfds, fd_set *readfds, fd_set *writefds, end: free(events); - if (sigmask && sigprocmask(SIG_SETMASK, &sigs, NULL)) - ret = -1; close_kq: close(kq); +restore_sigs: + if (sigmask && sigprocmask(SIG_SETMASK, &sigs, NULL)) + ret = -1; return ret; } diff --git a/sys/kern/vm_map.c b/sys/kern/vm_map.c index 957a517e7..76f4d3d9b 100644 --- a/sys/kern/vm_map.c +++ b/sys/kern/vm_map.c @@ -539,6 +539,11 @@ static int cow_page_fault(vm_map_t *map, vm_map_entry_t *ent, size_t off, if (old == NULL) return 0; + /* This check is safe. We are the only owner of this anon and the ref count + * will not change because we are under vm_map:mtx. */ + if (old->ref_cnt == 1) + return 0; + /* Current mapping will be replaced with new one so remove it from pmap. */ vaddr_t fault_page = off * PAGESIZE + ent->start; pmap_remove(map->pmap, fault_page, fault_page + PAGESIZE);