Source-Changes-HG archive

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

[src/trunk]: src/sys/dev efi(4): Fix access to efi_isopen.



details:   https://anonhg.NetBSD.org/src/rev/111e12d50b10
branches:  trunk
changeset: 370682:111e12d50b10
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sat Sep 24 11:06:03 2022 +0000

description:
efi(4): Fix access to efi_isopen.

- Qualify efi_isopen with volatile.
- Ensure open has acquire ordering and close has release ordering.
- Use atomic_swap, not atomic_cas -- simpler and may be cheaper.
- Use atomic_store, not atomic_swap -- simpler and usually cheaper.

(Could maybe just use __cpu_simple_lock to avoid having to write out
these details.)

diffstat:

 sys/dev/efi.c |  13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)

diffs (53 lines):

diff -r e8c22f2a1d73 -r 111e12d50b10 sys/dev/efi.c
--- a/sys/dev/efi.c     Sat Sep 24 11:05:47 2022 +0000
+++ b/sys/dev/efi.c     Sat Sep 24 11:06:03 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: efi.c,v 1.3 2022/04/01 06:51:12 skrll Exp $ */
+/* $NetBSD: efi.c,v 1.4 2022/09/24 11:06:03 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2021 Jared McNeill <jmcneill%invisible.ca@localhost>
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: efi.c,v 1.3 2022/04/01 06:51:12 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: efi.c,v 1.4 2022/09/24 11:06:03 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/conf.h>
@@ -78,7 +78,7 @@
  * that a SetVariable() call between calls to GetNextVariableName() may
  * produce unpredictable results, and we want to avoid this.
  */
-static u_int efi_isopen = 0;
+static volatile u_int efi_isopen = 0;
 
 static dev_type_open(efi_open);
 static dev_type_close(efi_close);
@@ -102,20 +102,23 @@
 static int
 efi_open(dev_t dev, int flags, int type, struct lwp *l)
 {
+
        if (efi_ops == NULL) {
                return ENXIO;
        }
-       if (atomic_cas_uint(&efi_isopen, 0, 1) == 1) {
+       if (atomic_swap_uint(&efi_isopen, 1) == 1) {
                return EBUSY;
        }
+       membar_acquire();
        return 0;
 }
 
 static int
 efi_close(dev_t dev, int flags, int type, struct lwp *l)
 {
+
        KASSERT(efi_isopen);
-       atomic_swap_uint(&efi_isopen, 0);
+       atomic_store_release(&efi_isopen, 0);
        return 0;
 }
 



Home | Main Index | Thread Index | Old Index