From patchwork Wed Oct 23 13:17:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 2001055 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=plHxrv1b; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=R96KKr45; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XYV2p613Dz1xx0 for ; Thu, 24 Oct 2024 00:17:36 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=K3kEazNg8t4/MP4D2WQyO/ta3cl5l+4SHMvT8Z4qmUk=; b=plHxrv1b2cjuEE QuE39v9X8kprRfqQypyhohz6As12DuOr5D+JOs39pijT5DAiKEgXK2V5AVQVLdA8HseLt9KDlNXM3 W1wL2axiSch0rPavY+MGJU4CrBY6Nw75I1u5OCJRckH9UMJZXGL/OuibiV10mvNq5IWrQd1obkw9h q8GOo2lirzN79VbhvYucZNFMWF3Ei7kdGpfgDOeNtYJW1dKbIX+O8fos+jSqA0j+8Jm1bJTQZ95ga n9A99jyY4NA3IhwKSEmIOXswVYXqLGIKrMVP+RGwxNf5Q5BK2OlJgyoUWXcLVPR5qFmskrYKu/PFu JnTbDYmlYVVtkLGIThkw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t3bEz-0000000EVNy-2fps; Wed, 23 Oct 2024 13:17:33 +0000 Received: from out-174.mta0.migadu.com ([91.218.175.174]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t3bEu-0000000EVMa-1oYf for kvm-riscv@lists.infradead.org; Wed, 23 Oct 2024 13:17:30 +0000 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1729689445; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QeV4UfkiS7XFzfn3GzKPrb95YL+mJ8EYcU6Ge+O/14M=; b=R96KKr45P98nWNOUWIJnWJDx/9nd3WZG25knN4pFUZzB4fuUvsjoVabJi4z3xcoy/ZHkVV CXySf4GOiO8G7USyOggyGxSnaR+X33cdujPH4+XrRKeS3u8v/fQaJMegQnZ05uEn/uHqqv b4cAIgKHO1E5RwIF5gyzjGNf6N80yJ0= From: Andrew Jones To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, kvmarm@lists.linux.dev Cc: atishp@rivosinc.com, jamestiotio@gmail.com, alexandru.elisei@arm.com, eric.auger@redhat.com Subject: [kvm-unit-tests PATCH 1/2] lib/on-cpus: Correct and simplify synchronization Date: Wed, 23 Oct 2024 15:17:20 +0200 Message-ID: <20241023131718.117452-5-andrew.jones@linux.dev> In-Reply-To: <20241023131718.117452-4-andrew.jones@linux.dev> References: <20241023131718.117452-4-andrew.jones@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241023_061728_757381_438EF1D0 X-CRM114-Status: GOOD ( 12.13 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: get/put_on_cpu_info() were trying to provide per-cpu locking for the per-cpu on_cpu info, but they were flawed since they would always set the "lock" since they were treating test_and_set/clear as cmp [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [91.218.175.174 listed in sa-trusted.bondedsender.org] 0.0 RCVD_IN_VALIDITY_SAFE_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [91.218.175.174 listed in sa-accredit.habeas.com] 0.0 RCVD_IN_VALIDITY_RPBL_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [91.218.175.174 listed in bl.score.senderscore.com] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: kvm-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kvm-riscv" Errors-To: kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org get/put_on_cpu_info() were trying to provide per-cpu locking for the per-cpu on_cpu info, but they were flawed since they would always set the "lock" since they were treating test_and_set/clear as cmpxchg (which they're not). Just revert to a normal spinlock to correct it. Also simplify the break case for on_cpu_async() - we don't care if func is NULL, we only care that the cpu is idle. And, finally, add a missing barrier to on_cpu_async(). Fixes: 018550041b38 ("arm/arm64: Remove spinlocks from on_cpu_async") Signed-off-by: Andrew Jones --- lib/on-cpus.c | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/lib/on-cpus.c b/lib/on-cpus.c index 892149338419..f6072117fa1b 100644 --- a/lib/on-cpus.c +++ b/lib/on-cpus.c @@ -9,6 +9,7 @@ #include #include #include +#include bool cpu0_calls_idle; @@ -18,18 +19,7 @@ struct on_cpu_info { cpumask_t waiters; }; static struct on_cpu_info on_cpu_info[NR_CPUS]; -static cpumask_t on_cpu_info_lock; - -static bool get_on_cpu_info(int cpu) -{ - return !cpumask_test_and_set_cpu(cpu, &on_cpu_info_lock); -} - -static void put_on_cpu_info(int cpu) -{ - int ret = cpumask_test_and_clear_cpu(cpu, &on_cpu_info_lock); - assert(ret); -} +static struct spinlock lock; static void __deadlock_check(int cpu, const cpumask_t *waiters, bool *found) { @@ -81,18 +71,14 @@ void do_idle(void) if (cpu == 0) cpu0_calls_idle = true; - set_cpu_idle(cpu, true); - smp_send_event(); - for (;;) { + set_cpu_idle(cpu, true); + smp_send_event(); + while (cpu_idle(cpu)) smp_wait_for_event(); smp_rmb(); on_cpu_info[cpu].func(on_cpu_info[cpu].data); - on_cpu_info[cpu].func = NULL; - smp_wmb(); - set_cpu_idle(cpu, true); - smp_send_event(); } } @@ -110,17 +96,17 @@ void on_cpu_async(int cpu, void (*func)(void *data), void *data) for (;;) { cpu_wait(cpu); - if (get_on_cpu_info(cpu)) { - if ((volatile void *)on_cpu_info[cpu].func == NULL) - break; - put_on_cpu_info(cpu); - } + spin_lock(&lock); + if (cpu_idle(cpu)) + break; + spin_unlock(&lock); } on_cpu_info[cpu].func = func; on_cpu_info[cpu].data = data; + smp_wmb(); set_cpu_idle(cpu, false); - put_on_cpu_info(cpu); + spin_unlock(&lock); smp_send_event(); }