From nobody Thu May 16 21:47:45 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600684953; cv=none; d=zohomail.com; s=zohoarc; b=HDuPivN/Nz3Gff5R1XZ0em2yLDbKb7+9vsTVeYKh6ancwl3qWpGIxKOKVK772wGTZHvnV034veAna7IGG2KNhlknb20F6/JjYUHuRtYA8ioD93p4/en3Bl6Jl4+OnNnT75oo3lYJ8+BsW2IUmYs6dNaFzbjgKIe3q1rK6GtRaWo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600684953; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=rIWQR4Fyrrte8nYlAtvYxFdoZSp0D2WoKSHF1VOh040=; b=lHdaVWIK2gJ4Ci8SpZG5GFIsu4h+3RBKRx+F/rf/yRQSgRcyaiA/moijJ2rjeaY8s1siaBzjDoRwEuWIZZ7kmkBT2TBjHUWpvVsUNlYxZu6fu9bxN6iXDScx62ZlpHWwqf0OY/j6auapoIHKZBsu0q/7FSnohvUjB9qdlNVQAd4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1600684953695775.5031936315027; Mon, 21 Sep 2020 03:42:33 -0700 (PDT) Received: from localhost ([::1]:43632 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kKJHE-00011G-9x for importer@patchew.org; Mon, 21 Sep 2020 06:42:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59082) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kKJG6-0000TF-TG for qemu-devel@nongnu.org; Mon, 21 Sep 2020 06:41:22 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:46900 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kKJG4-0003KA-LW for qemu-devel@nongnu.org; Mon, 21 Sep 2020 06:41:22 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-452-0KoNQcPCPb-_d_zhf_g9Gg-1; Mon, 21 Sep 2020 06:41:13 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8FD91107466C; Mon, 21 Sep 2020 10:41:12 +0000 (UTC) Received: from localhost (ovpn-114-170.ams2.redhat.com [10.36.114.170]) by smtp.corp.redhat.com (Postfix) with ESMTP id F180210013BD; Mon, 21 Sep 2020 10:41:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600684879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rIWQR4Fyrrte8nYlAtvYxFdoZSp0D2WoKSHF1VOh040=; b=INGG3JhdmMV0/oh8C277t2++sJi0IaxRMElj98SgDqVpMX8hfd628yUlWPX6haOSfx0w8/ TSbwlJajvS8vngj6acBncNS4IxBb/PdcXVXRNOA3AtUjsBdX3vWagSkNPhzEWR1hOzRxeo jAb9aSUZmkP9+iq0Ap1xm6diQyP1uEw= X-MC-Unique: 0KoNQcPCPb-_d_zhf_g9Gg-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [RFC] Move to C11 Atomics Date: Mon, 21 Sep 2020 11:41:07 +0100 Message-Id: <20200921104107.134323-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.61; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/21 04:38:49 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.501, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , Daniel Berrange , Eduardo Habkost , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch is incomplete but I am looking for feedback on the approach before fully implementing it (which will involve lots of changes). QEMU's atomic.h provides atomic operations and is intended to work with or without . Some of the atomic.h APIs are from C11 Atomics while others are Linux-inspired or QEMU-specific extensions. atomic.h works fine with gcc but clang enforces the following: atomic_fetch_add() and friends must use C11 Atomic atomic_* types. __atomic_fetch_add() and friends must use direct types (int, etc) and NOT C11 Atomic types. The consequences are: 1. atomic_fetch_*() produces compilation errors since QEMU code uses direct types and not C11 Atomic types. 2. atomic_fetch_*() cannot be used on the same variables as __atomic_fetch_*() because they support different types. This is a problem because QEMU's atomic.h builds on __atomic_fetch_*() and code expects to use both atomic_fetch_*() and QEMU atomic.h APIs on the same variables. I would like to move QEMU to C11 Atomics, removing QEMU-specific APIs which have C11 equivalents. The new atomic.h would #include and define additional APIs on top. It also needs to carry a fallback implementation for RHEL 7 because gcc does not have there. The upshot is that all atomic variables in QEMU need to use C11 Atomic atomic_* types. This is a big change! Also, existing atomic.h APIs that use __atomic_fetch_*() need to move to C11 Atomics instead so that they take atomic_* types. This patch contains a few examples of this conversion. Things to note: 1. Reimplement everything in terms of atomic_fetch_*() and other C11 Atomic APIs. For example atomic_fetch_inc() becomes atomic_fetch_add(ptr, 1). 2. atomic_*_fetch() is not available in C11 Atomics so emulate it by performing the operation twice (once atomic, then again non-atomic using the fetched old atomic value). Yuck! 3. Can everything in atomic.h really be converted to C11 Atomics? I'm not sure yet :(. Better ideas? Signed-off-by: Stefan Hajnoczi --- include/block/aio.h | 2 +- include/qemu/atomic.h | 79 +++++++++++++++++++++++++++++++++---------- include/qemu/bitops.h | 2 +- include/qom/object.h | 3 +- util/aio-posix.h | 2 +- util/async.c | 2 +- meson.build | 3 ++ 7 files changed, 70 insertions(+), 23 deletions(-) diff --git a/include/block/aio.h b/include/block/aio.h index b2f703fa3f..466c058880 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -220,7 +220,7 @@ struct AioContext { */ QEMUTimerListGroup tlg; =20 - int external_disable_cnt; + atomic_int external_disable_cnt; =20 /* Number of AioHandlers without .io_poll() */ int poll_disable_cnt; diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h index ff72db5115..4fbbd5f362 100644 --- a/include/qemu/atomic.h +++ b/include/qemu/atomic.h @@ -15,6 +15,32 @@ #ifndef QEMU_ATOMIC_H #define QEMU_ATOMIC_H =20 +#include +#include + +/* Use C11 Atomics if possible, otherwise fall back to custom definitions = */ +#ifdef CONFIG_STDATOMIC_H +#include +#else +/* Commonly used types from C11 "7.17.6 Atomic integer types" */ +typedef bool atomic_bool; +typedef char atomic_char; +typedef signed char atomic_schar; +typedef unsigned char atomic_uchar; +typedef short atomic_short; +typedef unsigned short atomic_ushort; +typedef int atomic_int; +typedef unsigned int atomic_uint; +typedef long atomic_long; +typedef unsigned long atomic_ulong; +typedef long long atomic_llong; +typedef unsigned long long atomic_ullong; +typedef intptr_t atomic_intptr_t; +typedef uintptr_t atomic_uintptr_t; +typedef size_t atomic_size_t; +typedef ptrdiff_t atomic_ptrdiff_t; +#endif + /* Compiler barrier */ #define barrier() ({ asm volatile("" ::: "memory"); (void)0; }) =20 @@ -205,10 +231,6 @@ atomic_cmpxchg__nocheck(ptr, old, new); \ }) =20 -/* Provide shorter names for GCC atomic builtins, return old value */ -#define atomic_fetch_inc(ptr) __atomic_fetch_add(ptr, 1, __ATOMIC_SEQ_CST) -#define atomic_fetch_dec(ptr) __atomic_fetch_sub(ptr, 1, __ATOMIC_SEQ_CST) - #ifndef atomic_fetch_add #define atomic_fetch_add(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ_C= ST) #define atomic_fetch_sub(ptr, n) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ_C= ST) @@ -217,22 +239,41 @@ #define atomic_fetch_xor(ptr, n) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ_C= ST) #endif =20 -#define atomic_inc_fetch(ptr) __atomic_add_fetch(ptr, 1, __ATOMIC_SEQ_C= ST) -#define atomic_dec_fetch(ptr) __atomic_sub_fetch(ptr, 1, __ATOMIC_SEQ_C= ST) -#define atomic_add_fetch(ptr, n) __atomic_add_fetch(ptr, n, __ATOMIC_SEQ_C= ST) -#define atomic_sub_fetch(ptr, n) __atomic_sub_fetch(ptr, n, __ATOMIC_SEQ_C= ST) -#define atomic_and_fetch(ptr, n) __atomic_and_fetch(ptr, n, __ATOMIC_SEQ_C= ST) -#define atomic_or_fetch(ptr, n) __atomic_or_fetch(ptr, n, __ATOMIC_SEQ_CS= T) -#define atomic_xor_fetch(ptr, n) __atomic_xor_fetch(ptr, n, __ATOMIC_SEQ_C= ST) +/* Provide shorter names for GCC atomic builtins, return old value */ +#define atomic_fetch_inc(ptr) atomic_fetch_add(ptr, 1) +#define atomic_fetch_dec(ptr) atomic_fetch_sub(ptr, 1) + +#define atomic_inc_fetch(ptr) (atomic_fetch_add(ptr, 1) + 1) +#define atomic_dec_fetch(ptr) (atomic_fetch_sub(ptr, 1) - 1) +#define atomic_add_fetch(ptr, n) ({ \ + typeof(n) _n =3D n; \ + atomic_fetch_add(ptr, _n) + _n; \ +}) +#define atomic_sub_fetch(ptr, n) ({ \ + typeof(n) _n =3D n; \ + atomic_fetch_sub(ptr, _n) - n; \ +}) +#define atomic_and_fetch(ptr, n) ({ \ + typeof(n) _n =3D n; \ + atomic_fetch_and(ptr, _n) & _n; \ +}) +#define atomic_or_fetch(ptr, n) ({ \ + typeof(n) _n =3D n; \ + atomic_fetch_or(ptr, _n) | _n; \ +}) +#define atomic_xor_fetch(ptr, n) ({ \ + typeof(n) _n =3D n; \ + atomic_fetch_xor(ptr, _n) ^ _n; \ +}) =20 /* And even shorter names that return void. */ -#define atomic_inc(ptr) ((void) __atomic_fetch_add(ptr, 1, __ATOMIC_SEQ= _CST)) -#define atomic_dec(ptr) ((void) __atomic_fetch_sub(ptr, 1, __ATOMIC_SEQ= _CST)) -#define atomic_add(ptr, n) ((void) __atomic_fetch_add(ptr, n, __ATOMIC_SEQ= _CST)) -#define atomic_sub(ptr, n) ((void) __atomic_fetch_sub(ptr, n, __ATOMIC_SEQ= _CST)) -#define atomic_and(ptr, n) ((void) __atomic_fetch_and(ptr, n, __ATOMIC_SEQ= _CST)) -#define atomic_or(ptr, n) ((void) __atomic_fetch_or(ptr, n, __ATOMIC_SEQ_= CST)) -#define atomic_xor(ptr, n) ((void) __atomic_fetch_xor(ptr, n, __ATOMIC_SEQ= _CST)) +#define atomic_inc(ptr) ((void) atomic_fetch_add(ptr, 1)) +#define atomic_dec(ptr) ((void) atomic_fetch_sub(ptr, 1)) +#define atomic_add(ptr, n) ((void) atomic_fetch_add(ptr, n)) +#define atomic_sub(ptr, n) ((void) atomic_fetch_sub(ptr, n)) +#define atomic_and(ptr, n) ((void) atomic_fetch_and(ptr, n)) +#define atomic_or(ptr, n) ((void) atomic_fetch_or(ptr, n)) +#define atomic_xor(ptr, n) ((void) atomic_fetch_xor(ptr, n)) =20 #else /* __ATOMIC_RELAXED */ =20 @@ -424,6 +465,8 @@ #define atomic_or(ptr, n) ((void) __sync_fetch_and_or(ptr, n)) #define atomic_xor(ptr, n) ((void) __sync_fetch_and_xor(ptr, n)) =20 +#error TODO + #endif /* __ATOMIC_RELAXED */ =20 #ifndef smp_wmb diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h index f55ce8b320..e9d676d112 100644 --- a/include/qemu/bitops.h +++ b/include/qemu/bitops.h @@ -49,7 +49,7 @@ static inline void set_bit(long nr, unsigned long *addr) static inline void set_bit_atomic(long nr, unsigned long *addr) { unsigned long mask =3D BIT_MASK(nr); - unsigned long *p =3D addr + BIT_WORD(nr); + atomic_ulong *p =3D (atomic_ulong *)(addr + BIT_WORD(nr)); =20 atomic_or(p, mask); } diff --git a/include/qom/object.h b/include/qom/object.h index 056f67ab3b..f51244b61f 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -15,6 +15,7 @@ #define QEMU_OBJECT_H =20 #include "qapi/qapi-builtin-types.h" +#include "qemu/atomic.h" #include "qemu/module.h" #include "qom/object.h" =20 @@ -550,7 +551,7 @@ struct Object ObjectClass *class; ObjectFree *free; GHashTable *properties; - uint32_t ref; + atomic_uint ref; Object *parent; }; =20 diff --git a/util/aio-posix.h b/util/aio-posix.h index c80c04506a..c5b446f0a1 100644 --- a/util/aio-posix.h +++ b/util/aio-posix.h @@ -33,7 +33,7 @@ struct AioHandler { QLIST_ENTRY(AioHandler) node_poll; #ifdef CONFIG_LINUX_IO_URING QSLIST_ENTRY(AioHandler) node_submitted; - unsigned flags; /* see fdmon-io_uring.c */ + atomic_uint flags; /* see fdmon-io_uring.c */ #endif int64_t poll_idle_timeout; /* when to stop userspace polling */ bool is_external; diff --git a/util/async.c b/util/async.c index 4266745dee..dcf1a32492 100644 --- a/util/async.c +++ b/util/async.c @@ -60,7 +60,7 @@ struct QEMUBH { QEMUBHFunc *cb; void *opaque; QSLIST_ENTRY(QEMUBH) next; - unsigned flags; + atomic_uint flags; }; =20 /* Called concurrently from any thread */ diff --git a/meson.build b/meson.build index f4d1ab1096..8d80033d90 100644 --- a/meson.build +++ b/meson.build @@ -433,8 +433,11 @@ keyutils =3D dependency('libkeyutils', required: false, =20 has_gettid =3D cc.has_function('gettid') =20 +has_stdatomic_h =3D cc.has_header('stdatomic.h') + # Create config-host.h =20 +config_host_data.set('CONFIG_STDATOMIC_H', has_stdatomic_h) config_host_data.set('CONFIG_SDL', sdl.found()) config_host_data.set('CONFIG_SDL_IMAGE', sdl_image.found()) config_host_data.set('CONFIG_VNC', vnc.found()) --=20 2.26.2