From nobody Thu Sep 28 22:28:17 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RxSmf1k6Lz4vhWS; Thu, 28 Sep 2023 22:28:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RxSmf0Bj7z3QqZ; Thu, 28 Sep 2023 22:28:18 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695940098; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=r5LSzKWn6FIxcGeF280lo5o60SaxDEBzcy19BezrEpg=; b=kaQsfliLTIBIQqVNoKOdYPnsR7Ro2Pv0esFBRmpSL0+UDDS5wp4Wh1krqaOHjJdevHgNf6 RX57vtsOavVqBFVM9DDPH9GJJGxSjyUNhkJaOPGQD4aHh1gI+FqYaTyEpFOLIWV/cVs3Gn NPlkLP4nLkRoXmzPlWYijooFVc2RKL1IyT6e6EGDF8pU8F7d7oTnb4vgqOpvRe5MvUbTzZ dJJqW9D9meTWqxxZydQxyrHpmkNlk5yJwJ/On4Vcwmj91cuykJou7gCEkdo0sCbKhKks8Y 8Ygb8bMI9a9YNj8DADRNrR9Vzs46H6DYofK32h4Jx8f+FkrVF9ChpXn4Mi1VoQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695940098; a=rsa-sha256; cv=none; b=HkbrBC1UQPP7cCRF/fVjWLNlqyGK+ba9U8Q6/hg+rG5h847jnWyh6Z4yz1K7uvOFuMJJo8 4KB1Pi2vyQWxZWaLnvphHz+31WLm1GqbbAczjZyTQKP4Lq54vfY7wdoVo89BEcmKlmXMBB 0Ahx2mmJdIvGn4AikQ0Y2B8iLNYGyv6yMohi8Fzg2BDmTvgcVQX5KhV0MdyY3i2raZ1p4X rO40ka705lQgM1+QedyUFTviuccHvLg0dShaQYyv6B3gNAldOENPzskh16KXYJlI9u3wnj 0X24341NTtfROXgMWh5FfDUmmZykZWphZ53aFxi+mzTFivnL9K1G2/pqQ2bwuQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695940098; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=r5LSzKWn6FIxcGeF280lo5o60SaxDEBzcy19BezrEpg=; b=lxITpE3NiGVISue5EL1n3uuqKvSg+cpnJmOp5Yb1voY2whIftDQL8XeyrsZ8MCtslqcuB/ ZNwNfHg/LgWFMmk19V12n/b8Xw9UbLpeLNEPXEU04YzrsFau9TIUz/bX9QJ2e+9X+ejREY f/L/J5SfofxVkOOkrjSpbUFSlBNhNze9Q5l5J01x5e9XMoOl/8Q3qRJmcwPVziWpWgCROX qC+OIf5I3Gw8a0xs9pV9JIiFEBr6f50YzY3QNlxbXhtcdJKUvEJ/u+xrsQSi49BDKlb5Y1 jA9JEZfpieo3Kq6euYGUNBH5TBd4dswc97byDhaburur85hxjd8thK4AF6Ua7Q== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RxSmd65sTzfx6; Thu, 28 Sep 2023 22:28:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38SMSHFg074044; Thu, 28 Sep 2023 22:28:17 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38SMSH86074041; Thu, 28 Sep 2023 22:28:17 GMT (envelope-from git) Date: Thu, 28 Sep 2023 22:28:17 GMT Message-Id: <202309282228.38SMSH86074041@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Warner Losh Subject: git: aebc51ee4791 - releng/14.0 - timerfd: Define a locking regime List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/releng/14.0 X-Git-Reftype: branch X-Git-Commit: aebc51ee479104bc92f5bfa97d1b970637cb24c5 Auto-Submitted: auto-generated The branch releng/14.0 has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=aebc51ee479104bc92f5bfa97d1b970637cb24c5 commit aebc51ee479104bc92f5bfa97d1b970637cb24c5 Author: Jake Freeland AuthorDate: 2023-09-17 15:01:46 +0000 Commit: Warner Losh CommitDate: 2023-09-28 22:26:23 +0000 timerfd: Define a locking regime Define a locking regime for the members of struct timerfd and document it so future code can follow the standard. The lock legend can be found in a comment above struct timerfd. Additionally, * Add assertions based on locking regime. * Fill kn_data with the expiration count when EVFILT_READ is triggered. * Report st_ctim for stat(2). * Check if file has f_type == DTYPE_TIMERFD before assigning timerfd pointer to f_data. MFC After: 3 days Reviewed by: imp, kib, markj Differential Revision: https://reviews.freebsd.org/D41600 (cherry picked from commit a1f506156c4db885d3cc177c93e9c8a28d535d30) (cherry picked from commit d00e6d87745528be7acf58f39eaa148d8cb6c7c2) Approved-by: re (cperciva) --- sys/kern/sys_timerfd.c | 61 +++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/sys/kern/sys_timerfd.c b/sys/kern/sys_timerfd.c index e4d2f10f28ef..a81b5ad0fade 100644 --- a/sys/kern/sys_timerfd.c +++ b/sys/kern/sys_timerfd.c @@ -73,28 +73,36 @@ static struct unrhdr64 tfdino_unr; #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 @@ -109,6 +117,7 @@ static inline void timerfd_getboottime(struct timespec *ts) { struct timeval tv; + getboottime(&tv); TIMEVAL_TO_TIMESPEC(&tv, ts); } @@ -274,6 +283,8 @@ filt_timerfdread(struct knote *kn, long hint) { struct timerfd *tfd = kn->kn_hook; + mtx_assert(&tfd->tfd_lock, MA_OWNED); + kn->kn_data = (int64_t)tfd->tfd_count; return (tfd->tfd_count > 0); } @@ -308,13 +319,13 @@ timerfd_stat(struct file *fp, struct stat *sb, struct ucred *active_cred) 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 +353,12 @@ static int 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 +384,7 @@ timerfd_curval(struct timerfd *tfd, struct itimerspec *old_value) { 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); @@ -472,11 +481,11 @@ kern_timerfd_gettime(struct thread *td, int fd, struct itimerspec *curr_value) 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 +513,11 @@ kern_timerfd_settime(struct thread *td, int fd, int flags, 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);