From mboxrd@z Thu Jan 1 00:00:00 1970 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6128DC76196 for ; Tue, 28 Mar 2023 21:34:04 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C945310E11B; Tue, 28 Mar 2023 21:34:03 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id D752910E10A for ; Tue, 28 Mar 2023 21:34:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680039240; 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=uqOJKbgb7ATLpuIGdDhmD1/D9xzym09yCeZarta+PK8=; b=dRMEMLlr5x4eb2g4ilXnuXv37GW2QajuO7XLyA1wJuPjHoqeyaw5kxi1ffH8QGo7irk4ey oOVexdTTSKiOUjq6hCWSPlv+LIHOKm+XMB0ZWd6diWQ4nr1IaeFjalNERhxAqoBSJ1V6sb fVef4sEbxYvJEZncxwO5lk5lNhhNqMc= Received: from mail-io1-f71.google.com (mail-io1-f71.google.com [209.85.166.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-581-ylWXxBQPOoCUiS5nayGdVQ-1; Tue, 28 Mar 2023 17:33:56 -0400 X-MC-Unique: ylWXxBQPOoCUiS5nayGdVQ-1 Received: by mail-io1-f71.google.com with SMTP id 187-20020a6b15c4000000b007590817bcfbso8627085iov.12 for ; Tue, 28 Mar 2023 14:33:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680039236; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uqOJKbgb7ATLpuIGdDhmD1/D9xzym09yCeZarta+PK8=; b=AzozeGbd0fPzV+y1gjgTqqowUmYEro33FTOGtD8hkhrACZVcKvonkvoL5rZtGXWOlV hSYhDPp83R1inRF3hhojWeQ70EEfBk+Una/IuJ/1L1EWP+p8A1UPMtp00Wm7oZrYdVip CXjBXiiZlDahyeJaX2siVZPY29J9N6ZB0XLm/VIPxXYMWxgPHb71+etMWzFwhq5ddLlZ Wh0SvOwj4cE4RKUBiAX78eDT5E4ORsRNiGTpR/AExJ1JQ/A1YzpxevYOOSfMGF/ksd7g VUxOMoSn8b+PGfcdoUvXoOneItEhuuXdzxFRoUfcGEnmAVlKexS3Ji1bvIe3RvBwRLi7 fDpA== X-Gm-Message-State: AAQBX9d758QhTSUVba5ran322dorBccNOMWK1MQAk8hpA3ygrX3MtVsO sRgja2cW7/eBqXNMwdc3W815Hp12z3nyzZKqurQ+eLMhBxHHIXaDiho061cxDb+pv2nMXrXFIwr igyl6Tsn/w73maCK26N6xl2RvMZF3 X-Received: by 2002:a05:6e02:782:b0:315:6e7f:f429 with SMTP id q2-20020a056e02078200b003156e7ff429mr12502990ils.9.1680039235804; Tue, 28 Mar 2023 14:33:55 -0700 (PDT) X-Google-Smtp-Source: AKy350bFTQnx3N9HNY32zRuYKOSoKbrxT758XDlpNXJefLuxy6Ef6baspB62jLxo+aOJR0orePzHQQ== X-Received: by 2002:a05:6e02:782:b0:315:6e7f:f429 with SMTP id q2-20020a056e02078200b003156e7ff429mr12502984ils.9.1680039235506; Tue, 28 Mar 2023 14:33:55 -0700 (PDT) Received: from redhat.com ([38.15.36.239]) by smtp.gmail.com with ESMTPSA id h22-20020a056638339600b00374bf3b62a0sm9785701jav.99.2023.03.28.14.33.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 14:33:54 -0700 (PDT) Date: Tue, 28 Mar 2023 15:33:52 -0600 From: Alex Williamson To: Yi Liu Message-ID: <20230328153352.6c1e2088.alex.williamson@redhat.com> In-Reply-To: <20230327094047.47215-9-yi.l.liu@intel.com> References: <20230327094047.47215-1-yi.l.liu@intel.com> <20230327094047.47215-9-yi.l.liu@intel.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.35; x86_64-redhat-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Subject: Re: [Intel-gfx] [PATCH v8 08/24] vfio: Block device access via device fd until device is opened X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mjrosato@linux.ibm.com, jasowang@redhat.com, xudong.hao@intel.com, peterx@redhat.com, terrence.xu@intel.com, chao.p.peng@linux.intel.com, linux-s390@vger.kernel.org, kvm@vger.kernel.org, lulu@redhat.com, yanting.jiang@intel.com, joro@8bytes.org, nicolinc@nvidia.com, jgg@nvidia.com, yan.y.zhao@intel.com, intel-gfx@lists.freedesktop.org, eric.auger@redhat.com, intel-gvt-dev@lists.freedesktop.org, yi.y.sun@linux.intel.com, cohuck@redhat.com, shameerali.kolothum.thodi@huawei.com, suravee.suthikulpanit@amd.com, robin.murphy@arm.com Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" On Mon, 27 Mar 2023 02:40:31 -0700 Yi Liu wrote: > Allow the vfio_device file to be in a state where the device FD is > opened but the device cannot be used by userspace (i.e. its .open_device() > hasn't been called). This inbetween state is not used when the device > FD is spawned from the group FD, however when we create the device FD > directly by opening a cdev it will be opened in the blocked state. > > The reason for the inbetween state is that userspace only gets a FD but > doesn't gain access permission until binding the FD to an iommufd. So in > the blocked state, only the bind operation is allowed. Completing bind > will allow user to further access the device. > > This is implemented by adding a flag in struct vfio_device_file to mark > the blocked state and using a simple smp_load_acquire() to obtain the > flag value and serialize all the device setup with the thread accessing > this device. > > Following this lockless scheme, it can safely handle the device FD > unbound->bound but it cannot handle bound->unbound. To allow this we'd > need to add a lock on all the vfio ioctls which seems costly. So once > device FD is bound, it remains bound until the FD is closed. > > Suggested-by: Jason Gunthorpe > Reviewed-by: Kevin Tian > Reviewed-by: Jason Gunthorpe > Tested-by: Terrence Xu > Tested-by: Nicolin Chen > Tested-by: Matthew Rosato > Signed-off-by: Yi Liu > --- > drivers/vfio/group.c | 11 ++++++++++- > drivers/vfio/vfio.h | 1 + > drivers/vfio/vfio_main.c | 41 ++++++++++++++++++++++++++++++++++------ > 3 files changed, 46 insertions(+), 7 deletions(-) > > diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c > index 9a7b2765eef6..4f267ae7bebc 100644 > --- a/drivers/vfio/group.c > +++ b/drivers/vfio/group.c > @@ -194,9 +194,18 @@ static int vfio_device_group_open(struct vfio_device_file *df) > df->iommufd = device->group->iommufd; > > ret = vfio_device_open(df); > - if (ret) > + if (ret) { > df->iommufd = NULL; > + goto out_put_kvm; > + } > + > + /* > + * Paired with smp_load_acquire() in vfio_device_fops::ioctl/ > + * read/write/mmap > + */ > + smp_store_release(&df->access_granted, true); > > +out_put_kvm: > if (device->open_count == 0) > vfio_device_put_kvm(device); > > diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h > index cffc08f5a6f1..854f2c97cb9a 100644 > --- a/drivers/vfio/vfio.h > +++ b/drivers/vfio/vfio.h > @@ -18,6 +18,7 @@ struct vfio_container; > > struct vfio_device_file { > struct vfio_device *device; > + bool access_granted; > spinlock_t kvm_ref_lock; /* protect kvm field */ > struct kvm *kvm; > struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */ > diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c > index 2ea6cb6d03c7..b515bbda4c74 100644 > --- a/drivers/vfio/vfio_main.c > +++ b/drivers/vfio/vfio_main.c > @@ -1114,6 +1114,10 @@ static long vfio_device_fops_unl_ioctl(struct file *filep, > struct vfio_device *device = df->device; > int ret; > > + /* Paired with smp_store_release() following vfio_device_open() */ > + if (!smp_load_acquire(&df->access_granted)) > + return -EINVAL; > + > ret = vfio_device_pm_runtime_get(device); > if (ret) > return ret; > @@ -1141,6 +1145,10 @@ static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf, > struct vfio_device_file *df = filep->private_data; > struct vfio_device *device = df->device; > > + /* Paired with smp_store_release() following vfio_device_open() */ > + if (!smp_load_acquire(&df->access_granted)) > + return -EINVAL; > + > if (unlikely(!device->ops->read)) > return -EINVAL; > > @@ -1154,6 +1162,10 @@ static ssize_t vfio_device_fops_write(struct file *filep, > struct vfio_device_file *df = filep->private_data; > struct vfio_device *device = df->device; > > + /* Paired with smp_store_release() following vfio_device_open() */ > + if (!smp_load_acquire(&df->access_granted)) > + return -EINVAL; > + > if (unlikely(!device->ops->write)) > return -EINVAL; > > @@ -1165,6 +1177,10 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma) > struct vfio_device_file *df = filep->private_data; > struct vfio_device *device = df->device; > > + /* Paired with smp_store_release() following vfio_device_open() */ > + if (!smp_load_acquire(&df->access_granted)) > + return -EINVAL; > + > if (unlikely(!device->ops->mmap)) > return -EINVAL; > > @@ -1201,6 +1217,24 @@ bool vfio_file_is_valid(struct file *file) > } > EXPORT_SYMBOL_GPL(vfio_file_is_valid); > > +/* > + * Return true if the input file is a vfio device file and has opened > + * the input device. Otherwise, return false. > + */ > +static bool vfio_file_has_device_access(struct file *file, > + struct vfio_device *device) > +{ > + struct vfio_device *vdev = vfio_device_from_file(file); > + struct vfio_device_file *df; > + > + if (!vdev || vdev != device) > + return false; > + > + df = file->private_data; > + > + return READ_ONCE(df->access_granted); Why did we change from smp_load_acquire() to READ_ONCE() here? Thanks, Alex > +} > + > /** > * vfio_file_has_dev - True if the VFIO file is a handle for device > * @file: VFIO file to check > @@ -1211,17 +1245,12 @@ EXPORT_SYMBOL_GPL(vfio_file_is_valid); > bool vfio_file_has_dev(struct file *file, struct vfio_device *device) > { > struct vfio_group *group; > - struct vfio_device *vdev; > > group = vfio_group_from_file(file); > if (group) > return vfio_group_has_dev(group, device); > > - vdev = vfio_device_from_file(file); > - if (vdev) > - return vdev == device; > - > - return false; > + return vfio_file_has_device_access(file, device); > } > EXPORT_SYMBOL_GPL(vfio_file_has_dev); > From mboxrd@z Thu Jan 1 00:00:00 1970 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 EF153C76196 for ; Tue, 28 Mar 2023 21:34:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229878AbjC1Vem (ORCPT ); Tue, 28 Mar 2023 17:34:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229865AbjC1Vel (ORCPT ); Tue, 28 Mar 2023 17:34:41 -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 ESMTPS id E8BFD26BD for ; Tue, 28 Mar 2023 14:33:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680039238; 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=uqOJKbgb7ATLpuIGdDhmD1/D9xzym09yCeZarta+PK8=; b=ArOl7+TrmC1sOk9WjOBfgQ4UqKHPTUYqqvQci2h0kr80tCeW5m+v5X6ybAjvr8xv9sBkqY QkUkofHO2V9/kCkNLuZ2h/z0nb1SSO6URZX07qjQKhNQsSCPNH4c0mhOcG4Yd0VWErGEnR pnUNtTl+WbEBlUFfgePWF9U+LwbvMTQ= Received: from mail-io1-f70.google.com (mail-io1-f70.google.com [209.85.166.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-336-iDIcBKg6NVeaq4GZhuAlgQ-1; Tue, 28 Mar 2023 17:33:56 -0400 X-MC-Unique: iDIcBKg6NVeaq4GZhuAlgQ-1 Received: by mail-io1-f70.google.com with SMTP id y69-20020a6bc848000000b00758ac1b67f8so8361194iof.17 for ; Tue, 28 Mar 2023 14:33:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680039236; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uqOJKbgb7ATLpuIGdDhmD1/D9xzym09yCeZarta+PK8=; b=EnZJHrQMw9QBCVeP8uK+qal3EierSs3EaUofRWbqcV6us9eLNjK/xoP0N1we5NZaBN OJe2EEjWctq9/BWRLWp5jf9ilO6W1YfBqUtkp0ZJzlfFJar+cSAjOXvQxMkDyTCMpvW1 NDtVuVKJZMjsxIaV+ypoxlPXi5p3bOHQUPtz/4ENUC5qEQypyBgTPM2jeiQpnQdbMIFv 3V97O1qgdJpSvAoxnO+L+53R/3fuVLhyfaz+yEICyrIKZphJvE9TMVOUe4KkCgBxFdKD wZwFVDK/QPw1TApi2QecVo3jGzFHNl05DVbnIi2f7T6QpM9jGLf5EiiuF5LHAxfJNyKI VJcw== X-Gm-Message-State: AAQBX9dFCZdRGmB9+285QIzeMJoN2DD+YT9LHfy6angCML8FsaA/dknq 0Ztl02Kvc/12J6Pcd1QXXf9Jz6kfOmTn5xPDV/wp0VXWDxc2QOoK8tQr8JUDJv0Ai+Y2bIVZV5v jNXPS77oITd6D8hv2/QDTsw== X-Received: by 2002:a05:6e02:782:b0:315:6e7f:f429 with SMTP id q2-20020a056e02078200b003156e7ff429mr12503005ils.9.1680039235810; Tue, 28 Mar 2023 14:33:55 -0700 (PDT) X-Google-Smtp-Source: AKy350bFTQnx3N9HNY32zRuYKOSoKbrxT758XDlpNXJefLuxy6Ef6baspB62jLxo+aOJR0orePzHQQ== X-Received: by 2002:a05:6e02:782:b0:315:6e7f:f429 with SMTP id q2-20020a056e02078200b003156e7ff429mr12502984ils.9.1680039235506; Tue, 28 Mar 2023 14:33:55 -0700 (PDT) Received: from redhat.com ([38.15.36.239]) by smtp.gmail.com with ESMTPSA id h22-20020a056638339600b00374bf3b62a0sm9785701jav.99.2023.03.28.14.33.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 14:33:54 -0700 (PDT) Date: Tue, 28 Mar 2023 15:33:52 -0600 From: Alex Williamson To: Yi Liu Cc: jgg@nvidia.com, kevin.tian@intel.com, joro@8bytes.org, robin.murphy@arm.com, cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-s390@vger.kernel.org, xudong.hao@intel.com, yan.y.zhao@intel.com, terrence.xu@intel.com, yanting.jiang@intel.com Subject: Re: [PATCH v8 08/24] vfio: Block device access via device fd until device is opened Message-ID: <20230328153352.6c1e2088.alex.williamson@redhat.com> In-Reply-To: <20230327094047.47215-9-yi.l.liu@intel.com> References: <20230327094047.47215-1-yi.l.liu@intel.com> <20230327094047.47215-9-yi.l.liu@intel.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.35; 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-s390@vger.kernel.org On Mon, 27 Mar 2023 02:40:31 -0700 Yi Liu wrote: > Allow the vfio_device file to be in a state where the device FD is > opened but the device cannot be used by userspace (i.e. its .open_device() > hasn't been called). This inbetween state is not used when the device > FD is spawned from the group FD, however when we create the device FD > directly by opening a cdev it will be opened in the blocked state. > > The reason for the inbetween state is that userspace only gets a FD but > doesn't gain access permission until binding the FD to an iommufd. So in > the blocked state, only the bind operation is allowed. Completing bind > will allow user to further access the device. > > This is implemented by adding a flag in struct vfio_device_file to mark > the blocked state and using a simple smp_load_acquire() to obtain the > flag value and serialize all the device setup with the thread accessing > this device. > > Following this lockless scheme, it can safely handle the device FD > unbound->bound but it cannot handle bound->unbound. To allow this we'd > need to add a lock on all the vfio ioctls which seems costly. So once > device FD is bound, it remains bound until the FD is closed. > > Suggested-by: Jason Gunthorpe > Reviewed-by: Kevin Tian > Reviewed-by: Jason Gunthorpe > Tested-by: Terrence Xu > Tested-by: Nicolin Chen > Tested-by: Matthew Rosato > Signed-off-by: Yi Liu > --- > drivers/vfio/group.c | 11 ++++++++++- > drivers/vfio/vfio.h | 1 + > drivers/vfio/vfio_main.c | 41 ++++++++++++++++++++++++++++++++++------ > 3 files changed, 46 insertions(+), 7 deletions(-) > > diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c > index 9a7b2765eef6..4f267ae7bebc 100644 > --- a/drivers/vfio/group.c > +++ b/drivers/vfio/group.c > @@ -194,9 +194,18 @@ static int vfio_device_group_open(struct vfio_device_file *df) > df->iommufd = device->group->iommufd; > > ret = vfio_device_open(df); > - if (ret) > + if (ret) { > df->iommufd = NULL; > + goto out_put_kvm; > + } > + > + /* > + * Paired with smp_load_acquire() in vfio_device_fops::ioctl/ > + * read/write/mmap > + */ > + smp_store_release(&df->access_granted, true); > > +out_put_kvm: > if (device->open_count == 0) > vfio_device_put_kvm(device); > > diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h > index cffc08f5a6f1..854f2c97cb9a 100644 > --- a/drivers/vfio/vfio.h > +++ b/drivers/vfio/vfio.h > @@ -18,6 +18,7 @@ struct vfio_container; > > struct vfio_device_file { > struct vfio_device *device; > + bool access_granted; > spinlock_t kvm_ref_lock; /* protect kvm field */ > struct kvm *kvm; > struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */ > diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c > index 2ea6cb6d03c7..b515bbda4c74 100644 > --- a/drivers/vfio/vfio_main.c > +++ b/drivers/vfio/vfio_main.c > @@ -1114,6 +1114,10 @@ static long vfio_device_fops_unl_ioctl(struct file *filep, > struct vfio_device *device = df->device; > int ret; > > + /* Paired with smp_store_release() following vfio_device_open() */ > + if (!smp_load_acquire(&df->access_granted)) > + return -EINVAL; > + > ret = vfio_device_pm_runtime_get(device); > if (ret) > return ret; > @@ -1141,6 +1145,10 @@ static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf, > struct vfio_device_file *df = filep->private_data; > struct vfio_device *device = df->device; > > + /* Paired with smp_store_release() following vfio_device_open() */ > + if (!smp_load_acquire(&df->access_granted)) > + return -EINVAL; > + > if (unlikely(!device->ops->read)) > return -EINVAL; > > @@ -1154,6 +1162,10 @@ static ssize_t vfio_device_fops_write(struct file *filep, > struct vfio_device_file *df = filep->private_data; > struct vfio_device *device = df->device; > > + /* Paired with smp_store_release() following vfio_device_open() */ > + if (!smp_load_acquire(&df->access_granted)) > + return -EINVAL; > + > if (unlikely(!device->ops->write)) > return -EINVAL; > > @@ -1165,6 +1177,10 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma) > struct vfio_device_file *df = filep->private_data; > struct vfio_device *device = df->device; > > + /* Paired with smp_store_release() following vfio_device_open() */ > + if (!smp_load_acquire(&df->access_granted)) > + return -EINVAL; > + > if (unlikely(!device->ops->mmap)) > return -EINVAL; > > @@ -1201,6 +1217,24 @@ bool vfio_file_is_valid(struct file *file) > } > EXPORT_SYMBOL_GPL(vfio_file_is_valid); > > +/* > + * Return true if the input file is a vfio device file and has opened > + * the input device. Otherwise, return false. > + */ > +static bool vfio_file_has_device_access(struct file *file, > + struct vfio_device *device) > +{ > + struct vfio_device *vdev = vfio_device_from_file(file); > + struct vfio_device_file *df; > + > + if (!vdev || vdev != device) > + return false; > + > + df = file->private_data; > + > + return READ_ONCE(df->access_granted); Why did we change from smp_load_acquire() to READ_ONCE() here? Thanks, Alex > +} > + > /** > * vfio_file_has_dev - True if the VFIO file is a handle for device > * @file: VFIO file to check > @@ -1211,17 +1245,12 @@ EXPORT_SYMBOL_GPL(vfio_file_is_valid); > bool vfio_file_has_dev(struct file *file, struct vfio_device *device) > { > struct vfio_group *group; > - struct vfio_device *vdev; > > group = vfio_group_from_file(file); > if (group) > return vfio_group_has_dev(group, device); > > - vdev = vfio_device_from_file(file); > - if (vdev) > - return vdev == device; > - > - return false; > + return vfio_file_has_device_access(file, device); > } > EXPORT_SYMBOL_GPL(vfio_file_has_dev); >