From nobody Wed Nov 5 18:40:05 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1535995383078953.2484389964369; Mon, 3 Sep 2018 10:23:03 -0700 (PDT) Received: from localhost ([::1]:46228 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fwsZ3-0000MC-M9 for importer@patchew.org; Mon, 03 Sep 2018 13:23:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34244) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fwsUp-00059O-GI for qemu-devel@nongnu.org; Mon, 03 Sep 2018 13:18:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fwsUm-00076o-HP for qemu-devel@nongnu.org; Mon, 03 Sep 2018 13:18:39 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:38629) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fwsUm-00075i-B9 for qemu-devel@nongnu.org; Mon, 03 Sep 2018 13:18:36 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 8913B21BBA; Mon, 3 Sep 2018 13:18:34 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 03 Sep 2018 13:18:34 -0400 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 18F851029E; Mon, 3 Sep 2018 13:18:34 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=mesmtp; bh=P3kGe5HbJ9MEkK DoRB2/ISNpqUztwP6HLO5iCY1S9Pc=; b=TZCU5xG4Gzyho2ua2UT+KXKvb5vSS6 8sLrXltRFWMMEVQyjHvyh74vUTNfBqO8iBROnmNvVpRrm9Klz6CX0n1Y7bjl7xNa mHIYf8UbvdTnIUkUOkp28OQKAE226HIexYzPR3yAZNfMQ1/DxsEXSdC2mZ1YrAgT eyYbsO0XBiS6w= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=P3kGe5HbJ9MEkKDoRB2/ISNpqUztwP6HLO5iCY1S9Pc=; b=CJkn9qTG PrwJZRnpaxfoVeAkxe0kSvh8gxgfSbVmmrUj0EuP7fGBAfKOtAH+J9eKVNKeYv9n aQUnH5cNxhPqYLiiZGGysTsRrLpYZwu+gA3DILf/Qz/jP1ldZ1RclnTi36S6oVmd LANCB+TWGg1xDiA4AAC0c6bjt2Tln3OzvgPf7DTGpSwiIaRZzPoz08388AQ++nez ApznI0OxuCKFPmsodOwol7vmqKcNykOsE/JDSY5R9PMUFEWNDRadTsRZDAF8lcZd 5idoIFGjk61uGV2maP/SmO/hznA17y6ECRmGhOTurRyrVTs4fgrnCWJ1Bb9TLt/d soSl41h55ULkIQ== X-ME-Proxy: X-ME-Sender: From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Mon, 3 Sep 2018 13:18:27 -0400 Message-Id: <20180903171831.15446-3-cota@braap.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180903171831.15446-1-cota@braap.org> References: <20180903171831.15446-1-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.25 Subject: [Qemu-devel] [PATCH 2/6] test-rcu-list: avoid torn accesses to n_reclaims and n_nodes_removed X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Richard Henderson , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost , Peter Crosthwaite Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Emilio G. Cota --- tests/test-rcu-list.c | 67 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/tests/test-rcu-list.c b/tests/test-rcu-list.c index 192bfbf02e..2606b7c19d 100644 --- a/tests/test-rcu-list.c +++ b/tests/test-rcu-list.c @@ -25,6 +25,23 @@ #include "qemu/rcu.h" #include "qemu/thread.h" #include "qemu/rcu_queue.h" +#include "qemu/seqlock.h" + +/* + * Abstraction to avoid torn accesses when there is a single thread updati= ng + * the count. + * + * If CONFIG_ATOMIC64 is defined, we simply use atomic accesses. Otherwise= , we + * use a seqlock without a lock, since only one thread can update the coun= t. + */ +struct Count { + long long val; +#ifndef CONFIG_ATOMIC64 + QemuSeqLock sequence; +#endif +}; + +typedef struct Count Count; =20 /* * Test variables. @@ -33,8 +50,8 @@ static QemuMutex counts_mutex; static long long n_reads =3D 0LL; static long long n_updates =3D 0LL; -static long long n_reclaims =3D 0LL; -static long long n_nodes_removed =3D 0LL; +static Count n_reclaims; +static Count n_nodes_removed; static long long n_nodes =3D 0LL; static int g_test_in_charge =3D 0; =20 @@ -60,6 +77,38 @@ static int select_random_el(int max) return (rand() % max); } =20 +static inline long long count_read(Count *c) +{ +#ifdef CONFIG_ATOMIC64 + /* use __nocheck because sizeof(void *) might be < sizeof(long long) */ + return atomic_read__nocheck(&c->val); +#else + unsigned int version; + long long val; + + do { + version =3D seqlock_read_begin(&c->sequence); + val =3D c->val; + } while (seqlock_read_retry(&c->sequence, version)); + return val; +#endif +} + +static inline void count_add(Count *c, long long val) +{ +#ifdef CONFIG_ATOMIC64 + atomic_set__nocheck(&c->val, c->val + val); +#else + seqlock_write_begin(&c->sequence); + c->val +=3D val; + seqlock_write_end(&c->sequence); +#endif +} + +static inline void count_inc(Count *c) +{ + count_add(c, 1); +} =20 static void create_thread(void *(*func)(void *)) { @@ -104,7 +153,7 @@ static void reclaim_list_el(struct rcu_head *prcu) struct list_element *el =3D container_of(prcu, struct list_element, rc= u); g_free(el); /* Accessed only from call_rcu thread. */ - n_reclaims++; + count_inc(&n_reclaims); } =20 #if TEST_LIST_TYPE =3D=3D 1 @@ -232,7 +281,7 @@ static void *rcu_q_updater(void *arg) qemu_mutex_lock(&counts_mutex); n_nodes +=3D n_nodes_local; n_updates +=3D n_updates_local; - n_nodes_removed +=3D n_removed_local; + count_add(&n_nodes_removed, n_removed_local); qemu_mutex_unlock(&counts_mutex); return NULL; } @@ -286,19 +335,21 @@ static void rcu_qtest(const char *test, int duration,= int nreaders) n_removed_local++; } qemu_mutex_lock(&counts_mutex); - n_nodes_removed +=3D n_removed_local; + count_add(&n_nodes_removed, n_removed_local); qemu_mutex_unlock(&counts_mutex); synchronize_rcu(); - while (n_nodes_removed > n_reclaims) { + while (count_read(&n_nodes_removed) > count_read(&n_reclaims)) { g_usleep(100); synchronize_rcu(); } if (g_test_in_charge) { - g_assert_cmpint(n_nodes_removed, =3D=3D, n_reclaims); + g_assert_cmpint(count_read(&n_nodes_removed), =3D=3D, + count_read(&n_reclaims)); } else { printf("%s: %d readers; 1 updater; nodes read: " \ "%lld, nodes removed: %lld; nodes reclaimed: %lld\n", - test, nthreadsrunning - 1, n_reads, n_nodes_removed, n_recl= aims); + test, nthreadsrunning - 1, n_reads, + count_read(&n_nodes_removed), count_read(&n_reclaims)); exit(0); } } --=20 2.17.1