1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2014 Dmitry Chagin <dchagin@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 */ 27 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> 33 #include <sys/lock.h> 34 #include <sys/mutex.h> 35 #include <sys/types.h> 36 #include <sys/user.h> 37 #include <sys/fcntl.h> 38 #include <sys/file.h> 39 #include <sys/filedesc.h> 40 #include <sys/filio.h> 41 #include <sys/stat.h> 42 #include <sys/errno.h> 43 #include <sys/event.h> 44 #include <sys/poll.h> 45 #include <sys/proc.h> 46 #include <sys/uio.h> 47 #include <sys/selinfo.h> 48 #include <sys/eventfd.h> 49 50 #include <security/audit/audit.h> 51 52 _Static_assert(EFD_CLOEXEC == O_CLOEXEC, "Mismatched EFD_CLOEXEC"); 53 _Static_assert(EFD_NONBLOCK == O_NONBLOCK, "Mismatched EFD_NONBLOCK"); 54 55 MALLOC_DEFINE(M_EVENTFD, "eventfd", "eventfd structures"); 56 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; 65 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 80 }; 81 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); 85 86 static struct filterops eventfd_rfiltops = { 87 .f_isfd = 1, 88 .f_detach = filt_eventfddetach, 89 .f_event = filt_eventfdread 90 }; 91 92 static struct filterops eventfd_wfiltops = { 93 .f_isfd = 1, 94 .f_detach = filt_eventfddetach, 95 .f_event = filt_eventfdwrite 96 }; 97 98 struct eventfd { 99 eventfd_t efd_count; 100 uint32_t efd_flags; 101 struct selinfo efd_sel; 102 struct mtx efd_lock; 103 }; 104 105 int 106 eventfd_create_file(struct thread *td, struct file *fp, uint32_t initval, 107 int flags) 108 { 109 struct eventfd *efd; 110 int fflags; 111 112 AUDIT_ARG_FFLAGS(flags); 113 AUDIT_ARG_VALUE(initval); 114 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); 120 121 fflags = FREAD | FWRITE; 122 if ((flags & EFD_NONBLOCK) != 0) 123 fflags |= FNONBLOCK; 124 finit(fp, fflags, DTYPE_EVENTFD, efd, &eventfdops); 125 126 return (0); 127 } 128 129 static int 130 eventfd_close(struct file *fp, struct thread *td) 131 { 132 struct eventfd *efd; 133 134 efd = fp->f_data; 135 seldrain(&efd->efd_sel); 136 knlist_destroy(&efd->efd_sel.si_note); 137 mtx_destroy(&efd->efd_lock); 138 free(efd, M_EVENTFD); 139 return (0); 140 } 141 142 static int 143 eventfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 144 int flags, struct thread *td) 145 { 146 struct eventfd *efd; 147 eventfd_t count; 148 int error; 149 150 if (uio->uio_resid < sizeof(eventfd_t)) 151 return (EINVAL); 152 153 error = 0; 154 efd = fp->f_data; 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); 159 return (EAGAIN); 160 } 161 error = mtx_sleep(&efd->efd_count, &efd->efd_lock, PCATCH, 162 "efdrd", 0); 163 } 164 if (error == 0) { 165 MPASS(efd->efd_count > 0); 166 if ((efd->efd_flags & EFD_SEMAPHORE) != 0) { 167 count = 1; 168 --efd->efd_count; 169 } else { 170 count = efd->efd_count; 171 efd->efd_count = 0; 172 } 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); 178 } else 179 mtx_unlock(&efd->efd_lock); 180 181 return (error); 182 } 183 184 static int 185 eventfd_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 186 int flags, struct thread *td) 187 { 188 struct eventfd *efd; 189 eventfd_t count; 190 int error; 191 192 if (uio->uio_resid < sizeof(eventfd_t)) 193 return (EINVAL); 194 195 error = uiomove(&count, sizeof(eventfd_t), uio); 196 if (error != 0) 197 return (error); 198 if (count == UINT64_MAX) 199 return (EINVAL); 200 201 efd = fp->f_data; 202 mtx_lock(&efd->efd_lock); 203 retry: 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); 209 return (EAGAIN); 210 } 211 error = mtx_sleep(&efd->efd_count, &efd->efd_lock, 212 PCATCH, "efdwr", 0); 213 if (error == 0) 214 goto retry; 215 } 216 if (error == 0) { 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); 222 } 223 mtx_unlock(&efd->efd_lock); 224 225 return (error); 226 } 227 228 static int 229 eventfd_poll(struct file *fp, int events, struct ucred *active_cred, 230 struct thread *td) 231 { 232 struct eventfd *efd; 233 int revents; 234 235 efd = fp->f_data; 236 revents = 0; 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 > 241 efd->efd_count) 242 revents |= events & (POLLOUT | POLLWRNORM); 243 if (revents == 0) 244 selrecord(td, &efd->efd_sel); 245 mtx_unlock(&efd->efd_lock); 246 247 return (revents); 248 } 249 250 static int 251 eventfd_kqfilter(struct file *fp, struct knote *kn) 252 { 253 struct eventfd *efd = fp->f_data; 254 255 mtx_lock(&efd->efd_lock); 256 switch (kn->kn_filter) { 257 case EVFILT_READ: 258 kn->kn_fop = &eventfd_rfiltops; 259 break; 260 case EVFILT_WRITE: 261 kn->kn_fop = &eventfd_wfiltops; 262 break; 263 default: 264 mtx_unlock(&efd->efd_lock); 265 return (EINVAL); 266 } 267 268 kn->kn_hook = efd; 269 knlist_add(&efd->efd_sel.si_note, kn, 1); 270 mtx_unlock(&efd->efd_lock); 271 272 return (0); 273 } 274 275 static void 276 filt_eventfddetach(struct knote *kn) 277 { 278 struct eventfd *efd = kn->kn_hook; 279 280 mtx_lock(&efd->efd_lock); 281 knlist_remove(&efd->efd_sel.si_note, kn, 1); 282 mtx_unlock(&efd->efd_lock); 283 } 284 285 static int 286 filt_eventfdread(struct knote *kn, long hint) 287 { 288 struct eventfd *efd = kn->kn_hook; 289 int ret; 290 291 mtx_assert(&efd->efd_lock, MA_OWNED); 292 kn->kn_data = (int64_t)efd->efd_count; 293 ret = efd->efd_count > 0; 294 295 return (ret); 296 } 297 298 static int 299 filt_eventfdwrite(struct knote *kn, long hint) 300 { 301 struct eventfd *efd = kn->kn_hook; 302 int ret; 303 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; 307 308 return (ret); 309 } 310 311 static int 312 eventfd_ioctl(struct file *fp, u_long cmd, void *data, 313 struct ucred *active_cred, struct thread *td) 314 { 315 switch (cmd) { 316 case FIONBIO: 317 case FIOASYNC: 318 return (0); 319 } 320 321 return (ENOTTY); 322 } 323 324 static int 325 eventfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred) 326 { 327 bzero((void *)st, sizeof *st); 328 st->st_mode = S_IFIFO; 329 return (0); 330 } 331 332 static int 333 eventfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 334 { 335 struct eventfd *efd = fp->f_data; 336 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); 343 return (0); 344 } 345