From nobody Thu Nov 13 21:51:50 2025 Delivered-To: importer@patchew.org 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; 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 ARC-Seal: i=1; a=rsa-sha256; t=1584029672; cv=none; d=zohomail.com; s=zohoarc; b=lFqljRlPZOGK30tN1jYgvIjx65EZuMkv+xTvPqRAy33TdA7WfFxZIkmUIcTeFk4zrwgfuhKjaebLF9k16xfVzAwBOqDiYbJpIqS46OZztcDNW6bS6UYE6C/CIIFTcLkVnXobJglAuk0zqHYdbjvfO+S8schu9oCkCUsWWmtEpfw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1584029672; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Uwrs/CORLnGGhUdeF1wuS44JPM7elseCQ8oqDPJu/lU=; b=DtJ19XKMBTdLS5dc3X7wYxR5weMv9FPmmNkiwnq3cuU6HkhURgF3ORwAKS6uSHu2JL3h4Dqmg4GdHvzRWxosSqAIuA37wfY8wVUc4ubLVKhMfe3jCEADw0nbnsapY0TqLRFfwzYAF5JuP4+8vfAD443sEWufU0nIkSxj0vQ3xRw= ARC-Authentication-Results: i=1; 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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1584029672646708.619350433615; Thu, 12 Mar 2020 09:14:32 -0700 (PDT) Received: from localhost ([::1]:44568 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCQTf-0003u6-NV for importer@patchew.org; Thu, 12 Mar 2020 12:14:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57079) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCQSI-0001gK-OP for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:13:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jCQSH-0007n4-MD for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:13:06 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:56668 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jCQSH-0007mt-HS for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:13:05 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-103-OVNBp4c8MMy9ejoGfHl9Vw-1; Thu, 12 Mar 2020 12:13:01 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EC4CA18B9FC3; Thu, 12 Mar 2020 16:12:59 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-131.ams2.redhat.com [10.36.117.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id 049CF5C1B5; Thu, 12 Mar 2020 16:12:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584029585; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Uwrs/CORLnGGhUdeF1wuS44JPM7elseCQ8oqDPJu/lU=; b=h2kSMlJHdhAjOCKGGyrIn4weBP+TZBuSfbpJfSFRryA6btetLeU+DDGLt944VdoOOFQU/+ J7aRNqhErpLQetkUkIUF67Ma+YpXXaIddVXRo5diycQLGvTwdR+yip3Uu/Oo1CpxhhspWc Vls3DIATCeNrm1JHg9KFcwTLTGjJJto= X-MC-Unique: OVNBp4c8MMy9ejoGfHl9Vw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RFCv2 1/4] openpic_kvm: Use kvm_device_ioctl() instead of ioctl() Date: Thu, 12 Mar 2020 17:12:14 +0100 Message-Id: <20200312161217.3590-2-david@redhat.com> In-Reply-To: <20200312161217.3590-1-david@redhat.com> References: <20200312161217.3590-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , Markus Armbruster , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , qemu-s390x@nongnu.org, Igor Mammedov , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Let's use the official variant, which will e.g., trace the call. Cc: Markus Armbruster Cc: "Philippe Mathieu-Daud=C3=A9" Cc: "Marc-Andr=C3=A9 Lureau" Cc: Paolo Bonzini Signed-off-by: David Hildenbrand --- hw/intc/openpic_kvm.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/hw/intc/openpic_kvm.c b/hw/intc/openpic_kvm.c index e4bf47d885..a5aa0ce915 100644 --- a/hw/intc/openpic_kvm.c +++ b/hw/intc/openpic_kvm.c @@ -70,10 +70,10 @@ static void kvm_openpic_write(void *opaque, hwaddr addr= , uint64_t val, attr.attr =3D addr; attr.addr =3D (uint64_t)(unsigned long)&val32; =20 - ret =3D ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr); + ret =3D kvm_device_ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr); if (ret < 0) { qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__, - strerror(errno), attr.attr); + strerror(-ret), attr.attr); } } =20 @@ -96,10 +96,10 @@ static uint64_t kvm_openpic_read(void *opaque, hwaddr a= ddr, unsigned size) attr.attr =3D addr; attr.addr =3D (uint64_t)(unsigned long)&val; =20 - ret =3D ioctl(opp->fd, KVM_GET_DEVICE_ATTR, &attr); + ret =3D kvm_device_ioctl(opp->fd, KVM_GET_DEVICE_ATTR, &attr); if (ret < 0) { qemu_log_mask(LOG_UNIMP, "%s: %s %" PRIx64 "\n", __func__, - strerror(errno), attr.attr); + strerror(-ret), attr.attr); return 0; } =20 @@ -145,10 +145,10 @@ static void kvm_openpic_region_add(MemoryListener *li= stener, attr.attr =3D KVM_DEV_MPIC_BASE_ADDR; attr.addr =3D (uint64_t)(unsigned long)®_base; =20 - ret =3D ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr); + ret =3D kvm_device_ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr); if (ret < 0) { fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__, - strerror(errno), reg_base); + strerror(-ret), reg_base); } } =20 @@ -179,10 +179,10 @@ static void kvm_openpic_region_del(MemoryListener *li= stener, attr.attr =3D KVM_DEV_MPIC_BASE_ADDR; attr.addr =3D (uint64_t)(unsigned long)®_base; =20 - ret =3D ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr); + ret =3D kvm_device_ioctl(opp->fd, KVM_SET_DEVICE_ATTR, &attr); if (ret < 0) { fprintf(stderr, "%s: %s %" PRIx64 "\n", __func__, - strerror(errno), reg_base); + strerror(-ret), reg_base); } } =20 --=20 2.24.1 From nobody Thu Nov 13 21:51:50 2025 Delivered-To: importer@patchew.org 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; 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 ARC-Seal: i=1; a=rsa-sha256; t=1584029749; cv=none; d=zohomail.com; s=zohoarc; b=bibIk/qrTNQ+omddoZFc5vqu+LJZ34o6Fx9Rt9T/hGur7yy00CwG0PKtZAHvd0pZUL9h+ygWcKTSBro94MI3RHdq0GO0Vtqu2ZwUla31l1lsxM65jPsWRiBj2jRqRgGHWjXGnIJ8lAI4RPWLzby0KUWHdvXa58PckL5JemmwT7k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1584029749; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7AoJdkA/vEE2eyNwUtS1zvah6ZFRv1wygcX5sCtbETQ=; b=Wm9Y9qesJDQeQws7YtjSvQGGD9ZxAi99lZYuuTFcojsDTN0nQTfGdT4D22yivxbtKge4qDfBK76ldNNjcsMrCk7XR2ZA332KxOB0AF0Oz2ybJI2EoC+BBxo2Ni4Gzf1KG/DCaD+dxB4i/Nus+0m2dCNH/jZAMdh4R5yCwMtJ44M= ARC-Authentication-Results: i=1; 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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1584029749253245.44491440879017; Thu, 12 Mar 2020 09:15:49 -0700 (PDT) Received: from localhost ([::1]:44600 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCQUu-0007Fy-Cr for importer@patchew.org; Thu, 12 Mar 2020 12:15:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57233) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCQSp-0002f5-Ha for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:13:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jCQSo-0008Cs-5X for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:13:39 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:20585 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jCQSo-0008Cg-1I for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:13:38 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-282-qbgfe4NMMluAJnv9obeMRQ-1; Thu, 12 Mar 2020 12:13:32 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 32F3B108FD5E; Thu, 12 Mar 2020 16:13:31 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-131.ams2.redhat.com [10.36.117.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id EAC475C1B5; Thu, 12 Mar 2020 16:13:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584029617; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7AoJdkA/vEE2eyNwUtS1zvah6ZFRv1wygcX5sCtbETQ=; b=F3wuXd70HnpoujDVaJu+L1ckRLJD/kRFpIznrl2p760U/Zc8Vd/rHhjiYhVbveF4t0hYwm fXQa0/MXv82YeYnd4w6v5T3a95RkFTNgEiSaGN7qdEXcRf2m7wg1UT4SB2Vq4e7eKMAQv1 OkMt1/32+76JBG11CZqveHq4MKiQpt8= X-MC-Unique: qbgfe4NMMluAJnv9obeMRQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RFCv2 2/4] intc/s390_flic_kvm.c: Use kvm_device_ioctl() instead of ioctl() Date: Thu, 12 Mar 2020 17:12:15 +0100 Message-Id: <20200312161217.3590-3-david@redhat.com> In-Reply-To: <20200312161217.3590-1-david@redhat.com> References: <20200312161217.3590-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , David Hildenbrand , Cornelia Huck , "Dr . David Alan Gilbert" , Peter Xu , Halil Pasic , Christian Borntraeger , qemu-s390x@nongnu.org, Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Let's use the official variant, which will e.g., trace the call. kvm_device_ioctl() will do the rc -> -errno conversion automatically, so we can drop that code. Cc: Cornelia Huck Cc: Halil Pasic Cc: Christian Borntraeger Cc: qemu-s390x@nongnu.org Signed-off-by: David Hildenbrand Reviewed-by: Christian Borntraeger --- hw/intc/s390_flic_kvm.c | 43 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/hw/intc/s390_flic_kvm.c b/hw/intc/s390_flic_kvm.c index a306b26faa..ce05306406 100644 --- a/hw/intc/s390_flic_kvm.c +++ b/hw/intc/s390_flic_kvm.c @@ -66,11 +66,8 @@ static int flic_get_all_irqs(KVMS390FLICState *flic, .addr =3D (uint64_t) buf, .attr =3D len, }; - int rc; =20 - rc =3D ioctl(flic->fd, KVM_GET_DEVICE_ATTR, &attr); - - return rc =3D=3D -1 ? -errno : rc; + return kvm_device_ioctl(flic->fd, KVM_GET_DEVICE_ATTR, &attr); } =20 static void flic_enable_pfault(KVMS390FLICState *flic) @@ -78,11 +75,8 @@ static void flic_enable_pfault(KVMS390FLICState *flic) struct kvm_device_attr attr =3D { .group =3D KVM_DEV_FLIC_APF_ENABLE, }; - int rc; - - rc =3D ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); =20 - if (rc) { + if (kvm_device_ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr)) { fprintf(stderr, "flic: couldn't enable pfault\n"); } } @@ -92,11 +86,8 @@ static void flic_disable_wait_pfault(KVMS390FLICState *f= lic) struct kvm_device_attr attr =3D { .group =3D KVM_DEV_FLIC_APF_DISABLE_WAIT, }; - int rc; =20 - rc =3D ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); - - if (rc) { + if (kvm_device_ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr)) { fprintf(stderr, "flic: couldn't disable pfault\n"); } } @@ -111,16 +102,13 @@ static void flic_disable_wait_pfault(KVMS390FLICState= *flic) static int flic_enqueue_irqs(void *buf, uint64_t len, KVMS390FLICState *flic) { - int rc; struct kvm_device_attr attr =3D { .group =3D KVM_DEV_FLIC_ENQUEUE, .addr =3D (uint64_t) buf, .attr =3D len, }; =20 - rc =3D ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); - - return rc ? -errno : 0; + return kvm_device_ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); } =20 static void kvm_s390_inject_flic(S390FLICState *fs, struct kvm_s390_irq *i= rq) @@ -187,7 +175,6 @@ static int kvm_s390_clear_io_flic(S390FLICState *fs, ui= nt16_t subchannel_id, uint16_t subchannel_nr) { KVMS390FLICState *flic =3D s390_get_kvm_flic(fs); - int rc; uint32_t sid =3D subchannel_id << 16 | subchannel_nr; struct kvm_device_attr attr =3D { .group =3D KVM_DEV_FLIC_CLEAR_IO_IRQ, @@ -197,8 +184,7 @@ static int kvm_s390_clear_io_flic(S390FLICState *fs, ui= nt16_t subchannel_id, if (unlikely(!flic->clear_io_supported)) { return -ENOSYS; } - rc =3D ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); - return rc ? -errno : 0; + return kvm_device_ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); } =20 static int kvm_s390_modify_ais_mode(S390FLICState *fs, uint8_t isc, @@ -218,7 +204,7 @@ static int kvm_s390_modify_ais_mode(S390FLICState *fs, = uint8_t isc, return -ENOSYS; } =20 - return ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr) ? -errno : 0; + return kvm_device_ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); } =20 static int kvm_s390_inject_airq(S390FLICState *fs, uint8_t type, @@ -235,7 +221,7 @@ static int kvm_s390_inject_airq(S390FLICState *fs, uint= 8_t type, return -ENOSYS; } =20 - return ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr) ? -errno : 0; + return kvm_device_ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); } =20 /** @@ -285,7 +271,6 @@ static int kvm_s390_register_io_adapter(S390FLICState *= fs, uint32_t id, .flags =3D flags, }; KVMS390FLICState *flic =3D KVM_S390_FLIC(fs); - int r; struct kvm_device_attr attr =3D { .group =3D KVM_DEV_FLIC_ADAPTER_REGISTER, .addr =3D (uint64_t)&adapter, @@ -296,9 +281,7 @@ static int kvm_s390_register_io_adapter(S390FLICState *= fs, uint32_t id, return 0; } =20 - r =3D ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); - - return r ? -errno : 0; + return kvm_device_ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); } =20 static int kvm_s390_io_adapter_map(S390FLICState *fs, uint32_t id, @@ -314,15 +297,13 @@ static int kvm_s390_io_adapter_map(S390FLICState *fs,= uint32_t id, .addr =3D (uint64_t)&req, }; KVMS390FLICState *flic =3D s390_get_kvm_flic(fs); - int r; =20 if (!kvm_gsi_routing_enabled()) { /* nothing to do */ return 0; } =20 - r =3D ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); - return r ? -errno : 0; + return kvm_device_ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); } =20 static int kvm_s390_add_adapter_routes(S390FLICState *fs, @@ -519,7 +500,7 @@ static int kvm_flic_ais_post_load(void *opaque, int ver= sion_id) return -ENOSYS; } =20 - return ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr) ? -errno : 0; + return kvm_device_ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); } =20 static const VMStateDescription kvm_s390_flic_ais_tmp =3D { @@ -636,9 +617,9 @@ static void kvm_s390_flic_reset(DeviceState *dev) } } =20 - rc =3D ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); + rc =3D kvm_device_ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr); if (rc) { - trace_flic_reset_failed(errno); + trace_flic_reset_failed(-rc); } =20 flic_enable_pfault(flic); --=20 2.24.1 From nobody Thu Nov 13 21:51:50 2025 Delivered-To: importer@patchew.org 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; 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 ARC-Seal: i=1; a=rsa-sha256; t=1584029892; cv=none; d=zohomail.com; s=zohoarc; b=AqCrqA5unPZGYkk0LVzlFzP5qWvjlU0BCkIOF0RfWXtAZ09CIdFw4fOdzyGshUD9m529fOc0JsGEaUbgzMecCrEQ1XNTLSb89tDMNEOXdh+CmvEE/Hx/WcCDYsp65mWWc+2JFMTfVbjLyO+pwGhwkwnCoNYPrMvLFy8dElNiqnE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1584029892; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Z7Ux+hft2bjNmRhZXzv82rPU0Rif6f5eByKMYnOSff8=; b=YqWLkPJYA5CrHLoyoCngKEHRfQhSyikOjTh+jv0adgNsPd1vIZrjXYz7wdP9ZsrImRt0Qkr4HxEF9l2IE0X9N4PS+REdxy1jHHT7o6QhGPf7jZuBS/EzIgO8BUPfQP/IfR0so3krKmRzY3V5/K3PoKMURK3k2Yd6FOQWPQWDywg= ARC-Authentication-Results: i=1; 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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1584029892982351.9760954985078; Thu, 12 Mar 2020 09:18:12 -0700 (PDT) Received: from localhost ([::1]:44646 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCQXD-0005er-NK for importer@patchew.org; Thu, 12 Mar 2020 12:18:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57320) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCQTE-0003do-92 for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:14:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jCQTC-0008V8-JF for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:14:04 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:37590 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jCQTC-0008UC-Fo for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:14:02 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-224-3x4XsIjyO7-VY86DMSwXEQ-1; Thu, 12 Mar 2020 12:13:54 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CA464109A5B1; Thu, 12 Mar 2020 16:13:53 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-131.ams2.redhat.com [10.36.117.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id CD55B5C1B5; Thu, 12 Mar 2020 16:13:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584029641; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z7Ux+hft2bjNmRhZXzv82rPU0Rif6f5eByKMYnOSff8=; b=RqE63U5of2v5diiwBTFXraDE1Sb8gNn6WXhMiKPc9LWXbifh0TfanPO0uoYv0/7PsLV9q+ olXWwYzfuU/I403zSM2bzN5Ro5g7ohnI8xoEk9nGFKSgwobXXrNiNexbwKJUgGYfEEWdEV fImBbK/dOB3GhKxbpzLacYdYi9RK+KI= X-MC-Unique: 3x4XsIjyO7-VY86DMSwXEQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RFCv2 3/4] memory: Add region_resize() callback to memory notifier Date: Thu, 12 Mar 2020 17:12:16 +0100 Message-Id: <20200312161217.3590-4-david@redhat.com> In-Reply-To: <20200312161217.3590-1-david@redhat.com> References: <20200312161217.3590-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , qemu-s390x@nongnu.org, Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Let's provide a way for memory notifiers to get notified about a resize. If the region_resize() callback is not implemented by a notifier, we mimic the old behavior by removing the old section and adding the new, resized section. This callback is helpful when backends (like KVM) want to implement atomic resizes of memory sections (e.g., resize while VCPUs are running and using the section). For now, we won't bother about a resize() callback for coalesced MMIO regions. The main future use case is for virtio-mem to resize ram memory regions while the guest is running. Cc: Richard Henderson Cc: Paolo Bonzini Cc: "Dr. David Alan Gilbert" Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Igor Mammedov Signed-off-by: David Hildenbrand --- include/exec/memory.h | 18 +++++++++++ memory.c | 72 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 85 insertions(+), 5 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 74805dd448..704eb64d75 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -492,6 +492,24 @@ struct MemoryListener { */ void (*region_nop)(MemoryListener *listener, MemoryRegionSection *sect= ion); =20 + /** + * @region_resize: + * + * Called during an address space update transaction, + * for a section of the address space that is in the same place in the + * address space as in the last transaction, however, the size changed. + * Dirty memory logging can change as well. + * + * Note: If this callback is not implemented, the resize is communicat= ed + * via a region_del(), followed by a region_add() instead. + * + * @listener: The #MemoryListener. + * @section: The old #MemoryRegionSection. + * @new: The new size. + */ + void (*region_resize)(MemoryListener *listener, + MemoryRegionSection *section, Int128 new); + /** * @log_start: * diff --git a/memory.c b/memory.c index 09be40edd2..097b0db0e4 100644 --- a/memory.c +++ b/memory.c @@ -246,6 +246,17 @@ static bool flatrange_equal(FlatRange *a, FlatRange *b) && a->nonvolatile =3D=3D b->nonvolatile; } =20 +static bool flatrange_resized(FlatRange *a, FlatRange *b) +{ + return a->mr =3D=3D b->mr + && int128_eq(a->addr.start, b->addr.start) + && int128_ne(a->addr.size, b->addr.size) + && a->offset_in_region =3D=3D b->offset_in_region + && a->romd_mode =3D=3D b->romd_mode + && a->readonly =3D=3D b->readonly + && a->nonvolatile =3D=3D b->nonvolatile; +} + static FlatView *flatview_new(MemoryRegion *mr_root) { FlatView *view; @@ -887,6 +898,45 @@ static void flat_range_coalesced_io_add(FlatRange *fr,= AddressSpace *as) } } =20 +static void memory_listener_resize_region(FlatRange *fr, AddressSpace *as, + Int128 new, bool adding) +{ + FlatView *as_view =3D address_space_to_flatview(as); + MemoryRegionSection old_mrs =3D section_from_flat_range(fr, as_view); + MemoryRegionSection new_mrs =3D old_mrs; + MemoryListener *listener; + + new_mrs.size =3D new; + + if (adding) { + QTAILQ_FOREACH(listener, &as->listeners, link_as) { + if (listener->region_resize) { + /* Grow in the adding phase. */ + if (int128_lt(fr->addr.size, new)) { + listener->region_resize(listener, &old_mrs, new); + } + continue; + } + if (listener->region_add) { + listener->region_add(listener, &new_mrs); + } + } + } else { + QTAILQ_FOREACH_REVERSE(listener, &as->listeners, link_as) { + if (listener->region_resize) { + /* Shrink in the removal phase. */ + if (int128_gt(fr->addr.size, new)) { + listener->region_resize(listener, &old_mrs, new); + } + continue; + } + if (listener->region_del) { + listener->region_del(listener, &old_mrs); + } + } + } +} + static void address_space_update_topology_pass(AddressSpace *as, const FlatView *old_view, const FlatView *new_view, @@ -911,11 +961,23 @@ static void address_space_update_topology_pass(Addres= sSpace *as, frnew =3D NULL; } =20 - if (frold - && (!frnew - || int128_lt(frold->addr.start, frnew->addr.start) - || (int128_eq(frold->addr.start, frnew->addr.start) - && !flatrange_equal(frold, frnew)))) { + if (frold && frnew && flatrange_resized(frold, frnew)) { + /* In both and only the size (+ eventually logging) changed. */ + + memory_listener_resize_region(frold, as, frnew->addr.size, + adding); + if (adding) { + flat_range_coalesced_io_add(frnew, as); + } else { + flat_range_coalesced_io_del(frold, as); + } + + ++iold; + ++inew; + } else if (frold && (!frnew + || int128_lt(frold->addr.start, frnew->addr.s= tart) + || (int128_eq(frold->addr.start, frnew->addr.= start) + && !flatrange_equal(frold, frnew)))) { /* In old but not in new, or in both but attributes changed. */ =20 if (!adding) { --=20 2.24.1 From nobody Thu Nov 13 21:51:50 2025 Delivered-To: importer@patchew.org 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; 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 ARC-Seal: i=1; a=rsa-sha256; t=1584029794; cv=none; d=zohomail.com; s=zohoarc; b=a4FchZhqpjEaHi3DBsLs/Zbu2SDQxvaCzgW1G7gHbYDX5XwyeJW6BB9Yh5PM8A0ShOFAQ7im/3gSl3kUtiexnPkYLOWUfMFEujBIz4P09RTz16EcWEVyY7fjK51eRTU+/trLdUTGk0kD4I8d1HlO3IvqgkgXhpC3qKOsT6r+ZEk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1584029794; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=aUmlo8MrH4gk71Z4QNFrQL6GEhldMh1YHNoBnWn6ApQ=; b=TWp13QDNGX3geWpDq8rJhAZ4w6XaNOoZSYm3qBaSvARu30rlIYS9lQv5h0siL/1NMykC6aEX9C6M6WD4ytZ1uIxtJdDDbf7c8dR3Yi1w6qMrxfrj07DMoamRtqVslwuiJqN+f3/ks1dK5wnOO8rxrG4HNpsEkgOoIXRUbefjY0o= ARC-Authentication-Results: i=1; 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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1584029794921674.5043464300124; Thu, 12 Mar 2020 09:16:34 -0700 (PDT) Received: from localhost ([::1]:44626 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCQVe-0001nO-39 for importer@patchew.org; Thu, 12 Mar 2020 12:16:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57328) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCQTF-0003fD-N2 for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:14:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jCQTC-0008VK-Nv for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:14:04 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:41933 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jCQTC-0008Uj-Je for qemu-devel@nongnu.org; Thu, 12 Mar 2020 12:14:02 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-33-dmC-x3q7M-Wm-r0xaaxhTA-1; Thu, 12 Mar 2020 12:14:00 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 322E68017CC; Thu, 12 Mar 2020 16:13:59 +0000 (UTC) Received: from t480s.redhat.com (ovpn-117-131.ams2.redhat.com [10.36.117.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id B507A5C1B5; Thu, 12 Mar 2020 16:13:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584029642; 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=aUmlo8MrH4gk71Z4QNFrQL6GEhldMh1YHNoBnWn6ApQ=; b=LIPsZ84jMGJXs8m5JNvnN8x056rUiTf4pymEBnN5CvQq0A6c4Zem58Avli8P3TI29S8lCi rruqMH6vsOrgsr0Apk7aq9IJ3ulTuUP8Dg8kwy2S5Oq7koYuN4+ViSahY4BOL17P0Mcv2C rNoY3tt+xNzeYOK5Ye5K3iPwgCEncUk= X-MC-Unique: dmC-x3q7M-Wm-r0xaaxhTA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH RFCv2 4/4] kvm: Implement atomic memory region resizes via region_resize() Date: Thu, 12 Mar 2020 17:12:17 +0100 Message-Id: <20200312161217.3590-5-david@redhat.com> In-Reply-To: <20200312161217.3590-1-david@redhat.com> References: <20200312161217.3590-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , kvm@vger.kernel.org, David Hildenbrand , "Dr . David Alan Gilbert" , Peter Xu , qemu-s390x@nongnu.org, Igor Mammedov , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" virtio-mem wants to resize (esp. grow) ram memory regions while the guest is already aware of them and makes use of them. Resizing a KVM slot can only currently be done by removing it and re-adding it. While the kvm slot is temporarily removed, VCPUs that try to access memory on these slots (via KVM_RUN) will fault. But also, other ioctls might depend on all slots being in place. Let's inhibit most KVM ioctls while performing the resize. Once we have an ioctl that can perform atomic resizes (e.g., KVM_SET_USER_MEMORY_REGION extensions), we can make inhibiting optional at runtime. To minimize cache-line bouncing, use separate indicators (in_ioctl) and locks (ioctl_mutex) per CPU. Also, make sure to hold the kvm_slots_lock while performing both actions (removing+re-adding). We have to wait until all IOCTLs were exited and block new ones from getting executed. Kick all CPUs, so they will exit the KVM_RUN ioctl. This approach cannot result in a deadlock as long as the inhibitor does not hold any locks that might hinder an IOCTL from getting finished and exited - something fairly unusual. The inhibitor will always hold the BQL. AFAIKs, one possible candidate would be userfaultfd. If a page cannot be placed (e.g., during postcopy), because we're waiting for a lock, or if the userfaultfd thread cannot process a fault, because it is waiting for a lock, there could be a deadlock. However, the BQL is not applicable here, because any other guest memory access while holding the BQL would already result in a deadlock. Nothing else in the kernel should block forever and wait for userspace intervention. Note1: Resizes of memory regions currently seems to happen during bootup only, so I don't think any existing RT users should be affected. Note2: pause_all_vcpus()/resume_all_vcpus() or start_exclusive()/end_exclusive() cannot be used, as they either drop the BQL or require to be called without the BQL - something inhibitors cannot handle. We need a low-level locking mechanism that is deadlock-free even when not releasing the BQL. Cc: Richard Henderson Cc: Paolo Bonzini Cc: "Dr. David Alan Gilbert" Cc: Eduardo Habkost Cc: Marcel Apfelbaum Cc: Igor Mammedov Cc: kvm@vger.kernel.org Signed-off-by: David Hildenbrand --- accel/kvm/kvm-all.c | 129 +++++++++++++++++++++++++++++++++++++++--- hw/core/cpu.c | 2 + include/hw/core/cpu.h | 4 ++ 3 files changed, 128 insertions(+), 7 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 439a4efe52..be159a2f78 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -149,6 +149,21 @@ bool kvm_msi_use_devid; static bool kvm_immediate_exit; static hwaddr kvm_max_slot_size =3D ~0; =20 +/* + * While holding kvm_ioctl_mutex and all cpu->ioctl_mutex, no new KVM ioct= ls + * can be started, but kvm ioctl inhibitors will have to wait for existing= ones + * to finish (indicated by cpu->in_ioctl and kvm_in_ioctl, both updated wi= th + * kvm_ioctl_mutex or under the cpu->ioctl_mutex when entering the ioctl). + */ +QemuMutex kvm_ioctl_mutex; +/* + * Atomic counter of active KVM ioctls except + * - The KVM ioctl inhibitor is doing an ioctl + * - kvm_ioctl(): Harmless and not interesting for inhibitors. + * - kvm_vcpu_ioctl(): Tracked via cpu->in_ioctl. + */ +static int kvm_in_ioctl; + static const KVMCapabilityInfo kvm_required_capabilites[] =3D { KVM_CAP_INFO(USER_MEMORY), KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS), @@ -1023,6 +1038,7 @@ void kvm_set_max_memslot_size(hwaddr max_slot_size) kvm_max_slot_size =3D max_slot_size; } =20 +/* Called with KVMMemoryListener.slots_lock held */ static void kvm_set_phys_mem(KVMMemoryListener *kml, MemoryRegionSection *section, bool add) { @@ -1052,14 +1068,12 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, ram =3D memory_region_get_ram_ptr(mr) + section->offset_within_region + (start_addr - section->offset_within_address_space); =20 - kvm_slots_lock(kml); - if (!add) { do { slot_size =3D MIN(kvm_max_slot_size, size); mem =3D kvm_lookup_matching_slot(kml, start_addr, slot_size); if (!mem) { - goto out; + return; } if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) { kvm_physical_sync_dirty_bitmap(kml, section); @@ -1079,7 +1093,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, start_addr +=3D slot_size; size -=3D slot_size; } while (size); - goto out; + return; } =20 /* register the new slot */ @@ -1108,9 +1122,6 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, ram +=3D slot_size; size -=3D slot_size; } while (size); - -out: - kvm_slots_unlock(kml); } =20 static void kvm_region_add(MemoryListener *listener, @@ -1119,7 +1130,9 @@ static void kvm_region_add(MemoryListener *listener, KVMMemoryListener *kml =3D container_of(listener, KVMMemoryListener, l= istener); =20 memory_region_ref(section->mr); + kvm_slots_lock(kml); kvm_set_phys_mem(kml, section, true); + kvm_slots_unlock(kml); } =20 static void kvm_region_del(MemoryListener *listener, @@ -1127,10 +1140,76 @@ static void kvm_region_del(MemoryListener *listener, { KVMMemoryListener *kml =3D container_of(listener, KVMMemoryListener, l= istener); =20 + kvm_slots_lock(kml); kvm_set_phys_mem(kml, section, false); + kvm_slots_unlock(kml); memory_region_unref(section->mr); } =20 +/* + * Certain updates (e.g., resizing memory regions) require temporarily rem= oving + * kvm memory slots. Make sure any ioctl sees a consistent memory slot sta= te. + */ +static void kvm_ioctl_inhibit_begin(void) +{ + CPUState *cpu; + + /* + * We allow to inhibit only when holding the BQL, so we can identify + * when an inhibitor wants to issue an ioctl easily. + */ + g_assert(qemu_mutex_iothread_locked()); + + CPU_FOREACH(cpu) { + qemu_mutex_lock(&cpu->ioctl_mutex); + } + qemu_mutex_lock(&kvm_ioctl_mutex); + + /* Inhibiting happens rarely, we can keep things simple and spin here.= */ + while (true) { + bool any_cpu_in_ioctl =3D false; + + CPU_FOREACH(cpu) { + if (atomic_read(&cpu->in_ioctl)) { + any_cpu_in_ioctl =3D true; + qemu_cpu_kick(cpu); + } + } + if (!any_cpu_in_ioctl && !atomic_read(&kvm_in_ioctl)) { + break; + } + g_usleep(100); + } +} + +static void kvm_ioctl_inhibit_end(void) +{ + CPUState *cpu; + + qemu_mutex_unlock(&kvm_ioctl_mutex); + CPU_FOREACH(cpu) { + qemu_mutex_unlock(&cpu->ioctl_mutex); + } +} + +static void kvm_region_resize(MemoryListener *listener, + MemoryRegionSection *section, Int128 new) +{ + KVMMemoryListener *kml =3D container_of(listener, KVMMemoryListener, + listener); + MemoryRegionSection new_section =3D *section; + + new_section.size =3D new; + + kvm_slots_lock(kml); + /* Inhibit KVM ioctls while temporarily removing slots. */ + kvm_ioctl_inhibit_begin(); + kvm_set_phys_mem(kml, section, false); + kvm_set_phys_mem(kml, &new_section, true); + kvm_ioctl_inhibit_end(); + kvm_slots_unlock(kml); +} + static void kvm_log_sync(MemoryListener *listener, MemoryRegionSection *section) { @@ -1249,6 +1328,7 @@ void kvm_memory_listener_register(KVMState *s, KVMMem= oryListener *kml, =20 kml->listener.region_add =3D kvm_region_add; kml->listener.region_del =3D kvm_region_del; + kml->listener.region_resize =3D kvm_region_resize; kml->listener.log_start =3D kvm_log_start; kml->listener.log_stop =3D kvm_log_stop; kml->listener.log_sync =3D kvm_log_sync; @@ -1894,6 +1974,7 @@ static int kvm_init(MachineState *ms) assert(TARGET_PAGE_SIZE <=3D qemu_real_host_page_size); =20 s->sigmask_len =3D 8; + qemu_mutex_init(&kvm_ioctl_mutex); =20 #ifdef KVM_CAP_SET_GUEST_DEBUG QTAILQ_INIT(&s->kvm_sw_breakpoints); @@ -2304,6 +2385,34 @@ static void kvm_eat_signals(CPUState *cpu) } while (sigismember(&chkset, SIG_IPI)); } =20 +static void kvm_cpu_set_in_ioctl(CPUState *cpu, bool in_ioctl) +{ + if (unlikely(qemu_mutex_iothread_locked())) { + return; + } + if (in_ioctl) { + qemu_mutex_lock(&cpu->ioctl_mutex); + cpu->in_ioctl =3D true; + qemu_mutex_unlock(&cpu->ioctl_mutex); + } else { + atomic_set(&cpu->in_ioctl, false); + } +} + +static void kvm_set_in_ioctl(bool in_ioctl) +{ + if (likely(qemu_mutex_iothread_locked())) { + return; + } + if (in_ioctl) { + qemu_mutex_lock(&kvm_ioctl_mutex); + kvm_in_ioctl++; + qemu_mutex_unlock(&kvm_ioctl_mutex); + } else { + atomic_dec(&kvm_in_ioctl); + } +} + int kvm_cpu_exec(CPUState *cpu) { struct kvm_run *run =3D cpu->kvm_run; @@ -2488,7 +2597,9 @@ int kvm_vm_ioctl(KVMState *s, int type, ...) va_end(ap); =20 trace_kvm_vm_ioctl(type, arg); + kvm_set_in_ioctl(true); ret =3D ioctl(s->vmfd, type, arg); + kvm_set_in_ioctl(false); if (ret =3D=3D -1) { ret =3D -errno; } @@ -2506,7 +2617,9 @@ int kvm_vcpu_ioctl(CPUState *cpu, int type, ...) va_end(ap); =20 trace_kvm_vcpu_ioctl(cpu->cpu_index, type, arg); + kvm_cpu_set_in_ioctl(cpu, true); ret =3D ioctl(cpu->kvm_fd, type, arg); + kvm_cpu_set_in_ioctl(cpu, false); if (ret =3D=3D -1) { ret =3D -errno; } @@ -2524,7 +2637,9 @@ int kvm_device_ioctl(int fd, int type, ...) va_end(ap); =20 trace_kvm_device_ioctl(fd, type, arg); + kvm_set_in_ioctl(true); ret =3D ioctl(fd, type, arg); + kvm_set_in_ioctl(false); if (ret =3D=3D -1) { ret =3D -errno; } diff --git a/hw/core/cpu.c b/hw/core/cpu.c index fe65ca62ac..3591dc3874 100644 --- a/hw/core/cpu.c +++ b/hw/core/cpu.c @@ -379,6 +379,7 @@ static void cpu_common_initfn(Object *obj) cpu->nr_threads =3D 1; =20 qemu_mutex_init(&cpu->work_mutex); + qemu_mutex_init(&cpu->ioctl_mutex); QTAILQ_INIT(&cpu->breakpoints); QTAILQ_INIT(&cpu->watchpoints); =20 @@ -389,6 +390,7 @@ static void cpu_common_finalize(Object *obj) { CPUState *cpu =3D CPU(obj); =20 + qemu_mutex_destroy(&cpu->ioctl_mutex); qemu_mutex_destroy(&cpu->work_mutex); } =20 diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index 73e9a869a4..3abf251037 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -431,6 +431,10 @@ struct CPUState { /* shared by kvm, hax and hvf */ bool vcpu_dirty; =20 + /* kvm only for now: CPU is in kvm_vcpu_ioctl() (esp. KVM_RUN) */ + bool in_ioctl; + QemuMutex ioctl_mutex; + /* Used to keep track of an outstanding cpu throttle thread for migrat= ion * autoconverge */ --=20 2.24.1