Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp927573pxb; Wed, 29 Sep 2021 12:43:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzBh1FicwPeyMbQSVet+wHLP8k0oBWXlGpO/Kr2dUiQTjqe79KoUwWfayoETCHsj7gw53iA X-Received: by 2002:a05:6402:142d:: with SMTP id c13mr2094346edx.390.1632944581443; Wed, 29 Sep 2021 12:43:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632944581; cv=none; d=google.com; s=arc-20160816; b=jd+RyaYSxVNMKzOuCAD0yfV87QCQqLSGUwtEWawK9RHGLBUy6ra/Tw3UL8QdaQqd1h N5QSCgzIQKl+3N0MwSEpxNjfxl7XEi9sf2UEQ9QjTeaqK3V1SzQlqeCav0EBEGnZ8sR/ kSj0TCj3mWjTN+Bj+pzBw8I00Fc1o0pyTuKoxwG+wtPbCq7tU9ueaU2DUDpFH2l9p5E/ 5ZwZY5eWpFFRDpmkq2Mrn9Zyjb3Z3J7tUUOUz6+jBK0QTqvCz9P076v1KwIIEktAYnG7 9AMYbhLIjJxo1CfYA72FDuSnTUec4jhPVyQUfc5MDM2JMWSNDQxp2H2O6GBH8sAbDga8 cdRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=85NOeq9kRcTFRqNT/gP4PrOFWq1cbQJO0LsYgy0Arqs=; b=SoRkIEnSYryJK/fNNXKeJh6SZXma6Rd9Gbn4gg69nuWDNagK/oebaD8nKWEKyF3zbj MEoG+ZenXzqjBid4kHax+AAcdK6rUSA+QSNR3lkw8jLt7XRjheMxvgpkY5l/p++2wLeb +bVjupdx+I4H9BUW97nRzAwMUh0dAiYXrjTpHfxbas4VtExQDgMsC7exfvsBZfwbe6FC 3G63HxoN3GF+pe/5hmSdaiB7ZxpynqAMmPgyxiqI3rHVTMwwnmYBmGzsU2fnZj6l/0UP O/f2D3NlJS1WJ7E5S8vwN22AqmQ0ghqYN9yjAQKOBAtzwUmDUfIvCKpm7x4xMu4qUnKk C1sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Hm2HGl/9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id d5si1108151ejd.235.2021.09.29.12.42.34; Wed, 29 Sep 2021 12:43:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Hm2HGl/9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1344062AbhI2THI (ORCPT + 99 others); Wed, 29 Sep 2021 15:07:08 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:33827 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244887AbhI2THH (ORCPT ); Wed, 29 Sep 2021 15:07:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1632942325; 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=85NOeq9kRcTFRqNT/gP4PrOFWq1cbQJO0LsYgy0Arqs=; b=Hm2HGl/9kWRnGkFEFQbmXmy/Etsd+FpVD7+K6PrAJlqaruG/OVZXCC9H8q+JRY7dJaURKw mm9931Ck5zRMn1GzBNRmbd7seuSLIRiqYgpc6N318zBZyHCo/msTvAdwatHCs1IJ7w1IPC meHKIZyi9hqXWQWt8hYMYaCs53qUVtU= Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-232-lZ0_XGNbPdW1C0K9pS3dgQ-1; Wed, 29 Sep 2021 15:05:24 -0400 X-MC-Unique: lZ0_XGNbPdW1C0K9pS3dgQ-1 Received: by mail-oi1-f199.google.com with SMTP id y5-20020aca3205000000b0027644481fe7so2551705oiy.10 for ; Wed, 29 Sep 2021 12:05:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=85NOeq9kRcTFRqNT/gP4PrOFWq1cbQJO0LsYgy0Arqs=; b=5TvAVwdN/VyHks1Yw3f5yXKKxgwTXObmhFut6BumpYeCFKWydVoKUDF7IxJKZkLwIq C5WWEXARKavYQn6NzYSsIiW/H1TKjS7rkkzjIC1MscPPkS9dy+aUSrShD41JZ6q29UEb sT7USkJ6XKIQTEHHppde6maFU6xOuL7VtUclIeq7CIzrwDY+I2UuMCDyc47UKxivCsVY PsEtMWMsnc6bjuzO7QhNDGpz/XYw6/4mv8tNzE7Ns28+PSx8eVjobIvvt//dF/jAJBNg pLRiMy9rGe+FMQmkxwt9+fGEaSaIS0RxwKimHwHem4IXNsMDgeGzFQbcda9hSL7gDafH 7RQQ== X-Gm-Message-State: AOAM532kDrX8HysBD2PEVBdauCaIavrAwtNc+YKbdmvbWXrA7bObTlcA 9bQQITc/8NH6LZJQ2Vmd/CzuTJRzw9WYoR1fDgQHcy2p328lc0pDapouUOpEIyjYLHC9iMJq8e6 d4OywFGsITXZUoNi4zPdV7u0s X-Received: by 2002:a9d:4705:: with SMTP id a5mr1401103otf.237.1632942323858; Wed, 29 Sep 2021 12:05:23 -0700 (PDT) X-Received: by 2002:a9d:4705:: with SMTP id a5mr1401070otf.237.1632942323621; Wed, 29 Sep 2021 12:05:23 -0700 (PDT) Received: from redhat.com ([198.99.80.109]) by smtp.gmail.com with ESMTPSA id p9sm111523ots.66.2021.09.29.12.05.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 12:05:23 -0700 (PDT) Date: Wed, 29 Sep 2021 13:05:21 -0600 From: Alex Williamson To: David Gibson Cc: Liu Yi L , jgg@nvidia.com, hch@lst.de, jasowang@redhat.com, joro@8bytes.org, jean-philippe@linaro.org, kevin.tian@intel.com, parav@mellanox.com, lkml@metux.net, pbonzini@redhat.com, lushenming@huawei.com, eric.auger@redhat.com, corbet@lwn.net, ashok.raj@intel.com, yi.l.liu@linux.intel.com, jun.j.tian@intel.com, hao.wu@intel.com, dave.jiang@intel.com, jacob.jun.pan@linux.intel.com, kwankhede@nvidia.com, robin.murphy@arm.com, kvm@vger.kernel.org, iommu@lists.linux-foundation.org, dwmw2@infradead.org, linux-kernel@vger.kernel.org, baolu.lu@linux.intel.com, nicolinc@nvidia.com Subject: Re: [RFC 02/20] vfio: Add device class for /dev/vfio/devices Message-ID: <20210929130521.738c56ed.alex.williamson@redhat.com> In-Reply-To: References: <20210919063848.1476776-1-yi.l.liu@intel.com> <20210919063848.1476776-3-yi.l.liu@intel.com> X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 29 Sep 2021 12:08:59 +1000 David Gibson wrote: > On Sun, Sep 19, 2021 at 02:38:30PM +0800, Liu Yi L wrote: > > This patch introduces a new interface (/dev/vfio/devices/$DEVICE) for > > userspace to directly open a vfio device w/o relying on container/group > > (/dev/vfio/$GROUP). Anything related to group is now hidden behind > > iommufd (more specifically in iommu core by this RFC) in a device-centric > > manner. > > > > In case a device is exposed in both legacy and new interfaces (see next > > patch for how to decide it), this patch also ensures that when the device > > is already opened via one interface then the other one must be blocked. > > > > Signed-off-by: Liu Yi L > [snip] > > > +static bool vfio_device_in_container(struct vfio_device *device) > > +{ > > + return !!(device->group && device->group->container); > > You don't need !! here. && is already a logical operation, so returns > a valid bool. > > > +} > > + > > static int vfio_device_fops_release(struct inode *inode, struct file *filep) > > { > > struct vfio_device *device = filep->private_data; > > @@ -1560,7 +1691,16 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) > > > > module_put(device->dev->driver->owner); > > > > - vfio_group_try_dissolve_container(device->group); > > + if (vfio_device_in_container(device)) { > > + vfio_group_try_dissolve_container(device->group); > > + } else { > > + atomic_dec(&device->opened); > > + if (device->group) { > > + mutex_lock(&device->group->opened_lock); > > + device->group->opened--; > > + mutex_unlock(&device->group->opened_lock); > > + } > > + } > > > > vfio_device_put(device); > > > > @@ -1613,6 +1753,7 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma) > > > > static const struct file_operations vfio_device_fops = { > > .owner = THIS_MODULE, > > + .open = vfio_device_fops_open, > > .release = vfio_device_fops_release, > > .read = vfio_device_fops_read, > > .write = vfio_device_fops_write, > > @@ -2295,6 +2436,52 @@ static struct miscdevice vfio_dev = { > > .mode = S_IRUGO | S_IWUGO, > > }; > > > > +static char *vfio_device_devnode(struct device *dev, umode_t *mode) > > +{ > > + return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev)); > > Others have pointed out some problems with the use of dev_name() > here. I'll add that I think you'll make things much easier if instead > of using one huge "devices" subdir, you use a separate subdir for each > vfio sub-driver (so, one for PCI, one for each type of mdev, one for > platform, etc.). That should make avoiding name conflicts a lot simpler. It seems like this is unnecessary if we use the vfioX naming approach. Conflicts are trivial to ignore if we don't involve dev_name() and looking for the correct major:minor chardev in the correct subdirectory seems like a hassle for userspace. Thanks, Alex