Module Name: src Committed By: martin Date: Wed Nov 20 14:01:59 UTC 2024
Modified Files: src/sys/compat/netbsd32 [netbsd-9]: netbsd32_ioctl.c src/sys/ddb [netbsd-9]: db_xxx.c src/sys/kern [netbsd-9]: kern_descrip.c kern_event.c kern_sig.c subr_exec_fd.c sys_aio.c sys_descrip.c sys_select.c uipc_socket2.c uipc_usrreq.c src/sys/miscfs/fdesc [netbsd-9]: fdesc_vnops.c src/sys/miscfs/procfs [netbsd-9]: procfs_vnops.c Log Message: Pull up following revision(s) (requested by riastradh in ticket #1921): sys/kern/kern_event.c: revision 1.106 sys/kern/sys_select.c: revision 1.51 sys/kern/subr_exec_fd.c: revision 1.10 sys/kern/sys_aio.c: revision 1.46 sys/kern/kern_descrip.c: revision 1.244 sys/kern/kern_descrip.c: revision 1.245 sys/ddb/db_xxx.c: revision 1.72 sys/ddb/db_xxx.c: revision 1.73 sys/miscfs/fdesc/fdesc_vnops.c: revision 1.132 sys/kern/uipc_usrreq.c: revision 1.195 sys/kern/sys_descrip.c: revision 1.36 sys/kern/uipc_usrreq.c: revision 1.196 sys/kern/uipc_socket2.c: revision 1.135 sys/kern/uipc_socket2.c: revision 1.136 sys/kern/kern_sig.c: revision 1.383 sys/kern/kern_sig.c: revision 1.384 sys/compat/netbsd32/netbsd32_ioctl.c: revision 1.107 sys/miscfs/procfs/procfs_vnops.c: revision 1.208 sys/kern/subr_exec_fd.c: revision 1.9 sys/kern/kern_descrip.c: revision 1.252 (all via patch) Load struct filedesc::fd_dt with atomic_load_consume. Exceptions: when fd_refcnt <= 1, or when holding fd_lock. While here: - Restore KASSERT(mutex_owned(&fdp->fd_lock)) in fd_unused. => This is used only in fd_close and fd_abort, where it holds. - Move bounds check assertion in fd_putfile to where it matters. - Store fd_dt with atomic_store_release. - Move load of fd_dt under lock in knote_fdclose. - Omit membar_consumer in fdesc_readdir. => atomic_load_consume serves the same purpose now. => Was needed only on alpha anyway. Load struct fdfile::ff_file with atomic_load_consume. Exceptions: when we're only testing whether it's there, not about to dereference it. Note: We do not use atomic_store_release to set it because the preceding mutex_exit should be enough. (That said, it's not clear the mutex_enter/exit is needed unless refcnt > 0 already, in which case maybe it would be a win to switch from the membar implied by mutex_enter to the membar implied by atomic_store_release -- which I would generally expect to be much cheaper. And a little clearer without a long comment.) kern_descrip.c: Fix membars around reference count decrement. In general, the `last one out hit the lights' style of reference counting (as opposed to the `whoever's destroying must wait for pending users to finish' style) requires memory barriers like so: ... usage of resources associated with object ... membar_release(); if (atomic_dec_uint_nv(&obj->refcnt) != 0) return; membar_acquire(); ... freeing of resources associated with object ... This way, all usage happens-before all freeing. This fixes several errors: - fd_close failed to ensure whatever its caller did would happen-before the freeing, in the case where another thread is concurrently trying to close the fd (ff->ff_file == NULL). Fix: Add membar_release before atomic_dec_uint(&ff->ff_refcnt) in that branch. - fd_close failed to ensure all loads its caller had issued will have happened-before the freeing, in the case where the fd is still in use by another thread (fdp->fd_refcnt > 1 and ff->ff_refcnt-- > 0). Fix: Change membar_producer to membar_release before atomic_dec_uint(&ff->ff_refcnt). - fd_close failed to ensure that any usage of fp by other callers would happen-before any freeing it does. Fix: Add membar_acquire after atomic_dec_uint_nv(&ff->ff_refcnt). - fd_free failed to ensure that any usage of fdp by other callers would happen-before any freeing it does. Fix: Add membar_acquire after atomic_dec_uint_nv(&fdp->fd_refcnt). While here, change membar_exit -> membar_release. No semantic change, just updating away from the legacy API. To generate a diff of this commit: cvs rdiff -u -r1.103.2.2 -r1.103.2.3 src/sys/compat/netbsd32/netbsd32_ioctl.c cvs rdiff -u -r1.71 -r1.71.22.1 src/sys/ddb/db_xxx.c cvs rdiff -u -r1.243.4.2 -r1.243.4.3 src/sys/kern/kern_descrip.c cvs rdiff -u -r1.104.4.2 -r1.104.4.3 src/sys/kern/kern_event.c cvs rdiff -u -r1.364.2.9 -r1.364.2.10 src/sys/kern/kern_sig.c cvs rdiff -u -r1.8 -r1.8.4.1 src/sys/kern/subr_exec_fd.c cvs rdiff -u -r1.44 -r1.44.4.1 src/sys/kern/sys_aio.c cvs rdiff -u -r1.33 -r1.33.2.1 src/sys/kern/sys_descrip.c cvs rdiff -u -r1.46 -r1.46.2.1 src/sys/kern/sys_select.c cvs rdiff -u -r1.134.2.2 -r1.134.2.3 src/sys/kern/uipc_socket2.c cvs rdiff -u -r1.194.2.1 -r1.194.2.2 src/sys/kern/uipc_usrreq.c cvs rdiff -u -r1.130.4.1 -r1.130.4.2 src/sys/miscfs/fdesc/fdesc_vnops.c cvs rdiff -u -r1.206.4.2 -r1.206.4.3 src/sys/miscfs/procfs/procfs_vnops.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/compat/netbsd32/netbsd32_ioctl.c diff -u src/sys/compat/netbsd32/netbsd32_ioctl.c:1.103.2.2 src/sys/compat/netbsd32/netbsd32_ioctl.c:1.103.2.3 --- src/sys/compat/netbsd32/netbsd32_ioctl.c:1.103.2.2 Mon Nov 18 17:38:03 2024 +++ src/sys/compat/netbsd32/netbsd32_ioctl.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: netbsd32_ioctl.c,v 1.103.2.2 2024/11/18 17:38:03 martin Exp $ */ +/* $NetBSD: netbsd32_ioctl.c,v 1.103.2.3 2024/11/20 14:01:59 martin Exp $ */ /* * Copyright (c) 1998, 2001 Matthew R. Green @@ -31,13 +31,14 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: netbsd32_ioctl.c,v 1.103.2.2 2024/11/18 17:38:03 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: netbsd32_ioctl.c,v 1.103.2.3 2024/11/20 14:01:59 martin Exp $"); #if defined(_KERNEL_OPT) #include "opt_ntp.h" #endif #include <sys/param.h> +#include <sys/atomic.h> #include <sys/systm.h> #include <sys/filedesc.h> #include <sys/ioctl.h> Index: src/sys/ddb/db_xxx.c diff -u src/sys/ddb/db_xxx.c:1.71 src/sys/ddb/db_xxx.c:1.71.22.1 --- src/sys/ddb/db_xxx.c:1.71 Fri Feb 27 00:47:30 2015 +++ src/sys/ddb/db_xxx.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: db_xxx.c,v 1.71 2015/02/27 00:47:30 ozaki-r Exp $ */ +/* $NetBSD: db_xxx.c,v 1.71.22.1 2024/11/20 14:01:59 martin Exp $ */ /* * Copyright (c) 1982, 1986, 1989, 1991, 1993 @@ -37,7 +37,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.71 2015/02/27 00:47:30 ozaki-r Exp $"); +__KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.71.22.1 2024/11/20 14:01:59 martin Exp $"); #ifdef _KERNEL_OPT #include "opt_kgdb.h" @@ -50,6 +50,7 @@ __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1 #endif #include <sys/param.h> +#include <sys/atomic.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/proc.h> @@ -173,12 +174,12 @@ db_show_files_cmd(db_expr_t addr, bool h p = (struct proc *) (uintptr_t) addr; fdp = p->p_fd; - dt = fdp->fd_dt; + dt = atomic_load_consume(&fdp->fd_dt); for (i = 0; i < dt->dt_nfiles; i++) { if ((ff = dt->dt_ff[i]) == NULL) continue; - fp = ff->ff_file; + fp = atomic_load_consume(&ff->ff_file); /* Only look at vnodes... */ if (fp != NULL && fp->f_type == DTYPE_VNODE Index: src/sys/kern/kern_descrip.c diff -u src/sys/kern/kern_descrip.c:1.243.4.2 src/sys/kern/kern_descrip.c:1.243.4.3 --- src/sys/kern/kern_descrip.c:1.243.4.2 Sun Nov 17 13:27:41 2024 +++ src/sys/kern/kern_descrip.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_descrip.c,v 1.243.4.2 2024/11/17 13:27:41 martin Exp $ */ +/* $NetBSD: kern_descrip.c,v 1.243.4.3 2024/11/20 14:01:59 martin Exp $ */ /*- * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. @@ -70,7 +70,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.243.4.2 2024/11/17 13:27:41 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.243.4.3 2024/11/20 14:01:59 martin Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -98,6 +98,18 @@ __KERNEL_RCSID(0, "$NetBSD: kern_descrip #include <sys/ktrace.h> /* + * XXX netbsd-9 lacks membar_release (rw/w) and membar_acquire (r/rw) + * -- simulate with membar_exit (legacy name for rw/w) and membar_sync + * (rw/rw) + */ +#ifndef membar_release +#define membar_release membar_exit +#endif +#ifndef membar_acquire +#define membar_acquire membar_sync +#endif + +/* * A list (head) of open files, counter, and lock protecting them. */ struct filelist filehead __cacheline_aligned; @@ -186,7 +198,7 @@ fd_isused(filedesc_t *fdp, unsigned fd) { u_int off = fd >> NDENTRYSHIFT; - KASSERT(fd < fdp->fd_dt->dt_nfiles); + KASSERT(fd < atomic_load_consume(&fdp->fd_dt)->dt_nfiles); return (fdp->fd_lomap[off] & (1U << (fd & NDENTRYMASK))) != 0; } @@ -201,6 +213,8 @@ fd_checkmaps(filedesc_t *fdp) fdtab_t *dt; u_int fd; + KASSERT(fdp->fd_refcnt <= 1 || mutex_owned(&fdp->fd_lock)); + dt = fdp->fd_dt; if (fdp->fd_refcnt == -1) { /* @@ -324,13 +338,7 @@ fd_unused(filedesc_t *fdp, unsigned fd) ff = fdp->fd_dt->dt_ff[fd]; - /* - * Don't assert the lock is held here, as we may be copying - * the table during exec() and it is not needed there. - * procfs and sysctl are locked out by proc::p_reflock. - * - * KASSERT(mutex_owned(&fdp->fd_lock)); - */ + KASSERT(mutex_owned(&fdp->fd_lock)); KASSERT(ff != NULL); KASSERT(ff->ff_file == NULL); KASSERT(ff->ff_allocated); @@ -373,7 +381,7 @@ fd_getfile(unsigned fd) * We are doing this unlocked. See fd_tryexpand(). */ fdp = curlwp->l_fd; - dt = fdp->fd_dt; + dt = atomic_load_consume(&fdp->fd_dt); if (__predict_false(fd >= dt->dt_nfiles)) { return NULL; } @@ -407,7 +415,7 @@ fd_getfile(unsigned fd) * If the file is not open or is being closed then put the * reference back. */ - fp = ff->ff_file; + fp = atomic_load_consume(&ff->ff_file); if (__predict_true(fp != NULL)) { return fp; } @@ -426,9 +434,9 @@ fd_putfile(unsigned fd) u_int u, v; fdp = curlwp->l_fd; - ff = fdp->fd_dt->dt_ff[fd]; + KASSERT(fd < atomic_load_consume(&fdp->fd_dt)->dt_nfiles); + ff = atomic_load_consume(&fdp->fd_dt)->dt_ff[fd]; - KASSERT(fd < fdp->fd_dt->dt_nfiles); KASSERT(ff != NULL); KASSERT((ff->ff_refcnt & FR_MASK) > 0); KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]); @@ -455,7 +463,7 @@ fd_putfile(unsigned fd) * CPU. */ #ifndef __HAVE_ATOMIC_AS_MEMBAR - membar_exit(); + membar_release(); #endif /* @@ -561,7 +569,7 @@ fd_getfile2(proc_t *p, unsigned fd) mutex_exit(&fdp->fd_lock); return NULL; } - if ((fp = ff->ff_file) == NULL) { + if ((fp = atomic_load_consume(&ff->ff_file)) == NULL) { mutex_exit(&fdp->fd_lock); return NULL; } @@ -593,18 +601,22 @@ fd_close(unsigned fd) l = curlwp; p = l->l_proc; fdp = l->l_fd; - ff = fdp->fd_dt->dt_ff[fd]; + ff = atomic_load_consume(&fdp->fd_dt)->dt_ff[fd]; KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]); mutex_enter(&fdp->fd_lock); KASSERT((ff->ff_refcnt & FR_MASK) > 0); - if (__predict_false(ff->ff_file == NULL)) { + fp = atomic_load_consume(&ff->ff_file); + if (__predict_false(fp == NULL)) { /* * Another user of the file is already closing, and is * waiting for other users of the file to drain. Release * our reference, and wake up the closer. */ +#ifndef __HAVE_ATOMIC_AS_MEMBAR + membar_release(); +#endif atomic_dec_uint(&ff->ff_refcnt); cv_broadcast(&ff->ff_closing); mutex_exit(&fdp->fd_lock); @@ -624,7 +636,6 @@ fd_close(unsigned fd) * will prevent them from adding additional uses to this file * while we are closing it. */ - fp = ff->ff_file; ff->ff_file = NULL; ff->ff_exclose = false; @@ -641,9 +652,12 @@ fd_close(unsigned fd) } else { /* Multi threaded. */ #ifndef __HAVE_ATOMIC_AS_MEMBAR - membar_producer(); + membar_release(); #endif refcnt = atomic_dec_uint_nv(&ff->ff_refcnt); +#ifndef __HAVE_ATOMIC_AS_MEMBAR + membar_acquire(); +#endif } if (__predict_false(refcnt != 0)) { /* @@ -756,7 +770,7 @@ fd_dup2(file_t *fp, unsigned newfd, int * Ensure there are enough slots in the descriptor table, * and allocate an fdfile_t up front in case we need it. */ - while (newfd >= fdp->fd_dt->dt_nfiles) { + while (newfd >= atomic_load_consume(&fdp->fd_dt)->dt_nfiles) { fd_tryexpand(curproc); } ff = pool_cache_get(fdfile_cache, PR_WAITOK); @@ -1003,7 +1017,7 @@ fd_tryexpand(proc_t *p) fdp = p->p_fd; newhimap = NULL; newlomap = NULL; - oldnfiles = fdp->fd_dt->dt_nfiles; + oldnfiles = atomic_load_consume(&fdp->fd_dt)->dt_nfiles; if (oldnfiles < NDEXTENT) numfiles = NDEXTENT; @@ -1070,8 +1084,7 @@ fd_tryexpand(proc_t *p) * All other modifications must become globally visible before * the change to fd_dt. See fd_getfile(). */ - membar_producer(); - fdp->fd_dt = newdt; + atomic_store_release(&fdp->fd_dt, newdt); KASSERT(newdt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]); fd_checkmaps(fdp); mutex_exit(&fdp->fd_lock); @@ -1138,6 +1151,7 @@ fd_affix(proc_t *p, file_t *fp, unsigned { fdfile_t *ff; filedesc_t *fdp; + fdtab_t *dt; KASSERT(p == curproc || p == &proc0); @@ -1152,10 +1166,12 @@ fd_affix(proc_t *p, file_t *fp, unsigned * The memory barriers provided by lock activity in this routine * ensure that any updates to the file structure become globally * visible before the file becomes visible to other LWPs in the - * current process. + * current process; otherwise we would set ff->ff_file with + * atomic_store_release(&ff->ff_file, fp) at the bottom. */ fdp = p->p_fd; - ff = fdp->fd_dt->dt_ff[fd]; + dt = atomic_load_consume(&fdp->fd_dt); + ff = dt->dt_ff[fd]; KASSERT(ff != NULL); KASSERT(ff->ff_file == NULL); @@ -1179,7 +1195,7 @@ fd_abort(proc_t *p, file_t *fp, unsigned KASSERT(p == curproc || p == &proc0); fdp = p->p_fd; - ff = fdp->fd_dt->dt_ff[fd]; + ff = atomic_load_consume(&fdp->fd_dt)->dt_ff[fd]; ff->ff_exclose = false; KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]); @@ -1463,7 +1479,8 @@ fd_copy(void) KASSERT(i >= NDFDFILE || *nffp == (fdfile_t *)newfdp->fd_dfdfile[i]); ff = *ffp; - if (ff == NULL || (fp = ff->ff_file) == NULL) { + if (ff == NULL || + (fp = atomic_load_consume(&ff->ff_file)) == NULL) { /* Descriptor unused, or descriptor half open. */ KASSERT(!fd_isused(newfdp, i)); continue; @@ -1525,15 +1542,19 @@ fd_free(void) filedesc_t * const fdp = l->l_fd; const bool noadvlock = (l->l_proc->p_flag & PK_ADVLOCK) == 0; - KASSERT(fdp->fd_dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]); + KASSERT(atomic_load_consume(&fdp->fd_dt)->dt_ff[0] == + (fdfile_t *)fdp->fd_dfdfile[0]); KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE); KASSERT(fdp->fd_dtbuiltin.dt_link == NULL); #ifndef __HAVE_ATOMIC_AS_MEMBAR - membar_exit(); + membar_release(); #endif if (atomic_dec_uint_nv(&fdp->fd_refcnt) > 0) return; +#ifndef __HAVE_ATOMIC_AS_MEMBAR + membar_acquire(); +#endif /* * Close any files that the process holds open. @@ -1549,7 +1570,7 @@ fd_free(void) ff == (fdfile_t *)fdp->fd_dfdfile[fd]); if (ff == NULL) continue; - if ((fp = ff->ff_file) != NULL) { + if ((fp = atomic_load_consume(&ff->ff_file)) != NULL) { /* * Must use fd_close() here if there is * a reference from kqueue or we might have posix @@ -1657,7 +1678,7 @@ fd_dupopen(int old, int *newp, int mode, return EBADF; } fdp = curlwp->l_fd; - dt = fdp->fd_dt; + dt = atomic_load_consume(&fdp->fd_dt); ff = dt->dt_ff[old]; /* @@ -1730,7 +1751,7 @@ fd_closeexec(void) return; } fdp->fd_exclose = false; - dt = fdp->fd_dt; + dt = atomic_load_consume(&fdp->fd_dt); for (fd = 0; fd <= fdp->fd_lastfile; fd++) { if ((ff = dt->dt_ff[fd]) == NULL) { @@ -1796,7 +1817,7 @@ void fd_set_exclose(struct lwp *l, int fd, bool exclose) { filedesc_t *fdp = l->l_fd; - fdfile_t *ff = fdp->fd_dt->dt_ff[fd]; + fdfile_t *ff = atomic_load_consume(&fdp->fd_dt)->dt_ff[fd]; ff->ff_exclose = exclose; if (exclose) @@ -1976,7 +1997,7 @@ sysctl_file_marker_reset(void) if ((ff = dt->dt_ff[i]) == NULL) { continue; } - if ((fp = ff->ff_file) == NULL) { + if ((fp = atomic_load_consume(&ff->ff_file)) == NULL) { continue; } fp->f_marker = 0; @@ -2078,7 +2099,7 @@ sysctl_kern_file(SYSCTLFN_ARGS) if ((ff = dt->dt_ff[i]) == NULL) { continue; } - if ((fp = ff->ff_file) == NULL) { + if ((fp = atomic_load_consume(&ff->ff_file)) == NULL) { continue; } @@ -2233,7 +2254,8 @@ sysctl_kern_file2(SYSCTLFN_ARGS) if ((ff = dt->dt_ff[i]) == NULL) { continue; } - if ((fp = ff->ff_file) == NULL) { + if ((fp = atomic_load_consume(&ff->ff_file)) == + NULL) { continue; } Index: src/sys/kern/kern_event.c diff -u src/sys/kern/kern_event.c:1.104.4.2 src/sys/kern/kern_event.c:1.104.4.3 --- src/sys/kern/kern_event.c:1.104.4.2 Sun Feb 7 16:42:41 2021 +++ src/sys/kern/kern_event.c Wed Nov 20 14:01:58 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_event.c,v 1.104.4.2 2021/02/07 16:42:41 martin Exp $ */ +/* $NetBSD: kern_event.c,v 1.104.4.3 2024/11/20 14:01:58 martin Exp $ */ /*- * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. @@ -58,7 +58,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.104.4.2 2021/02/07 16:42:41 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.104.4.3 2024/11/20 14:01:58 martin Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -1691,8 +1691,8 @@ knote_fdclose(int fd) filedesc_t *fdp; fdp = curlwp->l_fd; - list = (struct klist *)&fdp->fd_dt->dt_ff[fd]->ff_knlist; mutex_enter(&fdp->fd_lock); + list = (struct klist *)&fdp->fd_dt->dt_ff[fd]->ff_knlist; while ((kn = SLIST_FIRST(list)) != NULL) { knote_detach(kn, fdp, true); mutex_enter(&fdp->fd_lock); Index: src/sys/kern/kern_sig.c diff -u src/sys/kern/kern_sig.c:1.364.2.9 src/sys/kern/kern_sig.c:1.364.2.10 --- src/sys/kern/kern_sig.c:1.364.2.9 Mon Nov 11 17:11:07 2019 +++ src/sys/kern/kern_sig.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_sig.c,v 1.364.2.9 2019/11/11 17:11:07 martin Exp $ */ +/* $NetBSD: kern_sig.c,v 1.364.2.10 2024/11/20 14:01:59 martin Exp $ */ /*- * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc. @@ -70,7 +70,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.9 2019/11/11 17:11:07 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.364.2.10 2024/11/20 14:01:59 martin Exp $"); #include "opt_ptrace.h" #include "opt_dtrace.h" @@ -1065,11 +1065,11 @@ kpsignal(struct proc *p, ksiginfo_t *ksi /* XXXSMP locking */ ksi->ksi_fd = -1; - dt = fdp->fd_dt; + dt = atomic_load_consume(&fdp->fd_dt); for (fd = 0; fd < dt->dt_nfiles; fd++) { if ((ff = dt->dt_ff[fd]) == NULL) continue; - if ((fp = ff->ff_file) == NULL) + if ((fp = atomic_load_consume(&ff->ff_file)) == NULL) continue; if (fp->f_data == data) { ksi->ksi_fd = fd; Index: src/sys/kern/subr_exec_fd.c diff -u src/sys/kern/subr_exec_fd.c:1.8 src/sys/kern/subr_exec_fd.c:1.8.4.1 --- src/sys/kern/subr_exec_fd.c:1.8 Mon Apr 8 13:05:23 2019 +++ src/sys/kern/subr_exec_fd.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: subr_exec_fd.c,v 1.8 2019/04/08 13:05:23 maya Exp $ */ +/* $NetBSD: subr_exec_fd.c,v 1.8.4.1 2024/11/20 14:01:59 martin Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -27,9 +27,10 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.8 2019/04/08 13:05:23 maya Exp $"); +__KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.8.4.1 2024/11/20 14:01:59 martin Exp $"); #include <sys/param.h> +#include <sys/atomic.h> #include <sys/file.h> #include <sys/filedesc.h> #include <sys/mutex.h> @@ -46,12 +47,13 @@ fd_ktrexecfd(void) fdfile_t *ff; lwp_t *l; fdtab_t *dt; + file_t *fp; int fd; l = curlwp; p = l->l_proc; fdp = p->p_fd; - dt = fdp->fd_dt; + dt = atomic_load_consume(&fdp->fd_dt); for (fd = 0; fd <= fdp->fd_lastfile; fd++) { if ((ff = dt->dt_ff[fd]) == NULL) { @@ -60,9 +62,9 @@ fd_ktrexecfd(void) } KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]); - if (ff->ff_file == NULL) + if ((fp = atomic_load_consume(&ff->ff_file)) == NULL) continue; - ktr_execfd(fd, ff->ff_file->f_type); + ktr_execfd(fd, fp->f_type); } } @@ -91,7 +93,7 @@ fd_checkstd(void) closed[0] = '\0'; if ((fdp = p->p_fd) == NULL) return (0); - dt = fdp->fd_dt; + dt = atomic_load_consume(&fdp->fd_dt); for (i = 0; i < CHECK_UPTO; i++) { KASSERT(i >= NDFDFILE || dt->dt_ff[i] == (fdfile_t *)fdp->fd_dfdfile[i]); Index: src/sys/kern/sys_aio.c diff -u src/sys/kern/sys_aio.c:1.44 src/sys/kern/sys_aio.c:1.44.4.1 --- src/sys/kern/sys_aio.c:1.44 Sun Feb 10 17:13:33 2019 +++ src/sys/kern/sys_aio.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: sys_aio.c,v 1.44 2019/02/10 17:13:33 christos Exp $ */ +/* $NetBSD: sys_aio.c,v 1.44.4.1 2024/11/20 14:01:59 martin Exp $ */ /* * Copyright (c) 2007 Mindaugas Rasiukevicius <rmind at NetBSD org> @@ -32,7 +32,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sys_aio.c,v 1.44 2019/02/10 17:13:33 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_aio.c,v 1.44.4.1 2024/11/20 14:01:59 martin Exp $"); #ifdef _KERNEL_OPT #include "opt_ddb.h" @@ -631,7 +631,7 @@ sys_aio_cancel(struct lwp *l, const stru /* Check for invalid file descriptor */ fildes = (unsigned int)SCARG(uap, fildes); - dt = fdp->fd_dt; + dt = atomic_load_consume(&fdp->fd_dt); if (fildes >= dt->dt_nfiles) return EBADF; if (dt->dt_ff[fildes] == NULL || dt->dt_ff[fildes]->ff_file == NULL) Index: src/sys/kern/sys_descrip.c diff -u src/sys/kern/sys_descrip.c:1.33 src/sys/kern/sys_descrip.c:1.33.2.1 --- src/sys/kern/sys_descrip.c:1.33 Tue May 21 18:09:31 2019 +++ src/sys/kern/sys_descrip.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: sys_descrip.c,v 1.33 2019/05/21 18:09:31 christos Exp $ */ +/* $NetBSD: sys_descrip.c,v 1.33.2.1 2024/11/20 14:01:59 martin Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -67,7 +67,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.33 2019/05/21 18:09:31 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.33.2.1 2024/11/20 14:01:59 martin Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -328,6 +328,7 @@ sys_fcntl(struct lwp *l, const struct sy } */ int fd, i, tmp, error, cmd, newmin; filedesc_t *fdp; + fdtab_t *dt; file_t *fp; struct flock fl; bool cloexec = false; @@ -396,7 +397,8 @@ sys_fcntl(struct lwp *l, const struct sy break; case F_GETFD: - *retval = fdp->fd_dt->dt_ff[fd]->ff_exclose; + dt = atomic_load_consume(&fdp->fd_dt); + *retval = dt->dt_ff[fd]->ff_exclose; break; case F_SETFD: Index: src/sys/kern/sys_select.c diff -u src/sys/kern/sys_select.c:1.46 src/sys/kern/sys_select.c:1.46.2.1 --- src/sys/kern/sys_select.c:1.46 Fri Jul 26 05:37:59 2019 +++ src/sys/kern/sys_select.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: sys_select.c,v 1.46 2019/07/26 05:37:59 msaitoh Exp $ */ +/* $NetBSD: sys_select.c,v 1.46.2.1 2024/11/20 14:01:59 martin Exp $ */ /*- * Copyright (c) 2007, 2008, 2009, 2010 The NetBSD Foundation, Inc. @@ -84,7 +84,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.46 2019/07/26 05:37:59 msaitoh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.46.2.1 2024/11/20 14:01:59 martin Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -336,7 +336,7 @@ selcommon(register_t *retval, int nd, fd if (nd < 0) return (EINVAL); - nf = curlwp->l_fd->fd_dt->dt_nfiles; + nf = atomic_load_consume(&curlwp->l_fd->fd_dt)->dt_nfiles; if (nd > nf) { /* forgiving; slightly wrong */ nd = nf; Index: src/sys/kern/uipc_socket2.c diff -u src/sys/kern/uipc_socket2.c:1.134.2.2 src/sys/kern/uipc_socket2.c:1.134.2.3 --- src/sys/kern/uipc_socket2.c:1.134.2.2 Sat Oct 2 11:07:55 2021 +++ src/sys/kern/uipc_socket2.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: uipc_socket2.c,v 1.134.2.2 2021/10/02 11:07:55 martin Exp $ */ +/* $NetBSD: uipc_socket2.c,v 1.134.2.3 2024/11/20 14:01:59 martin Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -58,7 +58,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.134.2.2 2021/10/02 11:07:55 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.134.2.3 2024/11/20 14:01:59 martin Exp $"); #ifdef _KERNEL_OPT #include "opt_ddb.h" @@ -1622,13 +1622,13 @@ sofindproc(struct socket *so, int all, v pr("could not acquire fd_lock mutex\n"); continue; } - dt = fdp->fd_dt; + dt = atomic_load_consume(&fdp->fd_dt); for (i = 0; i < dt->dt_nfiles; i++) { ff = dt->dt_ff[i]; if (ff == NULL) continue; - fp = ff->ff_file; + fp = atomic_load_consume(&ff->ff_file); if (fp == NULL) continue; Index: src/sys/kern/uipc_usrreq.c diff -u src/sys/kern/uipc_usrreq.c:1.194.2.1 src/sys/kern/uipc_usrreq.c:1.194.2.2 --- src/sys/kern/uipc_usrreq.c:1.194.2.1 Tue Sep 22 18:39:01 2020 +++ src/sys/kern/uipc_usrreq.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: uipc_usrreq.c,v 1.194.2.1 2020/09/22 18:39:01 martin Exp $ */ +/* $NetBSD: uipc_usrreq.c,v 1.194.2.2 2024/11/20 14:01:59 martin Exp $ */ /*- * Copyright (c) 1998, 2000, 2004, 2008, 2009 The NetBSD Foundation, Inc. @@ -96,7 +96,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.194.2.1 2020/09/22 18:39:01 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.194.2.2 2024/11/20 14:01:59 martin Exp $"); #ifdef _KERNEL_OPT #include "opt_compat_netbsd.h" @@ -1525,6 +1525,7 @@ static int unp_internalize(struct mbuf **controlp) { filedesc_t *fdescp = curlwp->l_fd; + fdtab_t *dt; struct mbuf *control = *controlp; struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *); file_t **rp, **files; @@ -1587,7 +1588,8 @@ unp_internalize(struct mbuf **controlp) fdp = (int *)CMSG_DATA(cm) + nfds; rp = files + nfds; for (i = 0; i < nfds; i++) { - fp = fdescp->fd_dt->dt_ff[*--fdp]->ff_file; + dt = atomic_load_consume(&fdescp->fd_dt); + fp = atomic_load_consume(&dt->dt_ff[*--fdp]->ff_file); KASSERT(fp != NULL); mutex_enter(&fp->f_lock); *--rp = fp; Index: src/sys/miscfs/fdesc/fdesc_vnops.c diff -u src/sys/miscfs/fdesc/fdesc_vnops.c:1.130.4.1 src/sys/miscfs/fdesc/fdesc_vnops.c:1.130.4.2 --- src/sys/miscfs/fdesc/fdesc_vnops.c:1.130.4.1 Mon May 3 09:03:22 2021 +++ src/sys/miscfs/fdesc/fdesc_vnops.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: fdesc_vnops.c,v 1.130.4.1 2021/05/03 09:03:22 martin Exp $ */ +/* $NetBSD: fdesc_vnops.c,v 1.130.4.2 2024/11/20 14:01:59 martin Exp $ */ /* * Copyright (c) 1992, 1993 @@ -41,7 +41,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: fdesc_vnops.c,v 1.130.4.1 2021/05/03 09:03:22 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: fdesc_vnops.c,v 1.130.4.2 2024/11/20 14:01:59 martin Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -207,7 +207,7 @@ fdesc_lookup(void *v) int error, ix = -1; fdtab_t *dt; - dt = curlwp->l_fd->fd_dt; + dt = atomic_load_consume(&curlwp->l_fd->fd_dt); if (cnp->cn_namelen == 1 && *pname == '.') { *vpp = dvp; @@ -577,7 +577,7 @@ fdesc_readdir(void *v) break; } - dt = curlwp->l_fd->fd_dt; + dt = atomic_load_consume(&curlwp->l_fd->fd_dt); if (uio->uio_resid < UIO_MX) return EINVAL; @@ -639,7 +639,6 @@ fdesc_readdir(void *v) *cookies++ = i + 1; } } else { - membar_consumer(); if (ap->a_ncookies) { ncookies = uimin(ncookies, dt->dt_nfiles + 2); cookies = malloc(ncookies * sizeof(off_t), Index: src/sys/miscfs/procfs/procfs_vnops.c diff -u src/sys/miscfs/procfs/procfs_vnops.c:1.206.4.2 src/sys/miscfs/procfs/procfs_vnops.c:1.206.4.3 --- src/sys/miscfs/procfs/procfs_vnops.c:1.206.4.2 Fri Jun 17 15:25:21 2022 +++ src/sys/miscfs/procfs/procfs_vnops.c Wed Nov 20 14:01:59 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: procfs_vnops.c,v 1.206.4.2 2022/06/17 15:25:21 martin Exp $ */ +/* $NetBSD: procfs_vnops.c,v 1.206.4.3 2024/11/20 14:01:59 martin Exp $ */ /*- * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc. @@ -105,9 +105,10 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.206.4.2 2022/06/17 15:25:21 martin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.206.4.3 2024/11/20 14:01:59 martin Exp $"); #include <sys/param.h> +#include <sys/atomic.h> #include <sys/systm.h> #include <sys/time.h> #include <sys/kernel.h> @@ -1390,7 +1391,7 @@ procfs_readdir(void *v) return ESRCH; } - nfd = p->p_fd->fd_dt->dt_nfiles; + nfd = atomic_load_consume(&p->p_fd->fd_dt)->dt_nfiles; lim = uimin((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); if (i >= lim) {