From 082a6b2a92888cf799c7a0408a78e2d7ad9320bb Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Fri, 14 Feb 2020 23:15:41 +0000 Subject: [PATCH] Make atomic_load_ptr type-aware Returned value has type based on the argument, meaning consumers no longer have to cast in the commmon case. This commit keeps the kernel compilable without patching the rest. --- sys/amd64/amd64/pmap.c | 6 +++--- sys/sys/_cscan_atomic.h | 6 +++++- sys/sys/atomic_common.h | 2 +- sys/x86/x86/mp_x86.c | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/sys/amd64/amd64/pmap.c b/sys/amd64/amd64/pmap.c index 3f1963954c91..4542612c2e98 100644 --- a/sys/amd64/amd64/pmap.c +++ b/sys/amd64/amd64/pmap.c @@ -796,7 +796,7 @@ pmap_delayed_invl_start_u(void) PV_STAT(i = 0); for (p = &pmap_invl_gen_head;; p = prev.next) { PV_STAT(i++); - prevl = atomic_load_ptr(&p->next); + prevl = (uintptr_t)atomic_load_ptr(&p->next); if ((prevl & PMAP_INVL_GEN_NEXT_INVALID) != 0) { PV_STAT(atomic_add_long(&invl_start_restart, 1)); lock_delay(&lda); @@ -903,7 +903,7 @@ pmap_delayed_invl_finish_u(void) again: for (p = &pmap_invl_gen_head; p != NULL; p = (void *)prevl) { - prevl = atomic_load_ptr(&p->next); + prevl = (uintptr_t)atomic_load_ptr(&p->next); if ((prevl & PMAP_INVL_GEN_NEXT_INVALID) != 0) { PV_STAT(atomic_add_long(&invl_finish_restart, 1)); lock_delay(&lda); @@ -954,7 +954,7 @@ DB_SHOW_COMMAND(di_queue, pmap_di_queue) for (p = &pmap_invl_gen_head, first = true; p != NULL; p = pn, first = false) { - nextl = atomic_load_ptr(&p->next); + nextl = (uintptr_t)atomic_load_ptr(&p->next); pn = (void *)(nextl & ~PMAP_INVL_GEN_NEXT_INVALID); td = first ? NULL : __containerof(p, struct thread, td_md.md_invl_gen); diff --git a/sys/sys/_cscan_atomic.h b/sys/sys/_cscan_atomic.h index 93c557bcee58..045f66c05565 100644 --- a/sys/sys/_cscan_atomic.h +++ b/sys/sys/_cscan_atomic.h @@ -170,7 +170,11 @@ void kcsan_atomic_thread_fence_seq_cst(void); #define atomic_fcmpset_acq_ptr kcsan_atomic_fcmpset_acq_ptr #define atomic_fcmpset_rel_ptr kcsan_atomic_fcmpset_rel_ptr #define atomic_fetchadd_ptr kcsan_atomic_fetchadd_ptr -#define atomic_load_ptr(x) kcsan_atomic_load_ptr((volatile uintptr_t *)(x)) +#define atomic_load_ptr(x) ({ \ + __typeof(*x) __retptr; \ + __retptr = (void *)kcsan_atomic_load_ptr((volatile uintptr_t *)(x)); \ + __retptr; \ +}) #define atomic_load_acq_ptr kcsan_atomic_load_acq_ptr #define atomic_readandclear_ptr kcsan_atomic_readandclear_ptr #define atomic_set_ptr kcsan_atomic_set_ptr diff --git a/sys/sys/atomic_common.h b/sys/sys/atomic_common.h index 889d491cc1af..63311b5a3124 100644 --- a/sys/sys/atomic_common.h +++ b/sys/sys/atomic_common.h @@ -41,7 +41,7 @@ #define atomic_load_short(p) (*(volatile u_short *)(p)) #define atomic_load_int(p) (*(volatile u_int *)(p)) #define atomic_load_long(p) (*(volatile u_long *)(p)) -#define atomic_load_ptr(p) (*(volatile uintptr_t*)(p)) +#define atomic_load_ptr(p) (*(volatile __typeof(p))(p)) #define atomic_load_8(p) (*(volatile uint8_t *)(p)) #define atomic_load_16(p) (*(volatile uint16_t *)(p)) #define atomic_load_32(p) (*(volatile uint32_t *)(p)) diff --git a/sys/x86/x86/mp_x86.c b/sys/x86/x86/mp_x86.c index 5aec0e094144..e8d8b9801748 100644 --- a/sys/x86/x86/mp_x86.c +++ b/sys/x86/x86/mp_x86.c @@ -1106,7 +1106,7 @@ smp_after_idle_runnable(void *arg __unused) for (cpu = 1; cpu < mp_ncpus; cpu++) { pc = pcpu_find(cpu); - while (atomic_load_ptr(&pc->pc_curpcb) == (uintptr_t)NULL) + while (atomic_load_ptr(&pc->pc_curpcb) == NULL) cpu_spinwait(); kmem_free((vm_offset_t)bootstacks[cpu], kstack_pages * PAGE_SIZE);