From nobody Fri Nov 7 23:01:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1548810593956896.9601442366084; Tue, 29 Jan 2019 17:09:53 -0800 (PST) Received: from localhost ([127.0.0.1]:58050 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1goeO0-0008E4-P3 for importer@patchew.org; Tue, 29 Jan 2019 20:09:52 -0500 Received: from eggs.gnu.org ([209.51.188.92]:35551) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1goe3o-0008QS-Pw for qemu-devel@nongnu.org; Tue, 29 Jan 2019 19:49:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1goe3k-000192-QF for qemu-devel@nongnu.org; Tue, 29 Jan 2019 19:48:58 -0500 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:60639) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1goe3i-0000yQ-VV for qemu-devel@nongnu.org; Tue, 29 Jan 2019 19:48:55 -0500 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 8C7CF22131; Tue, 29 Jan 2019 19:48:20 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 29 Jan 2019 19:48:20 -0500 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 46CF9E4664; Tue, 29 Jan 2019 19:48:20 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; s=mesmtp; bh=m3kLTZqe+FpEt4latAPLDgeEyYLXJMip1h/ZJ5ZUxIY=; b=SIKbQpldoRKg kphVZNMOqad4nWGoVt7KX/rc2pFNkfsWOxIYmXjyfjofivHLthos/jc5WVUdpGhB WfBCcvfcCTsBre7nwN5zGEqJsE24H4wlknCY0IY1Jq5prhV0OJFTajzaD44SqI3V kE7cBa9n4Xm1KoFB7FqqL5RlVL5cFAQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :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=fm1; bh=m3kLTZqe+FpEt4latAPLDgeEyYLXJMip1h/ZJ5ZUx IY=; b=LJHgw73YNdBtbnByXpHZc5rr8vY+W4xbg9y1IuEdmKGT6eldNdiNQXWm1 jUyIV0z6Z/Wa5tKFG8d67CYEuXjVQ92QlZwJlv6x7GDhvJ24Q6yCKMeUh1RCa48O /R1C5XorTjRwHrriPoK1KG1NxIbIqKG8Osoa5CW+XxIxVrlybiPCtsB4LpnTIqFj DriN3cB94EksmuKojvPmEia50Q0UyRzCoPa+wMLlioL4Q5hupSQC89NTfh7Dct10 xxM3e3D6ea7k9XbK1AXZaL2nkxMHU3Hv9D1zkSALF2ixiDI/ji7mOshbVfXLwG22 CTiSE3z/r/Oabs8RWs1ZXYfsuzPVw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrjeefgddvkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthenuceurghilhhouhhtmecufedt tdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvufffkffojg hfgggtgfesthekredtredtjeenucfhrhhomhepfdfgmhhilhhiohcuifdrucevohhtrgdf uceotghothgrsegsrhgrrghprdhorhhgqeenucfkphepuddvkedrheelrddvtddrvdduie enucfrrghrrghmpehmrghilhhfrhhomheptghothgrsegsrhgrrghprdhorhhgnecuvehl uhhsthgvrhfuihiivgepke X-ME-Proxy: From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Tue, 29 Jan 2019 19:47:30 -0500 Message-Id: <20190130004811.27372-33-cota@braap.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190130004811.27372-1-cota@braap.org> References: <20190130004811.27372-1-cota@braap.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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 v6 32/73] cpu: define cpu_interrupt_request helpers 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 Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Add a comment about how atomic_read works here. The comment refers to a "BQL-less CPU loop", which will materialize toward the end of this series. Note that the modifications to cpu_reset_interrupt are there to avoid deadlock during the CPU lock transition; once that is complete, cpu_interrupt_request will be simple again. Reviewed-by: Richard Henderson Reviewed-by: Alex Benn=C3=A9e Signed-off-by: Emilio G. Cota --- include/qom/cpu.h | 37 +++++++++++++++++++++++++++++++++++++ qom/cpu.c | 27 +++++++++++++++++++++------ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/include/qom/cpu.h b/include/qom/cpu.h index 5047047666..4a87c1fef7 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -513,6 +513,43 @@ static inline void cpu_halted_set(CPUState *cpu, uint3= 2_t val) cpu_mutex_unlock(cpu); } =20 +/* + * When sending an interrupt, setters OR the appropriate bit and kick the + * destination vCPU. The latter can then read interrupt_request without + * acquiring the CPU lock, because once the kick-induced completes, they'l= l read + * an up-to-date interrupt_request. + * Setters always acquire the lock, which guarantees that (1) concurrent + * updates from different threads won't result in data races, and (2) the + * BQL-less CPU loop will always see an up-to-date interrupt_request, sinc= e the + * loop holds the CPU lock. + */ +static inline uint32_t cpu_interrupt_request(CPUState *cpu) +{ + return atomic_read(&cpu->interrupt_request); +} + +static inline void cpu_interrupt_request_or(CPUState *cpu, uint32_t mask) +{ + if (cpu_mutex_locked(cpu)) { + atomic_set(&cpu->interrupt_request, cpu->interrupt_request | mask); + return; + } + cpu_mutex_lock(cpu); + atomic_set(&cpu->interrupt_request, cpu->interrupt_request | mask); + cpu_mutex_unlock(cpu); +} + +static inline void cpu_interrupt_request_set(CPUState *cpu, uint32_t val) +{ + if (cpu_mutex_locked(cpu)) { + atomic_set(&cpu->interrupt_request, val); + return; + } + cpu_mutex_lock(cpu); + atomic_set(&cpu->interrupt_request, val); + cpu_mutex_unlock(cpu); +} + static inline void cpu_tb_jmp_cache_clear(CPUState *cpu) { unsigned int i; diff --git a/qom/cpu.c b/qom/cpu.c index c5106d5af8..00add81a7f 100644 --- a/qom/cpu.c +++ b/qom/cpu.c @@ -98,14 +98,29 @@ static void cpu_common_get_memory_mapping(CPUState *cpu, * BQL here if we need to. cpu_interrupt assumes it is held.*/ void cpu_reset_interrupt(CPUState *cpu, int mask) { - bool need_lock =3D !qemu_mutex_iothread_locked(); + bool has_bql =3D qemu_mutex_iothread_locked(); + bool has_cpu_lock =3D cpu_mutex_locked(cpu); =20 - if (need_lock) { - qemu_mutex_lock_iothread(); + if (has_bql) { + if (has_cpu_lock) { + atomic_set(&cpu->interrupt_request, cpu->interrupt_request & ~= mask); + } else { + cpu_mutex_lock(cpu); + atomic_set(&cpu->interrupt_request, cpu->interrupt_request & ~= mask); + cpu_mutex_unlock(cpu); + } + return; + } + + if (has_cpu_lock) { + cpu_mutex_unlock(cpu); } - cpu->interrupt_request &=3D ~mask; - if (need_lock) { - qemu_mutex_unlock_iothread(); + qemu_mutex_lock_iothread(); + cpu_mutex_lock(cpu); + atomic_set(&cpu->interrupt_request, cpu->interrupt_request & ~mask); + qemu_mutex_unlock_iothread(); + if (!has_cpu_lock) { + cpu_mutex_unlock(cpu); } } =20 --=20 2.17.1