Module Name:    src
Committed By:   riastradh
Date:           Sat Feb  1 02:23:23 UTC 2020

Modified Files:
        src/sys/ddb: db_xxx.c
        src/sys/kern: kern_descrip.c kern_sig.c subr_exec_fd.c uipc_socket2.c
            uipc_usrreq.c

Log Message:
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.)


To generate a diff of this commit:
cvs rdiff -u -r1.72 -r1.73 src/sys/ddb/db_xxx.c
cvs rdiff -u -r1.244 -r1.245 src/sys/kern/kern_descrip.c
cvs rdiff -u -r1.383 -r1.384 src/sys/kern/kern_sig.c
cvs rdiff -u -r1.9 -r1.10 src/sys/kern/subr_exec_fd.c
cvs rdiff -u -r1.135 -r1.136 src/sys/kern/uipc_socket2.c
cvs rdiff -u -r1.195 -r1.196 src/sys/kern/uipc_usrreq.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/ddb/db_xxx.c
diff -u src/sys/ddb/db_xxx.c:1.72 src/sys/ddb/db_xxx.c:1.73
--- src/sys/ddb/db_xxx.c:1.72	Sat Feb  1 02:23:04 2020
+++ src/sys/ddb/db_xxx.c	Sat Feb  1 02:23:23 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_xxx.c,v 1.72 2020/02/01 02:23:04 riastradh Exp $	*/
+/*	$NetBSD: db_xxx.c,v 1.73 2020/02/01 02:23:23 riastradh 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.72 2020/02/01 02:23:04 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.73 2020/02/01 02:23:23 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kgdb.h"
@@ -179,7 +179,7 @@ db_show_files_cmd(db_expr_t addr, bool h
 		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.244 src/sys/kern/kern_descrip.c:1.245
--- src/sys/kern/kern_descrip.c:1.244	Sat Feb  1 02:23:04 2020
+++ src/sys/kern/kern_descrip.c	Sat Feb  1 02:23:23 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_descrip.c,v 1.244 2020/02/01 02:23:04 riastradh Exp $	*/
+/*	$NetBSD: kern_descrip.c,v 1.245 2020/02/01 02:23:23 riastradh 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.244 2020/02/01 02:23:04 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.245 2020/02/01 02:23:23 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -403,7 +403,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;
 	}
@@ -557,7 +557,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;
 	}
@@ -595,7 +595,8 @@ fd_close(unsigned 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
@@ -620,7 +621,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;
 
@@ -1150,7 +1150,8 @@ 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;
 	dt = atomic_load_consume(&fdp->fd_dt);
@@ -1462,7 +1463,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;
@@ -1549,7 +1551,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
@@ -1977,7 +1979,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;
@@ -2079,7 +2081,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;
 			}
 
@@ -2234,7 +2236,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_sig.c
diff -u src/sys/kern/kern_sig.c:1.383 src/sys/kern/kern_sig.c:1.384
--- src/sys/kern/kern_sig.c:1.383	Sat Feb  1 02:23:04 2020
+++ src/sys/kern/kern_sig.c	Sat Feb  1 02:23:23 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.383 2020/02/01 02:23:04 riastradh Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.384 2020/02/01 02:23:23 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.383 2020/02/01 02:23:04 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.384 2020/02/01 02:23:23 riastradh Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -1072,7 +1072,7 @@ kpsignal(struct proc *p, ksiginfo_t *ksi
 		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.9 src/sys/kern/subr_exec_fd.c:1.10
--- src/sys/kern/subr_exec_fd.c:1.9	Sat Feb  1 02:23:04 2020
+++ src/sys/kern/subr_exec_fd.c	Sat Feb  1 02:23:23 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_exec_fd.c,v 1.9 2020/02/01 02:23:04 riastradh Exp $	*/
+/*	$NetBSD: subr_exec_fd.c,v 1.10 2020/02/01 02:23:23 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.9 2020/02/01 02:23:04 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.10 2020/02/01 02:23:23 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -47,6 +47,7 @@ fd_ktrexecfd(void)
 	fdfile_t *ff;
 	lwp_t *l;
 	fdtab_t *dt;
+	file_t *fp;
 	int fd;
 
 	l = curlwp;
@@ -61,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);
 	}
 }
 

Index: src/sys/kern/uipc_socket2.c
diff -u src/sys/kern/uipc_socket2.c:1.135 src/sys/kern/uipc_socket2.c:1.136
--- src/sys/kern/uipc_socket2.c:1.135	Sat Feb  1 02:23:04 2020
+++ src/sys/kern/uipc_socket2.c	Sat Feb  1 02:23:23 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_socket2.c,v 1.135 2020/02/01 02:23:04 riastradh Exp $	*/
+/*	$NetBSD: uipc_socket2.c,v 1.136 2020/02/01 02:23:23 riastradh 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.135 2020/02/01 02:23:04 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.136 2020/02/01 02:23:23 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1610,7 +1610,7 @@ sofindproc(struct socket *so, int all, v
 			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.195 src/sys/kern/uipc_usrreq.c:1.196
--- src/sys/kern/uipc_usrreq.c:1.195	Sat Feb  1 02:23:04 2020
+++ src/sys/kern/uipc_usrreq.c	Sat Feb  1 02:23:23 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_usrreq.c,v 1.195 2020/02/01 02:23:04 riastradh Exp $	*/
+/*	$NetBSD: uipc_usrreq.c,v 1.196 2020/02/01 02:23:23 riastradh 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.195 2020/02/01 02:23:04 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.196 2020/02/01 02:23:23 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -1587,7 +1587,7 @@ unp_internalize(struct mbuf **controlp)
 	rp = files + nfds;
 	for (i = 0; i < nfds; i++) {
 		dt = atomic_load_consume(&fdescp->fd_dt);
-		fp = dt->dt_ff[*--fdp]->ff_file;
+		fp = atomic_load_consume(&dt->dt_ff[*--fdp]->ff_file);
 		KASSERT(fp != NULL);
 		mutex_enter(&fp->f_lock);
 		*--rp = fp;

Reply via email to