From nobody Mon Feb 9 02:14:49 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1636616369967167.21116471672417; Wed, 10 Nov 2021 23:39:29 -0800 (PST) Received: from localhost ([::1]:52698 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ml4gC-0006pm-NK for importer@patchew.org; Thu, 11 Nov 2021 02:39:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:36162) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ml4d4-0003ec-KX for qemu-devel@nongnu.org; Thu, 11 Nov 2021 02:36:14 -0500 Received: from [2a00:1450:4864:20::52f] (port=38771 helo=mail-ed1-x52f.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ml4d2-000798-Ge for qemu-devel@nongnu.org; Thu, 11 Nov 2021 02:36:13 -0500 Received: by mail-ed1-x52f.google.com with SMTP id z21so20618034edb.5 for ; Wed, 10 Nov 2021 23:36:10 -0800 (PST) Received: from avogadro.lan ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id w1sm1062098edd.49.2021.11.10.23.36.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Nov 2021 23:36:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uf2jq1W6vW08aozBiEOJUT0U2HATlOOWfn5+VD0PxHA=; b=itBzEHnwMQaYiCBtTFYk2UV0rv2UYYqv0nSq2ScJ44xeCWKfeVMcCpOwRfKa6xI5gS 7R32B/jGjQKkCzPTU6VCNUGIMw1Uo5KZ/vGldhAPAl+5yVNMmyDsgugdjw+2AkVAb1f7 geAR64+Erpd9dz5QjiMh+cD32G6hDeZtrbuNw0P8WEdXO7v7asgEXO2f8KqXgCJahZ4O 6LPvcmoAXRT09b84PdsFjhvVC8+fWN/kOOApWEVPlmL6fjcdfspT/sPVi+0Yzn3MGYk0 KPIzjxyepd0l+5UYTSffUSV8J+Amt7Stx7ANncpwJWSSwakYWleCyWWeMFYA25YOE11s cluQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=uf2jq1W6vW08aozBiEOJUT0U2HATlOOWfn5+VD0PxHA=; b=cfyvmGLRIXHErE10BeXpSDIK4i9US2oC3tHzsJ/+hSOD3IYefVj/alcWb0Cs5lURVQ lt0GI91j3Eog+N4DBTYwZA/KFADuS23HmCkjhTpCBtP+dcgLSFlS/FixgUxdRvrxsmhd RfPyACESsWtT9OAMcTXtbsuNNRcbdekxYugzv5JeDxdLrCTPEOM4Dddkyb4KYgBGGVPG Gco2zLFIaLwNzueQMaNu6Pb2g0fhsvAZ1uenzx0r4Tss2H3oJ90CSyWW49LyDJsXK+dN HqKXb+FYmNvgHVEiLeUPgFLW30ZQNGYy4e3Xi90bb4CzX03fgpZRRye8GsxuxcFaX1rK DMiA== X-Gm-Message-State: AOAM530qRTtKlRtfT9PVhRQ0q3w53fMJHwMbY/8BBLihbu+WJImyClQL jRwlX/dgR/960u04QhlMSOtabEU/xx0= X-Google-Smtp-Source: ABdhPJwdXA2lRxzl8/OJ5RQks0WU4oJpFn0foUxmGn5go2PXF2k7BocZp3+QIMB75MiZbleHm65QLQ== X-Received: by 2002:a17:907:7242:: with SMTP id ds2mr7017869ejc.269.1636616169805; Wed, 10 Nov 2021 23:36:09 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 1/5] target/i386: sgx: mark device not user creatable Date: Thu, 11 Nov 2021 08:36:03 +0100 Message-Id: <20211111073607.195697-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111073607.195697-1-pbonzini@redhat.com> References: <20211111073607.195697-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::52f (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::52f; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x52f.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Thomas Huth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1636616370682100003 Content-Type: text/plain; charset="utf-8" The device is created by the machine based on the sgx-epc property. It should not be created by users. Reported-by: Thomas Huth Signed-off-by: Paolo Bonzini --- hw/i386/sgx-epc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/hw/i386/sgx-epc.c b/hw/i386/sgx-epc.c index 55e2217eae..e508827e78 100644 --- a/hw/i386/sgx-epc.c +++ b/hw/i386/sgx-epc.c @@ -154,6 +154,7 @@ static void sgx_epc_class_init(ObjectClass *oc, void *d= ata) dc->realize =3D sgx_epc_realize; dc->unrealize =3D sgx_epc_unrealize; dc->desc =3D "SGX EPC section"; + dc->user_creatable =3D false; device_class_set_props(dc, sgx_epc_properties); =20 mdc->get_addr =3D sgx_epc_md_get_addr; --=20 2.33.1 From nobody Mon Feb 9 02:14:49 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1636616763864631.392269197861; Wed, 10 Nov 2021 23:46:03 -0800 (PST) Received: from localhost ([::1]:37590 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ml4mY-0007HX-Qp for importer@patchew.org; Thu, 11 Nov 2021 02:46:02 -0500 Received: from eggs.gnu.org ([209.51.188.92]:36194) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ml4d6-0003f6-41 for qemu-devel@nongnu.org; Thu, 11 Nov 2021 02:36:16 -0500 Received: from [2a00:1450:4864:20::535] (port=45989 helo=mail-ed1-x535.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ml4d2-00079B-H2 for qemu-devel@nongnu.org; Thu, 11 Nov 2021 02:36:15 -0500 Received: by mail-ed1-x535.google.com with SMTP id f4so20549349edx.12 for ; Wed, 10 Nov 2021 23:36:11 -0800 (PST) Received: from avogadro.lan ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id w1sm1062098edd.49.2021.11.10.23.36.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Nov 2021 23:36:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iA8vB4TfqWHRgjLEj3itlzFwZkdkH8YIgO+YxnoGWAo=; b=XwM4fEyrzBLX4JUQPx4bpU7Bgs4vA7GVWppSHfcs2NkUoGrps5QmvdvfwJYU1Kgd0a CkWDNoA3s2RTUjvdVfINB+15oQLzHgYh+g3z/3r+3tXddYhSox0MeuabcgB5OZ5BHLDr HDP/D2rkxrrlqe4Is7XZh4e0Zo0uhuJr/GrCes7ZOZ9mZtGNn+htJe7o5oTvM2m4hR/c j4vZmEPgzn/zL7cWuZX6+fwjZRbDUAhAfifd85Z4gj2WI1XXj7eFFXyAnOtzEHDtx/nR YB8pfA235Bo3PQDHD/fYPEb0JDSjqHI2F+H1MYtCJbsEuKEx+s8cQLHgG/jPBo7hMLRa yDBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=iA8vB4TfqWHRgjLEj3itlzFwZkdkH8YIgO+YxnoGWAo=; b=AnGszSqfHlHz9ChqPJGPmlzkSGcC0FV3QreqpEVjFBDHTf107NqgG0Qut7QWiLRV2Y Jzwa7B8iqF/HgVa45fXY37lKe+g3zsQg8HnwTmtfpGx2vqK2GoYWu1IdGcNprvxuRWAK MXEoi8PeO1/hiEKdXZOgqcS9cchLJQXwi7oNCoR9fj0g83pJBKYIXiqiyi0vKDBFXlQd lrRB9HaN/KucY9RFccZyku8qT0N2D5gh53HfCVarQl3bRorpplBqB7FlYLsjMt+a86uB ibhcZWqbLIRh6vbdYxoa0F//wtgnzUW5fxwFz+FIqoNuHac9dknJWWO+uK/pll3xhxrN nA8A== X-Gm-Message-State: AOAM533y5I2I1Ubu7h3lFihM/93stdVvv2QIiaa4l7O2rBCDG8bAO3vV KtfTej7d3ycRNmGN16bO3i0kHKZeco0= X-Google-Smtp-Source: ABdhPJyPENLq0q1xzQBHKViFdnQO6HiDGeFs7YfDF9bMUD/6gC41EdOnQhUpVVRJq9Vo1mMkbExFoQ== X-Received: by 2002:a05:6402:1744:: with SMTP id v4mr7294297edx.366.1636616170474; Wed, 10 Nov 2021 23:36:10 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 2/5] rcu: Introduce force_rcu notifier Date: Thu, 11 Nov 2021 08:36:04 +0100 Message-Id: <20211111073607.195697-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111073607.195697-1-pbonzini@redhat.com> References: <20211111073607.195697-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::535 (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::535; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x535.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Richard Henderson , Greg Kurz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1636616765544100001 Content-Type: text/plain; charset="utf-8" From: Greg Kurz The drain_rcu_call() function can be blocked as long as an RCU reader stays in a read-side critical section. This is typically what happens when a TCG vCPU is executing a busy loop. It can deadlock the QEMU monitor as reported in https://gitlab.com/qemu-project/qemu/-/issues/650 . This can be avoided by allowing drain_rcu_call() to enforce an RCU grace period. Since each reader might need to do specific actions to end a read-side critical section, do it with notifiers. Prepare ground for this by adding a notifier list to the RCU reader struct and use it in wait_for_readers() if drain_rcu_call() is in progress. An API is added for readers to register their notifiers. This is largely based on a draft from Paolo Bonzini. Suggested-by: Paolo Bonzini Signed-off-by: Greg Kurz Reviewed-by: Richard Henderson Message-Id: <20211109183523.47726-2-groug@kaod.org> Signed-off-by: Paolo Bonzini --- include/qemu/rcu.h | 15 +++++++++++++++ util/rcu.c | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/qemu/rcu.h b/include/qemu/rcu.h index 515d327cf1..e69efbd47f 100644 --- a/include/qemu/rcu.h +++ b/include/qemu/rcu.h @@ -27,6 +27,7 @@ #include "qemu/thread.h" #include "qemu/queue.h" #include "qemu/atomic.h" +#include "qemu/notify.h" #include "qemu/sys_membarrier.h" =20 #ifdef __cplusplus @@ -66,6 +67,13 @@ struct rcu_reader_data { =20 /* Data used for registry, protected by rcu_registry_lock */ QLIST_ENTRY(rcu_reader_data) node; + + /* + * NotifierList used to force an RCU grace period. Accessed under + * rcu_registry_lock. Note that the notifier is called _outside_ + * the thread! + */ + NotifierList force_rcu; }; =20 extern __thread struct rcu_reader_data rcu_reader; @@ -180,6 +188,13 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(RCUReadAuto, rcu_read_au= to_unlock) #define RCU_READ_LOCK_GUARD() \ g_autoptr(RCUReadAuto) _rcu_read_auto __attribute__((unused)) =3D rcu_= read_auto_lock() =20 +/* + * Force-RCU notifiers tell readers that they should exit their + * read-side critical section. + */ +void rcu_add_force_rcu_notifier(Notifier *n); +void rcu_remove_force_rcu_notifier(Notifier *n); + #ifdef __cplusplus } #endif diff --git a/util/rcu.c b/util/rcu.c index 13ac0f75cb..c91da9f137 100644 --- a/util/rcu.c +++ b/util/rcu.c @@ -46,6 +46,7 @@ unsigned long rcu_gp_ctr =3D RCU_GP_LOCKED; =20 QemuEvent rcu_gp_event; +static int in_drain_call_rcu; static QemuMutex rcu_registry_lock; static QemuMutex rcu_sync_lock; =20 @@ -107,6 +108,8 @@ static void wait_for_readers(void) * get some extra futex wakeups. */ qatomic_set(&index->waiting, false); + } else if (qatomic_read(&in_drain_call_rcu)) { + notifier_list_notify(&index->force_rcu, NULL); } } =20 @@ -339,8 +342,10 @@ void drain_call_rcu(void) * assumed. */ =20 + qatomic_inc(&in_drain_call_rcu); call_rcu1(&rcu_drain.rcu, drain_rcu_callback); qemu_event_wait(&rcu_drain.drain_complete_event); + qatomic_dec(&in_drain_call_rcu); =20 if (locked) { qemu_mutex_lock_iothread(); @@ -363,6 +368,20 @@ void rcu_unregister_thread(void) qemu_mutex_unlock(&rcu_registry_lock); } =20 +void rcu_add_force_rcu_notifier(Notifier *n) +{ + qemu_mutex_lock(&rcu_registry_lock); + notifier_list_add(&rcu_reader.force_rcu, n); + qemu_mutex_unlock(&rcu_registry_lock); +} + +void rcu_remove_force_rcu_notifier(Notifier *n) +{ + qemu_mutex_lock(&rcu_registry_lock); + notifier_remove(n); + qemu_mutex_unlock(&rcu_registry_lock); +} + static void rcu_init_complete(void) { QemuThread thread; --=20 2.33.1 From nobody Mon Feb 9 02:14:49 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1636616610608274.7105511030303; Wed, 10 Nov 2021 23:43:30 -0800 (PST) Received: from localhost ([::1]:34392 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ml4k5-000571-Az for importer@patchew.org; Thu, 11 Nov 2021 02:43:29 -0500 Received: from eggs.gnu.org ([209.51.188.92]:36200) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ml4d6-0003f8-2w for qemu-devel@nongnu.org; Thu, 11 Nov 2021 02:36:16 -0500 Received: from [2a00:1450:4864:20::52e] (port=42968 helo=mail-ed1-x52e.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ml4d3-00079H-0d for qemu-devel@nongnu.org; Thu, 11 Nov 2021 02:36:15 -0500 Received: by mail-ed1-x52e.google.com with SMTP id v11so20689812edc.9 for ; Wed, 10 Nov 2021 23:36:12 -0800 (PST) Received: from avogadro.lan ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id w1sm1062098edd.49.2021.11.10.23.36.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Nov 2021 23:36:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=c76gKdka3+70DvRFkGRLX7+gFfnrCbIIFnpyii1x3Ts=; b=DH88N2SoIoqMT4HxyLHhIQ/D2/QgiF+YJwCtv7Or2yvpS/tPQiszib7wZV8MEoGDz8 zQhaSytOaI/ujpoaUK6EgWuxngO1qV1cbsIBS+Be+2M8Ut04XhPzi7T41Y/iI88ce13z dT1S86sAxtQvhk1WQ35nAiRucViZP1Xiyuln3jhqAv04WD5L+WE+xm+qu5pyc6V8nMG5 zjtOxwbicAAPEEkSERVG7huNXYRjXZFhV8YMxUUFldtl3UlANmjQWs5n+XRsDT6qNC4a p/WYM48qgbruGI4WBF6YV8otBNpS6EQRMXzrdXVRyNz7MifPTFmhGpd91l9MZWQsjany DdyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=c76gKdka3+70DvRFkGRLX7+gFfnrCbIIFnpyii1x3Ts=; b=V8pUA1VeJ6SEnjTp70Ggus9OAaTo4zBeFbI1Qc7lZvXv1lTTx0rQxhb+NJKBMYJVOC FR2e4tSl3uPLBo3Pt1HGlzGZFL2AHGcDLu7VCA/WSa6hoNOh4AHeZM2ZsAhiNNySSUPs MUrjjLJCllPpU91KfqLRKHQMXj7z+EmMd29ZKTVVA3FQAzdfTfFmJF3qPePmYHpykXGB QKketxGaSRDEXDFm2aUNSr7Dpl4vrAHocHhuv7fS0yQ5Zrc3ndhWSxdNYvn745kn4puC gzrCLvo1zEv4lWRJoddbTZb9/vU7LPXu5FbSetfdnz9mwDvkW+CkKgyuyFZB0aTw9X0t x/dg== X-Gm-Message-State: AOAM532xtRGAW8obr9RZB5kDQ8GujeeKqUHsTVLSh690nxzqWg8RupRC 3A7aiblpZRRM80YWQolJZaqLqv7Mwzo= X-Google-Smtp-Source: ABdhPJxm32R9g2K+FEClMeQ76Omp7yKifrjnCVydffVp9SHLV8bkbyplq8eZ2iANGCwEJbJtHlu3xA== X-Received: by 2002:a17:907:1c15:: with SMTP id nc21mr6765508ejc.260.1636616171195; Wed, 10 Nov 2021 23:36:11 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 3/5] accel/tcg: Register a force_rcu notifier Date: Thu, 11 Nov 2021 08:36:05 +0100 Message-Id: <20211111073607.195697-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111073607.195697-1-pbonzini@redhat.com> References: <20211111073607.195697-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::52e (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::52e; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x52e.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Richard Henderson , Greg Kurz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1636616612364100001 Content-Type: text/plain; charset="utf-8" From: Greg Kurz A TCG vCPU doing a busy loop systematicaly hangs the QEMU monitor if the user passes 'device_add' without argument. This is because drain_cpu_all() which is called from qmp_device_add() cannot return if readers don't exit read-side critical sections. That is typically what busy-looping TCG vCPUs do: int cpu_exec(CPUState *cpu) { [...] rcu_read_lock(); [...] while (!cpu_handle_exception(cpu, &ret)) { // Busy loop keeps vCPU here } [...] rcu_read_unlock(); return ret; } For MTTCG, have all vCPU threads register a force_rcu notifier that will kick them out of the loop using async_run_on_cpu(). The notifier is called with the rcu_registry_lock mutex held, using async_run_on_cpu() ensures there are no deadlocks. For RR, a single thread runs all vCPUs. Just register a single notifier that kicks the current vCPU to the next one. For MTTCG: Suggested-by: Paolo Bonzini For RR: Suggested-by: Richard Henderson Fixes: 7bed89958bfb ("device_core: use drain_call_rcu in in qmp_device_add") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/650 Signed-off-by: Greg Kurz Reviewed-by: Richard Henderson Message-Id: <20211109183523.47726-3-groug@kaod.org> Signed-off-by: Paolo Bonzini --- accel/tcg/tcg-accel-ops-mttcg.c | 26 ++++++++++++++++++++++++++ accel/tcg/tcg-accel-ops-rr.c | 10 ++++++++++ 2 files changed, 36 insertions(+) diff --git a/accel/tcg/tcg-accel-ops-mttcg.c b/accel/tcg/tcg-accel-ops-mttc= g.c index 847d2079d2..29632bd4c0 100644 --- a/accel/tcg/tcg-accel-ops-mttcg.c +++ b/accel/tcg/tcg-accel-ops-mttcg.c @@ -28,6 +28,7 @@ #include "sysemu/tcg.h" #include "sysemu/replay.h" #include "qemu/main-loop.h" +#include "qemu/notify.h" #include "qemu/guest-random.h" #include "exec/exec-all.h" #include "hw/boards.h" @@ -35,6 +36,26 @@ #include "tcg-accel-ops.h" #include "tcg-accel-ops-mttcg.h" =20 +typedef struct MttcgForceRcuNotifier { + Notifier notifier; + CPUState *cpu; +} MttcgForceRcuNotifier; + +static void do_nothing(CPUState *cpu, run_on_cpu_data d) +{ +} + +static void mttcg_force_rcu(Notifier *notify, void *data) +{ + CPUState *cpu =3D container_of(notify, MttcgForceRcuNotifier, notifier= )->cpu; + + /* + * Called with rcu_registry_lock held, using async_run_on_cpu() ensures + * that there are no deadlocks. + */ + async_run_on_cpu(cpu, do_nothing, RUN_ON_CPU_NULL); +} + /* * In the multi-threaded case each vCPU has its own thread. The TLS * variable current_cpu can be used deep in the code to find the @@ -43,12 +64,16 @@ =20 static void *mttcg_cpu_thread_fn(void *arg) { + MttcgForceRcuNotifier force_rcu; CPUState *cpu =3D arg; =20 assert(tcg_enabled()); g_assert(!icount_enabled()); =20 rcu_register_thread(); + force_rcu.notifier.notify =3D mttcg_force_rcu; + force_rcu.cpu =3D cpu; + rcu_add_force_rcu_notifier(&force_rcu.notifier); tcg_register_thread(); =20 qemu_mutex_lock_iothread(); @@ -100,6 +125,7 @@ static void *mttcg_cpu_thread_fn(void *arg) =20 tcg_cpus_destroy(cpu); qemu_mutex_unlock_iothread(); + rcu_remove_force_rcu_notifier(&force_rcu.notifier); rcu_unregister_thread(); return NULL; } diff --git a/accel/tcg/tcg-accel-ops-rr.c b/accel/tcg/tcg-accel-ops-rr.c index a5fd26190e..bf59f53dbc 100644 --- a/accel/tcg/tcg-accel-ops-rr.c +++ b/accel/tcg/tcg-accel-ops-rr.c @@ -28,6 +28,7 @@ #include "sysemu/tcg.h" #include "sysemu/replay.h" #include "qemu/main-loop.h" +#include "qemu/notify.h" #include "qemu/guest-random.h" #include "exec/exec-all.h" =20 @@ -133,6 +134,11 @@ static void rr_deal_with_unplugged_cpus(void) } } =20 +static void rr_force_rcu(Notifier *notify, void *data) +{ + rr_kick_next_cpu(); +} + /* * In the single-threaded case each vCPU is simulated in turn. If * there is more than a single vCPU we create a simple timer to kick @@ -143,10 +149,13 @@ static void rr_deal_with_unplugged_cpus(void) =20 static void *rr_cpu_thread_fn(void *arg) { + Notifier force_rcu; CPUState *cpu =3D arg; =20 assert(tcg_enabled()); rcu_register_thread(); + force_rcu.notify =3D rr_force_rcu; + rcu_add_force_rcu_notifier(&force_rcu); tcg_register_thread(); =20 qemu_mutex_lock_iothread(); @@ -255,6 +264,7 @@ static void *rr_cpu_thread_fn(void *arg) rr_deal_with_unplugged_cpus(); } =20 + rcu_remove_force_rcu_notifier(&force_rcu); rcu_unregister_thread(); return NULL; } --=20 2.33.1 From nobody Mon Feb 9 02:14:49 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1636616561894665.1667795138021; Wed, 10 Nov 2021 23:42:41 -0800 (PST) Received: from localhost ([::1]:59204 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ml4jI-0002p4-MJ for importer@patchew.org; Thu, 11 Nov 2021 02:42:40 -0500 Received: from eggs.gnu.org ([209.51.188.92]:36188) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ml4d5-0003f5-Fd for qemu-devel@nongnu.org; Thu, 11 Nov 2021 02:36:16 -0500 Received: from [2a00:1450:4864:20::52b] (port=44943 helo=mail-ed1-x52b.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ml4d3-00079K-96 for qemu-devel@nongnu.org; Thu, 11 Nov 2021 02:36:15 -0500 Received: by mail-ed1-x52b.google.com with SMTP id j21so20421738edt.11 for ; Wed, 10 Nov 2021 23:36:12 -0800 (PST) Received: from avogadro.lan ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id w1sm1062098edd.49.2021.11.10.23.36.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Nov 2021 23:36:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=j/DgBRVtXg3n5HEpQplwCbP73DDyCNVr2PK8DT2sJzk=; b=YMCMUcaxWyAWaY39CCybxSOyzfbAydI/lefgb+UViJlfUH4ql64RAJlo6K+QxDZOKX feOK5tYp5cb2HRVMo+RZMTaXVR7MfOpc2diqDRUp0ZV4UPOWhk7OJekU5VvMql0/u7hW qyOyKNOfJ+5Z1no/98FwNCXyQtlZek6sDeai5eo7TO8pq+Y+O1a2f0EndO53mWIpDY/u 4XB9dPloO73qF90B/ZSoRpuW0ZDOECDcwijmsOulvQNmoXIsXr+Iai+K9sFXcb6iAWQ0 B7ANmfFOVoOaV4875fbOLi2Y6oIxU0attXJsGBCSbC/A1slF34mYwMZJhI1exCBuDEfJ T9QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=j/DgBRVtXg3n5HEpQplwCbP73DDyCNVr2PK8DT2sJzk=; b=INGYGFTKcRbrNehedpmDFlmCNpL7/zS5FhCR5g/2bkPkrgnJvzslSoMa9uJvmYrcGm V7vyOsT8ieY68hHKSp3qQNUgiZrHkCImX7DmgPei+OPEkLK+StVn2sl2tQdt07W5ZO5F Ajf1qcblFQIgToWoz75HhKAycpTbOk33vjFIDmaAKf4hTOZZhzs43I7a5Y8whbL0l8K3 u5ANpFT8v1HjRa7nC9tMrrK/uxvDxJtPr1Ri6OnWOhWkwne/ISBoDV7T2c1bTiNCug8D jp5840Rlr1AiD9Igf4MtyaTjiJQGOFUDOkpmo3n5tSKcyzaavEep3stb44HsmXfkrYZL KeYA== X-Gm-Message-State: AOAM531GfawsKFLmldRn3CaBcAENV8V++SzYY/UFLc4qT3khOYce+r6z KIG1p6YzbGCi0Ovvcgs0WQqB+/IMHhk= X-Google-Smtp-Source: ABdhPJyvVNcAIGb1+EdFiwGzril4qjsF1F0TDuhM3tG2ZdjrJyKiJfP0zUkitBGpCHYNTaQVpeBwkg== X-Received: by 2002:a50:9d48:: with SMTP id j8mr4531847edk.192.1636616171788; Wed, 10 Nov 2021 23:36:11 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 4/5] numa: avoid crash with SGX and "info numa" Date: Thu, 11 Nov 2021 08:36:06 +0100 Message-Id: <20211111073607.195697-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111073607.195697-1-pbonzini@redhat.com> References: <20211111073607.195697-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::52b (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::52b; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x52b.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1636616562857100001 Content-Type: text/plain; charset="utf-8" Add the MEMORY_DEVICE_INFO_KIND_SGX_EPC case, so that enclave memory is included in the output of "info numa" instead of crashing the monitor. Fixes: a7c565a941 ("sgx-epc: Add the fill_device_info() callback support", = 2021-09-30) Signed-off-by: Paolo Bonzini --- hw/core/numa.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/hw/core/numa.c b/hw/core/numa.c index 510d096a88..e6050b2273 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -756,6 +756,7 @@ static void numa_stat_memory_devices(NumaNodeMem node_m= em[]) PCDIMMDeviceInfo *pcdimm_info; VirtioPMEMDeviceInfo *vpi; VirtioMEMDeviceInfo *vmi; + SgxEPCDeviceInfo *se; =20 for (info =3D info_list; info; info =3D info->next) { MemoryDeviceInfo *value =3D info->value; @@ -781,6 +782,12 @@ static void numa_stat_memory_devices(NumaNodeMem node_= mem[]) node_mem[vmi->node].node_mem +=3D vmi->size; node_mem[vmi->node].node_plugged_mem +=3D vmi->size; break; + case MEMORY_DEVICE_INFO_KIND_SGX_EPC: + se =3D value->u.sgx_epc.data; + /* TODO: once we support numa, assign to right node */ + node_mem[0].node_mem +=3D se->size; + node_mem[0].node_plugged_mem +=3D se->size; + break; default: g_assert_not_reached(); } --=20 2.33.1 From nobody Mon Feb 9 02:14:49 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1636616358286128.80414063099977; Wed, 10 Nov 2021 23:39:18 -0800 (PST) Received: from localhost ([::1]:51978 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ml4g1-0006MI-9X for importer@patchew.org; Thu, 11 Nov 2021 02:39:17 -0500 Received: from eggs.gnu.org ([209.51.188.92]:36196) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ml4d6-0003f7-40 for qemu-devel@nongnu.org; Thu, 11 Nov 2021 02:36:16 -0500 Received: from [2a00:1450:4864:20::530] (port=33522 helo=mail-ed1-x530.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ml4d4-00079O-CQ for qemu-devel@nongnu.org; Thu, 11 Nov 2021 02:36:15 -0500 Received: by mail-ed1-x530.google.com with SMTP id m14so20938079edd.0 for ; Wed, 10 Nov 2021 23:36:13 -0800 (PST) Received: from avogadro.lan ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id w1sm1062098edd.49.2021.11.10.23.36.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Nov 2021 23:36:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=736eFu8WL4rj1c9Mylb9tpssBbzithUw4sVbHR+zcx0=; b=UnOxO0f1FyaHZsOS1wRbLqX6lZYZmTjxCgHMmwjOQ3PI2KoRJJLqH13BjEtThXTQI2 vypS7SRiXTMuW7bLt9WChClAGhgHM2xQcx5ATmxlQkS1kVirBWkcgbAvzVXvPRchxsSP eQrt1oTsCgpLEqR+5zZ2/YycX2/sHC1Xbg5c+wi7YcG5+oHAFUGjxodd3BNBEaEOSEbW X1qV411kvBCz0d1Y+V26TYRI4sHhLpy2lUTn7OK8wwigEbaDtwnG2M/VZPtgVDLHh4Wt 6znUWzRsveJ+pNmcC7dq3DFTKikqrMGtsCXBmFHtvlo6/WH731qW8nWDlHaul/e7aXAh Uftg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=736eFu8WL4rj1c9Mylb9tpssBbzithUw4sVbHR+zcx0=; b=V9pjjLDmJ5Vt8TOfoEZqelbD90WCP2nB93HjD7Bhssu2GfT+PWDBbFHwB4sh0/fZyz rTfhqzS0c2di75WQLBrSKPHhV3saDE6HeQQD+2i8wvb3KkKQcijE7wMG1UijaE7GlqxD FopGghiEYa8zJ8GOsynt3+/wclHWCR9ZCUUKieXRM8u9eMybC5PE4mh3BMWv2/0X6sIP rLp1rfAD9Nr16NP32cvMiedmEbm98W+kLS7s/RqKiQd2yPLGNEw3gJWuPOeIHkefayGZ TOQAFfd+i9vtrCO7CSrNdZzExkVyrk9wZKRVoid8aERoBtcwr7pZNqeCWbj7t/2SO7aR EP6w== X-Gm-Message-State: AOAM530B3BtYc2H9ejDnFYzdw0Ha3qvxhFNzN58Eo8asfkbqsdUhPU/0 WpaQ7H2hMsFoceDZ4OaEsWr7NnkpSjY= X-Google-Smtp-Source: ABdhPJz74xIt/zrhhrb00lqrpzqG1zaQ1gI8La69BC3eLideGN6gfbrQiTzQzKV6rUK4gnP17ZcJaA== X-Received: by 2002:a17:907:980e:: with SMTP id ji14mr6749544ejc.346.1636616172473; Wed, 10 Nov 2021 23:36:12 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 5/5] sgx: Reset the vEPC regions during VM reboot Date: Thu, 11 Nov 2021 08:36:07 +0100 Message-Id: <20211111073607.195697-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211111073607.195697-1-pbonzini@redhat.com> References: <20211111073607.195697-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::530 (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::530; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x530.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yang Zhong Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1636616360276100001 Content-Type: text/plain; charset="utf-8" From: Yang Zhong For bare-metal SGX on real hardware, the hardware provides guarantees SGX state at reboot. For instance, all pages start out uninitialized. The vepc driver provides a similar guarantee today for freshly-opened vepc instances, but guests such as Windows expect all pages to be in uninitialized state on startup, including after every guest reboot. Qemu can invoke the ioctl to bring its vEPC pages back to uninitialized state. There is a possibility that some pages fail to be removed if they are SECS pages, and the child and SECS pages could be in separate vEPC regions. Therefore, the ioctl returns the number of EREMOVE failures, telling Qemu to try the ioctl again after it's done with all vEPC regions. The related kernel patches: Link: https://lkml.kernel.org/r/20211021201155.1523989-3-pbonzini@redhat.com Signed-off-by: Yang Zhong Message-Id: <20211101162009.62161-6-yang.zhong@intel.com> Signed-off-by: Paolo Bonzini --- hw/i386/sgx.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/hw/i386/sgx.c b/hw/i386/sgx.c index 11607568b6..8fef3dd8fa 100644 --- a/hw/i386/sgx.c +++ b/hw/i386/sgx.c @@ -21,6 +21,8 @@ #include "qapi/qapi-commands-misc-target.h" #include "exec/address-spaces.h" #include "sysemu/hw_accel.h" +#include "sysemu/reset.h" +#include =20 #define SGX_MAX_EPC_SECTIONS 8 #define SGX_CPUID_EPC_INVALID 0x0 @@ -29,6 +31,11 @@ #define SGX_CPUID_EPC_SECTION 0x1 #define SGX_CPUID_EPC_MASK 0xF =20 +#define SGX_MAGIC 0xA4 +#define SGX_IOC_VEPC_REMOVE_ALL _IO(SGX_MAGIC, 0x04) + +#define RETRY_NUM 2 + static uint64_t sgx_calc_section_metric(uint64_t low, uint64_t high) { return (low & MAKE_64BIT_MASK(12, 20)) + @@ -59,6 +66,46 @@ static uint64_t sgx_calc_host_epc_section_size(void) return size; } =20 +static void sgx_epc_reset(void *opaque) +{ + PCMachineState *pcms =3D PC_MACHINE(qdev_get_machine()); + HostMemoryBackend *hostmem; + SGXEPCDevice *epc; + int failures; + int fd, i, j, r; + static bool warned =3D false; + + /* + * The second pass is needed to remove SECS pages that could not + * be removed during the first. + */ + for (i =3D 0; i < RETRY_NUM; i++) { + failures =3D 0; + for (j =3D 0; j < pcms->sgx_epc.nr_sections; j++) { + epc =3D pcms->sgx_epc.sections[j]; + hostmem =3D MEMORY_BACKEND(epc->hostmem); + fd =3D memory_region_get_fd(host_memory_backend_get_memory(hos= tmem)); + + r =3D ioctl(fd, SGX_IOC_VEPC_REMOVE_ALL); + if (r =3D=3D -ENOTTY && !warned) { + warned =3D true; + warn_report("kernel does not support SGX_IOC_VEPC_REMOVE_A= LL"); + warn_report("SGX might operate incorrectly in the guest af= ter reset"); + break; + } else if (r > 0) { + /* SECS pages remain */ + failures++; + if (i =3D=3D 1) { + error_report("cannot reset vEPC section %d", j); + } + } + } + if (!failures) { + break; + } + } +} + SGXInfo *qmp_query_sgx_capabilities(Error **errp) { SGXInfo *info =3D NULL; @@ -190,4 +237,7 @@ void pc_machine_init_sgx_epc(PCMachineState *pcms) } =20 memory_region_set_size(&sgx_epc->mr, sgx_epc->size); + + /* register the reset callback for sgx epc */ + qemu_register_reset(sgx_epc_reset, NULL); } --=20 2.33.1