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