From 227a31caf8fb841f02ccf339c9b2a673fbd9b853 Mon Sep 17 00:00:00 2001 From: bluhm Date: Thu, 10 Mar 2022 15:21:08 +0000 Subject: [PATCH] Use atomic load and store functions to access refcnt and wait variables. Although not necessary everywhere, using atomic functions exclusively for variables marked as atomic is clearer. OK mvs@ visa@ --- sys/dev/pci/if_iwm.c | 6 +++--- sys/dev/pci/if_iwx.c | 6 +++--- sys/kern/kern_synch.c | 24 ++++++++++++------------ sys/netinet/ip_ipsp.c | 6 +++--- sys/sys/proc.h | 6 +++--- sys/sys/refcnt.h | 11 ++++++++--- 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/sys/dev/pci/if_iwm.c b/sys/dev/pci/if_iwm.c index 26d48bf5b80..f813c071381 100644 --- a/sys/dev/pci/if_iwm.c +++ b/sys/dev/pci/if_iwm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if_iwm.c,v 1.391 2022/02/08 14:24:36 stsp Exp $ */ +/* $OpenBSD: if_iwm.c,v 1.392 2022/03/10 15:21:08 bluhm Exp $ */ /* * Copyright (c) 2014, 2016 genua gmbh @@ -9975,7 +9975,7 @@ iwm_init(struct ifnet *ifp) generation = ++sc->sc_generation; - KASSERT(sc->task_refs.refs == 0); + KASSERT(atomic_load_int(&sc->task_refs.r_refs) == 0); refcnt_init(&sc->task_refs); err = iwm_preinit(sc); @@ -10116,7 +10116,7 @@ iwm_stop(struct ifnet *ifp) iwm_del_task(sc, systq, &sc->mac_ctxt_task); iwm_del_task(sc, systq, &sc->phy_ctxt_task); iwm_del_task(sc, systq, &sc->bgscan_done_task); - KASSERT(sc->task_refs.refs >= 1); + KASSERT(atomic_load_int(&sc->task_refs.r_refs) >= 1); refcnt_finalize(&sc->task_refs, "iwmstop"); iwm_stop_device(sc); diff --git a/sys/dev/pci/if_iwx.c b/sys/dev/pci/if_iwx.c index 57bdcce6445..9ea26c4f3cb 100644 --- a/sys/dev/pci/if_iwx.c +++ b/sys/dev/pci/if_iwx.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if_iwx.c,v 1.134 2022/01/21 15:51:02 stsp Exp $ */ +/* $OpenBSD: if_iwx.c,v 1.135 2022/03/10 15:21:08 bluhm Exp $ */ /* * Copyright (c) 2014, 2016 genua gmbh @@ -8017,7 +8017,7 @@ iwx_init(struct ifnet *ifp) if (sc->sc_nvm.sku_cap_11n_enable) iwx_setup_ht_rates(sc); - KASSERT(sc->task_refs.refs == 0); + KASSERT(atomic_load_int(&sc->task_refs.r_refs) == 0); refcnt_init(&sc->task_refs); ifq_clr_oactive(&ifp->if_snd); ifp->if_flags |= IFF_RUNNING; @@ -8139,7 +8139,7 @@ iwx_stop(struct ifnet *ifp) iwx_del_task(sc, systq, &sc->mac_ctxt_task); iwx_del_task(sc, systq, &sc->phy_ctxt_task); iwx_del_task(sc, systq, &sc->bgscan_done_task); - KASSERT(sc->task_refs.refs >= 1); + KASSERT(atomic_load_int(&sc->task_refs.r_refs) >= 1); refcnt_finalize(&sc->task_refs, "iwxstop"); iwx_stop_device(sc); diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c index d888cb2b84a..27e36501ae9 100644 --- a/sys/kern/kern_synch.c +++ b/sys/kern/kern_synch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_synch.c,v 1.182 2022/02/19 23:56:18 deraadt Exp $ */ +/* $OpenBSD: kern_synch.c,v 1.183 2022/03/10 15:21:08 bluhm Exp $ */ /* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */ /* @@ -804,7 +804,7 @@ sys___thrwakeup(struct proc *p, void *v, register_t *retval) void refcnt_init(struct refcnt *r) { - r->refs = 1; + atomic_store_int(&r->r_refs, 1); } void @@ -813,10 +813,10 @@ refcnt_take(struct refcnt *r) #ifdef DIAGNOSTIC u_int refcnt; - refcnt = atomic_inc_int_nv(&r->refs); + refcnt = atomic_inc_int_nv(&r->r_refs); KASSERT(refcnt != 0); #else - atomic_inc_int(&r->refs); + atomic_inc_int(&r->r_refs); #endif } @@ -825,7 +825,7 @@ refcnt_rele(struct refcnt *r) { u_int refcnt; - refcnt = atomic_dec_int_nv(&r->refs); + refcnt = atomic_dec_int_nv(&r->r_refs); KASSERT(refcnt != ~0); return (refcnt == 0); @@ -844,10 +844,10 @@ refcnt_finalize(struct refcnt *r, const char *wmesg) struct sleep_state sls; u_int refcnt; - refcnt = atomic_dec_int_nv(&r->refs); + refcnt = atomic_dec_int_nv(&r->r_refs); while (refcnt) { sleep_setup(&sls, r, PWAIT, wmesg, 0); - refcnt = r->refs; + refcnt = atomic_load_int(&r->r_refs); sleep_finish(&sls, refcnt); } } @@ -855,13 +855,13 @@ refcnt_finalize(struct refcnt *r, const char *wmesg) void cond_init(struct cond *c) { - c->c_wait = 1; + atomic_store_int(&c->c_wait, 1); } void cond_signal(struct cond *c) { - c->c_wait = 0; + atomic_store_int(&c->c_wait, 0); wakeup_one(c); } @@ -870,12 +870,12 @@ void cond_wait(struct cond *c, const char *wmesg) { struct sleep_state sls; - int wait; + unsigned int wait; - wait = c->c_wait; + wait = atomic_load_int(&c->c_wait); while (wait) { sleep_setup(&sls, c, PWAIT, wmesg, 0); - wait = c->c_wait; + wait = atomic_load_int(&c->c_wait); sleep_finish(&sls, wait); } } diff --git a/sys/netinet/ip_ipsp.c b/sys/netinet/ip_ipsp.c index 7b1b045e2de..a24885c9971 100644 --- a/sys/netinet/ip_ipsp.c +++ b/sys/netinet/ip_ipsp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_ipsp.c,v 1.268 2022/01/04 06:32:39 yasuoka Exp $ */ +/* $OpenBSD: ip_ipsp.c,v 1.269 2022/03/10 15:21:08 bluhm Exp $ */ /* * The authors of this code are John Ioannidis (ji@tla.org), * Angelos D. Keromytis (kermit@csd.uch.gr), @@ -565,7 +565,7 @@ tdb_printit(void *addr, int full, int (*pr)(const char *, ...)) DUMP(inext, "%p"); DUMP(onext, "%p"); DUMP(xform, "%p"); - pr("%18s: %d\n", "refcnt", tdb->tdb_refcnt.refs); + pr("%18s: %d\n", "refcnt", tdb->tdb_refcnt.r_refs); DUMP(encalgxform, "%p"); DUMP(authalgxform, "%p"); DUMP(compalgxform, "%p"); @@ -625,7 +625,7 @@ tdb_printit(void *addr, int full, int (*pr)(const char *, ...)) pr(" %s", ipsp_address(&tdb->tdb_src, buf, sizeof(buf))); pr("->%s", ipsp_address(&tdb->tdb_dst, buf, sizeof(buf))); pr(":%d", tdb->tdb_sproto); - pr(" #%d", tdb->tdb_refcnt.refs); + pr(" #%d", tdb->tdb_refcnt.r_refs); pr(" %08x\n", tdb->tdb_flags); } } diff --git a/sys/sys/proc.h b/sys/sys/proc.h index 51e0ea853fd..241fc9e0c9a 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.h,v 1.328 2022/02/25 18:05:49 rob Exp $ */ +/* $OpenBSD: proc.h,v 1.329 2022/03/10 15:21:08 bluhm Exp $ */ /* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */ /*- @@ -589,10 +589,10 @@ struct sleep_state { }; struct cond { - int c_wait; + unsigned int c_wait; /* [a] initialized and waiting */ }; -#define COND_INITIALIZER() { 1 } +#define COND_INITIALIZER() { .c_wait = 1 } #if defined(MULTIPROCESSOR) void proc_trampoline_mp(void); /* XXX */ diff --git a/sys/sys/refcnt.h b/sys/sys/refcnt.h index 85e84cfdc2d..26423346e51 100644 --- a/sys/sys/refcnt.h +++ b/sys/sys/refcnt.h @@ -1,4 +1,4 @@ -/* $OpenBSD: refcnt.h,v 1.4 2016/06/07 07:53:33 mpi Exp $ */ +/* $OpenBSD: refcnt.h,v 1.5 2022/03/10 15:21:08 bluhm Exp $ */ /* * Copyright (c) 2015 David Gwynne @@ -19,11 +19,16 @@ #ifndef _SYS_REFCNT_H_ #define _SYS_REFCNT_H_ +/* + * Locks used to protect struct members in this file: + * a atomic operations + */ + struct refcnt { - unsigned int refs; + unsigned int r_refs; /* [a] reference counter */ }; -#define REFCNT_INITIALIZER() { .refs = 1 } +#define REFCNT_INITIALIZER() { .r_refs = 1 } #ifdef _KERNEL -- 2.20.1