From patchwork Thu Mar 25 15:07:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kurz X-Patchwork-Id: 12164263 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BB18C433C1 for ; Thu, 25 Mar 2021 15:08:54 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 69BF161A16 for ; Thu, 25 Mar 2021 15:08:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 69BF161A16 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kaod.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:33094 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lPRbQ-0006SR-8K for qemu-devel@archiver.kernel.org; Thu, 25 Mar 2021 11:08:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34998) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPRaX-0004mB-Vy for qemu-devel@nongnu.org; Thu, 25 Mar 2021 11:07:57 -0400 Received: from us-smtp-delivery-44.mimecast.com ([205.139.111.44]:42467) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPRaW-0007ht-Go for qemu-devel@nongnu.org; Thu, 25 Mar 2021 11:07:57 -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-302-ISCFLV-BMp-_l3JNhkOYjg-1; Thu, 25 Mar 2021 11:07:52 -0400 X-MC-Unique: ISCFLV-BMp-_l3JNhkOYjg-1 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 EF0D380BCA6; Thu, 25 Mar 2021 15:07:50 +0000 (UTC) Received: from bahia.redhat.com (ovpn-113-20.ams2.redhat.com [10.36.113.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id F076E5C241; Thu, 25 Mar 2021 15:07:48 +0000 (UTC) From: Greg Kurz To: qemu-devel@nongnu.org Subject: [RFC 2/8] virtio: Introduce virtio_bus_set_host_notifiers() Date: Thu, 25 Mar 2021 16:07:29 +0100 Message-Id: <20210325150735.1098387-3-groug@kaod.org> In-Reply-To: <20210325150735.1098387-1-groug@kaod.org> References: <20210325150735.1098387-1-groug@kaod.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=groug@kaod.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kaod.org Received-SPF: softfail client-ip=205.139.111.44; envelope-from=groug@kaod.org; helo=us-smtp-delivery-44.mimecast.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_SOFTFAIL=0.665 autolearn=no autolearn_force=no X-Spam_action: no action 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: Fam Zheng , Kevin Wolf , qemu-block@nongnu.org, "Michael S. Tsirkin" , Greg Kurz , Max Reitz , Stefan Hajnoczi , Paolo Bonzini , David Gibson Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Multiqueue devices such as virtio-scsi or virtio-blk, all open-code the same pattern to setup/tear down host notifiers of the request virtqueues. Consolidate the pattern in a new virtio_bus_set_host_notifiers() API. Since virtio-scsi and virtio-blk both fallback to userspace if host notifiers can't be set, e.g. file descriptor exhaustion, go for a warning rather than an error. Also make it oneshot to avoid flooding the logs since the message would be very likely the same for all virtqueues. Devices will be converted to use virtio_bus_set_host_notifiers() in separate patches. Signed-off-by: Greg Kurz Reviewed-by: Stefan Hajnoczi --- include/hw/virtio/virtio-bus.h | 3 +++ hw/virtio/virtio-bus.c | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/hw/virtio/virtio-bus.h b/include/hw/virtio/virtio-bus.h index ef8abe49c5a1..6d1e4ee3e886 100644 --- a/include/hw/virtio/virtio-bus.h +++ b/include/hw/virtio/virtio-bus.h @@ -154,5 +154,8 @@ void virtio_bus_release_ioeventfd(VirtioBusState *bus); int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign); /* Tell the bus that the ioeventfd handler is no longer required. */ void virtio_bus_cleanup_host_notifier(VirtioBusState *bus, int n); +/* Call virtio_bus_set_host_notifier() for several consecutive vqs */ +int virtio_bus_set_host_notifiers(VirtioBusState *bus, int nvqs, int n_offset, + bool assign); #endif /* VIRTIO_BUS_H */ diff --git a/hw/virtio/virtio-bus.c b/hw/virtio/virtio-bus.c index d6332d45c3b2..c9e7cdb5c161 100644 --- a/hw/virtio/virtio-bus.c +++ b/hw/virtio/virtio-bus.c @@ -308,6 +308,42 @@ void virtio_bus_cleanup_host_notifier(VirtioBusState *bus, int n) event_notifier_cleanup(notifier); } +static void virtio_bus_unset_and_cleanup_host_notifiers(VirtioBusState *bus, + int nvqs, int n_offset) +{ + int i; + + for (i = 0; i < nvqs; i++) { + virtio_bus_set_host_notifier(bus, i + n_offset, false); + virtio_bus_cleanup_host_notifier(bus, i + n_offset); + } +} + +int virtio_bus_set_host_notifiers(VirtioBusState *bus, int nvqs, int n_offset, + bool assign) +{ + VirtIODevice *vdev = virtio_bus_get_device(bus); + int i; + int rc; + + if (assign) { + for (i = 0; i < nvqs; i++) { + rc = virtio_bus_set_host_notifier(bus, i + n_offset, true); + if (rc != 0) { + warn_report_once("%s: Failed to set host notifier (%s).\n", + vdev->name, strerror(-rc)); + + virtio_bus_unset_and_cleanup_host_notifiers(bus, i, n_offset); + return rc; + } + } + } else { + virtio_bus_unset_and_cleanup_host_notifiers(bus, nvqs, n_offset); + } + + return 0; +} + static char *virtio_bus_get_dev_path(DeviceState *dev) { BusState *bus = qdev_get_parent_bus(dev);