Home | History | Annotate | Line # | Download | only in kern
sys_eventfd.c revision 1.4
      1  1.4  thorpej /*	$NetBSD: sys_eventfd.c,v 1.4 2021/09/20 14:30:05 thorpej Exp $	*/
      2  1.2  thorpej 
      3  1.2  thorpej /*-
      4  1.2  thorpej  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.2  thorpej  * All rights reserved.
      6  1.2  thorpej  *
      7  1.2  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  thorpej  * by Jason R. Thorpe.
      9  1.2  thorpej  *
     10  1.2  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.2  thorpej  * modification, are permitted provided that the following conditions
     12  1.2  thorpej  * are met:
     13  1.2  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.2  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.2  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.2  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.2  thorpej  *
     19  1.2  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2  thorpej  */
     31  1.2  thorpej 
     32  1.2  thorpej #include <sys/cdefs.h>
     33  1.4  thorpej __KERNEL_RCSID(0, "$NetBSD: sys_eventfd.c,v 1.4 2021/09/20 14:30:05 thorpej Exp $");
     34  1.2  thorpej 
     35  1.2  thorpej /*
     36  1.2  thorpej  * eventfd
     37  1.2  thorpej  *
     38  1.2  thorpej  * Eventfd objects present a simple counting object associated with a
     39  1.2  thorpej  * file descriptor.  Writes and reads to this file descriptor increment
     40  1.2  thorpej  * and decrement the count, respectively.  When the count is non-zero,
     41  1.2  thorpej  * the descriptor is considered "readable", and when less than the max
     42  1.2  thorpej  * value (EVENTFD_MAXVAL), is considered "writable".
     43  1.2  thorpej  *
     44  1.2  thorpej  * This implementation is API compatible with the Linux eventfd(2)
     45  1.2  thorpej  * interface.
     46  1.2  thorpej  */
     47  1.2  thorpej 
     48  1.3    skrll #include <sys/param.h>
     49  1.2  thorpej #include <sys/types.h>
     50  1.2  thorpej #include <sys/condvar.h>
     51  1.2  thorpej #include <sys/eventfd.h>
     52  1.2  thorpej #include <sys/file.h>
     53  1.2  thorpej #include <sys/filedesc.h>
     54  1.2  thorpej #include <sys/kauth.h>
     55  1.2  thorpej #include <sys/mutex.h>
     56  1.2  thorpej #include <sys/poll.h>
     57  1.2  thorpej #include <sys/proc.h>
     58  1.2  thorpej #include <sys/select.h>
     59  1.2  thorpej #include <sys/stat.h>
     60  1.2  thorpej #include <sys/syscallargs.h>
     61  1.2  thorpej #include <sys/uio.h>
     62  1.2  thorpej 
     63  1.2  thorpej struct eventfd {
     64  1.2  thorpej 	kmutex_t	efd_lock;
     65  1.2  thorpej 	kcondvar_t	efd_read_wait;
     66  1.2  thorpej 	kcondvar_t	efd_write_wait;
     67  1.2  thorpej 	kcondvar_t	efd_restart_wait;
     68  1.2  thorpej 	struct selinfo	efd_read_sel;
     69  1.2  thorpej 	struct selinfo	efd_write_sel;
     70  1.2  thorpej 	eventfd_t	efd_val;
     71  1.2  thorpej 	int64_t		efd_nwaiters;
     72  1.2  thorpej 	bool		efd_restarting;
     73  1.2  thorpej 	bool		efd_has_read_waiters;
     74  1.2  thorpej 	bool		efd_has_write_waiters;
     75  1.2  thorpej 	bool		efd_is_semaphore;
     76  1.2  thorpej 
     77  1.2  thorpej 	/*
     78  1.2  thorpej 	 * Information kept for stat(2).
     79  1.2  thorpej 	 */
     80  1.2  thorpej 	struct timespec efd_btime;	/* time created */
     81  1.2  thorpej 	struct timespec	efd_mtime;	/* last write */
     82  1.2  thorpej 	struct timespec	efd_atime;	/* last read */
     83  1.2  thorpej };
     84  1.2  thorpej 
     85  1.2  thorpej #define	EVENTFD_MAXVAL	(UINT64_MAX - 1)
     86  1.2  thorpej 
     87  1.2  thorpej /*
     88  1.2  thorpej  * eventfd_create:
     89  1.2  thorpej  *
     90  1.2  thorpej  *	Create an eventfd object.
     91  1.2  thorpej  */
     92  1.2  thorpej static struct eventfd *
     93  1.2  thorpej eventfd_create(unsigned int const val, int const flags)
     94  1.2  thorpej {
     95  1.2  thorpej 	struct eventfd * const efd = kmem_zalloc(sizeof(*efd), KM_SLEEP);
     96  1.2  thorpej 
     97  1.2  thorpej 	mutex_init(&efd->efd_lock, MUTEX_DEFAULT, IPL_NONE);
     98  1.2  thorpej 	cv_init(&efd->efd_read_wait, "efdread");
     99  1.2  thorpej 	cv_init(&efd->efd_write_wait, "efdwrite");
    100  1.2  thorpej 	cv_init(&efd->efd_restart_wait, "efdrstrt");
    101  1.2  thorpej 	selinit(&efd->efd_read_sel);
    102  1.2  thorpej 	selinit(&efd->efd_write_sel);
    103  1.2  thorpej 	efd->efd_val = val;
    104  1.2  thorpej 	efd->efd_is_semaphore = !!(flags & EFD_SEMAPHORE);
    105  1.2  thorpej 	getnanotime(&efd->efd_btime);
    106  1.2  thorpej 
    107  1.2  thorpej 	/* Caller deals with EFD_CLOEXEC and EFD_NONBLOCK. */
    108  1.2  thorpej 
    109  1.2  thorpej 	return efd;
    110  1.2  thorpej }
    111  1.2  thorpej 
    112  1.2  thorpej /*
    113  1.2  thorpej  * eventfd_destroy:
    114  1.2  thorpej  *
    115  1.2  thorpej  *	Destroy an eventfd object.
    116  1.2  thorpej  */
    117  1.2  thorpej static void
    118  1.2  thorpej eventfd_destroy(struct eventfd * const efd)
    119  1.2  thorpej {
    120  1.2  thorpej 
    121  1.2  thorpej 	KASSERT(efd->efd_nwaiters == 0);
    122  1.2  thorpej 	KASSERT(efd->efd_restarting == false);
    123  1.2  thorpej 	KASSERT(efd->efd_has_read_waiters == false);
    124  1.2  thorpej 	KASSERT(efd->efd_has_write_waiters == false);
    125  1.2  thorpej 
    126  1.2  thorpej 	cv_destroy(&efd->efd_read_wait);
    127  1.2  thorpej 	cv_destroy(&efd->efd_write_wait);
    128  1.2  thorpej 	cv_destroy(&efd->efd_restart_wait);
    129  1.2  thorpej 
    130  1.2  thorpej 	seldestroy(&efd->efd_read_sel);
    131  1.2  thorpej 	seldestroy(&efd->efd_write_sel);
    132  1.2  thorpej 
    133  1.2  thorpej 	mutex_destroy(&efd->efd_lock);
    134  1.4  thorpej 
    135  1.4  thorpej 	kmem_free(efd, sizeof(*efd));
    136  1.2  thorpej }
    137  1.2  thorpej 
    138  1.2  thorpej /*
    139  1.2  thorpej  * eventfd_wait:
    140  1.2  thorpej  *
    141  1.2  thorpej  *	Block on an eventfd.  Handles non-blocking, as well as
    142  1.2  thorpej  *	the restart cases.
    143  1.2  thorpej  */
    144  1.2  thorpej static int
    145  1.2  thorpej eventfd_wait(struct eventfd * const efd, int const fflag, bool const is_write)
    146  1.2  thorpej {
    147  1.2  thorpej 	kcondvar_t *waitcv;
    148  1.2  thorpej 	int error;
    149  1.2  thorpej 
    150  1.2  thorpej 	if (fflag & FNONBLOCK) {
    151  1.2  thorpej 		return EAGAIN;
    152  1.2  thorpej 	}
    153  1.2  thorpej 
    154  1.2  thorpej 	/*
    155  1.2  thorpej 	 * We're going to block.  If there is a restart in-progress,
    156  1.2  thorpej 	 * wait for that to complete first.
    157  1.2  thorpej 	 */
    158  1.2  thorpej 	while (efd->efd_restarting) {
    159  1.2  thorpej 		cv_wait(&efd->efd_restart_wait, &efd->efd_lock);
    160  1.2  thorpej 	}
    161  1.2  thorpej 
    162  1.2  thorpej 	if (is_write) {
    163  1.2  thorpej 		efd->efd_has_write_waiters = true;
    164  1.2  thorpej 		waitcv = &efd->efd_write_wait;
    165  1.2  thorpej 	} else {
    166  1.2  thorpej 		efd->efd_has_read_waiters = true;
    167  1.2  thorpej 		waitcv = &efd->efd_read_wait;
    168  1.2  thorpej 	}
    169  1.2  thorpej 
    170  1.2  thorpej 	efd->efd_nwaiters++;
    171  1.2  thorpej 	KASSERT(efd->efd_nwaiters > 0);
    172  1.2  thorpej 	error = cv_wait_sig(waitcv, &efd->efd_lock);
    173  1.2  thorpej 	efd->efd_nwaiters--;
    174  1.2  thorpej 	KASSERT(efd->efd_nwaiters >= 0);
    175  1.2  thorpej 
    176  1.2  thorpej 	/*
    177  1.2  thorpej 	 * If a restart was triggered while we were asleep, we need
    178  1.2  thorpej 	 * to return ERESTART if no other error was returned.  If we
    179  1.2  thorpej 	 * are the last waiter coming out of the restart drain, clear
    180  1.2  thorpej 	 * the condition.
    181  1.2  thorpej 	 */
    182  1.2  thorpej 	if (efd->efd_restarting) {
    183  1.2  thorpej 		if (error == 0) {
    184  1.2  thorpej 			error = ERESTART;
    185  1.2  thorpej 		}
    186  1.2  thorpej 		if (efd->efd_nwaiters == 0) {
    187  1.2  thorpej 			efd->efd_restarting = false;
    188  1.2  thorpej 			cv_broadcast(&efd->efd_restart_wait);
    189  1.2  thorpej 		}
    190  1.2  thorpej 	}
    191  1.2  thorpej 
    192  1.2  thorpej 	return error;
    193  1.2  thorpej }
    194  1.2  thorpej 
    195  1.2  thorpej /*
    196  1.2  thorpej  * eventfd_wake:
    197  1.2  thorpej  *
    198  1.2  thorpej  *	Wake LWPs block on an eventfd.
    199  1.2  thorpej  */
    200  1.2  thorpej static void
    201  1.2  thorpej eventfd_wake(struct eventfd * const efd, bool const is_write)
    202  1.2  thorpej {
    203  1.2  thorpej 	kcondvar_t *waitcv = NULL;
    204  1.2  thorpej 	struct selinfo *sel;
    205  1.2  thorpej 	int pollev;
    206  1.2  thorpej 
    207  1.2  thorpej 	if (is_write) {
    208  1.2  thorpej 		if (efd->efd_has_read_waiters) {
    209  1.2  thorpej 			waitcv = &efd->efd_read_wait;
    210  1.2  thorpej 			efd->efd_has_read_waiters = false;
    211  1.2  thorpej 		}
    212  1.2  thorpej 		sel = &efd->efd_read_sel;
    213  1.2  thorpej 		pollev = POLLIN | POLLRDNORM;
    214  1.2  thorpej 	} else {
    215  1.2  thorpej 		if (efd->efd_has_write_waiters) {
    216  1.2  thorpej 			waitcv = &efd->efd_write_wait;
    217  1.2  thorpej 			efd->efd_has_write_waiters = false;
    218  1.2  thorpej 		}
    219  1.2  thorpej 		sel = &efd->efd_write_sel;
    220  1.2  thorpej 		pollev = POLLOUT | POLLWRNORM;
    221  1.2  thorpej 	}
    222  1.2  thorpej 	if (waitcv != NULL) {
    223  1.2  thorpej 		cv_broadcast(waitcv);
    224  1.2  thorpej 	}
    225  1.2  thorpej 	selnotify(sel, pollev, NOTE_SUBMIT);
    226  1.2  thorpej }
    227  1.2  thorpej 
    228  1.2  thorpej /*
    229  1.2  thorpej  * eventfd file operations
    230  1.2  thorpej  */
    231  1.2  thorpej 
    232  1.2  thorpej static int
    233  1.2  thorpej eventfd_fop_read(file_t * const fp, off_t * const offset,
    234  1.2  thorpej     struct uio * const uio, kauth_cred_t const cred, int const flags)
    235  1.2  thorpej {
    236  1.2  thorpej 	struct eventfd * const efd = fp->f_eventfd;
    237  1.2  thorpej 	int const fflag = fp->f_flag;
    238  1.2  thorpej 	eventfd_t return_value;
    239  1.2  thorpej 	int error;
    240  1.2  thorpej 
    241  1.2  thorpej 	if (uio->uio_resid < sizeof(eventfd_t)) {
    242  1.2  thorpej 		return EINVAL;
    243  1.2  thorpej 	}
    244  1.2  thorpej 
    245  1.2  thorpej 	mutex_enter(&efd->efd_lock);
    246  1.2  thorpej 
    247  1.2  thorpej 	while (efd->efd_val == 0) {
    248  1.2  thorpej 		if ((error = eventfd_wait(efd, fflag, false)) != 0) {
    249  1.2  thorpej 			mutex_exit(&efd->efd_lock);
    250  1.2  thorpej 			return error;
    251  1.2  thorpej 		}
    252  1.2  thorpej 	}
    253  1.2  thorpej 
    254  1.2  thorpej 	if (efd->efd_is_semaphore) {
    255  1.2  thorpej 		return_value = 1;
    256  1.2  thorpej 		efd->efd_val--;
    257  1.2  thorpej 	} else {
    258  1.2  thorpej 		return_value = efd->efd_val;
    259  1.2  thorpej 		efd->efd_val = 0;
    260  1.2  thorpej 	}
    261  1.2  thorpej 
    262  1.2  thorpej 	getnanotime(&efd->efd_atime);
    263  1.2  thorpej 	eventfd_wake(efd, false);
    264  1.2  thorpej 
    265  1.2  thorpej 	mutex_exit(&efd->efd_lock);
    266  1.2  thorpej 
    267  1.2  thorpej 	error = uiomove(&return_value, sizeof(return_value), uio);
    268  1.2  thorpej 
    269  1.2  thorpej 	return error;
    270  1.2  thorpej }
    271  1.2  thorpej 
    272  1.2  thorpej static int
    273  1.2  thorpej eventfd_fop_write(file_t * const fp, off_t * const offset,
    274  1.2  thorpej     struct uio * const uio, kauth_cred_t const cred, int const flags)
    275  1.2  thorpej {
    276  1.2  thorpej 	struct eventfd * const efd = fp->f_eventfd;
    277  1.2  thorpej 	int const fflag = fp->f_flag;
    278  1.2  thorpej 	eventfd_t write_value;
    279  1.2  thorpej 	int error;
    280  1.2  thorpej 
    281  1.2  thorpej 	if (uio->uio_resid < sizeof(eventfd_t)) {
    282  1.2  thorpej 		return EINVAL;
    283  1.2  thorpej 	}
    284  1.2  thorpej 
    285  1.2  thorpej 	if ((error = uiomove(&write_value, sizeof(write_value), uio)) != 0) {
    286  1.2  thorpej 		return error;
    287  1.2  thorpej 	}
    288  1.2  thorpej 
    289  1.2  thorpej 	if (write_value > EVENTFD_MAXVAL) {
    290  1.2  thorpej 		error = EINVAL;
    291  1.2  thorpej 		goto out;
    292  1.2  thorpej 	}
    293  1.2  thorpej 
    294  1.2  thorpej 	mutex_enter(&efd->efd_lock);
    295  1.2  thorpej 
    296  1.2  thorpej 	KASSERT(efd->efd_val <= EVENTFD_MAXVAL);
    297  1.2  thorpej 	while ((EVENTFD_MAXVAL - efd->efd_val) < write_value) {
    298  1.2  thorpej 		if ((error = eventfd_wait(efd, fflag, true)) != 0) {
    299  1.2  thorpej 			mutex_exit(&efd->efd_lock);
    300  1.2  thorpej 			goto out;
    301  1.2  thorpej 		}
    302  1.2  thorpej 	}
    303  1.2  thorpej 
    304  1.2  thorpej 	efd->efd_val += write_value;
    305  1.2  thorpej 	KASSERT(efd->efd_val <= EVENTFD_MAXVAL);
    306  1.2  thorpej 
    307  1.2  thorpej 	getnanotime(&efd->efd_mtime);
    308  1.2  thorpej 	eventfd_wake(efd, true);
    309  1.2  thorpej 
    310  1.2  thorpej 	mutex_exit(&efd->efd_lock);
    311  1.2  thorpej 
    312  1.2  thorpej  out:
    313  1.2  thorpej 	if (error) {
    314  1.2  thorpej 		/*
    315  1.2  thorpej 		 * Undo the effect of uiomove() so that the error
    316  1.2  thorpej 		 * gets reported correctly; see dofilewrite().
    317  1.2  thorpej 		 */
    318  1.2  thorpej 		uio->uio_resid += sizeof(write_value);
    319  1.2  thorpej 	}
    320  1.2  thorpej 	return error;
    321  1.2  thorpej }
    322  1.2  thorpej 
    323  1.2  thorpej static int
    324  1.2  thorpej eventfd_fop_poll(file_t * const fp, int const events)
    325  1.2  thorpej {
    326  1.2  thorpej 	struct eventfd * const efd = fp->f_eventfd;
    327  1.2  thorpej 	int revents = 0;
    328  1.2  thorpej 
    329  1.2  thorpej 	/*
    330  1.2  thorpej 	 * Note that Linux will return POLLERR if the eventfd count
    331  1.2  thorpej 	 * overflows, but that is not possible in the normal read/write
    332  1.2  thorpej 	 * API, only with Linux kernel-internal interfaces.  So, this
    333  1.2  thorpej 	 * implementation never returns POLLERR.
    334  1.2  thorpej 	 *
    335  1.2  thorpej 	 * Also note that the Linux eventfd(2) man page does not
    336  1.2  thorpej 	 * specifically discuss returning POLLRDNORM, but we check
    337  1.2  thorpej 	 * for that event in addition to POLLIN.
    338  1.2  thorpej 	 */
    339  1.2  thorpej 
    340  1.2  thorpej 	mutex_enter(&efd->efd_lock);
    341  1.2  thorpej 
    342  1.2  thorpej 	if (events & (POLLIN | POLLRDNORM)) {
    343  1.2  thorpej 		if (efd->efd_val != 0) {
    344  1.2  thorpej 			revents |= events & (POLLIN | POLLRDNORM);
    345  1.2  thorpej 		} else {
    346  1.2  thorpej 			selrecord(curlwp, &efd->efd_read_sel);
    347  1.2  thorpej 		}
    348  1.2  thorpej 	}
    349  1.2  thorpej 
    350  1.2  thorpej 	if (events & (POLLOUT | POLLWRNORM)) {
    351  1.2  thorpej 		if (efd->efd_val < EVENTFD_MAXVAL) {
    352  1.2  thorpej 			revents |= events & (POLLOUT | POLLWRNORM);
    353  1.2  thorpej 		} else {
    354  1.2  thorpej 			selrecord(curlwp, &efd->efd_write_sel);
    355  1.2  thorpej 		}
    356  1.2  thorpej 	}
    357  1.2  thorpej 
    358  1.2  thorpej 	mutex_exit(&efd->efd_lock);
    359  1.2  thorpej 
    360  1.2  thorpej 	return revents;
    361  1.2  thorpej }
    362  1.2  thorpej 
    363  1.2  thorpej static int
    364  1.2  thorpej eventfd_fop_stat(file_t * const fp, struct stat * const st)
    365  1.2  thorpej {
    366  1.2  thorpej 	struct eventfd * const efd = fp->f_eventfd;
    367  1.2  thorpej 
    368  1.2  thorpej 	memset(st, 0, sizeof(*st));
    369  1.2  thorpej 
    370  1.2  thorpej 	mutex_enter(&efd->efd_lock);
    371  1.2  thorpej 	st->st_size = (off_t)efd->efd_val;
    372  1.2  thorpej 	st->st_blksize = sizeof(eventfd_t);
    373  1.2  thorpej 	st->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
    374  1.2  thorpej 	st->st_blocks = 1;
    375  1.2  thorpej 	st->st_birthtimespec = st->st_ctimespec = efd->efd_btime;
    376  1.2  thorpej 	st->st_atimespec = efd->efd_atime;
    377  1.2  thorpej 	st->st_mtimespec = efd->efd_mtime;
    378  1.2  thorpej 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
    379  1.2  thorpej 	st->st_gid = kauth_cred_getegid(fp->f_cred);
    380  1.2  thorpej 	mutex_exit(&efd->efd_lock);
    381  1.2  thorpej 
    382  1.2  thorpej 	return 0;
    383  1.2  thorpej }
    384  1.2  thorpej 
    385  1.2  thorpej static int
    386  1.2  thorpej eventfd_fop_close(file_t * const fp)
    387  1.2  thorpej {
    388  1.2  thorpej 	struct eventfd * const efd = fp->f_eventfd;
    389  1.2  thorpej 
    390  1.2  thorpej 	fp->f_eventfd = NULL;
    391  1.2  thorpej 	eventfd_destroy(efd);
    392  1.2  thorpej 
    393  1.2  thorpej 	return 0;
    394  1.2  thorpej }
    395  1.2  thorpej 
    396  1.2  thorpej static void
    397  1.2  thorpej eventfd_filt_read_detach(struct knote * const kn)
    398  1.2  thorpej {
    399  1.2  thorpej 	struct eventfd * const efd = ((file_t *)kn->kn_obj)->f_eventfd;
    400  1.2  thorpej 
    401  1.2  thorpej 	mutex_enter(&efd->efd_lock);
    402  1.2  thorpej 	KASSERT(kn->kn_hook == efd);
    403  1.2  thorpej 	selremove_knote(&efd->efd_read_sel, kn);
    404  1.2  thorpej 	mutex_exit(&efd->efd_lock);
    405  1.2  thorpej }
    406  1.2  thorpej 
    407  1.2  thorpej static int
    408  1.2  thorpej eventfd_filt_read(struct knote * const kn, long const hint)
    409  1.2  thorpej {
    410  1.2  thorpej 	struct eventfd * const efd = ((file_t *)kn->kn_obj)->f_eventfd;
    411  1.2  thorpej 
    412  1.2  thorpej 	if (hint & NOTE_SUBMIT) {
    413  1.2  thorpej 		KASSERT(mutex_owned(&efd->efd_lock));
    414  1.2  thorpej 	} else {
    415  1.2  thorpej 		mutex_enter(&efd->efd_lock);
    416  1.2  thorpej 	}
    417  1.2  thorpej 
    418  1.2  thorpej 	kn->kn_data = (int64_t)efd->efd_val;
    419  1.2  thorpej 
    420  1.2  thorpej 	if ((hint & NOTE_SUBMIT) == 0) {
    421  1.2  thorpej 		mutex_exit(&efd->efd_lock);
    422  1.2  thorpej 	}
    423  1.2  thorpej 
    424  1.2  thorpej 	return (eventfd_t)kn->kn_data > 0;
    425  1.2  thorpej }
    426  1.2  thorpej 
    427  1.2  thorpej static const struct filterops eventfd_read_filterops = {
    428  1.2  thorpej 	.f_isfd = 1,
    429  1.2  thorpej 	.f_detach = eventfd_filt_read_detach,
    430  1.2  thorpej 	.f_event = eventfd_filt_read,
    431  1.2  thorpej };
    432  1.2  thorpej 
    433  1.2  thorpej static void
    434  1.2  thorpej eventfd_filt_write_detach(struct knote * const kn)
    435  1.2  thorpej {
    436  1.2  thorpej 	struct eventfd * const efd = ((file_t *)kn->kn_obj)->f_eventfd;
    437  1.2  thorpej 
    438  1.2  thorpej 	mutex_enter(&efd->efd_lock);
    439  1.2  thorpej 	KASSERT(kn->kn_hook == efd);
    440  1.2  thorpej 	selremove_knote(&efd->efd_write_sel, kn);
    441  1.2  thorpej 	mutex_exit(&efd->efd_lock);
    442  1.2  thorpej }
    443  1.2  thorpej 
    444  1.2  thorpej static int
    445  1.2  thorpej eventfd_filt_write(struct knote * const kn, long const hint)
    446  1.2  thorpej {
    447  1.2  thorpej 	struct eventfd * const efd = ((file_t *)kn->kn_obj)->f_eventfd;
    448  1.2  thorpej 
    449  1.2  thorpej 	if (hint & NOTE_SUBMIT) {
    450  1.2  thorpej 		KASSERT(mutex_owned(&efd->efd_lock));
    451  1.2  thorpej 	} else {
    452  1.2  thorpej 		mutex_enter(&efd->efd_lock);
    453  1.2  thorpej 	}
    454  1.2  thorpej 
    455  1.2  thorpej 	kn->kn_data = (int64_t)efd->efd_val;
    456  1.2  thorpej 
    457  1.2  thorpej 	if ((hint & NOTE_SUBMIT) == 0) {
    458  1.2  thorpej 		mutex_exit(&efd->efd_lock);
    459  1.2  thorpej 	}
    460  1.2  thorpej 
    461  1.2  thorpej 	return (eventfd_t)kn->kn_data < EVENTFD_MAXVAL;
    462  1.2  thorpej }
    463  1.2  thorpej 
    464  1.2  thorpej static const struct filterops eventfd_write_filterops = {
    465  1.2  thorpej 	.f_isfd = 1,
    466  1.2  thorpej 	.f_detach = eventfd_filt_write_detach,
    467  1.2  thorpej 	.f_event = eventfd_filt_write,
    468  1.2  thorpej };
    469  1.2  thorpej 
    470  1.2  thorpej static int
    471  1.2  thorpej eventfd_fop_kqfilter(file_t * const fp, struct knote * const kn)
    472  1.2  thorpej {
    473  1.2  thorpej 	struct eventfd * const efd = ((file_t *)kn->kn_obj)->f_eventfd;
    474  1.2  thorpej 	struct selinfo *sel;
    475  1.2  thorpej 
    476  1.2  thorpej 	switch (kn->kn_filter) {
    477  1.2  thorpej 	case EVFILT_READ:
    478  1.2  thorpej 		sel = &efd->efd_read_sel;
    479  1.2  thorpej 		kn->kn_fop = &eventfd_read_filterops;
    480  1.2  thorpej 		break;
    481  1.2  thorpej 
    482  1.2  thorpej 	case EVFILT_WRITE:
    483  1.2  thorpej 		sel = &efd->efd_write_sel;
    484  1.2  thorpej 		kn->kn_fop = &eventfd_write_filterops;
    485  1.2  thorpej 		break;
    486  1.2  thorpej 
    487  1.2  thorpej 	default:
    488  1.2  thorpej 		return EINVAL;
    489  1.2  thorpej 	}
    490  1.2  thorpej 
    491  1.2  thorpej 	kn->kn_hook = efd;
    492  1.2  thorpej 
    493  1.2  thorpej 	mutex_enter(&efd->efd_lock);
    494  1.2  thorpej 	selrecord_knote(sel, kn);
    495  1.2  thorpej 	mutex_exit(&efd->efd_lock);
    496  1.2  thorpej 
    497  1.2  thorpej 	return 0;
    498  1.2  thorpej }
    499  1.2  thorpej 
    500  1.2  thorpej static void
    501  1.2  thorpej eventfd_fop_restart(file_t * const fp)
    502  1.2  thorpej {
    503  1.2  thorpej 	struct eventfd * const efd = fp->f_eventfd;
    504  1.2  thorpej 
    505  1.2  thorpej 	/*
    506  1.2  thorpej 	 * Unblock blocked reads/writes in order to allow close() to complete.
    507  1.2  thorpej 	 * System calls return ERESTART so that the fd is revalidated.
    508  1.2  thorpej 	 */
    509  1.2  thorpej 
    510  1.2  thorpej 	mutex_enter(&efd->efd_lock);
    511  1.2  thorpej 
    512  1.2  thorpej 	if (efd->efd_nwaiters != 0) {
    513  1.2  thorpej 		efd->efd_restarting = true;
    514  1.2  thorpej 		if (efd->efd_has_read_waiters) {
    515  1.2  thorpej 			cv_broadcast(&efd->efd_read_wait);
    516  1.2  thorpej 			efd->efd_has_read_waiters = false;
    517  1.2  thorpej 		}
    518  1.2  thorpej 		if (efd->efd_has_write_waiters) {
    519  1.2  thorpej 			cv_broadcast(&efd->efd_write_wait);
    520  1.2  thorpej 			efd->efd_has_write_waiters = false;
    521  1.2  thorpej 		}
    522  1.2  thorpej 	}
    523  1.2  thorpej 
    524  1.2  thorpej 	mutex_exit(&efd->efd_lock);
    525  1.2  thorpej }
    526  1.2  thorpej 
    527  1.2  thorpej static const struct fileops eventfd_fileops = {
    528  1.2  thorpej 	.fo_name = "eventfd",
    529  1.2  thorpej 	.fo_read = eventfd_fop_read,
    530  1.2  thorpej 	.fo_write = eventfd_fop_write,
    531  1.2  thorpej 	.fo_ioctl = fbadop_ioctl,
    532  1.2  thorpej 	.fo_fcntl = fnullop_fcntl,
    533  1.2  thorpej 	.fo_poll = eventfd_fop_poll,
    534  1.2  thorpej 	.fo_stat = eventfd_fop_stat,
    535  1.2  thorpej 	.fo_close = eventfd_fop_close,
    536  1.2  thorpej 	.fo_kqfilter = eventfd_fop_kqfilter,
    537  1.2  thorpej 	.fo_restart = eventfd_fop_restart,
    538  1.2  thorpej };
    539  1.2  thorpej 
    540  1.2  thorpej /*
    541  1.2  thorpej  * eventfd(2) system call
    542  1.2  thorpej  */
    543  1.2  thorpej int
    544  1.2  thorpej do_eventfd(struct lwp * const l, unsigned int const val, int const flags,
    545  1.2  thorpej     register_t *retval)
    546  1.2  thorpej {
    547  1.2  thorpej 	file_t *fp;
    548  1.2  thorpej 	int fd, error;
    549  1.2  thorpej 
    550  1.2  thorpej 	if (flags & ~(EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE)) {
    551  1.2  thorpej 		return EINVAL;
    552  1.2  thorpej 	}
    553  1.2  thorpej 
    554  1.2  thorpej 	if ((error = fd_allocfile(&fp, &fd)) != 0) {
    555  1.2  thorpej 		return error;
    556  1.2  thorpej 	}
    557  1.2  thorpej 
    558  1.2  thorpej 	fp->f_flag = FREAD | FWRITE;
    559  1.2  thorpej 	if (flags & EFD_NONBLOCK) {
    560  1.2  thorpej 		fp->f_flag |= FNONBLOCK;
    561  1.2  thorpej 	}
    562  1.2  thorpej 	fp->f_type = DTYPE_EVENTFD;
    563  1.2  thorpej 	fp->f_ops = &eventfd_fileops;
    564  1.2  thorpej 	fp->f_eventfd = eventfd_create(val, flags);
    565  1.2  thorpej 	fd_set_exclose(l, fd, !!(flags & EFD_CLOEXEC));
    566  1.2  thorpej 	fd_affix(curproc, fp, fd);
    567  1.2  thorpej 
    568  1.2  thorpej 	*retval = fd;
    569  1.2  thorpej 	return 0;
    570  1.2  thorpej }
    571  1.2  thorpej 
    572  1.2  thorpej int
    573  1.2  thorpej sys_eventfd(struct lwp *l, const struct sys_eventfd_args *uap,
    574  1.2  thorpej     register_t *retval)
    575  1.2  thorpej {
    576  1.2  thorpej 	/* {
    577  1.2  thorpej 		syscallarg(unsigned int) val;
    578  1.2  thorpej 		syscallarg(int) flags;
    579  1.2  thorpej 	} */
    580  1.2  thorpej 
    581  1.2  thorpej 	return do_eventfd(l, SCARG(uap, val), SCARG(uap, flags), retval);
    582  1.2  thorpej }
    583