diff --git a/sys/compat/freebsd32/freebsd32_misc.c b/sys/compat/freebsd32/freebsd32_misc.c --- a/sys/compat/freebsd32/freebsd32_misc.c +++ b/sys/compat/freebsd32/freebsd32_misc.c @@ -82,6 +82,7 @@ #include #include #include +#include #include #include #include @@ -3138,6 +3139,60 @@ return (error); } +int +freebsd32_timerfd_gettime(struct thread *td, + struct freebsd32_timerfd_gettime_args *uap) +{ + struct itimerspec curr_value; + struct itimerspec32 curr_value32; + int error; + + error = kern_timerfd_gettime(td, uap->fd, &curr_value); + if (error == 0) { + CP(curr_value, curr_value32, it_value.tv_sec); + CP(curr_value, curr_value32, it_value.tv_nsec); + CP(curr_value, curr_value32, it_interval.tv_sec); + CP(curr_value, curr_value32, it_interval.tv_nsec); + error = copyout(&curr_value32, uap->curr_value, + sizeof(curr_value32)); + } + + return (error); +} + +int +freebsd32_timerfd_settime(struct thread *td, + struct freebsd32_timerfd_settime_args *uap) +{ + struct itimerspec new_value, old_value; + struct itimerspec32 new_value32, old_value32; + int error; + + error = copyin(uap->new_value, &new_value32, sizeof(new_value32)); + if (error != 0) + return (error); + CP(new_value32, new_value, it_value.tv_sec); + CP(new_value32, new_value, it_value.tv_nsec); + CP(new_value32, new_value, it_interval.tv_sec); + CP(new_value32, new_value, it_interval.tv_nsec); + if (uap->old_value == NULL) { + error = kern_timerfd_settime(td, uap->fd, uap->flags, + &new_value, NULL); + } else { + error = kern_timerfd_settime(td, uap->fd, uap->flags, + &new_value, &old_value); + if (error == 0) { + CP(old_value, old_value32, it_value.tv_sec); + CP(old_value, old_value32, it_value.tv_nsec); + CP(old_value, old_value32, it_interval.tv_sec); + CP(old_value, old_value32, it_interval.tv_nsec); + error = copyout(&old_value32, uap->old_value, + sizeof(old_value32)); + } + } + return (error); +} + int freebsd32_clock_getcpuclockid2(struct thread *td, struct freebsd32_clock_getcpuclockid2_args *uap) diff --git a/sys/kern/sys_timerfd.c b/sys/kern/sys_timerfd.c --- a/sys/kern/sys_timerfd.c +++ b/sys/kern/sys_timerfd.c @@ -54,11 +54,6 @@ #include -#ifdef COMPAT_FREEBSD32 -#include -#include -#endif - static MALLOC_DEFINE(M_TIMERFD, "timerfd", "timerfd structures"); static struct sx timerfd_list_lock; @@ -73,28 +68,36 @@ #define TFD_CANCELED 4 /* Jumped, CANCEL_ON_SET=true. */ #define TFD_JUMPED (TFD_ZREAD | TFD_CANCELED) +/* + * One structure allocated per timerfd descriptor. + * + * Locking semantics: + * (t) locked by tfd_lock mtx + * (l) locked by timerfd_list_lock sx + * (c) const until freeing + */ struct timerfd { /* User specified. */ - struct itimerspec tfd_time; /* tfd timer */ - clockid_t tfd_clockid; /* timing base */ - int tfd_flags; /* creation flags */ - int tfd_timflags; /* timer flags */ + struct itimerspec tfd_time; /* (t) tfd timer */ + clockid_t tfd_clockid; /* (c) timing base */ + int tfd_flags; /* (c) creation flags */ + int tfd_timflags; /* (t) timer flags */ /* Used internally. */ - timerfd_t tfd_count; /* expiration count since last read */ - bool tfd_expired; /* true upon initial expiration */ - struct mtx tfd_lock; /* mtx lock */ - struct callout tfd_callout; /* expiration notification */ - struct selinfo tfd_sel; /* I/O alerts */ - struct timespec tfd_boottim; /* cached boottime */ - int tfd_jumped; /* timer jump status */ - LIST_ENTRY(timerfd) entry; /* entry in list */ + timerfd_t tfd_count; /* (t) expiration count since read */ + bool tfd_expired; /* (t) true upon initial expiration */ + struct mtx tfd_lock; /* tfd mtx lock */ + struct callout tfd_callout; /* (t) expiration notification */ + struct selinfo tfd_sel; /* (t) I/O alerts */ + struct timespec tfd_boottim; /* (t) cached boottime */ + int tfd_jumped; /* (t) timer jump status */ + LIST_ENTRY(timerfd) entry; /* (l) entry in list */ /* For stat(2). */ - ino_t tfd_ino; /* inode number */ - struct timespec tfd_atim; /* time of last read */ - struct timespec tfd_mtim; /* time of last settime */ - struct timespec tfd_birthtim; /* creation time */ + ino_t tfd_ino; /* (c) inode number */ + struct timespec tfd_atim; /* (t) time of last read */ + struct timespec tfd_mtim; /* (t) time of last settime */ + struct timespec tfd_birthtim; /* (c) creation time */ }; static void @@ -273,8 +276,13 @@ filt_timerfdread(struct knote *kn, long hint) { struct timerfd *tfd = kn->kn_hook; + timerfd_t count; + + mtx_lock(&tfd->tfd_lock); + count = tfd->tfd_count; + mtx_unlock(&tfd->tfd_lock); - return (tfd->tfd_count > 0); + return (count > 0); } static struct filterops timerfd_rfiltops = { @@ -293,7 +301,9 @@ kn->kn_fop = &timerfd_rfiltops; kn->kn_hook = tfd; - knlist_add(&tfd->tfd_sel.si_note, kn, 0); + mtx_lock(&tfd->tfd_lock); + knlist_add(&tfd->tfd_sel.si_note, kn, 1); + mtx_unlock(&tfd->tfd_lock); return (0); } @@ -308,13 +318,13 @@ sb->st_uid = fp->f_cred->cr_uid; sb->st_gid = fp->f_cred->cr_gid; sb->st_blksize = PAGE_SIZE; - mtx_lock(&tfd->tfd_lock); - sb->st_ino = tfd->tfd_ino; sb->st_atim = tfd->tfd_atim; sb->st_mtim = tfd->tfd_mtim; - sb->st_birthtim = tfd->tfd_birthtim; mtx_unlock(&tfd->tfd_lock); + sb->st_ctim = sb->st_mtim; + sb->st_ino = tfd->tfd_ino; + sb->st_birthtim = tfd->tfd_birthtim; return (0); } @@ -342,15 +352,12 @@ timerfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) { - struct timerfd *tfd = fp->f_data; kif->kf_type = KF_TYPE_TIMERFD; - mtx_lock(&tfd->tfd_lock); kif->kf_un.kf_timerfd.kf_timerfd_clockid = tfd->tfd_clockid; kif->kf_un.kf_timerfd.kf_timerfd_flags = tfd->tfd_flags; kif->kf_un.kf_timerfd.kf_timerfd_addr = (uintptr_t)tfd; - mtx_unlock(&tfd->tfd_lock); return (0); } @@ -376,6 +383,7 @@ { struct timespec curr_value; + mtx_assert(&tfd->tfd_lock, MA_OWNED); *old_value = tfd->tfd_time; if (timespecisset(&tfd->tfd_time.it_value)) { nanouptime(&curr_value); @@ -445,7 +453,7 @@ tfd->tfd_clockid = (clockid_t)clockid; tfd->tfd_flags = flags; tfd->tfd_ino = alloc_unr64(&tfdino_unr); - mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF); + mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF | MTX_RECURSE); callout_init_mtx(&tfd->tfd_callout, &tfd->tfd_lock, 0); knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock); timerfd_getboottime(&tfd->tfd_boottim); @@ -472,11 +480,11 @@ error = fget(td, fd, &cap_write_rights, &fp); if (error != 0) return (error); - tfd = fp->f_data; - if (tfd == NULL || fp->f_type != DTYPE_TIMERFD) { + if (fp->f_type != DTYPE_TIMERFD) { fdrop(fp, td); return (EINVAL); } + tfd = fp->f_data; mtx_lock(&tfd->tfd_lock); timerfd_curval(tfd, curr_value); @@ -504,11 +512,11 @@ error = fget(td, fd, &cap_write_rights, &fp); if (error != 0) return (error); - tfd = fp->f_data; - if (tfd == NULL || fp->f_type != DTYPE_TIMERFD) { + if (fp->f_type != DTYPE_TIMERFD) { fdrop(fp, td); return (EINVAL); } + tfd = fp->f_data; mtx_lock(&tfd->tfd_lock); getnanotime(&tfd->tfd_mtim); @@ -589,59 +597,3 @@ } return (error); } - -#ifdef COMPAT_FREEBSD32 -int -freebsd32_timerfd_gettime(struct thread *td, - struct freebsd32_timerfd_gettime_args *uap) -{ - struct itimerspec curr_value; - struct itimerspec32 curr_value32; - int error; - - error = kern_timerfd_gettime(td, uap->fd, &curr_value); - if (error == 0) { - CP(curr_value, curr_value32, it_value.tv_sec); - CP(curr_value, curr_value32, it_value.tv_nsec); - CP(curr_value, curr_value32, it_interval.tv_sec); - CP(curr_value, curr_value32, it_interval.tv_nsec); - error = copyout(&curr_value32, uap->curr_value, - sizeof(curr_value32)); - } - - return (error); -} - -int -freebsd32_timerfd_settime(struct thread *td, - struct freebsd32_timerfd_settime_args *uap) -{ - struct itimerspec new_value, old_value; - struct itimerspec32 new_value32, old_value32; - int error; - - error = copyin(uap->new_value, &new_value32, sizeof(new_value32)); - if (error != 0) - return (error); - CP(new_value32, new_value, it_value.tv_sec); - CP(new_value32, new_value, it_value.tv_nsec); - CP(new_value32, new_value, it_interval.tv_sec); - CP(new_value32, new_value, it_interval.tv_nsec); - if (uap->old_value == NULL) { - error = kern_timerfd_settime(td, uap->fd, uap->flags, - &new_value, NULL); - } else { - error = kern_timerfd_settime(td, uap->fd, uap->flags, - &new_value, &old_value); - if (error == 0) { - CP(old_value, old_value32, it_value.tv_sec); - CP(old_value, old_value32, it_value.tv_nsec); - CP(old_value, old_value32, it_interval.tv_sec); - CP(old_value, old_value32, it_interval.tv_nsec); - error = copyout(&old_value32, uap->old_value, - sizeof(old_value32)); - } - } - return (error); -} -#endif diff --git a/sys/sys/timerfd.h b/sys/sys/timerfd.h --- a/sys/sys/timerfd.h +++ b/sys/sys/timerfd.h @@ -30,7 +30,6 @@ #include #include -#include #include typedef uint64_t timerfd_t; @@ -54,6 +53,8 @@ #else /* _KERNEL */ +struct thread; + int kern_timerfd_create(struct thread *td, int clockid, int flags); int kern_timerfd_gettime(struct thread *td, int fd, struct itimerspec *curr_value);