summaryrefslogtreecommitdiff
path: root/sys/kern/sys_select.c
AgeCommit message (Collapse)Author
2022-06-29sleepq(9): Pass syncobj through to sleepq_block.riastradh
Previously the usage pattern was: sleepq_enter(sq, l, lock); // locks l ... sleepq_enqueue(sq, ..., sobj, ...); // assumes l locked, sets l_syncobj ... (*) sleepq_block(...); // unlocks l As long as l remains locked from sleepq_enter to sleepq_block, l_syncobj is stable, and sleepq_block uses it via ktrcsw to determine whether the sleep is on a mutex in order to avoid creating ktrace context-switch records (which involves allocation which is forbidden in softint context, while taking and even sleeping for a mutex is allowed). However, in turnstile_block, the logic at (*) also involves turnstile_lendpri, which sometimes unlocks and relocks l. At that point, another thread can swoop in and sleepq_remove l, which sets l_syncobj to sched_syncobj. If that happens, ktrcsw does what is forbidden -- tries to allocate a ktrace record for the context switch. As an optimization, sleepq_block or turnstile_block could stop early if it detects that l_syncobj doesn't match -- we've already been requested to wake up at this point so there's no need to mi_switch. (And then it would be unnecessary to pass the syncobj through sleepq_block, because l_syncobj would remain stable.) But I'll leave that to another change. Reported-by: syzbot+8b9d7b066c32dbcdc63b@syzkaller.appspotmail.com
2022-04-09select(9): Use membar_acquire/release and atomic_store_release.riastradh
No store-before-load ordering here -- this was obviously always intended to be load-before-load/store all along.
2022-02-12Add inline functions to manipulate the klists that link up knotesthorpej
via kn_selnext: - klist_init() - klist_fini() - klist_insert() - klist_remove() These provide some API insulation from the implementation details of these lists (but not completely; see vn_knote_attach() and vn_knote_detach()). Currently just a wrapper around SLIST(9). This will make it significantly easier to switch kn_selnext linkage to a different kind of list.
2021-12-10s/occured/occurred/ in comments, log messages and man pages.andvar
2021-09-29- Change selremove_knote() from returning void to bool, and returnthorpej
true if the last knote was removed and there are no more knotes on the selinfo. - Use this new return value in filt_sordetach(), filt_sowdetach(), filt_fifordetach(), and filt_fifowdetach() to know when to clear SB_KOTE without having to know select/kqueue implementation details.
2020-12-11Add sel{record,remove}_knote(), so hide some of the details surroundingthorpej
knote / kevent registration in the selinfo structure.
2020-04-19Set LW_SINTR earlier so it doesn't pose a problem for doing interruptablead
waits with turnstiles (not currently done).
2020-03-26Change sleepq_t from a TAILQ to a LIST and remove SOBJ_SLEEPQ_FIFO. Onlyad
select/poll used the FIFO method and that was for collisions which rarely occur. Shrinks sleep_t and condvar_t.
2020-02-15- List all of the syncobjs in syncobj.h.ad
- Update a comment.
2020-02-01Load struct filedesc::fd_dt with atomic_load_consume.riastradh
Exceptions: when fd_refcnt <= 1, or when holding fd_lock. While here: - Restore KASSERT(mutex_owned(&fdp->fd_lock)) in fd_unused. => This is used only in fd_close and fd_abort, where it holds. - Move bounds check assertion in fd_putfile to where it matters. - Store fd_dt with atomic_store_release. - Move load of fd_dt under lock in knote_fdclose. - Omit membar_consumer in fdesc_readdir. => atomic_load_consume serves the same purpose now. => Was needed only on alpha anyway.
2019-11-22Minor correction to previous.ad
2019-11-21Minor improvements to select/poll:ad
- Increase the maximum number of clusters from 32 to 64 for large systems. kcpuset_t could potentially be used here but that's an excursion I don't want to go on right now. uint32_t -> uint64_t is very simple. - In the case of a non-blocking select/poll, or where we won't block because there are events ready to report, stop registering interest in the back-end objects early. - Change the wmesg for poll back to "poll".
2019-09-20Validate usec ranges in sys___select50()kamil
Later in the code selcommon() checks for proper timespec, check only correct usec of timeval before type conversions.
2019-08-20 Use unsigned to avoid undefined behavior. Found by kUBSan.msaitoh
2019-07-26 Set sc_mask correctly in selsysinit() to avoid undefined behavior.msaitoh
Found by KUBSan.
2019-05-08Add slop of 1000 and explain why.christos
2019-05-07Use the max limit (aka maxfiles or the moral equivalent of OPEN_MAX) whichchristos
makes poll(2) align with the Posix documentation (which allows EINVAL if nfds > OPEN_MAX). From: Anthony Mallet
2019-05-05Remove the slop code. Suggested by mrg@christos
2019-05-04PR/54158: Anthony Mallet: poll(2) does not allow polling all possible fdschristos
(hardcoded limit to 1000 + #<open-fds>). Changed to limit by the max of the resource limit of open descriptors and the above.
2018-01-30Apply C99-style struct initialization to syncobj_tozaki-r
2017-06-01remove checks for failure after memory allocation calls that cannot fail:chs
kmem_alloc() with KM_SLEEP kmem_zalloc() with KM_SLEEP percpu_alloc() pserialize_create() psref_class_create() all of these paths include an assertion that the allocation has not failed, so callers should not assert that again.
2014-04-25Remove pollsock(). Since it took only a single socket, it was essentiallypooka
a complicated way to call soreceive() with a sb_timeo. The only user (netsmb) already did that anyway, so just had to delete the call to pollsock().
2014-02-25Ensure that the top level sysctl nodes (kern, vfs, net, ...) exist beforepooka
the sysctl link sets are processed, and remove redundancy. Shaves >13kB off of an amd64 GENERIC, not to mention >1k duplicate lines of code.
2013-01-26Assert equality, not assignment, in selrecord.riastradh
Code inspection suggests that this fix is not likely to reveal any latent problems.
2011-08-29Add kern.direct_select sysctl. Default to 0 for now.rmind
2011-08-09No need to lock the selcluster in selscan() if eitherhannken
NO_DIRECT_SELECT is defined or all polls return an event.
2011-08-06Fix the races of direct select()/poll():hannken
- When sel_do_scan() restarts do a full initialization with selclear() so we start from an empty set without registered events. Defer the evaluation of l_selret after selclear() and add the count of direct events to the count of events. - For selscan()/pollscan() zero the output descriptors before we poll and for selscan() take the sc_lock before we change them. - Change sel_setevents() to not count events already set. Reviewed by: YAMAMOTO Takashi <yamt@netbsd.org> Should fix PR #44763 (select/poll direct-set optimization seems racy) and PR #45187 (select(2) sometimes doesn't wakeup)
2011-05-28If a signal did not fire, restore the original signal mask for pselect/polltschristos
using a signal mask. Tested by tron.
2011-05-18No need to mask twice. The setup function does it.christos
2011-05-18PR/43625: Mark Davies: Fix pselect(2) to honor the temporary mask. pselect(2)christos
(and pollts(2)) are similar to sigsuspend(2) in that they temporarily change the process signal mask and wait for signal delivery. Factor out and share the code that does this.
2011-03-06In a case of direct select, set only masked events, do not wakeup LWPrmind
if no polled/selected events were set; also, count the correct return value for the select.
2010-12-18- Fix a few possible locking issues in execve1() and exit1(). Add a notermind
that scheduler locks are special in this regard - adaptive locks cannot be in the path due to turnstiles. Randomly spotted/reported by uebayasi@. - Remove unused lwp_relock() and replace lwp_lock_retry() by simplifying lwp_lock() and sleepq_enter() a little. - Give alllwp its own cache-line and mark lwp_cache pointer as read-mostly. OK ad@
2010-10-15Re-enable direct select.rmind
2010-07-12sel_setevents: fix error - match event-set, as intended.rmind
Spotted by Enami Tsugutomo.
2010-07-11Disable direct select for now, since it still brings problems.rmind
2010-07-10sel_setevents: fix direct injecting of fd bit for select() case.rmind
2010-07-08sel_do_scan: do not bother to assert for SEL_SCANNING state before blocking,rmind
as it might also be SEL_BLOCKING due to spurious wake-ups. That has no harm.
2010-07-08Implement direct select/poll support, currently effective for socket andrmind
pipe subsystems. Avoids overhead of second selscan() on wake-up, and thus improves performance on certain workloads (especially when polling on many file-descriptors). Also, clean-up sys/fd_set.h header and improve macros. Welcome to 5.99.36!
2010-04-25Make select/poll work with more than 32 CPUs.ad
No ABI change.
2009-12-20Add comment about locking.rmind
2009-12-12Bounding the 'nfds' arg to poll() at the current process limit for actualdsl
open files is rather gross - the poll map isn't required to be dense. Instead limit to a much larger value (1000 + dt_nfiles) so that user programs cannot allocate indefinite sized blocks of kvm. If the limit is exceeded, then return EINVAL instead of silently truncating the list. (The silent truncation in select isn't quite as bad - although even there any high bits that are set ought to generate an EBADF response.) Move the code that converts ERESTART and EWOULDBLOCK into common code. Effectively fixes PR/17507 since the new limit is unlikely to be detected.
2009-11-11- selcommon/pollcommon: drop redundant l argument.rmind
- Use cached curlwp->l_fd, instead of p->p_fd. - Inline selscan/pollscan.
2009-11-01- Move inittimeleft() and gettimeleft() to subr_time.c, where they belong.rmind
- Move abstimeout2timo() there too and export. Use it in lwp_park().
2009-11-01Move common logic in selcommon() and pollcommon() into sel_do_scan().rmind
Avoids code duplication. XXX: pollsock() should be converted too, except it's a bit ugly.
2009-10-21Remove uarea swap-out functionality:rmind
- Addresses the issue described in PR/38828. - Some simplification in threading and sleepq subsystems. - Eliminates pmap_collect() and, as a side note, allows pmap optimisations. - Eliminates XS_CTL_DATA_ONSTACK in scsipi code. - Avoids few scans on LWP list and thus potentially long holds of proc_lock. - Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k. - Removes __SWAP_BROKEN cases. Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on acorn26 (thanks to <bjh21>). Discussed on <tech-kern>, reviewed by <ad>.
2009-05-24More changes to improve kern_descrip.c.ad
- Avoid atomics in more places. - Remove the per-descriptor mutex, and just use filedesc_t::fd_lock. It was only being used to synchronize close, and in any case we needed to take fd_lock to free the descriptor slot. - Optimize certain paths for the <NDFDFILE case. - Sprinkle more comments and assertions. - Cache more stuff in filedesc_t. - Fix numerous minor bugs spotted along the way. - Restructure how the open files array is maintained, for clarity and so that we can eliminate the membar_consumer() call in fd_getfile(). This is mostly syntactic sugar; the main functional change is that fd_nfiles now lives alongside the open file array. Some measurements with libmicro: - simple file syscalls are like close() are between 1 to 10% faster. - some nice improvements, e.g. poll(1000) which is ~50% faster.
2009-03-29Move the internal poll/select related API's to use timespec insteadchristos
of timeval (rides the uvm bump).
2009-03-21Allocate sleep queue locks with mutex_obj_alloc. Reduces memory usagead
on !MP kernels, and reduces false sharing on MP ones.
2009-01-11merge christos-time_tchristos
2008-11-20pollcommon: use a more appropriate type than char[].yamt