From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id E6484A052A; Tue, 26 Jan 2021 13:59:19 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 9AA03141492; Tue, 26 Jan 2021 13:59:19 +0100 (CET) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mails.dpdk.org (Postfix) with ESMTP id 53A63141490 for ; Tue, 26 Jan 2021 13:59:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611665956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m/R3QPELpNQqvpeDBOCyekFKkT0rM3OsjDrjVr6Gsrs=; b=S/1A1WjnWxJmnt6c6AKmdm2PLPJqmjnis3if+7zw72BhyOzyS93LxNM34Mckq7YH8fLIx6 OHgwsLRaHqB7R/5nkAJh1Vu9nofMaL8U5xT1MsIxFUO/OWmWPYdKQL3z4Zscm+NsRJZ/Z6 wjKfJweKrdvqyi3Ks/D6aIDKMXTW30Y= 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-422-dXP9rA8UNPOHtLCoIN6O1Q-1; Tue, 26 Jan 2021 07:59:11 -0500 X-MC-Unique: dXP9rA8UNPOHtLCoIN6O1Q-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E8CD781C9ED; Tue, 26 Jan 2021 12:59:09 +0000 (UTC) Received: from [10.36.110.31] (unknown [10.36.110.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8C5DA1002EE6; Tue, 26 Jan 2021 12:59:05 +0000 (UTC) To: "Xia, Chenbo" , "dev@dpdk.org" , "olivier.matz@6wind.com" , "amorenoz@redhat.com" , "david.marchand@redhat.com" References: <20210126101639.250481-1-maxime.coquelin@redhat.com> <20210126101639.250481-45-maxime.coquelin@redhat.com> From: Maxime Coquelin Message-ID: <552d365a-effc-056f-0681-e72eab0a8b41@redhat.com> Date: Tue, 26 Jan 2021 13:59:03 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=maxime.coquelin@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [dpdk-dev] [PATCH v4 44/44] net/virtio: handle Virtio-user setup failure properly X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On 1/26/21 1:02 PM, Xia, Chenbo wrote: > Hi Maxime, > >> -----Original Message----- >> From: Maxime Coquelin >> Sent: Tuesday, January 26, 2021 6:17 PM >> To: dev@dpdk.org; Xia, Chenbo ; olivier.matz@6wind.com; >> amorenoz@redhat.com; david.marchand@redhat.com >> Cc: Maxime Coquelin >> Subject: [PATCH v4 44/44] net/virtio: handle Virtio-user setup failure >> properly >> >> This patch fixes virtio_user_dev_setup() error path, >> by cleaning all resources it allocates. It introduces >> virtio_user_dev_uninit_notify() that cleans all open >> FDs. It implies assigning all FDs to -1 at init time. >> >> With these changes done, virtio_user_dev_init_notify() >> can be simplified. >> >> Suggested-by: Adrian Moreno >> Signed-off-by: Maxime Coquelin >> --- >> .../net/virtio/virtio_user/virtio_user_dev.c | 70 +++++++++++++------ >> 1 file changed, 47 insertions(+), 23 deletions(-) >> >> diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c >> b/drivers/net/virtio/virtio_user/virtio_user_dev.c >> index a1e32158bb..2998473622 100644 >> --- a/drivers/net/virtio/virtio_user/virtio_user_dev.c >> +++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c >> @@ -283,13 +283,7 @@ virtio_user_dev_init_notify(struct virtio_user_dev *dev) >> int callfd; >> int kickfd; >> >> - for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) { >> - if (i >= dev->max_queue_pairs * 2) { >> - dev->kickfds[i] = -1; >> - dev->callfds[i] = -1; >> - continue; >> - } >> - >> + for (i = 0; i < dev->max_queue_pairs * 2; i++) { >> /* May use invalid flag, but some backend uses kickfd and >> * callfd as criteria to judge if dev is alive. so finally we >> * use real event_fd. >> @@ -297,28 +291,49 @@ virtio_user_dev_init_notify(struct virtio_user_dev *dev) >> callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); >> if (callfd < 0) { >> PMD_DRV_LOG(ERR, "(%s) callfd error, %s", dev->path, >> strerror(errno)); >> - break; >> + goto err; >> } >> kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); >> if (kickfd < 0) { >> close(callfd); >> PMD_DRV_LOG(ERR, "(%s) kickfd error, %s", dev->path, >> strerror(errno)); >> - break; >> + goto err; >> } >> dev->callfds[i] = callfd; >> dev->kickfds[i] = kickfd; >> } >> >> - if (i < VIRTIO_MAX_VIRTQUEUES) { >> - for (j = 0; j < i; ++j) { >> - close(dev->callfds[j]); >> + return 0; >> +err: >> + for (j = 0; j < i; j++) { >> + if (dev->kickfds[j] >= 0) { >> close(dev->kickfds[j]); >> + dev->kickfds[j] = -1; >> + } >> + if (dev->callfds[j] >= 0) { >> + close(dev->callfds[j]); >> + dev->callfds[j] = -1; >> } >> - >> - return -1; >> } >> >> - return 0; >> + return -1; >> +} >> + >> +static void >> +virtio_user_dev_uninit_notify(struct virtio_user_dev *dev) >> +{ >> + uint32_t i; >> + >> + for (i = 0; i < dev->max_queue_pairs; ++i) { > > Should be 'dev->max_queue_pairs * 2'? Oops, good catch! > I believe you will fix this in the final version before applying them > in the tree. > > So with above fixed: > > Reviewed-by: Chenbo Xia Thanks, I will do the change while applying. Thanks, Maxime >> + if (dev->kickfds[i] >= 0) { >> + close(dev->kickfds[i]); >> + dev->kickfds[i] = -1; >> + } >> + if (dev->callfds[i] >= 0) { >> + close(dev->callfds[i]); >> + dev->callfds[i] = -1; >> + } >> + } >> } >> >> static int >> @@ -427,15 +442,22 @@ virtio_user_dev_setup(struct virtio_user_dev *dev) >> >> if (virtio_user_dev_init_notify(dev) < 0) { >> PMD_INIT_LOG(ERR, "(%s) Failed to init notifiers\n", dev->path); >> - return -1; >> + goto destroy; >> } >> >> if (virtio_user_fill_intr_handle(dev) < 0) { >> PMD_INIT_LOG(ERR, "(%s) Failed to init interrupt handler\n", dev- >>> path); >> - return -1; >> + goto uninit; >> } >> >> return 0; >> + >> +uninit: >> + virtio_user_dev_uninit_notify(dev); >> +destroy: >> + dev->ops->destroy(dev); >> + >> + return -1; >> } >> >> /* Use below macro to filter features from vhost backend */ >> @@ -466,9 +488,16 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char >> *path, int queues, >> enum virtio_user_backend_type backend_type) >> { >> uint64_t backend_features; >> + int i; >> >> pthread_mutex_init(&dev->mutex, NULL); >> strlcpy(dev->path, path, PATH_MAX); >> + >> + for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; i++) { >> + dev->kickfds[i] = -1; >> + dev->callfds[i] = -1; >> + } >> + >> dev->started = 0; >> dev->max_queue_pairs = queues; >> dev->queue_pairs = 1; /* mq disabled by default */ >> @@ -565,16 +594,11 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char >> *path, int queues, >> void >> virtio_user_dev_uninit(struct virtio_user_dev *dev) >> { >> - uint32_t i; >> - >> virtio_user_stop_device(dev); >> >> rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev); >> >> - for (i = 0; i < dev->max_queue_pairs * 2; ++i) { >> - close(dev->callfds[i]); >> - close(dev->kickfds[i]); >> - } >> + virtio_user_dev_uninit_notify(dev); >> >> free(dev->ifname); >> >> -- >> 2.29.2 >