From patchwork Thu May 20 13:35:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaetan Rivet X-Patchwork-Id: 1481632 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=u256.net header.i=@u256.net header.a=rsa-sha256 header.s=fm1 header.b=xsBYCEzs; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm2 header.b=eixw0xOt; dkim-atps=neutral Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fm9kN3RkKz9sWW for ; Thu, 20 May 2021 23:36:28 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id CB582404D0; Thu, 20 May 2021 13:36:25 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id g1wR7WBG60zP; Thu, 20 May 2021 13:36:23 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp2.osuosl.org (Postfix) with ESMTP id A4CAD40598; Thu, 20 May 2021 13:36:19 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 057BBC0043; Thu, 20 May 2021 13:36:14 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 1A042C0022 for ; Thu, 20 May 2021 13:36:06 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 64291401B7 for ; Thu, 20 May 2021 13:36:05 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id FId-1Xzqhsr1 for ; Thu, 20 May 2021 13:36:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by smtp2.osuosl.org (Postfix) with ESMTPS id 454F340218 for ; Thu, 20 May 2021 13:36:01 +0000 (UTC) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id C32B15C01DC for ; Thu, 20 May 2021 09:35:57 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Thu, 20 May 2021 09:35:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=u256.net; h=from :to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=fm1; bh=cVGsFfrsNrce8JcyRoxVBZ0YXt 9KCyCUFgAWe4iu4eA=; b=xsBYCEzsBbHpGTQDe0IQo/5XCIJ2yTnzJNlh0Ptij1 6YMUr5TTiEbmiZ+/UlBw8+pX6Fbd6s/ATa/hOGcu9H/MIkw3cE8clTIoKH6XD3s6 ePT43tmcHnLT/ihDRTlVnucGMJiLlzF0ud3h9c0B/1PZSRme7+W/n9tEjIEvCocx oMLRj2XQ24SA4oEwagfMqrnAD/W6+e5B/dhd8R84A7xipCpl2DMyY750nE36U27q Y/GonwQGgYkD9AfwFHJO2X4NZHrjsUUjoAdw95o7ZWzADjwbsVoB1bm8c/fNAx9P 6L7QVXKYTLUHgGfpUoBb1hTlkxDctj4FPETMG8cvkz6w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=cVGsFfrsNrce8JcyRoxVBZ0YXt9KCyCUFgAWe4iu4eA=; b=eixw0xOt mo03xoj0ZQWYrE6vphSaPu5j1Tkfzf9JKPbDbN3UJVZYZV6Fmcd1dbI0YCN4pY3h aKhcEcGmQ5/NjoBcHZ+8H0wiiAkqcubAyYMZ9Yp+DlGVin8zUjvbKkUBruJYieLF zPoOLrW3l6cVPvft32ECf0hsZoPkDNe7dWUY7RvsoBgPI5NQQl3MMMul4/2QIKn7 eC8vBOpjOJd50W7PW0df9i5nugJTQJCUtpxwX55Lw9UJcGxWdsBVPUW7WVxnSgSf maboLN/mCeBtUr5v5w/Qjq8shUjxUli+E6rqAWfgoOS9ckkRRHncQJZxpT4Wucj8 or0Z4uWaP0zL1w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdejuddgieejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepifgrvghtrghnucftihhvvghtuceoghhrihhvvgesuhdvheei rdhnvghtqeenucggtffrrghtthgvrhhnpeduheekveehfedtueejjeeigeehvdfggefhte dugedutdefhfekhedugeetveeuhfenucffohhmrghinhepshhhsgdrtghinecukfhppeek iedrvdehgedrvdegfedrudegieenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpehgrhhivhgvsehuvdehiedrnhgvth X-ME-Proxy: Received: from inocybe.home (lfbn-poi-1-930-146.w86-254.abo.wanadoo.fr [86.254.243.146]) by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 20 May 2021 09:35:57 -0400 (EDT) From: Gaetan Rivet To: ovs-dev@openvswitch.org Date: Thu, 20 May 2021 15:35:45 +0200 Message-Id: <647e9fe267af1fadb877cafcd3f00aec238b41d4.1621517561.git.grive@u256.net> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [ovs-dev] [PATCH v2 1/8] configure: add --enable-asan option X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Add a configure option to enable ASAN in a simple way. Adding an AC variable to allow checking for support in the testsuite. Signed-off-by: Gaetan Rivet --- .ci/linux-build.sh | 4 ++-- NEWS | 1 + acinclude.m4 | 16 ++++++++++++++++ configure.ac | 1 + tests/atlocal.in | 1 + 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.ci/linux-build.sh b/.ci/linux-build.sh index 0210d6a77..19600a668 100755 --- a/.ci/linux-build.sh +++ b/.ci/linux-build.sh @@ -229,10 +229,10 @@ fi if [ "$ASAN" ]; then # This will override default option configured in tests/atlocal.in. export ASAN_OPTIONS='detect_leaks=1' + EXTRA_OPTS="$EXTRA_OPTS --enable-asan" # -O2 generates few false-positive memory leak reports in test-ovsdb # application, so lowering optimizations to -O1 here. - CLFAGS_ASAN="-O1 -fno-omit-frame-pointer -fno-common -fsanitize=address" - CFLAGS_FOR_OVS="${CFLAGS_FOR_OVS} ${CLFAGS_ASAN}" + CFLAGS_FOR_OVS="${CFLAGS_FOR_OVS} -O1" fi save_OPTS="${OPTS} $*" diff --git a/NEWS b/NEWS index 402ce5969..79e18b85b 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,7 @@ Post-v2.15.0 - DPDK: * OVS validated with DPDK 20.11.1. It is recommended to use this version until further releases. + - New configure option '--enable-asan' enables AddressSanitizer. v2.15.0 - 15 Feb 2021 diff --git a/acinclude.m4 b/acinclude.m4 index 15a54d636..615e7f962 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -58,6 +58,22 @@ AC_DEFUN([OVS_ENABLE_WERROR], fi AC_SUBST([SPARSE_WERROR])]) +dnl OVS_ENABLE_ASAN +AC_DEFUN([OVS_ENABLE_ASAN], + [AC_ARG_ENABLE( + [asan], + [AC_HELP_STRING([--enable-asan], + [Enable the Address Sanitizer])], + [ASAN_ENABLED=yes], [ASAN_ENABLED=no]) + AC_SUBST([ASAN_ENABLED]) + AC_CONFIG_COMMANDS_PRE([ + if test "$ASAN_ENABLED" = "yes"; then + OVS_CFLAGS="$OVS_CFLAGS -fno-omit-frame-pointer" + OVS_CFLAGS="$OVS_CFLAGS -fno-common -fsanitize=address" + fi + ]) + ]) + dnl OVS_CHECK_LINUX dnl dnl Configure linux kernel source tree diff --git a/configure.ac b/configure.ac index c077034d4..eec5a9d1b 100644 --- a/configure.ac +++ b/configure.ac @@ -182,6 +182,7 @@ OVS_CONDITIONAL_CC_OPTION([-Wno-unused-parameter], [HAVE_WNO_UNUSED_PARAMETER]) OVS_CONDITIONAL_CC_OPTION([-mavx512f], [HAVE_AVX512F]) OVS_CHECK_CC_OPTION([-mavx512f], [CFLAGS="$CFLAGS -DHAVE_AVX512F"]) OVS_ENABLE_WERROR +OVS_ENABLE_ASAN OVS_ENABLE_SPARSE OVS_CTAGS_IDENTIFIERS OVS_CHECK_DPCLS_AUTOVALIDATOR diff --git a/tests/atlocal.in b/tests/atlocal.in index cfca7e192..f61e752bf 100644 --- a/tests/atlocal.in +++ b/tests/atlocal.in @@ -220,6 +220,7 @@ export OVS_SYSLOG_METHOD OVS_CTL_TIMEOUT=30 export OVS_CTL_TIMEOUT +ASAN_ENABLED='@ASAN_ENABLED@' # Add some default flags to make the tests run better under Address # Sanitizer, if it was used for the build. # From patchwork Thu May 20 13:35:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaetan Rivet X-Patchwork-Id: 1481622 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=u256.net header.i=@u256.net header.a=rsa-sha256 header.s=fm1 header.b=ollWgcBm; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm2 header.b=MUp6M17e; dkim-atps=neutral Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fm9k46sCdz9sV5 for ; Thu, 20 May 2021 23:36:12 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 791F3404C4; Thu, 20 May 2021 13:36:10 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id fQDoayX0KqSt; Thu, 20 May 2021 13:36:09 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp2.osuosl.org (Postfix) with ESMTP id 7C6E9404B6; Thu, 20 May 2021 13:36:08 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 941FEC0023; Thu, 20 May 2021 13:36:06 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 31F7AC000D for ; Thu, 20 May 2021 13:36:04 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 23F6983B99 for ; Thu, 20 May 2021 13:36:04 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp1.osuosl.org (amavisd-new); dkim=pass (2048-bit key) header.d=u256.net header.b="ollWgcBm"; dkim=pass (2048-bit key) header.d=messagingengine.com header.b="MUp6M17e" Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 5tHFtLsg06vL for ; Thu, 20 May 2021 13:36:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by smtp1.osuosl.org (Postfix) with ESMTPS id 43BF883B07 for ; Thu, 20 May 2021 13:36:01 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 5731C5C0063 for ; Thu, 20 May 2021 09:35:58 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Thu, 20 May 2021 09:35:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=u256.net; h=from :to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=fm1; bh=3J0EyAW/iM2oCD7koXFRGaBLay B8VAFSemX2uEV2u2k=; b=ollWgcBm0RTXWpZ1xeLlBuvnlzHM9Wvih/JL6VsftV OV5lCBHn1XIE32qZCISstUQYYx9wHGiHM5HZeTy7OSE+ychtIXekM6xPIr7Oz2ad UBnoalXZWVslL5eey9fi969Q9mfc/vcrOf1PJOO4PSH3tmeXScT36S+DHXz2xzPH qzpzwlajnn4T0h4j1gBFQdZtAmwEtuorGaKZQHMOSrAUioZn6I8K+HLw/uG9nm3e K+72OQ3CJHxb+jRoUDtoY+HEOmkQFhbPe/ZkVDF3UZEb3TGrJa4J/siOSYIv+UGy R0Z3NX6SljzTQyp4ve8KOCWrmhEWGecG9VsUJ678RDMg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=3J0EyAW/iM2oCD7koXFRGaBLayB8VAFSemX2uEV2u2k=; b=MUp6M17e dLmyYvmCuh7PGHf1VhcGL6lL4vQWyX3DL3awJtsVQLuNu8lZgN+Ut7rbm4RbcAKG 7tACe7LgB2zUlaY03ozsxxo+kKlJ+MJpBMcAMbbmn/Jc30UCxLJk7ilnnrit38Ez 26i0JMJBWpiIRRw6wPJIz2/ybaYZgWyCfCRC8S0BPz6ktOIp4LMEgs1h9/sv7j81 fDO6fvEGR1WieqGQH4BmqtsVUVQaleyOYkVTTi5G8tcfT1qA8u9keN0nWxGF0YXb jQzExc3LbSXghVqFrESGtVcsOZeqD/gTZhs7OgE0wGAGjiKEMwclMcquGfaofIwp +rP+2OVMwg+nMA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdejuddgieejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepifgrvghtrghnucftihhvvghtuceoghhrihhvvgesuhdvheei rdhnvghtqeenucggtffrrghtthgvrhhnpeduhfefieekieeiudetgfehkeeludektdekje ehudehffetieduheeigfekvdelfeenucffohhmrghinheprghprggthhgvrdhorhhgnecu kfhppeekiedrvdehgedrvdegfedrudegieenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpehgrhhivhgvsehuvdehiedrnhgvth X-ME-Proxy: Received: from inocybe.home (lfbn-poi-1-930-146.w86-254.abo.wanadoo.fr [86.254.243.146]) by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 20 May 2021 09:35:57 -0400 (EDT) From: Gaetan Rivet To: ovs-dev@openvswitch.org Date: Thu, 20 May 2021 15:35:46 +0200 Message-Id: <38bf92651fc4041a96771ce30dcdb65e6290a42a.1621517561.git.grive@u256.net> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [ovs-dev] [PATCH v2 2/8] tests: Add ovs-barrier unit test X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" No unit test exist currently for the ovs-barrier type. It is however crucial as a building block and should be verified to work as expected. Create a simple test verifying the basic function of ovs-barrier. Integrate the test as part of the test suite. Signed-off-by: Gaetan Rivet --- tests/automake.mk | 1 + tests/library.at | 5 + tests/test-barrier.c | 264 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 270 insertions(+) create mode 100644 tests/test-barrier.c diff --git a/tests/automake.mk b/tests/automake.mk index 1a528aa39..a32abd41c 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -448,6 +448,7 @@ tests_ovstest_SOURCES = \ tests/ovstest.h \ tests/test-aes128.c \ tests/test-atomic.c \ + tests/test-barrier.c \ tests/test-bundle.c \ tests/test-byte-order.c \ tests/test-classifier.c \ diff --git a/tests/library.at b/tests/library.at index 1702b7556..e572c22e3 100644 --- a/tests/library.at +++ b/tests/library.at @@ -246,6 +246,11 @@ AT_SETUP([ofpbuf module]) AT_CHECK([ovstest test-ofpbuf], [0], []) AT_CLEANUP +AT_SETUP([barrier module]) +AT_KEYWORDS([barrier]) +AT_CHECK([ovstest test-barrier], [0], []) +AT_CLEANUP + AT_SETUP([rcu]) AT_CHECK([ovstest test-rcu-quiesce], [0], []) AT_CLEANUP diff --git a/tests/test-barrier.c b/tests/test-barrier.c new file mode 100644 index 000000000..3bc5291cc --- /dev/null +++ b/tests/test-barrier.c @@ -0,0 +1,264 @@ +/* + * Copyright (c) 2021 NVIDIA Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +#include "ovs-thread.h" +#include "ovs-rcu.h" +#include "ovstest.h" +#include "random.h" +#include "util.h" + +#define DEFAULT_N_THREADS 4 +#define NB_STEPS 4 + +static bool verbose; +static struct ovs_barrier barrier; + +struct blocker_aux { + unsigned int tid; + bool leader; + int step; +}; + +static void * +basic_blocker_main(void *aux_) +{ + struct blocker_aux *aux = aux_; + size_t i; + + aux->step = 0; + for (i = 0; i < NB_STEPS; i++) { + ovs_barrier_block(&barrier); + aux->step++; + ovs_barrier_block(&barrier); + } + + return NULL; +} + +static void +basic_block_check(struct blocker_aux *aux, size_t n, int expected) +{ + size_t i; + + for (i = 0; i < n; i++) { + if (verbose) { + printf("aux[%" PRIuSIZE "]=%d == %d", i, aux[i].step, expected); + if (aux[i].step != expected) { + printf(" <--- X"); + } + printf("\n"); + } else { + ovs_assert(aux[i].step == expected); + } + } + ovs_barrier_block(&barrier); + ovs_barrier_block(&barrier); +} + +/* + * Basic barrier test. + * + * N writers and 1 reader participate in the test. + * Each thread goes through M steps (=NB_STEPS). + * The main thread participates as the reader. + * + * A Step is divided in three parts: + * 1. before + * (barrier) + * 2. during + * (barrier) + * 3. after + * + * Each writer updates a thread-local variable with the + * current step number within part 2 and waits. + * + * The reader checks all variables during part 3, expecting + * all variables to be equal. If any variable differs, it means + * its thread was not properly blocked by the barrier. + */ +static void +test_barrier_basic(size_t n_threads) +{ + struct blocker_aux *aux; + pthread_t *threads; + size_t i; + + ovs_barrier_init(&barrier, n_threads + 1); + + aux = xcalloc(n_threads, sizeof *aux); + threads = xmalloc(n_threads * sizeof *threads); + for (i = 0; i < n_threads; i++) { + threads[i] = ovs_thread_create("ovs-barrier", + basic_blocker_main, &aux[i]); + } + + for (i = 0; i < NB_STEPS; i++) { + basic_block_check(aux, n_threads, i); + } + ovs_barrier_destroy(&barrier); + + for (i = 0; i < n_threads; i++) { + xpthread_join(threads[i], NULL); + } + + free(threads); + free(aux); +} + +static unsigned int *shared_mem; + +static void * +lead_blocker_main(void *aux_) +{ + struct blocker_aux *aux = aux_; + size_t i; + + aux->step = 0; + for (i = 0; i < NB_STEPS; i++) { + if (aux->leader) { + shared_mem = xmalloc(sizeof *shared_mem); + if (verbose) { + printf("*T1: allocated shmem\n"); + } + } + xnanosleep(random_range(100) * 1000); + + ovs_barrier_block(&barrier); + + if (verbose) { + printf("%cT%u: ENTER, writing\n", + (aux->leader ? '*' : ' '), aux->tid); + } + + shared_mem[0] = 42; + + ovs_barrier_block(&barrier); + + if (verbose) { + printf("%cT%u: EXIT\n", + (aux->leader ? '*' : ' '), aux->tid); + } + + if (aux->leader) { + free(shared_mem); + if (verbose) { + printf("*T1: freed shmem\n"); + } + } + xnanosleep(random_range(100) * 1000); + } + + return NULL; +} + +/* + * Leader barrier test. + * + * N threads participates, one of which is marked as + * the leader (thread 0). The main thread does not + * participate. + * + * The test is divided in M steps (=NB_STEPS). + * A Step is divided in three parts: + * 1. before + * (barrier) + * 2. during + * (barrier) + * 3. after + * + * Part 1, the leader allocates a block of shared memory. + * Part 2, all threads write to the shared memory. + * Part 3: the leader frees the shared memory. + * + * If any thread is improperly blocked by the barrier, + * the shared memory accesses will trigger a segfault + * or a use-after-free if ASAN is enabled. + */ +static void +test_barrier_lead(size_t n_threads) +{ + struct blocker_aux *aux; + pthread_t *threads; + size_t i; + + ovs_barrier_init(&barrier, n_threads); + + aux = xcalloc(n_threads, sizeof *aux); + threads = xmalloc(n_threads * sizeof *threads); + + aux[0].leader = true; + + for (i = 0; i < n_threads; i++) { + aux[i].tid = i + 1; + threads[i] = ovs_thread_create("ovs-barrier", + lead_blocker_main, &aux[i]); + } + + for (i = 0; i < n_threads; i++) { + xpthread_join(threads[i], NULL); + } + + /* If the main thread does not participate in the barrier, + * it must wait for all threads to join before destroying it. + */ + ovs_barrier_destroy(&barrier); + + free(threads); + free(aux); +} + +static void +usage(char *test_name) +{ + fprintf(stderr, "Usage: %s [n_threads=%d] [-v]\n", + test_name, DEFAULT_N_THREADS); +} + +static void +test_barrier(int argc, char *argv[]) +{ + size_t n_threads = DEFAULT_N_THREADS; + char **args = argv + optind - 1; + + set_program_name(argv[0]); + + argc -= optind; + if (argc > 2) { + usage(args[0]); + return; + } + + while (argc-- > 0) { + args++; + if (!strcmp(args[0], "-v")) { + verbose = true; + } else { + n_threads = strtol(args[0], NULL, 10); + if (n_threads > 20) { + n_threads = 20; + } + } + } + + test_barrier_basic(n_threads); + test_barrier_lead(n_threads); +} + +OVSTEST_REGISTER("test-barrier", test_barrier); From patchwork Thu May 20 13:35:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaetan Rivet X-Patchwork-Id: 1481623 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=u256.net header.i=@u256.net header.a=rsa-sha256 header.s=fm1 header.b=GfSW5z36; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm2 header.b=TmorvLFP; dkim-atps=neutral Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fm9k75SVlz9sTD for ; Thu, 20 May 2021 23:36:15 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 0E31D840DB; Thu, 20 May 2021 13:36:13 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Cb2A1U4rmXtd; Thu, 20 May 2021 13:36:11 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTP id D7C7683BB0; Thu, 20 May 2021 13:36:09 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 97C3EC002A; Thu, 20 May 2021 13:36:07 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by lists.linuxfoundation.org (Postfix) with ESMTP id 48A2EC000F for ; Thu, 20 May 2021 13:36:04 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 1A250405DC for ; Thu, 20 May 2021 13:36:04 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp4.osuosl.org (amavisd-new); dkim=pass (2048-bit key) header.d=u256.net header.b="GfSW5z36"; dkim=pass (2048-bit key) header.d=messagingengine.com header.b="TmorvLFP" Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xo46uWCR8XLT for ; Thu, 20 May 2021 13:36:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by smtp4.osuosl.org (Postfix) with ESMTPS id 089F84059F for ; Thu, 20 May 2021 13:36:00 +0000 (UTC) Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id EB4F25C010E for ; Thu, 20 May 2021 09:35:58 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Thu, 20 May 2021 09:35:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=u256.net; h=from :to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=fm1; bh=MXeSeoq55lgpCfVuvZhHLpnYyZ 58xNvEANSeZNcgFG0=; b=GfSW5z3662pAmvvlUE2g59o5cS4YX7j7k0e5q1xq+Z U8yRZymWwv748pfmCz7SNHAoM311Qu0Y1lNs0QjHbAMZ8w9yoIkAdltqeLnuhhgN 0QFZYLxfU2bTotF6BeBsgbIgNqniVj/BkDq0vRbmtivqsFohNt4/7hMBY9zY9Nem 36konwWWoP0ilNuDemyO1DOToTsg0t+TDsWCLF9kGTXPqpbs4HzYrsn3cwjSEbag jxm2/5Y9oTPdi3Mpnw3bqdwEpeiXQNWKLLlP68iFSiEZ5rmusrViFiGZzGZr7QsI iCUSGL5NtqIGebOP/NkMi2Ha3r/d1b8Zd84JBJFZk8+A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=MXeSeoq55lgpCfVuvZhHLpnYyZ58xNvEANSeZNcgFG0=; b=TmorvLFP S8l05b6GQol+x3+Nn7jbrBtReyVhLb/LvQZ64Yoz3AN94cfTyqXYMxmiIMB2Hesv VAEG2NDyOsI9CNgPQZWb2BRQ197MfstTBRaMOlkftM920a4OSBV505cC2uXPsvml KNl2B6lFs+4WPe2fLxHKFylCq/3NcGNGhBIupYqRsIrKJFvhw8EIoUgUgx/d/HUe af+iZmSuluchdJvpdGLDe+HaCUPGdzR7IirqPFBx3QZtUgrNT9p/nH+veAbLdw/2 aS3+KxxZAz5ydEIs88mjPqfZi6Q1f9jdBUtIRa1eq5/DbD4Tc/NaWAa3PJAL0QoB RwnZN7sHqt1JsA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdejuddgieejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepifgrvghtrghnucftihhvvghtuceoghhrihhvvgesuhdvheei rdhnvghtqeenucggtffrrghtthgvrhhnpeehgfevffekteehteefieefvdehleefjeefhe evudetjefhkeeutdekieeuvdetheenucfkphepkeeirddvheegrddvgeefrddugeeinecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepghhrihhvvg esuhdvheeirdhnvght X-ME-Proxy: Received: from inocybe.home (lfbn-poi-1-930-146.w86-254.abo.wanadoo.fr [86.254.243.146]) by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 20 May 2021 09:35:58 -0400 (EDT) From: Gaetan Rivet To: ovs-dev@openvswitch.org Date: Thu, 20 May 2021 15:35:47 +0200 Message-Id: <9a79d2906cac9fe5b6f58e49e27e2bdb625450cf.1621517561.git.grive@u256.net> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [ovs-dev] [PATCH v2 3/8] tests: Add RCU postpone test X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Add a simple postponing test verifying RCU callbacks have executed and RCU exits in order. Add as part of library unit-tests. Signed-off-by: Gaetan Rivet --- tests/library.at | 8 ++++++- tests/test-rcu.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/tests/library.at b/tests/library.at index e572c22e3..6e8a154e5 100644 --- a/tests/library.at +++ b/tests/library.at @@ -251,10 +251,16 @@ AT_KEYWORDS([barrier]) AT_CHECK([ovstest test-barrier], [0], []) AT_CLEANUP -AT_SETUP([rcu]) +AT_SETUP([rcu quiescing]) +AT_KEYWORDS([rcu]) AT_CHECK([ovstest test-rcu-quiesce], [0], []) AT_CLEANUP +AT_SETUP([rcu postponing]) +AT_KEYWORDS([rcu]) +AT_CHECK([ovstest test-rcu], [0], []) +AT_CLEANUP + AT_SETUP([stopwatch module]) AT_CHECK([ovstest test-stopwatch], [0], [...... ], [ignore]) diff --git a/tests/test-rcu.c b/tests/test-rcu.c index 965f3c49f..38e1cbb6f 100644 --- a/tests/test-rcu.c +++ b/tests/test-rcu.c @@ -49,3 +49,64 @@ test_rcu_quiesce(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } OVSTEST_REGISTER("test-rcu-quiesce", test_rcu_quiesce); + +struct rcu_user_aux { + bool done; +}; + +static void +rcu_user_deferred(struct rcu_user_aux *aux) +{ + aux->done = true; +} + +static void * +rcu_user_main(void *aux_) +{ + struct rcu_user_aux *aux = aux_; + + ovsrcu_quiesce(); + + aux->done = false; + ovsrcu_postpone(rcu_user_deferred, aux); + + ovsrcu_quiesce(); + + return NULL; +} + +#define N_THREAD 4 + +static void +test_rcu(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +{ + struct rcu_user_aux main_aux = {0}; + struct rcu_user_aux aux[N_THREAD]; + pthread_t users[N_THREAD]; + size_t i; + + memset(aux, 0, sizeof aux); + + for (i = 0; i < ARRAY_SIZE(users); i++) { + users[i] = ovs_thread_create("user", rcu_user_main, &aux[i]); + } + + for (i = 0; i < ARRAY_SIZE(users); i++) { + xpthread_join(users[i], NULL); + } + + /* Register a last callback and verify that it will be properly executed + * even if the RCU lib is exited without this thread quiescing. + */ + ovsrcu_postpone(rcu_user_deferred, &main_aux); + + ovsrcu_exit(); + + ovs_assert(main_aux.done); + + for (i = 0; i < ARRAY_SIZE(users); i++) { + ovs_assert(aux[i].done); + } +} + +OVSTEST_REGISTER("test-rcu", test_rcu); From patchwork Thu May 20 13:35:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaetan Rivet X-Patchwork-Id: 1481633 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=u256.net header.i=@u256.net header.a=rsa-sha256 header.s=fm1 header.b=BIxec67w; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm2 header.b=iYPJKGqd; dkim-atps=neutral Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fm9kM4dzZz9sV5 for ; Thu, 20 May 2021 23:36:27 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id C4EDF84421; Thu, 20 May 2021 13:36:19 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id s9yqFxLQ_dwB; Thu, 20 May 2021 13:36:16 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTP id 773F5842F4; Thu, 20 May 2021 13:36:14 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id A55D2C0032; Thu, 20 May 2021 13:36:10 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [IPv6:2605:bc80:3010::137]) by lists.linuxfoundation.org (Postfix) with ESMTP id 6E715C0001 for ; Thu, 20 May 2021 13:36:05 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 4F421405BE for ; Thu, 20 May 2021 13:36:05 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp4.osuosl.org (amavisd-new); dkim=pass (2048-bit key) header.d=u256.net header.b="BIxec67w"; dkim=pass (2048-bit key) header.d=messagingengine.com header.b="iYPJKGqd" Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id G73uzU-4Vule for ; Thu, 20 May 2021 13:36:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by smtp4.osuosl.org (Postfix) with ESMTPS id 075C7404B5 for ; Thu, 20 May 2021 13:36:00 +0000 (UTC) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 856055C01B7 for ; Thu, 20 May 2021 09:35:59 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Thu, 20 May 2021 09:35:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=u256.net; h=from :to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=fm1; bh=4ya0fOHV8i/13olTgTVfox7loI 75eJhqhxCM+suIZyk=; b=BIxec67wPAOlh0iXEZrIba6JR6hc0j9hVCZTCTY1hU WujnjcWDICzCddPclF8Bqt5sKxG8i03nmE3beWQqNWdK9ktdoh52vZvWsWDUGIEi vUqclncZtfHqEOgk+GrszaJteI4Sw6HsccAZ0KhVmYK2eJpuarRXB/J/1cwQqjk5 xJ6wVO9DXp1jLpRxK+Wk1BiE+Fryo6JbZs4k1iZ3SrMLYnO1ZTGQgNvMQhQyZDd5 WtK1884pAWoPgbiKzYOf9MFYX78iBX7/1J3MIGV5fLcgHOpw93Ta1LkD9t6mJ1P2 rcaFgjLP2Ujuy7SnS2piB7vdPc016kSk8oonZNQfAK6w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=4ya0fOHV8i/13olTgTVfox7loI75eJhqhxCM+suIZyk=; b=iYPJKGqd /D+sLCcC53a6GTLw0YQivlLThGzIA0Hsn9HHRJIE7CgOmwISlSFZZHXFj6sUCPlK TmHwFG8LrG/ymqCefTuip0A0bXx8hEH0iknhxQsj5OC3nVVKCQ0Gp4hwDyykuDtZ XbjVjYybHXA0nPk+5XAOgp5iRIRHa9HrGnPwe4HAE1tOBhKs23ajSLykcnS/8nV4 qT1V6jFz8Yqd0rb9WTYZVSXdd7qiuB6bzK7mxM7V15IgWmdjO9+sTytmFs4nhLzr UB7vpFj2zYGf6hez8jXk6mHFZXT7/9VfQaw8c61ZhkVxqKEHINpoJzAAZeTzshmA hp014yB1fgjbZg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdejuddgieeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepifgrvghtrghnucftihhvvghtuceoghhrihhvvgesuhdvheei rdhnvghtqeenucggtffrrghtthgvrhhnpeduhfefieekieeiudetgfehkeeludektdekje ehudehffetieduheeigfekvdelfeenucffohhmrghinheprghprggthhgvrdhorhhgnecu kfhppeekiedrvdehgedrvdegfedrudegieenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpehgrhhivhgvsehuvdehiedrnhgvth X-ME-Proxy: Received: from inocybe.home (lfbn-poi-1-930-146.w86-254.abo.wanadoo.fr [86.254.243.146]) by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 20 May 2021 09:35:59 -0400 (EDT) From: Gaetan Rivet To: ovs-dev@openvswitch.org Date: Thu, 20 May 2021 15:35:48 +0200 Message-Id: <8f94d7e841a268b797b861a3f75cbdfe3fa1d791.1621517561.git.grive@u256.net> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [ovs-dev] [PATCH v2 4/8] tests: Add ASAN use-after-free validation with RCU X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" When using the RCU mechanism and deferring memory reclamation, potential use-after-free due to incorrect use of RCU can be hidden. Add a test triggering a UAF event. When the test suite is built with AddressSanitizer support, verify that the event triggers and the tool is usable with RCU. Signed-off-by: Gaetan Rivet --- tests/automake.mk | 1 + tests/library.at | 33 +++++++++++++++ tests/test-rcu-uaf.c | 98 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 tests/test-rcu-uaf.c diff --git a/tests/automake.mk b/tests/automake.mk index a32abd41c..4420a3f7f 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -472,6 +472,7 @@ tests_ovstest_SOURCES = \ tests/test-packets.c \ tests/test-random.c \ tests/test-rcu.c \ + tests/test-rcu-uaf.c \ tests/test-reconnect.c \ tests/test-rstp.c \ tests/test-sflow.c \ diff --git a/tests/library.at b/tests/library.at index 6e8a154e5..4a549f77e 100644 --- a/tests/library.at +++ b/tests/library.at @@ -261,6 +261,39 @@ AT_KEYWORDS([rcu]) AT_CHECK([ovstest test-rcu], [0], []) AT_CLEANUP +AT_SETUP([rcu quiesce use-after-free detection]) +AT_SKIP_IF([test "$IS_WIN32" = "yes"]) +AT_SKIP_IF([test "$ASAN_ENABLED" = "no"]) +# SIGABRT + 128 +exit_status=134 +AT_KEYWORDS([rcu asan]) +AT_CHECK([ovstest test-rcu-uaf quiesce], [$exit_status], [ignore], [ignore]) +# ASAN report is expected on success. +rm asan.* +AT_CLEANUP + +AT_SETUP([rcu try-quiesce use-after-free detection]) +AT_SKIP_IF([test "$IS_WIN32" = "yes"]) +AT_SKIP_IF([test "$ASAN_ENABLED" = "no"]) +# SIGABRT + 128 +exit_status=134 +AT_KEYWORDS([rcu asan]) +AT_CHECK([ovstest test-rcu-uaf try-quiesce], [$exit_status], [ignore], [ignore]) +# ASAN report is expected on success. +rm asan.* +AT_CLEANUP + +AT_SETUP([rcu quiesce-start-end use-after-free detection]) +AT_SKIP_IF([test "$IS_WIN32" = "yes"]) +AT_SKIP_IF([test "$ASAN_ENABLED" = "no"]) +AT_KEYWORDS([rcu asan]) +# SIGABRT + 128 +exit_status=134 +AT_CHECK([ovstest test-rcu-uaf quiesce-start-end], [$exit_status], [ignore], [ignore]) +# ASAN report is expected on success. +rm asan.* +AT_CLEANUP + AT_SETUP([stopwatch module]) AT_CHECK([ovstest test-stopwatch], [0], [...... ], [ignore]) diff --git a/tests/test-rcu-uaf.c b/tests/test-rcu-uaf.c new file mode 100644 index 000000000..f97738795 --- /dev/null +++ b/tests/test-rcu-uaf.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2021 NVIDIA Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +#include "ovs-thread.h" +#include "ovs-rcu.h" +#include "ovstest.h" +#include "util.h" + +enum ovsrcu_uaf_type { + OVSRCU_UAF_QUIESCE, + OVSRCU_UAF_TRY_QUIESCE, + OVSRCU_UAF_QUIESCE_START_END, +}; + +static void * +rcu_uaf_main(void *aux) +{ + enum ovsrcu_uaf_type *type = aux; + char *xx = xmalloc(2); + + xx[0] = 'a'; + ovsrcu_postpone(free, xx); + switch (*type) { + case OVSRCU_UAF_QUIESCE: + ovsrcu_quiesce(); + break; + case OVSRCU_UAF_TRY_QUIESCE: + while (ovsrcu_try_quiesce()) { + ; + } + break; + case OVSRCU_UAF_QUIESCE_START_END: + ovsrcu_quiesce_start(); + ovsrcu_quiesce_end(); + break; + default: + OVS_NOT_REACHED(); + } + xx[1] = 'b'; + + return NULL; +} + +static void +usage(char *test_name) +{ + fprintf(stderr, "Usage: %s \n", + test_name); +} + +static void +test_rcu_uaf(int argc, char *argv[]) +{ + char **args = argv + optind - 1; + enum ovsrcu_uaf_type type; + pthread_t quiescer; + + if (argc - optind != 1) { + usage(args[0]); + return; + } + + set_program_name(argv[0]); + + if (!strcmp(args[1], "quiesce")) { + type = OVSRCU_UAF_QUIESCE; + } else if (!strcmp(args[1], "try-quiesce")) { + type = OVSRCU_UAF_TRY_QUIESCE; + } else if (!strcmp(args[1], "quiesce-start-end")) { + type = OVSRCU_UAF_QUIESCE_START_END; + } else { + usage(args[0]); + return; + } + + /* Need to create a separate thread, to support try-quiesce. */ + quiescer = ovs_thread_create("rcu-uaf", rcu_uaf_main, &type); + xpthread_join(quiescer, NULL); +} + +OVSTEST_REGISTER("test-rcu-uaf", test_rcu_uaf); From patchwork Thu May 20 13:35:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaetan Rivet X-Patchwork-Id: 1481621 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=u256.net header.i=@u256.net header.a=rsa-sha256 header.s=fm1 header.b=d1XJ9N/T; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm2 header.b=dsSSzXd4; dkim-atps=neutral Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fm9k23C7Fz9sTD for ; Thu, 20 May 2021 23:36:10 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id D1E2283CCA; Thu, 20 May 2021 13:36:07 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id QhfV4OQzRK_z; Thu, 20 May 2021 13:36:06 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTP id 037A083B0D; Thu, 20 May 2021 13:36:06 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id C77DAC000D; Thu, 20 May 2021 13:36:05 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 279D3C0001 for ; Thu, 20 May 2021 13:36:04 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 165124049B for ; Thu, 20 May 2021 13:36:04 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp2.osuosl.org (amavisd-new); dkim=pass (2048-bit key) header.d=u256.net header.b="d1XJ9N/T"; dkim=pass (2048-bit key) header.d=messagingengine.com header.b="dsSSzXd4" Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id q0RCf0Czx9bu for ; Thu, 20 May 2021 13:36:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by smtp2.osuosl.org (Postfix) with ESMTPS id D25F3401B7 for ; Thu, 20 May 2021 13:36:01 +0000 (UTC) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 82B7C5C01E3 for ; Thu, 20 May 2021 09:36:00 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 20 May 2021 09:36:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=u256.net; h=from :to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=fm1; bh=lJ6+Jhax9UCDnizTO8oOunFeh8 8ZoBz10j40A7uY5iM=; b=d1XJ9N/TCwh9qJ/WFPb5QkUtXAy86FJUY3rf0r2Kyg XSUbEcfneyZFhuu8ID0NZY1fmJ8WnM1vkDUeqRH0uRrOEZ7t3XypXuo6nY6hkC7Y ciLI93andSKGmjfB8GMutbgaZqjTG095Dxft7ll8nHGSM/bmGE3LDUI8nORYGXz/ oRkUl11LmVfTEIraPV1LKLZpr0mJ2jpK0j404hEifmADmT0lhrOfxTA52OnmPQmJ 0hJcVbN5ynSAB5oaQocP3HxYG9DvanXuEemUHltgo7KW0G0TpUEZOcOGsAq0BrdI PiuIOj62utCFRwbati+6XDCyEM5TflB7QcGAt4k68Hwg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=lJ6+Jhax9UCDnizTO8oOunFeh88ZoBz10j40A7uY5iM=; b=dsSSzXd4 W5F8W0Ew0xtp8HvwN7d6gnmJzNIhuzYh/2B3yQnEw8JSeyKqAtutFfsjRfby7uvi gCid/KQCkrCfXGH8ZY1e+ZMMKW+FZhNrSTC/YKD1qFkSrdMmyJ0gUQYyv48DAQq+ iAuZEuY2hIPYF3KSJEQ7eVsNghXTJmkaijo0el23fDQpQfcH0mv24KTEhw0/XKgc q17jfXERjPCFVG/jFdNUWZPeibYzbRUj62tgBFaENW6glw0/RDEWWfuiXkVhsYj1 xr0dcai1aNb2brdi0kH73UbJcCnDTztTqf+enYoHH3UFiqBhp2QHkr3XMlaET9qW Yramrdi7Mza6GQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdejuddgieejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepifgrvghtrghnucftihhvvghtuceoghhrihhvvgesuhdvheei rdhnvghtqeenucggtffrrghtthgvrhhnpeehgfevffekteehteefieefvdehleefjeefhe evudetjefhkeeutdekieeuvdetheenucfkphepkeeirddvheegrddvgeefrddugeeinecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepghhrihhvvg esuhdvheeirdhnvght X-ME-Proxy: Received: from inocybe.home (lfbn-poi-1-930-146.w86-254.abo.wanadoo.fr [86.254.243.146]) by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 20 May 2021 09:35:59 -0400 (EDT) From: Gaetan Rivet To: ovs-dev@openvswitch.org Date: Thu, 20 May 2021 15:35:49 +0200 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [ovs-dev] [PATCH v2 5/8] ovs-thread: Fix barrier use-after-free X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" When a thread is blocked on a barrier, there is no guarantee regarding the moment it will resume, only that it will at some point in the future. One thread can resume first then proceed to destroy the barrier while another thread has not yet awoken. When it finally happens, the second thread will attempt a seq_read() on the barrier seq, while the first thread have already destroyed it, triggering a use-after-free. Introduce an additional indirection layer within the barrier. A internal barrier implementation holds all the necessary elements for a thread to safely block and destroy. Whenever a barrier is destroyed, the internal implementation is left available to still blocking threads if necessary. A reference counter is used to track threads still using the implementation. Note that current uses of ovs-barrier are not affected: RCU and revalidators will not destroy their barrier immediately after blocking on it. Fixes: d8043da7182a ("ovs-thread: Implement OVS specific barrier.") Signed-off-by: Gaetan Rivet --- lib/ovs-thread.c | 61 +++++++++++++++++++++++++++++++++++++++--------- lib/ovs-thread.h | 6 ++--- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/lib/ovs-thread.c b/lib/ovs-thread.c index b686e4548..805cba622 100644 --- a/lib/ovs-thread.c +++ b/lib/ovs-thread.c @@ -299,21 +299,53 @@ ovs_spin_init(const struct ovs_spin *spin) } #endif +struct ovs_barrier_impl { + uint32_t size; /* Number of threads to wait. */ + atomic_count count; /* Number of threads already hit the barrier. */ + struct seq *seq; + struct ovs_refcount refcnt; +}; + +static void +ovs_barrier_impl_ref(struct ovs_barrier_impl *impl) +{ + ovs_refcount_ref(&impl->refcnt); +} + +static void +ovs_barrier_impl_unref(struct ovs_barrier_impl *impl) +{ + if (ovs_refcount_unref(&impl->refcnt) == 1) { + seq_destroy(impl->seq); + free(impl); + } +} + /* Initializes the 'barrier'. 'size' is the number of threads * expected to hit the barrier. */ void ovs_barrier_init(struct ovs_barrier *barrier, uint32_t size) { - barrier->size = size; - atomic_count_init(&barrier->count, 0); - barrier->seq = seq_create(); + struct ovs_barrier_impl *impl; + + impl = xmalloc(sizeof *impl); + impl->size = size; + atomic_count_init(&impl->count, 0); + impl->seq = seq_create(); + ovs_refcount_init(&impl->refcnt); + + ovsrcu_set(&barrier->impl, impl); } /* Destroys the 'barrier'. */ void ovs_barrier_destroy(struct ovs_barrier *barrier) { - seq_destroy(barrier->seq); + struct ovs_barrier_impl *impl; + + impl = ovsrcu_get(struct ovs_barrier_impl *, &barrier->impl); + ovsrcu_set(&barrier->impl, NULL); + ovs_barrier_impl_unref(impl); } /* Makes the calling thread block on the 'barrier' until all @@ -325,23 +357,30 @@ ovs_barrier_destroy(struct ovs_barrier *barrier) void ovs_barrier_block(struct ovs_barrier *barrier) { - uint64_t seq = seq_read(barrier->seq); + struct ovs_barrier_impl *impl; uint32_t orig; + uint64_t seq; - orig = atomic_count_inc(&barrier->count); - if (orig + 1 == barrier->size) { - atomic_count_set(&barrier->count, 0); + impl = ovsrcu_get(struct ovs_barrier_impl *, &barrier->impl); + ovs_barrier_impl_ref(impl); + + seq = seq_read(impl->seq); + orig = atomic_count_inc(&impl->count); + if (orig + 1 == impl->size) { + atomic_count_set(&impl->count, 0); /* seq_change() serves as a release barrier against the other threads, * so the zeroed count is visible to them as they continue. */ - seq_change(barrier->seq); + seq_change(impl->seq); } else { /* To prevent thread from waking up by other event, * keeps waiting for the change of 'barrier->seq'. */ - while (seq == seq_read(barrier->seq)) { - seq_wait(barrier->seq, seq); + while (seq == seq_read(impl->seq)) { + seq_wait(impl->seq, seq); poll_block(); } } + + ovs_barrier_impl_unref(impl); } DEFINE_EXTERN_PER_THREAD_DATA(ovsthread_id, OVSTHREAD_ID_UNSET); diff --git a/lib/ovs-thread.h b/lib/ovs-thread.h index 7ee98bd4e..3b444ccdc 100644 --- a/lib/ovs-thread.h +++ b/lib/ovs-thread.h @@ -21,16 +21,16 @@ #include #include #include "ovs-atomic.h" +#include "ovs-rcu.h" #include "openvswitch/thread.h" #include "util.h" struct seq; /* Poll-block()-able barrier similar to pthread_barrier_t. */ +struct ovs_barrier_impl; struct ovs_barrier { - uint32_t size; /* Number of threads to wait. */ - atomic_count count; /* Number of threads already hit the barrier. */ - struct seq *seq; + OVSRCU_TYPE(struct ovs_barrier_impl *) impl; }; /* Wrappers for pthread_mutexattr_*() that abort the process on any error. */ From patchwork Thu May 20 13:35:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaetan Rivet X-Patchwork-Id: 1481627 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=u256.net header.i=@u256.net header.a=rsa-sha256 header.s=fm1 header.b=myMMOV1m; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm2 header.b=K/uH6nWG; dkim-atps=neutral Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fm9kH4KkDz9sWW for ; Thu, 20 May 2021 23:36:23 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id BB39C83DF8; Thu, 20 May 2021 13:36:16 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8kLb4IfayqLq; Thu, 20 May 2021 13:36:15 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTP id A97FC83D59; Thu, 20 May 2021 13:36:12 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 9E7B9C002F; Thu, 20 May 2021 13:36:09 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 36605C0001 for ; Thu, 20 May 2021 13:36:05 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 351F083B07 for ; Thu, 20 May 2021 13:36:04 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qLpeu1I3uGeQ for ; Thu, 20 May 2021 13:36:03 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by smtp1.osuosl.org (Postfix) with ESMTPS id A731083B0D for ; Thu, 20 May 2021 13:36:03 +0000 (UTC) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 241C75C010E for ; Thu, 20 May 2021 09:36:01 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Thu, 20 May 2021 09:36:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=u256.net; h=from :to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=fm1; bh=ENEjZXBBD2TeNNamaGEHXFnPpK cjacPUN4bgCWy6Ytg=; b=myMMOV1mqAZiZDnbpGyrjwsP/gM/dgWirb5SDmGMJF 3WkfwQ6PjHF0ZOK/oJSY4j3PEwEAkgoA/OLVcJs8Mk/N2rNXQzJAqjxZ9vvMPND7 L7CkHR1EvxWP1JvjDRWAo1QFiriQauMIKeoNpznWWXnWEL8SMC3pOvde1QeXXzGa wo02a/XQGm7z2vFbEHBXfmb/Mc2r4JrxlEED+EarN4/MMDgZBRapDr1tQWgdcvii Z8bBO/I1plUm0E2llnM2KY1SbNNKHvd3h3IALrRCpa0v0um2ZteQS0D5EIYSkl/K 5tEInwY6PShswFuqfez3o1zvPvejJL3jsZ97MPspyg3A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=ENEjZXBBD2TeNNamaGEHXFnPpKcjacPUN4bgCWy6Ytg=; b=K/uH6nWG IGzKgE47kFco4pWcFkxJGqFfCWi4grcmwLvHCmVGopBlWwqDg4KHOm2C0iR0EC/I 9/29LQxh2a+CBVUlAOBlS+2UuFjr6qVd2NqatQF+LC7qqkTeimpxJy4mZBA9DLAt +w3lhhcWb6jLOW9igdiyVK44fW4rpINmSiYpm5FqxoKycykxG7QujMh0kiQlmAQ0 Jkwr3ac4XMva/LLDAhx84G4XLnkNJ7fsv+wfIfr6GOuLW0leV5Pt/5HT3iiz2C2J Tn8BiLHFB2jROVDEo8ORGU/NYKSKFMOEWekdj99KOEjHDL2NPTG8rb0x+jFH+iT8 l3q+OLX+fGSokQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdejuddgieejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepifgrvghtrghnucftihhvvghtuceoghhrihhvvgesuhdvheei rdhnvghtqeenucggtffrrghtthgvrhhnpeehgfevffekteehteefieefvdehleefjeefhe evudetjefhkeeutdekieeuvdetheenucfkphepkeeirddvheegrddvgeefrddugeeinecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepghhrihhvvg esuhdvheeirdhnvght X-ME-Proxy: Received: from inocybe.home (lfbn-poi-1-930-146.w86-254.abo.wanadoo.fr [86.254.243.146]) by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 20 May 2021 09:36:00 -0400 (EDT) From: Gaetan Rivet To: ovs-dev@openvswitch.org Date: Thu, 20 May 2021 15:35:50 +0200 Message-Id: <08ee1d5d77b710a12bdd236a54a3621e85914791.1621517561.git.grive@u256.net> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [ovs-dev] [PATCH v2 6/8] ovs-thread: Quiesce when joining pthreads X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Joining pthreads makes the caller quiescent. It should register as such, as joined threads may wait on an RCU callback executing before quitting, deadlocking the caller. Signed-off-by: Gaetan Rivet --- lib/ovs-thread.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/ovs-thread.c b/lib/ovs-thread.c index 805cba622..bf58923f8 100644 --- a/lib/ovs-thread.c +++ b/lib/ovs-thread.c @@ -180,8 +180,6 @@ XPTHREAD_FUNC1(pthread_cond_destroy, pthread_cond_t *); XPTHREAD_FUNC1(pthread_cond_signal, pthread_cond_t *); XPTHREAD_FUNC1(pthread_cond_broadcast, pthread_cond_t *); -XPTHREAD_FUNC2(pthread_join, pthread_t, void **); - typedef void destructor_func(void *); XPTHREAD_FUNC2(pthread_key_create, pthread_key_t *, destructor_func *); XPTHREAD_FUNC1(pthread_key_delete, pthread_key_t); @@ -191,6 +189,20 @@ XPTHREAD_FUNC2(pthread_setspecific, pthread_key_t, const void *); XPTHREAD_FUNC3(pthread_sigmask, int, const sigset_t *, sigset_t *); #endif +void +xpthread_join(pthread_t thread, void **retval) +{ + int error; + + ovsrcu_quiesce_start(); + error = pthread_join(thread, retval); + ovsrcu_quiesce_end(); + + if (OVS_UNLIKELY(error)) { + ovs_abort(error, "%s failed", __func__); + } +} + static void ovs_mutex_init__(const struct ovs_mutex *l_, int type) { From patchwork Thu May 20 13:35:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaetan Rivet X-Patchwork-Id: 1481624 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=u256.net header.i=@u256.net header.a=rsa-sha256 header.s=fm1 header.b=fr6i86Xg; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm2 header.b=c+unNrLp; dkim-atps=neutral Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fm9kC0rbdz9sTD for ; Thu, 20 May 2021 23:36:19 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id F002D83CF9; Thu, 20 May 2021 13:36:14 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qlf18uecDFCo; Thu, 20 May 2021 13:36:13 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTP id 238DB83BBC; Thu, 20 May 2021 13:36:11 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id AF12FC0022; Thu, 20 May 2021 13:36:08 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 66830C0001 for ; Thu, 20 May 2021 13:36:04 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 48E90607BE for ; Thu, 20 May 2021 13:36:04 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp3.osuosl.org (amavisd-new); dkim=pass (2048-bit key) header.d=u256.net header.b="fr6i86Xg"; dkim=pass (2048-bit key) header.d=messagingengine.com header.b="c+unNrLp" Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id nZx7tGAgxETP for ; Thu, 20 May 2021 13:36:03 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by smtp3.osuosl.org (Postfix) with ESMTPS id B28BB605D9 for ; Thu, 20 May 2021 13:36:03 +0000 (UTC) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id B27D15C01B7 for ; Thu, 20 May 2021 09:36:01 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Thu, 20 May 2021 09:36:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=u256.net; h=from :to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=fm1; bh=fPf3qvElfOarNlye+EYY+qXW9k FSK71qaIqX3c+pQIk=; b=fr6i86Xgp/MXO3jn5LEPUSu1z8LMj5wdkfUvL5hd+R gylUwYa7B07Hs8tm/D66bDkwsQNYWrp79DlhOIrn0DVdIkzKx59ARhMmH6xTawTD 7CoTHQVKZswnzlD78+rq6OnvcFPXrWfnfC/nEl0r6Qk59c39G5wYYC5ACEtplFkq mDd7CYuwsNc2ubglBwvyus2eT1qkm1tyfUmjDIiBcxo/9JezvlirEYvOdpenfbqM RWvYn7jhrwos0ENHpfEUUjy1kFRf5T4rT9erE6kh4eEZVc+zPpGQKbGr5u8xqucj gtmRbMXSZ8kdaTZcSoTR9pxyxaI75LUnHjA7VAYUaekQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=fPf3qvElfOarNlye+EYY+qXW9kFSK71qaIqX3c+pQIk=; b=c+unNrLp EMet3EZInKZjJrwDlCdjjrlHiG8ZZlO1RM0AIcVkY4njlsGnO4vp2Cdl/aeNrbNI g0RQpK0mfT4OpLHDQA/aRn0L2zcyzDwAceeT69OdWMT24S4G/exK0AqZ1LvTdnXk d9QqrsTHWWRVW/LE0bKcXDgx/vpBM5qOOp2LgvHVyBy7O511ItI7WqIizSYDcSos qi6Da7dCIa56QJY++4bT6TASMTJh5rRFkBmc/SgCPJCSRJYsTCwFknt05wO4AaGe nbcEuT+8SRP1azHhFO7KUKgZcNAtsmyNn7a1HiSP+4wAbz1gBzXpwuZM52VB7gYg DFDPpJAkZsnHgw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdejuddgieeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepifgrvghtrghnucftihhvvghtuceoghhrihhvvgesuhdvheei rdhnvghtqeenucggtffrrghtthgvrhhnpeehgfevffekteehteefieefvdehleefjeefhe evudetjefhkeeutdekieeuvdetheenucfkphepkeeirddvheegrddvgeefrddugeeinecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepghhrihhvvg esuhdvheeirdhnvght X-ME-Proxy: Received: from inocybe.home (lfbn-poi-1-930-146.w86-254.abo.wanadoo.fr [86.254.243.146]) by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 20 May 2021 09:36:01 -0400 (EDT) From: Gaetan Rivet To: ovs-dev@openvswitch.org Date: Thu, 20 May 2021 15:35:51 +0200 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [ovs-dev] [PATCH v2 7/8] ovs-rcu: Remove unused perthread mutex X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" A mutex is allocated, initialized and destroyed, without being used in the perthread structure. Signed-off-by: Gaetan Rivet Acked-by: Ben Pfaff --- lib/ovs-rcu.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/ovs-rcu.c b/lib/ovs-rcu.c index cde1e925b..1866bd308 100644 --- a/lib/ovs-rcu.c +++ b/lib/ovs-rcu.c @@ -47,7 +47,6 @@ struct ovsrcu_cbset { struct ovsrcu_perthread { struct ovs_list list_node; /* In global list. */ - struct ovs_mutex mutex; uint64_t seqno; struct ovsrcu_cbset *cbset; char name[16]; /* This thread's name. */ @@ -84,7 +83,6 @@ ovsrcu_perthread_get(void) const char *name = get_subprogram_name(); perthread = xmalloc(sizeof *perthread); - ovs_mutex_init(&perthread->mutex); perthread->seqno = seq_read(global_seqno); perthread->cbset = NULL; ovs_strlcpy(perthread->name, name[0] ? name : "main", @@ -406,7 +404,6 @@ ovsrcu_unregister__(struct ovsrcu_perthread *perthread) ovs_list_remove(&perthread->list_node); ovs_mutex_unlock(&ovsrcu_threads_mutex); - ovs_mutex_destroy(&perthread->mutex); free(perthread); seq_change(global_seqno); From patchwork Thu May 20 13:35:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaetan Rivet X-Patchwork-Id: 1481629 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=u256.net header.i=@u256.net header.a=rsa-sha256 header.s=fm1 header.b=o/+z2BBm; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm2 header.b=WLrU35Za; dkim-atps=neutral Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fm9kK1Dy6z9sWc for ; Thu, 20 May 2021 23:36:25 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 61B1340584; Thu, 20 May 2021 13:36:23 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Xuu5fWVgH880; Thu, 20 May 2021 13:36:19 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp2.osuosl.org (Postfix) with ESMTP id 1038140570; Thu, 20 May 2021 13:36:18 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id AB8DAC0041; Thu, 20 May 2021 13:36:12 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 0AE26C001C for ; Thu, 20 May 2021 13:36:06 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 90A4983B07 for ; Thu, 20 May 2021 13:36:05 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp1.osuosl.org (amavisd-new); dkim=pass (2048-bit key) header.d=u256.net header.b="o/+z2BBm"; dkim=pass (2048-bit key) header.d=messagingengine.com header.b="WLrU35Za" Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PAgXwUn4KM_3 for ; Thu, 20 May 2021 13:36:03 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by smtp1.osuosl.org (Postfix) with ESMTPS id A608B83926 for ; Thu, 20 May 2021 13:36:03 +0000 (UTC) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 55DE15C01CA for ; Thu, 20 May 2021 09:36:02 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Thu, 20 May 2021 09:36:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=u256.net; h=from :to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=fm1; bh=fWVyBxzRpx8GFDnoO/0oh5e3Vp qbEgfOmmGSbk6tepY=; b=o/+z2BBmH6lgWP6qezL8P6ODIATKXp+HrAQ8yG4KJs BpycdIJiZ3A5Ea+LmIN8fb2hNUReIykyiLbaRUmqZA7lUsm7n8Na9Ad0jJ7nrmcu wD1dU3Oy9flTnl/gACdcarjIKSCl9UkLT3NzA81vyPUwLzwKrZKHC+GNQbTcrVJa 9lQu391mPcl86K2OP1aJcMaJYpJEm5d3JGf9ybod7hnfjWEHUJSlFAgY92LwV1Xs 79YPhcOZxVMdIdAV1Ct28KF4sYm4QdE71xhgnzY0ohQC7QWPoW45nIJt+y/UbUpf qdgCBXHevf745UcXZJ2JN/etTsc7d4CcT2sKk+MpioHg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=fWVyBxzRpx8GFDnoO/0oh5e3VpqbEgfOmmGSbk6tepY=; b=WLrU35Za T2YlwAZdN7fEdbEp2eMndPz07i/7rAlFGHOug8Puq1GFbEY12Jx81FQC/E6wISFo p3JZUZjkjm0OYG2MlKMLRxeFf9m9ML9AhcXYTWTF5sA77mgJp/TauTTKEIEbSq2J jR34I8iBF/Vnl7Bjag31Xcqv7tZq70HgIHvMdtIwoAWpEVPvIj/NwrHn/CVjkkGH oG/Qmi0HpR0jEz0ps0ayNhtsj5kHKZUypU07tPd/IhD7n57c52mLTok7eMtuNVyd dvRmX1Hk20X+BD9Psd0xU/B7NKXVaDJIjXTUydkchDY5wmRpWNzBG2bI4pRiGoBb 9RjCFusi278VRA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdejuddgieejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomhepifgrvghtrghnucftihhvvghtuceoghhrihhvvgesuhdvheei rdhnvghtqeenucggtffrrghtthgvrhhnpeduheekveehfedtueejjeeigeehvdfggefhte dugedutdefhfekhedugeetveeuhfenucffohhmrghinhepshhhsgdrtghinecukfhppeek iedrvdehgedrvdegfedrudegieenucevlhhushhtvghrufhiiigvpedunecurfgrrhgrmh epmhgrihhlfhhrohhmpehgrhhivhgvsehuvdehiedrnhgvth X-ME-Proxy: Received: from inocybe.home (lfbn-poi-1-930-146.w86-254.abo.wanadoo.fr [86.254.243.146]) by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 20 May 2021 09:36:01 -0400 (EDT) From: Gaetan Rivet To: ovs-dev@openvswitch.org Date: Thu, 20 May 2021 15:35:52 +0200 Message-Id: <28b891f132ade312c53f022fd2e277fbd07ed6fd.1621517561.git.grive@u256.net> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [ovs-dev] [PATCH v2 8/8] ovs-rcu: Add blocking RCU mode X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Add the configure option --enable-rcu-blocking, that modifies the RCU library. When enabled, quiescing from other threads will block, waiting on the RCU thread to execute the postponed jobs. This mode forces the deferred memory reclamation to happen deterministically, reducing the latency of the deferral and forcing memory to be freed any time a thread goes through a quiescent state. Some use-after-free that were hidden by deferred memory reclamation may become observable as a result. Previously the RCU mechanism would make them harder to detect. UAF detection tools should then be used in conjunction with this compilation flag, e.g. (assuming llvm installed): ./configure --enable-rcu-blocking --enable-asan && make # Verify the tool works: should trigger a UAF ./tests/ovstest test-rcu-uaf quiesce ./tests/ovstest test-rcu-uaf try-quiesce ./tests/ovstest test-rcu-uaf quiesce-start-end # The testsuite can be used as well make check TESTSUITEFLAGS='-k rcu' Signed-off-by: Gaetan Rivet --- .ci/linux-build.sh | 4 ++ .github/workflows/build-and-test.yml | 7 +++ NEWS | 1 + acinclude.m4 | 15 +++++ configure.ac | 1 + lib/ovs-rcu.c | 87 ++++++++++++++++++++++++++++ lib/ovs-rcu.h | 31 ++++++++++ tests/atlocal.in | 1 + tests/library.at | 3 + 9 files changed, 150 insertions(+) diff --git a/.ci/linux-build.sh b/.ci/linux-build.sh index 19600a668..c9e04c090 100755 --- a/.ci/linux-build.sh +++ b/.ci/linux-build.sh @@ -235,6 +235,10 @@ if [ "$ASAN" ]; then CFLAGS_FOR_OVS="${CFLAGS_FOR_OVS} -O1" fi +if [ "$RCU_BLOCK" ]; then + EXTRA_OPTS="$EXTRA_OPTS --enable-rcu-blocking" +fi + save_OPTS="${OPTS} $*" OPTS="${EXTRA_OPTS} ${save_OPTS}" diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index e2350c6d9..56f6f42fc 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -23,6 +23,7 @@ jobs: M32: ${{ matrix.m32 }} OPTS: ${{ matrix.opts }} TESTSUITE: ${{ matrix.testsuite }} + RCU_BLOCK: ${{ matrix.rcu_blocking }} name: linux ${{ join(matrix.*, ' ') }} runs-on: ubuntu-18.04 @@ -109,6 +110,12 @@ jobs: - compiler: gcc deb_package: deb + - compiler: clang + testsuite: test + kernel: 3.16 + asan: asan + rcu_blocking: rcu-blocking + steps: - name: checkout uses: actions/checkout@v2 diff --git a/NEWS b/NEWS index 79e18b85b..daafbe150 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,7 @@ Post-v2.15.0 * OVS validated with DPDK 20.11.1. It is recommended to use this version until further releases. - New configure option '--enable-asan' enables AddressSanitizer. + - New configure option '--enable-rcu-blocking' to debug RCU usage. v2.15.0 - 15 Feb 2021 diff --git a/acinclude.m4 b/acinclude.m4 index 615e7f962..b01264373 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -1386,6 +1386,21 @@ AC_DEFUN([OVS_ENABLE_SPARSE], [], [enable_sparse=no]) AM_CONDITIONAL([ENABLE_SPARSE_BY_DEFAULT], [test $enable_sparse = yes])]) +dnl OVS_ENABLE_RCU_BLOCKING +AC_DEFUN([OVS_ENABLE_RCU_BLOCKING], + [AC_ARG_ENABLE( + [rcu-blocking], + [AC_HELP_STRING([--enable-rcu-blocking], + [Enable the blocking RCU mode])], + [RCU_BLOCKING=yes], [RCU_BLOCKING=no]) + AC_SUBST([RCU_BLOCKING]) + AC_CONFIG_COMMANDS_PRE([ + if test "$RCU_BLOCKING" = "yes"; then + OVS_CFLAGS="$OVS_CFLAGS -DOVS_RCU_BLOCKING=1" + fi + ]) + ]) + dnl OVS_CTAGS_IDENTIFIERS dnl dnl ctags ignores symbols with extras identifiers. This is a list of diff --git a/configure.ac b/configure.ac index eec5a9d1b..de11ff777 100644 --- a/configure.ac +++ b/configure.ac @@ -184,6 +184,7 @@ OVS_CHECK_CC_OPTION([-mavx512f], [CFLAGS="$CFLAGS -DHAVE_AVX512F"]) OVS_ENABLE_WERROR OVS_ENABLE_ASAN OVS_ENABLE_SPARSE +OVS_ENABLE_RCU_BLOCKING OVS_CTAGS_IDENTIFIERS OVS_CHECK_DPCLS_AUTOVALIDATOR OVS_CHECK_BINUTILS_AVX512 diff --git a/lib/ovs-rcu.c b/lib/ovs-rcu.c index 1866bd308..295a56778 100644 --- a/lib/ovs-rcu.c +++ b/lib/ovs-rcu.c @@ -71,6 +71,84 @@ static void ovsrcu_unregister__(struct ovsrcu_perthread *); static bool ovsrcu_call_postponed(void); static void *ovsrcu_postpone_thread(void *arg OVS_UNUSED); +#ifdef OVS_RCU_BLOCKING + +static struct seq *postpone_wait; +DEFINE_STATIC_PER_THREAD_DATA(bool, need_wait, false); +DEFINE_STATIC_PER_THREAD_DATA(uint64_t, quiescent_seqno, 0); + +static void +ovsrcu_postpone_end(void) +{ + if (single_threaded()) { + return; + } + seq_change(postpone_wait); +} + +static bool +ovsrcu_do_not_block(void) +{ + /* Do not wait on the postpone thread if it has been cleared for exit. */ + return single_threaded() || + (strncmp(get_subprogram_name(), "urcu", strlen("urcu")) == 0) || + latch_is_set(&postpone_exit); +} + +static void +ovsrcu_postpone_wait_enable(bool seq_locked) +{ + if (ovsrcu_do_not_block()) { + return; + } + + *quiescent_seqno_get() = seq_locked ? + seq_read_protected(postpone_wait) : + seq_read(postpone_wait); + *need_wait_get() = true; +} + +static void +ovsrcu_postpone_wait(void) +{ + struct ovsrcu_perthread *perthread; + + if (ovsrcu_do_not_block() || !*need_wait_get()) { + return; + } + + *need_wait_get() = false; + + /* Unregistering the potential current perthread ensures + * this thread is seen as quiescent. + * + * It is safe to call, as any path taken to reach this function will + * have flushed this thread cbset already. + */ + perthread = pthread_getspecific(perthread_key); + if (perthread) { + ovs_assert(perthread->cbset == NULL); + pthread_setspecific(perthread_key, NULL); + ovsrcu_unregister__(perthread); + } + + /* Flush pollable events to ensure only the RCU wait will + * wake the thread. */ + poll_immediate_wake(); + poll_block(); + + seq_wait(postpone_wait, *quiescent_seqno_get()); + poll_block(); +} + +#else + +#define ovsrcu_postpone_end() do { } while (0) +#define ovsrcu_postpone_wait_enable(b) do { } while (0) +#define ovsrcu_postpone_wait() do { } while (0) + +#endif /* OVS_RCU_BLOCKING */ + static struct ovsrcu_perthread * ovsrcu_perthread_get(void) { @@ -122,6 +200,7 @@ ovsrcu_quiesced(void) ovsthread_once_done(&once); } } + ovsrcu_postpone_wait(); } /* Indicates the beginning of a quiescent state. See "Details" near the top of @@ -154,6 +233,7 @@ ovsrcu_quiesce(void) perthread = ovsrcu_perthread_get(); perthread->seqno = seq_read(global_seqno); if (perthread->cbset) { + ovsrcu_postpone_wait_enable(false); ovsrcu_flush_cbset(perthread); } seq_change(global_seqno); @@ -172,6 +252,7 @@ ovsrcu_try_quiesce(void) if (!seq_try_lock()) { perthread->seqno = seq_read_protected(global_seqno); if (perthread->cbset) { + ovsrcu_postpone_wait_enable(true); ovsrcu_flush_cbset__(perthread, true); } seq_change_protected(global_seqno); @@ -349,6 +430,8 @@ ovsrcu_call_postponed(void) free(cbset); } + ovsrcu_postpone_end(); + return true; } @@ -397,6 +480,7 @@ static void ovsrcu_unregister__(struct ovsrcu_perthread *perthread) { if (perthread->cbset) { + ovsrcu_postpone_wait_enable(false); ovsrcu_flush_cbset(perthread); } @@ -432,6 +516,9 @@ ovsrcu_init_module(void) { static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; if (ovsthread_once_start(&once)) { +#ifdef OVS_RCU_BLOCKING + postpone_wait = seq_create(); +#endif global_seqno = seq_create(); xpthread_key_create(&perthread_key, ovsrcu_thread_exit_cb); fatal_signal_add_hook(ovsrcu_cancel_thread_exit_cb, NULL, NULL, true); diff --git a/lib/ovs-rcu.h b/lib/ovs-rcu.h index ecc4c9201..0b668d132 100644 --- a/lib/ovs-rcu.h +++ b/lib/ovs-rcu.h @@ -155,6 +155,37 @@ * port_delete(id); * } * + * Debugging + * --------- + * + * Deferred release of resources can prevent memory error detection. + * For example, keeping references to objects protected by RCU across quiescing + * periods creates a race condition. The reference can either remain available + * or be released, depending on how the RCU thread is scheduled. It can make + * tools such as an Address Sanitizer (ASAN) less effective. + * + * A special mode of operation is proposed making the RCU blocking. In this + * mode, a user thread having postponed callbacks will wait for their + * execution at the end of their quiescent period. If a reference was + * scheduled for release at some point, this mode will ensure that the release + * is executed deterministically. + * + * 1| void foo() + * 2| { + * 3| char *var = xmalloc(1); + * 4| + * 5| var[0] = 'a'; + * 6| ovsrcu_postpone(free, var); + * 7| ovsrcu_quiesce(); + * 8| var[0] = 'b'; + * 9| } + * + * If this mode is enabled, the user thread will stop line 7, until the RCU + * thread executed 'free(var)'. If ASAN is in use, it will immediately + * detect the use-after-free line 8. + * + * This mode is enabled during configure by '--enable-rcu-blocking'. It should + * be used in conjunction with '--enable-asan'. */ #include "compiler.h" diff --git a/tests/atlocal.in b/tests/atlocal.in index f61e752bf..c677049c9 100644 --- a/tests/atlocal.in +++ b/tests/atlocal.in @@ -4,6 +4,7 @@ OPENSSL_SUPPORTS_SNI='@OPENSSL_SUPPORTS_SNI@' HAVE_UNBOUND='@HAVE_UNBOUND@' EGREP='@EGREP@' PYTHON3='@PYTHON3@' +RCU_BLOCKING='@RCU_BLOCKING@' # PYTHONCOERCECLOCALE=0 disables the Unicode compatibility warning on # stderr that breaks almost any Python3 test (PEP 0538) diff --git a/tests/library.at b/tests/library.at index 4a549f77e..cae612eea 100644 --- a/tests/library.at +++ b/tests/library.at @@ -264,6 +264,7 @@ AT_CLEANUP AT_SETUP([rcu quiesce use-after-free detection]) AT_SKIP_IF([test "$IS_WIN32" = "yes"]) AT_SKIP_IF([test "$ASAN_ENABLED" = "no"]) +AT_SKIP_IF([test "$RCU_BLOCKING" = "no"]) # SIGABRT + 128 exit_status=134 AT_KEYWORDS([rcu asan]) @@ -275,6 +276,7 @@ AT_CLEANUP AT_SETUP([rcu try-quiesce use-after-free detection]) AT_SKIP_IF([test "$IS_WIN32" = "yes"]) AT_SKIP_IF([test "$ASAN_ENABLED" = "no"]) +AT_SKIP_IF([test "$RCU_BLOCKING" = "no"]) # SIGABRT + 128 exit_status=134 AT_KEYWORDS([rcu asan]) @@ -286,6 +288,7 @@ AT_CLEANUP AT_SETUP([rcu quiesce-start-end use-after-free detection]) AT_SKIP_IF([test "$IS_WIN32" = "yes"]) AT_SKIP_IF([test "$ASAN_ENABLED" = "no"]) +AT_SKIP_IF([test "$RCU_BLOCKING" = "no"]) AT_KEYWORDS([rcu asan]) # SIGABRT + 128 exit_status=134