From patchwork Wed Jul 17 21:33:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13735813 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4971187325 for ; Wed, 17 Jul 2024 21:33:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721252029; cv=none; b=SRBRWi31W1JzPUHzWpMynTMsJ2jyTpC2LiY/JOSHkNdEmgSw0Lixs1azCcLdLE3f3qkNq4l2d8F1tPbruC9BlB8t3l7fnTDO4TaVVTq6wCLkLte1KczYthDwP4qLxiK9xr4sSklYqjXLn+lmWc6tregq6pl0VCIEg+tTKGEJDKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721252029; c=relaxed/simple; bh=w58AnK/9cYSCW/0hjde3wNFPdaQA6i/Jsz5cMviAgzI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WzW+4R7il/MW1Jk9J1FyFjwPGjK7AyJmss4/M25lPxnYZ/RUfaRHbXv7fXw0LXcKYpaiyoznAq3T0zuxfMbfqqijVdvbL9zU2NU5UuhwKZ/G4d6/dbj7FPb5/zs5/bGg+NZrzzBh0NLOOmpXo3L9t/GrUJAltzd6EesxNMhy61Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--axelrasmussen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DRvrmgrk; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--axelrasmussen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DRvrmgrk" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-64a5503e253so3064717b3.0 for ; Wed, 17 Jul 2024 14:33:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721252026; x=1721856826; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zFqYg+YWN8lw8m2oONz34aZr3QiDMRuKYi+jOTj3Buo=; b=DRvrmgrkuU5VYNQVRbyN5Gb1OlDqrMhzaEelelm5PKy4gze9dkdTPl9M1uYTNNtHp7 xvxu3d6eIDBgmoatlw1dpAaIK7VcHbx3Ya5h5ciiM0EHP1bg0BFDLW4uEVl0RLU5NeTW gvYKfI9myeK9Rewfqc72DRjW60FefLA0Tpgnf/V/Albp9gizM9ZQ7zE5SojyIb8M/3XQ G3SitEmXzu3tXVb8tTUgfcC76kkLH4oGswJnXMfqXVvf235/fKECZKvsRTX682/raFIk VbzAzpePrf8R4itZuZ5USwRH+GMKpGSTwnfvn+ZGSY8CA4RPzMwiml+Wl8gIsdmOlL3H yr9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721252026; x=1721856826; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zFqYg+YWN8lw8m2oONz34aZr3QiDMRuKYi+jOTj3Buo=; b=a6BnY6CgIkUvltsWBdXfaYvxroO+ypaw5eyHIHJ4p+UnDRhWTeSyfr3a3X3k7E826p ZzTXRAbODf9jB62r7qQpTJbTATOT/l1Z+fPwFpLZeGDmVoEqXNm+7d5++HqeVbTHGmO8 QC1H42n+7y3SDGu+uWqqVYb66qQSHEdRVvvQwG9cgYay5hljbTnbFrVWb3Kmnoza1aXe FbrMpHpIyC95xVltuX49UPofMnQ3HoBHzEIC5DUvTS/fSz8GDqxhFn0zCHBkpqnU+d7z NyTEeZibtSrD+IhBgBAEn075z8wwAkM7VoF2QmUmNxno6QqD/8wx3djYsmfgBtQPHG50 8gWQ== X-Forwarded-Encrypted: i=1; AJvYcCX/fowm7oG0hPt7uTD96Hm9O7dQNuEtSb99iweCY9+aVUQpiLHGk2v5mvgXj2863RHA3azDGp1Mmxu4nA5K7iTzlvkZ X-Gm-Message-State: AOJu0Yz4yLlZHQ5KLIHY9Pqas+x/jiD9AqHetj8Z2vAiW5tdH2uLFCsP dMB6Lki1nUyfgoeogIwSRr7GWUeHdMSy2JJmNPR3+cML8ucCaar0mlaxdylkKW2k9UeOlejPbar ApWjY4kvPRu/DajZvNDwGVe1SfVQYTw== X-Google-Smtp-Source: AGHT+IEvWAa/WVkj4a7IeqojFpVPagMimkl7uCbjdcqjK6SqV8Cb7d3Zi1aZGbLEHZostrBZpYlyRysM4WfF8ysd/RDf X-Received: from axel.svl.corp.google.com ([2620:15c:2a3:200:a503:d697:557b:840c]) (user=axelrasmussen job=sendgmr) by 2002:a05:690c:6605:b0:62c:f01d:3470 with SMTP id 00721157ae682-66604d73884mr354997b3.6.1721252025952; Wed, 17 Jul 2024 14:33:45 -0700 (PDT) Date: Wed, 17 Jul 2024 14:33:37 -0700 In-Reply-To: <20240717213339.1921530-1-axelrasmussen@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240717213339.1921530-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.45.2.993.g49e7a77208-goog Message-ID: <20240717213339.1921530-2-axelrasmussen@google.com> Subject: [PATCH 6.9 1/3] vfio: Create vfio_fs_type with inode per device From: Axel Rasmussen To: stable@vger.kernel.org Cc: Alex Williamson , Ankit Agrawal , Eric Auger , Jason Gunthorpe , Kevin Tian , Kunwu Chan , Leah Rumancik , Miaohe Lin , Stefan Hajnoczi , Yi Liu , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jason Gunthorpe , Axel Rasmussen From: Alex Williamson commit b7c5e64fecfa88764791679cca4786ac65de739e upstream. By linking all the device fds we provide to userspace to an address space through a new pseudo fs, we can use tools like unmap_mapping_range() to zap all vmas associated with a device. Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Link: https://lore.kernel.org/r/20240530045236.1005864-2-alex.williamson@redhat.com Signed-off-by: Alex Williamson Signed-off-by: Axel Rasmussen --- drivers/vfio/device_cdev.c | 7 ++++++ drivers/vfio/group.c | 7 ++++++ drivers/vfio/vfio_main.c | 44 ++++++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 1 + 4 files changed, 59 insertions(+) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index e75da0a70d1f..bb1817bd4ff3 100644 --- a/drivers/vfio/device_cdev.c +++ b/drivers/vfio/device_cdev.c @@ -39,6 +39,13 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep) filep->private_data = df; + /* + * Use the pseudo fs inode on the device to link all mmaps + * to the same address space, allowing us to unmap all vmas + * associated to this device using unmap_mapping_range(). + */ + filep->f_mapping = device->inode->i_mapping; + return 0; err_put_registration: diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index 610a429c6191..ded364588d29 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -286,6 +286,13 @@ static struct file *vfio_device_open_file(struct vfio_device *device) */ filep->f_mode |= (FMODE_PREAD | FMODE_PWRITE); + /* + * Use the pseudo fs inode on the device to link all mmaps + * to the same address space, allowing us to unmap all vmas + * associated to this device using unmap_mapping_range(). + */ + filep->f_mapping = device->inode->i_mapping; + if (device->group->type == VFIO_NO_IOMMU) dev_warn(device->dev, "vfio-noiommu device opened by user " "(%s:%d)\n", current->comm, task_pid_nr(current)); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index e97d796a54fb..a5a62d9d963f 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -22,8 +22,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -43,9 +45,13 @@ #define DRIVER_AUTHOR "Alex Williamson " #define DRIVER_DESC "VFIO - User Level meta-driver" +#define VFIO_MAGIC 0x5646494f /* "VFIO" */ + static struct vfio { struct class *device_class; struct ida device_ida; + struct vfsmount *vfs_mount; + int fs_count; } vfio; #ifdef CONFIG_VFIO_NOIOMMU @@ -186,6 +192,8 @@ static void vfio_device_release(struct device *dev) if (device->ops->release) device->ops->release(device); + iput(device->inode); + simple_release_fs(&vfio.vfs_mount, &vfio.fs_count); kvfree(device); } @@ -228,6 +236,34 @@ struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev, } EXPORT_SYMBOL_GPL(_vfio_alloc_device); +static int vfio_fs_init_fs_context(struct fs_context *fc) +{ + return init_pseudo(fc, VFIO_MAGIC) ? 0 : -ENOMEM; +} + +static struct file_system_type vfio_fs_type = { + .name = "vfio", + .owner = THIS_MODULE, + .init_fs_context = vfio_fs_init_fs_context, + .kill_sb = kill_anon_super, +}; + +static struct inode *vfio_fs_inode_new(void) +{ + struct inode *inode; + int ret; + + ret = simple_pin_fs(&vfio_fs_type, &vfio.vfs_mount, &vfio.fs_count); + if (ret) + return ERR_PTR(ret); + + inode = alloc_anon_inode(vfio.vfs_mount->mnt_sb); + if (IS_ERR(inode)) + simple_release_fs(&vfio.vfs_mount, &vfio.fs_count); + + return inode; +} + /* * Initialize a vfio_device so it can be registered to vfio core. */ @@ -246,6 +282,11 @@ static int vfio_init_device(struct vfio_device *device, struct device *dev, init_completion(&device->comp); device->dev = dev; device->ops = ops; + device->inode = vfio_fs_inode_new(); + if (IS_ERR(device->inode)) { + ret = PTR_ERR(device->inode); + goto out_inode; + } if (ops->init) { ret = ops->init(device); @@ -260,6 +301,9 @@ static int vfio_init_device(struct vfio_device *device, struct device *dev, return 0; out_uninit: + iput(device->inode); + simple_release_fs(&vfio.vfs_mount, &vfio.fs_count); +out_inode: vfio_release_device_set(device); ida_free(&vfio.device_ida, device->index); return ret; diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 8b1a29820409..000a6cab2d31 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -64,6 +64,7 @@ struct vfio_device { struct completion comp; struct iommufd_access *iommufd_access; void (*put_kvm)(struct kvm *kvm); + struct inode *inode; #if IS_ENABLED(CONFIG_IOMMUFD) struct iommufd_device *iommufd_device; u8 iommufd_attached:1;