Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Load struct fdfile::ff_file with atomic_load_consume.



details:   https://anonhg.NetBSD.org/src/rev/54cfc1a0b6ad
branches:  trunk
changeset: 744408:54cfc1a0b6ad
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sat Feb 01 02:23:23 2020 +0000

description:
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.)

diffstat:

 sys/ddb/db_xxx.c        |   6 +++---
 sys/kern/kern_descrip.c |  27 +++++++++++++++------------
 sys/kern/kern_sig.c     |   6 +++---
 sys/kern/subr_exec_fd.c |   9 +++++----
 sys/kern/uipc_socket2.c |   6 +++---
 sys/kern/uipc_usrreq.c  |   6 +++---
 6 files changed, 32 insertions(+), 28 deletions(-)

diffs (257 lines):

diff -r d81835cc4b79 -r 54cfc1a0b6ad sys/ddb/db_xxx.c
--- a/sys/ddb/db_xxx.c  Sat Feb 01 02:23:03 2020 +0000
+++ b/sys/ddb/db_xxx.c  Sat Feb 01 02:23:23 2020 +0000
@@ -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 @@
                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
diff -r d81835cc4b79 -r 54cfc1a0b6ad sys/kern/kern_descrip.c
--- a/sys/kern/kern_descrip.c   Sat Feb 01 02:23:03 2020 +0000
+++ b/sys/kern/kern_descrip.c   Sat Feb 01 02:23:23 2020 +0000
@@ -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 @@
         * 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 @@
                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 @@
 
        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 @@
         * 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 @@
         * 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 @@
                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 @@
                    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 @@
                        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 @@
                        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 @@
                                if ((ff = dt->dt_ff[i]) == NULL) {
                                        continue;
                                }
-                               if ((fp = ff->ff_file) == NULL) {
+                               if ((fp = atomic_load_consume(&ff->ff_file)) ==
+                                   NULL) {
                                        continue;
                                }
 
diff -r d81835cc4b79 -r 54cfc1a0b6ad sys/kern/kern_sig.c
--- a/sys/kern/kern_sig.c       Sat Feb 01 02:23:03 2020 +0000
+++ b/sys/kern/kern_sig.c       Sat Feb 01 02:23:23 2020 +0000
@@ -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 @@
                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;
diff -r d81835cc4b79 -r 54cfc1a0b6ad sys/kern/subr_exec_fd.c
--- a/sys/kern/subr_exec_fd.c   Sat Feb 01 02:23:03 2020 +0000
+++ b/sys/kern/subr_exec_fd.c   Sat Feb 01 02:23:23 2020 +0000
@@ -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 @@
        fdfile_t *ff;
        lwp_t *l;
        fdtab_t *dt;
+       file_t *fp;
        int fd;
 
        l = curlwp;
@@ -61,9 +62,9 @@
                }
                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);
        }
 }
 
diff -r d81835cc4b79 -r 54cfc1a0b6ad sys/kern/uipc_socket2.c
--- a/sys/kern/uipc_socket2.c   Sat Feb 01 02:23:03 2020 +0000
+++ b/sys/kern/uipc_socket2.c   Sat Feb 01 02:23:23 2020 +0000
@@ -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 @@
                        if (ff == NULL)
                                continue;
 
-                       fp = ff->ff_file;
+                       fp = atomic_load_consume(&ff->ff_file);
                        if (fp == NULL)
                                continue;
 
diff -r d81835cc4b79 -r 54cfc1a0b6ad sys/kern/uipc_usrreq.c
--- a/sys/kern/uipc_usrreq.c    Sat Feb 01 02:23:03 2020 +0000
+++ b/sys/kern/uipc_usrreq.c    Sat Feb 01 02:23:23 2020 +0000
@@ -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 @@
        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;



Home | Main Index | Thread Index | Old Index