2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2014 Dmitry Chagin <dchagin@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
32 #include <sys/limits.h>
34 #include <sys/mutex.h>
35 #include <sys/types.h>
37 #include <sys/fcntl.h>
39 #include <sys/filedesc.h>
40 #include <sys/filio.h>
42 #include <sys/errno.h>
43 #include <sys/event.h>
47 #include <sys/selinfo.h>
48 #include <sys/eventfd.h>
50 #include <security/audit/audit.h>
52 _Static_assert(EFD_CLOEXEC == O_CLOEXEC, "Mismatched EFD_CLOEXEC");
53 _Static_assert(EFD_NONBLOCK == O_NONBLOCK, "Mismatched EFD_NONBLOCK");
55 MALLOC_DEFINE(M_EVENTFD, "eventfd", "eventfd structures");
57 static fo_rdwr_t eventfd_read;
58 static fo_rdwr_t eventfd_write;
59 static fo_ioctl_t eventfd_ioctl;
60 static fo_poll_t eventfd_poll;
61 static fo_kqfilter_t eventfd_kqfilter;
62 static fo_stat_t eventfd_stat;
63 static fo_close_t eventfd_close;
64 static fo_fill_kinfo_t eventfd_fill_kinfo;
66 static struct fileops eventfdops = {
67 .fo_read = eventfd_read,
68 .fo_write = eventfd_write,
69 .fo_truncate = invfo_truncate,
70 .fo_ioctl = eventfd_ioctl,
71 .fo_poll = eventfd_poll,
72 .fo_kqfilter = eventfd_kqfilter,
73 .fo_stat = eventfd_stat,
74 .fo_close = eventfd_close,
75 .fo_chmod = invfo_chmod,
76 .fo_chown = invfo_chown,
77 .fo_sendfile = invfo_sendfile,
78 .fo_fill_kinfo = eventfd_fill_kinfo,
79 .fo_flags = DFLAG_PASSABLE
82 static void filt_eventfddetach(struct knote *kn);
83 static int filt_eventfdread(struct knote *kn, long hint);
84 static int filt_eventfdwrite(struct knote *kn, long hint);
86 static struct filterops eventfd_rfiltops = {
88 .f_detach = filt_eventfddetach,
89 .f_event = filt_eventfdread
92 static struct filterops eventfd_wfiltops = {
94 .f_detach = filt_eventfddetach,
95 .f_event = filt_eventfdwrite
101 struct selinfo efd_sel;
106 eventfd_create_file(struct thread *td, struct file *fp, uint32_t initval,
112 AUDIT_ARG_FFLAGS(flags);
113 AUDIT_ARG_VALUE(initval);
115 efd = malloc(sizeof(*efd), M_EVENTFD, M_WAITOK | M_ZERO);
116 efd->efd_flags = flags;
117 efd->efd_count = initval;
118 mtx_init(&efd->efd_lock, "eventfd", NULL, MTX_DEF);
119 knlist_init_mtx(&efd->efd_sel.si_note, &efd->efd_lock);
121 fflags = FREAD | FWRITE;
122 if ((flags & EFD_NONBLOCK) != 0)
124 finit(fp, fflags, DTYPE_EVENTFD, efd, &eventfdops);
130 eventfd_close(struct file *fp, struct thread *td)
135 seldrain(&efd->efd_sel);
136 knlist_destroy(&efd->efd_sel.si_note);
137 mtx_destroy(&efd->efd_lock);
138 free(efd, M_EVENTFD);
143 eventfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
144 int flags, struct thread *td)
150 if (uio->uio_resid < sizeof(eventfd_t))
155 mtx_lock(&efd->efd_lock);
156 while (error == 0 && efd->efd_count == 0) {
157 if ((fp->f_flag & FNONBLOCK) != 0) {
158 mtx_unlock(&efd->efd_lock);
161 error = mtx_sleep(&efd->efd_count, &efd->efd_lock, PCATCH,
165 MPASS(efd->efd_count > 0);
166 if ((efd->efd_flags & EFD_SEMAPHORE) != 0) {
170 count = efd->efd_count;
173 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
174 selwakeup(&efd->efd_sel);
175 wakeup(&efd->efd_count);
176 mtx_unlock(&efd->efd_lock);
177 error = uiomove(&count, sizeof(eventfd_t), uio);
179 mtx_unlock(&efd->efd_lock);
185 eventfd_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
186 int flags, struct thread *td)
192 if (uio->uio_resid < sizeof(eventfd_t))
195 error = uiomove(&count, sizeof(eventfd_t), uio);
198 if (count == UINT64_MAX)
202 mtx_lock(&efd->efd_lock);
204 if (UINT64_MAX - efd->efd_count <= count) {
205 if ((fp->f_flag & FNONBLOCK) != 0) {
206 mtx_unlock(&efd->efd_lock);
207 /* Do not not return the number of bytes written */
208 uio->uio_resid += sizeof(eventfd_t);
211 error = mtx_sleep(&efd->efd_count, &efd->efd_lock,
217 MPASS(UINT64_MAX - efd->efd_count > count);
218 efd->efd_count += count;
219 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
220 selwakeup(&efd->efd_sel);
221 wakeup(&efd->efd_count);
223 mtx_unlock(&efd->efd_lock);
229 eventfd_poll(struct file *fp, int events, struct ucred *active_cred,
237 mtx_lock(&efd->efd_lock);
238 if ((events & (POLLIN | POLLRDNORM)) != 0 && efd->efd_count > 0)
239 revents |= events & (POLLIN | POLLRDNORM);
240 if ((events & (POLLOUT | POLLWRNORM)) != 0 && UINT64_MAX - 1 >
242 revents |= events & (POLLOUT | POLLWRNORM);
244 selrecord(td, &efd->efd_sel);
245 mtx_unlock(&efd->efd_lock);
251 eventfd_kqfilter(struct file *fp, struct knote *kn)
253 struct eventfd *efd = fp->f_data;
255 mtx_lock(&efd->efd_lock);
256 switch (kn->kn_filter) {
258 kn->kn_fop = &eventfd_rfiltops;
261 kn->kn_fop = &eventfd_wfiltops;
264 mtx_unlock(&efd->efd_lock);
269 knlist_add(&efd->efd_sel.si_note, kn, 1);
270 mtx_unlock(&efd->efd_lock);
276 filt_eventfddetach(struct knote *kn)
278 struct eventfd *efd = kn->kn_hook;
280 mtx_lock(&efd->efd_lock);
281 knlist_remove(&efd->efd_sel.si_note, kn, 1);
282 mtx_unlock(&efd->efd_lock);
286 filt_eventfdread(struct knote *kn, long hint)
288 struct eventfd *efd = kn->kn_hook;
291 mtx_assert(&efd->efd_lock, MA_OWNED);
292 kn->kn_data = (int64_t)efd->efd_count;
293 ret = efd->efd_count > 0;
299 filt_eventfdwrite(struct knote *kn, long hint)
301 struct eventfd *efd = kn->kn_hook;
304 mtx_assert(&efd->efd_lock, MA_OWNED);
305 kn->kn_data = (int64_t)(UINT64_MAX - 1 - efd->efd_count);
306 ret = UINT64_MAX - 1 > efd->efd_count;
312 eventfd_ioctl(struct file *fp, u_long cmd, void *data,
313 struct ucred *active_cred, struct thread *td)
325 eventfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred)
327 bzero((void *)st, sizeof *st);
328 st->st_mode = S_IFIFO;
333 eventfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
335 struct eventfd *efd = fp->f_data;
337 kif->kf_type = KF_TYPE_EVENTFD;
338 mtx_lock(&efd->efd_lock);
339 kif->kf_un.kf_eventfd.kf_eventfd_value = efd->efd_count;
340 kif->kf_un.kf_eventfd.kf_eventfd_flags = efd->efd_flags;
341 kif->kf_un.kf_eventfd.kf_eventfd_addr = (uintptr_t)efd;
342 mtx_unlock(&efd->efd_lock);