From nobody Sun May 10 17:10:49 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8ABEBC433EF for ; Thu, 28 Apr 2022 13:22:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347103AbiD1N0J (ORCPT ); Thu, 28 Apr 2022 09:26:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240001AbiD1N0E (ORCPT ); Thu, 28 Apr 2022 09:26:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E0948AC913 for ; Thu, 28 Apr 2022 06:22:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651152169; 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=W6dlhZBY+AtMsp+AcyPDmHg9swfew9GIgRtcUZ3Lw40=; b=ERGRWPExNBg0Pvy0b1aC2RyZ9uCcm09vBbygDZnBwciKtm+n9Ib9QUd7nZLGQGTrh5QajR ncKogxSpXRWn6zXHwX6Wi8ji3XWCbMPZ7bjRh2m63jTc/GtG5QbdL6yqhUqbdGBS71SlNQ Mwl0kEKBSIi081bEWFfOIPEaHIMq3F8= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-468-sTkK3GmwP96ImfcHDMbiKQ-1; Thu, 28 Apr 2022 09:22:47 -0400 X-MC-Unique: sTkK3GmwP96ImfcHDMbiKQ-1 Received: by mail-wm1-f70.google.com with SMTP id r186-20020a1c44c3000000b00393f52ed5ceso3612005wma.7 for ; Thu, 28 Apr 2022 06:22:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=W6dlhZBY+AtMsp+AcyPDmHg9swfew9GIgRtcUZ3Lw40=; b=Lr6V+2rwsvvOfM9w88WUDrgMh3UdL6ZTd5ZCw3ZeYsTVHs0DPB+pYzh+GwVYpnKMsI o8s7hHIg6pEem1/FhPqQtG7yH8Q1VDn3oHWArEfYF+EjCo86hysOR1a2r0I2Txj8VVrb PdvVUysROJoPWB4Hp5ZrsRp+J2htxD1ulRrL8zntbC8KMRVlrCgboEt87KptTDjAC9qD rSA7zcoTO6SzkLpQCc0FeYgNg4d2+tYT+gNx0KaPAxXJOueZ5SpuwgoOEreSQ1KE78Jg mf2u8WVGX3W2KTN8NAVhCKHFmVsO490+25vE3sRVrWXLcXc+w/r0InTbp0ipTHhSDP+D zaYQ== X-Gm-Message-State: AOAM533xsBKqpd2MeCuO7smvDwOOx0SFSDQS0jSROYLhUwe3sfmLg75Y 7vwdfTpYC16iRwCcsqujaPICgO3hRYKcnbcLXoEUpI7KXOEofcL5YSmwaDR/VahPTnUhlFpB20y Zp3Ci0zznLXXHTGPdo1u2C9fy X-Received: by 2002:a05:6000:510:b0:203:e469:f0a3 with SMTP id a16-20020a056000051000b00203e469f0a3mr26951305wrf.710.1651152166448; Thu, 28 Apr 2022 06:22:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxYAkm50ZItGhHTzdsIHOIHFbRvU2OlAJNnEs3e9LhZU6vcKw9GOx6UJGzwm9gFfHP9K8b2vw== X-Received: by 2002:a05:6000:510:b0:203:e469:f0a3 with SMTP id a16-20020a056000051000b00203e469f0a3mr26951279wrf.710.1651152166162; Thu, 28 Apr 2022 06:22:46 -0700 (PDT) Received: from step1.redhat.com (host-87-11-6-234.retail.telecomitalia.it. [87.11.6.234]) by smtp.gmail.com with ESMTPSA id f7-20020a05600c4e8700b00393f1393abfsm4680978wmq.41.2022.04.28.06.22.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 06:22:45 -0700 (PDT) From: Stefano Garzarella To: netdev@vger.kernel.org Cc: Stefan Hajnoczi , Jason Wang , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, "David S. Miller" , Paolo Abeni , Vilas R K , linux-kernel@vger.kernel.org, Stefano Garzarella , Jakub Kicinski , "Michael S. Tsirkin" Subject: [PATCH net-next 1/2] vsock/virtio: factor our the code to initialize and delete VQs Date: Thu, 28 Apr 2022 15:22:40 +0200 Message-Id: <20220428132241.152679-2-sgarzare@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428132241.152679-1-sgarzare@redhat.com> References: <20220428132241.152679-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add virtio_vsock_vqs_init() and virtio_vsock_vqs_del() with the code that was in virtio_vsock_probe() and virtio_vsock_remove to initialize and delete VQs. These new functions will be used in the next commit to support device suspend/resume Signed-off-by: Stefano Garzarella Acked-by: Michael S. Tsirkin --- net/vmw_vsock/virtio_transport.c | 150 +++++++++++++++++-------------- 1 file changed, 84 insertions(+), 66 deletions(-) diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index ba1c8cc0c467..31f4f6f40614 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -566,67 +566,28 @@ static void virtio_transport_rx_work(struct work_stru= ct *work) mutex_unlock(&vsock->rx_lock); } =20 -static int virtio_vsock_probe(struct virtio_device *vdev) +static int virtio_vsock_vqs_init(struct virtio_vsock *vsock) { - vq_callback_t *callbacks[] =3D { - virtio_vsock_rx_done, - virtio_vsock_tx_done, - virtio_vsock_event_done, - }; + struct virtio_device *vdev =3D vsock->vdev; static const char * const names[] =3D { "rx", "tx", "event", }; - struct virtio_vsock *vsock =3D NULL; + vq_callback_t *callbacks[] =3D { + virtio_vsock_rx_done, + virtio_vsock_tx_done, + virtio_vsock_event_done, + }; int ret; =20 - ret =3D mutex_lock_interruptible(&the_virtio_vsock_mutex); - if (ret) - return ret; - - /* Only one virtio-vsock device per guest is supported */ - if (rcu_dereference_protected(the_virtio_vsock, - lockdep_is_held(&the_virtio_vsock_mutex))) { - ret =3D -EBUSY; - goto out; - } - - vsock =3D kzalloc(sizeof(*vsock), GFP_KERNEL); - if (!vsock) { - ret =3D -ENOMEM; - goto out; - } - - vsock->vdev =3D vdev; - - ret =3D virtio_find_vqs(vsock->vdev, VSOCK_VQ_MAX, - vsock->vqs, callbacks, names, + ret =3D virtio_find_vqs(vdev, VSOCK_VQ_MAX, vsock->vqs, callbacks, names, NULL); if (ret < 0) - goto out; + return ret; =20 virtio_vsock_update_guest_cid(vsock); =20 - vsock->rx_buf_nr =3D 0; - vsock->rx_buf_max_nr =3D 0; - atomic_set(&vsock->queued_replies, 0); - - mutex_init(&vsock->tx_lock); - mutex_init(&vsock->rx_lock); - mutex_init(&vsock->event_lock); - spin_lock_init(&vsock->send_pkt_list_lock); - INIT_LIST_HEAD(&vsock->send_pkt_list); - INIT_WORK(&vsock->rx_work, virtio_transport_rx_work); - INIT_WORK(&vsock->tx_work, virtio_transport_tx_work); - INIT_WORK(&vsock->event_work, virtio_transport_event_work); - INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work); - - if (virtio_has_feature(vdev, VIRTIO_VSOCK_F_SEQPACKET)) - vsock->seqpacket_allow =3D true; - - vdev->priv =3D vsock; - virtio_device_ready(vdev); =20 mutex_lock(&vsock->tx_lock); @@ -643,30 +604,15 @@ static int virtio_vsock_probe(struct virtio_device *v= dev) vsock->event_run =3D true; mutex_unlock(&vsock->event_lock); =20 - rcu_assign_pointer(the_virtio_vsock, vsock); - - mutex_unlock(&the_virtio_vsock_mutex); - return 0; - -out: - kfree(vsock); - mutex_unlock(&the_virtio_vsock_mutex); - return ret; } =20 -static void virtio_vsock_remove(struct virtio_device *vdev) +static void virtio_vsock_vqs_del(struct virtio_vsock *vsock) { - struct virtio_vsock *vsock =3D vdev->priv; + struct virtio_device *vdev =3D vsock->vdev; struct virtio_vsock_pkt *pkt; =20 - mutex_lock(&the_virtio_vsock_mutex); - - vdev->priv =3D NULL; - rcu_assign_pointer(the_virtio_vsock, NULL); - synchronize_rcu(); - - /* Reset all connected sockets when the device disappear */ + /* Reset all connected sockets when the VQs disappear */ vsock_for_each_connected_socket(&virtio_transport.transport, virtio_vsock_reset_sock); =20 @@ -711,6 +657,78 @@ static void virtio_vsock_remove(struct virtio_device *= vdev) =20 /* Delete virtqueues and flush outstanding callbacks if any */ vdev->config->del_vqs(vdev); +} + +static int virtio_vsock_probe(struct virtio_device *vdev) +{ + struct virtio_vsock *vsock =3D NULL; + int ret; + + ret =3D mutex_lock_interruptible(&the_virtio_vsock_mutex); + if (ret) + return ret; + + /* Only one virtio-vsock device per guest is supported */ + if (rcu_dereference_protected(the_virtio_vsock, + lockdep_is_held(&the_virtio_vsock_mutex))) { + ret =3D -EBUSY; + goto out; + } + + vsock =3D kzalloc(sizeof(*vsock), GFP_KERNEL); + if (!vsock) { + ret =3D -ENOMEM; + goto out; + } + + vsock->vdev =3D vdev; + + vsock->rx_buf_nr =3D 0; + vsock->rx_buf_max_nr =3D 0; + atomic_set(&vsock->queued_replies, 0); + + mutex_init(&vsock->tx_lock); + mutex_init(&vsock->rx_lock); + mutex_init(&vsock->event_lock); + spin_lock_init(&vsock->send_pkt_list_lock); + INIT_LIST_HEAD(&vsock->send_pkt_list); + INIT_WORK(&vsock->rx_work, virtio_transport_rx_work); + INIT_WORK(&vsock->tx_work, virtio_transport_tx_work); + INIT_WORK(&vsock->event_work, virtio_transport_event_work); + INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work); + + if (virtio_has_feature(vdev, VIRTIO_VSOCK_F_SEQPACKET)) + vsock->seqpacket_allow =3D true; + + vdev->priv =3D vsock; + + ret =3D virtio_vsock_vqs_init(vsock); + if (ret < 0) + goto out; + + rcu_assign_pointer(the_virtio_vsock, vsock); + + mutex_unlock(&the_virtio_vsock_mutex); + + return 0; + +out: + kfree(vsock); + mutex_unlock(&the_virtio_vsock_mutex); + return ret; +} + +static void virtio_vsock_remove(struct virtio_device *vdev) +{ + struct virtio_vsock *vsock =3D vdev->priv; + + mutex_lock(&the_virtio_vsock_mutex); + + vdev->priv =3D NULL; + rcu_assign_pointer(the_virtio_vsock, NULL); + synchronize_rcu(); + + virtio_vsock_vqs_del(vsock); =20 /* Other works can be queued before 'config->del_vqs()', so we flush * all works before to free the vsock object to avoid use after free. --=20 2.35.1 From nobody Sun May 10 17:10:49 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2CFF2C43217 for ; Thu, 28 Apr 2022 13:23:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347142AbiD1N0O (ORCPT ); Thu, 28 Apr 2022 09:26:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347105AbiD1N0M (ORCPT ); Thu, 28 Apr 2022 09:26:12 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3D67AAC927 for ; Thu, 28 Apr 2022 06:22:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651152173; 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=a14xq4pv5r/FkYeXeUtrnJ56gM2JRtUzYDAyxUxKyXw=; b=Tt248EhYFzQ1e2QqfYV5aGSPG/ngkVC8YQdIZJkjiYuiU0HMOA35v1TbBI5pgu7/G6rBlr dW2xXHL3i07Uv3uPefDHVDRtj69desMiwMf12uvzCGskizj59OJJ+bRf6qjf/B0SVdOkt1 GBi7drlRIjEF0iM4bHMMWyTrh7qGsJ0= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-263-lYfuRa2JPnaHhPowZP6EOw-1; Thu, 28 Apr 2022 09:22:49 -0400 X-MC-Unique: lYfuRa2JPnaHhPowZP6EOw-1 Received: by mail-wm1-f69.google.com with SMTP id k41-20020a05600c1ca900b0039415d40dbbso517405wms.5 for ; Thu, 28 Apr 2022 06:22:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=a14xq4pv5r/FkYeXeUtrnJ56gM2JRtUzYDAyxUxKyXw=; b=rechXkeQkYfBANUsV275z486wqBUIY0BKcLDeIL2rhjWjfAM/jnOZPjxlIPVDvPV8W poMTw8L4b9aFQImnGT4Vq1SNn2G12OLC4hG+bIqhkLut73GYrbdWIneyJghvC3NloffH c3THNDuYI33sD48QE+uBfYqisrDg+gjBOvJX59LznZI5uDfzMe+p7s/4slrv35tUGUnp /QgPp5GgXXfrNKwSj66uV012UpYWuLSTQJFsXVM2hcpuKqpI2UZ8ZO19t4ct6XG0r4HF NSrxYmx5rg4paXrmrwoc0yaINQ7HU3Seq5/GwUCH83Ax7dhv51G24jXQW/ev7eG74I9p BLqA== X-Gm-Message-State: AOAM531MV0nz6s4Dzqn3nAsR9LEMbYxq1tWDU3iacG0HX50jADJLv6E9 rKpcZVOWe9BDiqH38UYUsbArl8vwM9iPjDuab4lNHMWi0FP4uv4YPMEiM5ptURsE/yGhdukHvF3 a498DqBDTaSnENjON5JP2uOsW X-Received: by 2002:a05:600c:502b:b0:38f:f7c6:3609 with SMTP id n43-20020a05600c502b00b0038ff7c63609mr31169266wmr.15.1651152168729; Thu, 28 Apr 2022 06:22:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQVnCQJ38EwfNhzDeSYDCstkye3h+BWbMt9XxC8erbs+iXajlC/fBdW5XhbJa/npUVsWgZzw== X-Received: by 2002:a05:600c:502b:b0:38f:f7c6:3609 with SMTP id n43-20020a05600c502b00b0038ff7c63609mr31169254wmr.15.1651152168532; Thu, 28 Apr 2022 06:22:48 -0700 (PDT) Received: from step1.redhat.com (host-87-11-6-234.retail.telecomitalia.it. [87.11.6.234]) by smtp.gmail.com with ESMTPSA id f7-20020a05600c4e8700b00393f1393abfsm4680978wmq.41.2022.04.28.06.22.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 06:22:47 -0700 (PDT) From: Stefano Garzarella To: netdev@vger.kernel.org Cc: Stefan Hajnoczi , Jason Wang , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, "David S. Miller" , Paolo Abeni , Vilas R K , linux-kernel@vger.kernel.org, Stefano Garzarella , Jakub Kicinski , "Michael S. Tsirkin" Subject: [PATCH net-next 2/2] vsock/virtio: add support for device suspend/resume Date: Thu, 28 Apr 2022 15:22:41 +0200 Message-Id: <20220428132241.152679-3-sgarzare@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428132241.152679-1-sgarzare@redhat.com> References: <20220428132241.152679-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Implement .freeze and .restore callbacks of struct virtio_driver to support device suspend/resume. During suspension all connected sockets are reset and VQs deleted. During resume the VQs are re-initialized. Reported by: Vilas R K Signed-off-by: Stefano Garzarella Acked-by: Michael S. Tsirkin --- net/vmw_vsock/virtio_transport.c | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index 31f4f6f40614..ad64f403536a 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -743,6 +743,49 @@ static void virtio_vsock_remove(struct virtio_device *= vdev) kfree(vsock); } =20 +#ifdef CONFIG_PM_SLEEP +static int virtio_vsock_freeze(struct virtio_device *vdev) +{ + struct virtio_vsock *vsock =3D vdev->priv; + + mutex_lock(&the_virtio_vsock_mutex); + + rcu_assign_pointer(the_virtio_vsock, NULL); + synchronize_rcu(); + + virtio_vsock_vqs_del(vsock); + + mutex_unlock(&the_virtio_vsock_mutex); + + return 0; +} + +static int virtio_vsock_restore(struct virtio_device *vdev) +{ + struct virtio_vsock *vsock =3D vdev->priv; + int ret; + + mutex_lock(&the_virtio_vsock_mutex); + + /* Only one virtio-vsock device per guest is supported */ + if (rcu_dereference_protected(the_virtio_vsock, + lockdep_is_held(&the_virtio_vsock_mutex))) { + ret =3D -EBUSY; + goto out; + } + + ret =3D virtio_vsock_vqs_init(vsock); + if (ret < 0) + goto out; + + rcu_assign_pointer(the_virtio_vsock, vsock); + +out: + mutex_unlock(&the_virtio_vsock_mutex); + return ret; +} +#endif /* CONFIG_PM_SLEEP */ + static struct virtio_device_id id_table[] =3D { { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID }, { 0 }, @@ -760,6 +803,10 @@ static struct virtio_driver virtio_vsock_driver =3D { .id_table =3D id_table, .probe =3D virtio_vsock_probe, .remove =3D virtio_vsock_remove, +#ifdef CONFIG_PM_SLEEP + .freeze =3D virtio_vsock_freeze, + .restore =3D virtio_vsock_restore, +#endif }; =20 static int __init virtio_vsock_init(void) --=20 2.35.1