Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp5812526ybv; Wed, 12 Feb 2020 00:27:58 -0800 (PST) X-Google-Smtp-Source: APXvYqxP4AKNT5v9A3OzZgHE7PJKRVv2LaL56rbC6FNF7c51VWBiRvEgauSc87E3nKeHq4CoDmDP X-Received: by 2002:aca:ea43:: with SMTP id i64mr5625923oih.30.1581496078670; Wed, 12 Feb 2020 00:27:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581496078; cv=none; d=google.com; s=arc-20160816; b=X9UESVJlCn4OsMVC/zbbNq2UG+2+9IkBTPww1+WOEa1xU+N4z2IAX3M8GFSvHq6cnm PtMp6v9llwgVyHm6ELAiJ1mebC3F6vaFlkw0wncvWNLRwxdJzEa+8RncDng7YG88J5jS BOOponzPoGM5cesu64vxqMfbM/zem5q+831rygz+sFGl6GkQcEJ5PWeOJEjdRjuG2PlW gyZ0ZNlS2YjnLdZJqm6PvDYYyC1VpOji90zBifNW1p7al9LyfKfe4VeKdy+XZ00Zld4M i+WnO2qkoscWPaWg2dlBwtF9g0XumKvmW/YLQVXLJ7OAvBc06iylY7PEsKCXnHCuapP8 4+uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=0qA1nDcFUvrV3P04ERfz3LJJdXRR+wWSmOw4ogMcuSU=; b=reXiv6OCU7M8vlZFB70gTi0CJ2jTgE+fXy1gmtFWqJQ+4wkY5KGmzuP30q3u9OZWBj Y5SkQC92+V1ZrCOmcI5n16i0drcwTlO8kBYLOBsYYeWC4cuoK5gXWnN4bEGJxD9kXQu0 jkFTSLc7hor6fHkyuk5nfarIMNb5KKq2qdsvLrIgNBhBZ7k943i5pL9+cEV2sNwGWumO eaQNjKCkAqPCKi9PEtPySc5qPFUk6sl2fF5MasvCaRBU7yqYGO3Idk2PucQp6n6/Moqe 7tsm/zn+3waEqEzxcayRnbZU80rBWGSHad8MpXMuCuMGYjc2j2P53HPCiEiiAuTF8ZIv VcFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OcUyGKkA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l204si3118812oig.31.2020.02.12.00.27.45; Wed, 12 Feb 2020 00:27:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OcUyGKkA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728464AbgBLI1j (ORCPT + 99 others); Wed, 12 Feb 2020 03:27:39 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:56931 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728192AbgBLI1j (ORCPT ); Wed, 12 Feb 2020 03:27:39 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581496058; 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=0qA1nDcFUvrV3P04ERfz3LJJdXRR+wWSmOw4ogMcuSU=; b=OcUyGKkAkz/wplYYA7cgAVr4h1cgVSf2J5eYVNAqMqmtYlF6I3UMCEWk1pM4uHuO5+Sk2Y GgIUDL+/9lXvSJDA+5rA+A6mASe2Bgc3fsb350t69wehaAbnOWLiCrJOM/APPmhPE551oE Nl7XhQF0PZyRs1LPtjUIGRUz412X5/I= 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-212-v6ZEW2x5PFuPkYDjaZSrCg-1; Wed, 12 Feb 2020 03:27:36 -0500 X-MC-Unique: v6ZEW2x5PFuPkYDjaZSrCg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E058B1857340; Wed, 12 Feb 2020 08:27:33 +0000 (UTC) Received: from [10.72.13.111] (ovpn-13-111.pek2.redhat.com [10.72.13.111]) by smtp.corp.redhat.com (Postfix) with ESMTP id 03FA85DA83; Wed, 12 Feb 2020 08:27:17 +0000 (UTC) Subject: Re: [PATCH V2 5/5] vdpasim: vDPA device simulator To: Jason Gunthorpe Cc: mst@redhat.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, tiwei.bie@intel.com, maxime.coquelin@redhat.com, cunming.liang@intel.com, zhihong.wang@intel.com, rob.miller@broadcom.com, xiao.w.wang@intel.com, haotian.wang@sifive.com, lingshan.zhu@intel.com, eperezma@redhat.com, lulu@redhat.com, parav@mellanox.com, kevin.tian@intel.com, stefanha@redhat.com, rdunlap@infradead.org, hch@infradead.org, aadam@redhat.com, jiri@mellanox.com, shahafs@mellanox.com, hanand@xilinx.com, mhabets@solarflare.com References: <20200210035608.10002-1-jasowang@redhat.com> <20200210035608.10002-6-jasowang@redhat.com> <20200211135254.GJ4271@mellanox.com> From: Jason Wang Message-ID: <3dbadf4e-c4c5-22cc-f970-f25fa42c13d8@redhat.com> Date: Wed, 12 Feb 2020 16:27:16 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20200211135254.GJ4271@mellanox.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020/2/11 =E4=B8=8B=E5=8D=889:52, Jason Gunthorpe wrote: > On Mon, Feb 10, 2020 at 11:56:08AM +0800, Jason Wang wrote: >> + >> +static struct vdpasim *vdpasim_create(void) >> +{ >> + struct vdpasim *vdpasim; >> + struct virtio_net_config *config; >> + struct vdpa_device *vdpa; >> + struct device *dev; >> + int ret =3D -ENOMEM; >> + >> + vdpasim =3D kzalloc(sizeof(*vdpasim), GFP_KERNEL); >> + if (!vdpasim) >> + goto err_vdpa_alloc; >> + >> + vdpasim->buffer =3D kmalloc(PAGE_SIZE, GFP_KERNEL); >> + if (!vdpasim->buffer) >> + goto err_buffer_alloc; >> + >> + vdpasim->iommu =3D vhost_iotlb_alloc(2048, 0); >> + if (!vdpasim->iommu) >> + goto err_iotlb; >> + >> + config =3D &vdpasim->config; >> + config->mtu =3D 1500; >> + config->status =3D VIRTIO_NET_S_LINK_UP; >> + eth_random_addr(config->mac); >> + >> + INIT_WORK(&vdpasim->work, vdpasim_work); >> + spin_lock_init(&vdpasim->lock); >> + >> + vdpa =3D &vdpasim->vdpa; >> + vdpa->dev.release =3D vdpasim_release_dev; > The driver should not provide the release function. > > Again the safest model is 'vdpa_alloc_device' which combines the > kzalloc and the vdpa_init_device() and returns something that is > error unwound with put_device() > > The subsystem owns the release and does the kfree and other cleanup > like releasing the IDA. So I think if we agree bus instead of class is used. vDPA bus can=20 provide a release function in vdpa_alloc_device()? > >> + vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu); >> + vringh_set_iotlb(&vdpasim->vqs[1].vring, vdpasim->iommu); >> + >> + dev =3D &vdpa->dev; >> + dev->coherent_dma_mask =3D DMA_BIT_MASK(64); >> + set_dma_ops(dev, &vdpasim_dma_ops); >> + >> + ret =3D vdpa_init_device(vdpa, &vdpasim_dev->dev, dev, >> + &vdpasim_net_config_ops); >> + if (ret) >> + goto err_init; >> + >> + ret =3D vdpa_register_device(vdpa); >> + if (ret) >> + goto err_register; > See? This error unwind is now all wrong: > >> + >> + return vdpasim; >> + >> +err_register: >> + put_device(&vdpa->dev); > Double put_device Yes. > >> +err_init: >> + vhost_iotlb_free(vdpasim->iommu); >> +err_iotlb: >> + kfree(vdpasim->buffer); >> +err_buffer_alloc: >> + kfree(vdpasim); > kfree after vdpa_init_device() is incorrect, as the put_device now > does kfree via release Ok, will fix. > >> +static int __init vdpasim_dev_init(void) >> +{ >> + struct device *dev; >> + int ret =3D 0; >> + >> + vdpasim_dev =3D kzalloc(sizeof(*vdpasim_dev), GFP_KERNEL); >> + if (!vdpasim_dev) >> + return -ENOMEM; >> + >> + dev =3D &vdpasim_dev->dev; >> + dev->release =3D vdpasim_device_release; >> + dev_set_name(dev, "%s", VDPASIM_NAME); >> + >> + ret =3D device_register(&vdpasim_dev->dev); >> + if (ret) >> + goto err_register; >> + >> + if (!vdpasim_create()) >> + goto err_register; > Wrong error unwind here too Will fix. Thanks > >> + return 0; >> + >> +err_register: >> + kfree(vdpasim_dev); >> + vdpasim_dev =3D NULL; >> + return ret; >> +} > Jason >