From nobody Sun Sep 17 15:04: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 4RpWRP4fg6z4t1p1; Sun, 17 Sep 2023 15:04:17 +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 4RpWRP3jFtz4FbQ; Sun, 17 Sep 2023 15:04:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694963057; 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=Hoz7WOQin++lWOVY83TNamFRf39RPg3GOjYWXnmzQbY=; b=vwAWayHjL/5JxkCnG2QlwYz6SmDz0OJoKUvjeMRq2eveERS4bKGvdk5SHQ6XRjOLNppSrk B+xxur37kPaNxrjG6M9r3vthtKirZykQbTWqlh404X3NISzK7iuHwB+U0VFvwafoliqhHp q71tDZQKkON3FvEALy/zIXTWAmsDJphFCeEtpJL8gqVhynSeZGPwZmN0aveHDa0mGoDU5u h1Dn1r3ntlxWDfv0k3e9lQEhC0zD1MdNXA5kQacKItDVlZvfDSpjCRe8OTFEI2dPv5bNDC 2KYug43BNWLwCPZkGVnt3Mi1yz/U7uuKfvb4zfImSRauW5eBQr9B+OCSstV4EQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1694963057; a=rsa-sha256; cv=none; b=oSMMfFvNLUqiKbcPLqXoROoivIVKKThKqXHehBzQHJvs/+X5iQuhi8Nko5OhTRIvOAyEGE NvbKtRTuRZQJQE1sj2i7NpbfjyElmyWsPig2YwJ2RtaLi3nF4r1cHha33cevqGAxWkw2Oa rlYzutTJdOTyBzt/xHQGg6pH01yZdhxf6pPLj4/3Rk0sfJ35uDbZzc6t4gOtcMPaRJDs37 /VZoijW7v25nMWIPfxzUYmEz7K7wH7K2LLBoXDoufDwf3kBZh5GaOauSwXUSJjAY8wFpTL 2y2alHCEuV/oUbcdjx8pP67f7zA6eRubTn2tWgexeOFH+D2I+YTeLsogLhQO9w== 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=1694963057; 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=Hoz7WOQin++lWOVY83TNamFRf39RPg3GOjYWXnmzQbY=; b=oUsN0436jQfN2h3mW//QjmAgOw6tDpJevLqIC2vRzSAHfpMo+6CIr+l4Y4q6imwiWwGfBq 89CEAgWGMjcXXPPIFQv2fnfkzX4bhPqyLwovv2uBpOWO8ZPYxdFSEtUbmed9a04rR/sbRS B1nnkiNZ3gnURJ/uGYjZLPScrd62F4kmMOiPWacJizi4XWdHnc411DBZjHIPSrXrJkCv2b phMyZmwF6DvK5n3r/VZ4ENJF2H8BfzVC5wpo5Pv3VRgJvsbLVqFLCNUCA2Y2VMUyo2iG3C 9DzH+G3NCEzmD/hE1L9qGeTU0jK9zIyH1hWf41hfyKGCVIe6HQ6OyL+1c8vp5w== 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 4RpWRP2FQcztXf; Sun, 17 Sep 2023 15:04: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 38HF4HMD072251; Sun, 17 Sep 2023 15:04:17 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38HF4HkQ072248; Sun, 17 Sep 2023 15:04:17 GMT (envelope-from git) Date: Sun, 17 Sep 2023 15:04:17 GMT Message-Id: <202309171504.38HF4HkQ072248@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: d00e6d877455 - stable/14 - 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/stable/14 X-Git-Reftype: branch X-Git-Commit: d00e6d87745528be7acf58f39eaa148d8cb6c7c2 Auto-Submitted: auto-generated The branch stable/14 has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=d00e6d87745528be7acf58f39eaa148d8cb6c7c2 commit d00e6d87745528be7acf58f39eaa148d8cb6c7c2 Author: Jake Freeland AuthorDate: 2023-09-17 15:01:46 +0000 Commit: Warner Losh CommitDate: 2023-09-17 15:01:46 +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) --- 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);