From nobody Thu Oct 2 01:01:44 2025 Received: from fra-out-008.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-008.esa.eu-central-1.outbound.mail-perimeter.amazon.com [35.158.23.94]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4FE5305068; Wed, 24 Sep 2025 14:11:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=35.158.23.94 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723072; cv=none; b=o/RriMJMo6rDTOHizx/Nvblwl+qL3mmF0GNnFfPSMX0By0wQ1ZcRO6PBlfLiBdJEQax8MeuYyyTKbEmKsgOQ8Iz45RfSnZexUOYQoa3O4dMbnlhskhJnmrGEJxVutsz7YfoHJ6UM2OGss3dQhSZJ2yryanDo+N/uifkDcFlHUoU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723072; c=relaxed/simple; bh=gAuRNltqJzgZyWEIHLg7Fofa8CPfuqAb71v/VsiS5HA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Zc5pcnAA4pcy2T0/qJGYjHHr3JJ4VpWl0I0IzjlExZxgfwsedRMpWiqfus5Z4B+iSqwyDjFW8BgaHjc1R/zk8fqYEkFp5rkS0kocuF/gXYnNlUw9iJoHUZr4QPr3uYO0CI0F3BJnv4xstWOKZ2jn7lyHqcEYRCZbUYcDgfA++ZI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de; spf=pass smtp.mailfrom=amazon.de; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b=NeyjzZQf; arc=none smtp.client-ip=35.158.23.94 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b="NeyjzZQf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazoncorp2; t=1758723070; x=1790259070; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vlSsIuF9Wb2Tm0znC1D8CWXPAsIXTh2j5+LYsrwoARM=; b=NeyjzZQfBF4kyF/qXp4EW/CAmAtTM9MIw4OENqhEJyBbVu+my4tkspFh mwd2Ln35SfuKYvq4frHaALoPkTE2SUr6e/JBnG095VSRQMH64TebuOEiC ZZHbCu9Zwj40hH5Lf+t5HrToso/lzTvEZFxlymqc1nak7E1duZBH6Ymro OI/H0d7e1UFr4XjxiTo922MEQKmCOKngugkHFdPViVjlBS5nIXNv7lSl9 kq1u1WI0GW3tbqIzFV1B69LSqqoQFXNRvTDYz12+Zt03vDbM8Pp2c0X/4 Ywl6O7N/iTsjQAmJliYSEXat4PPjFEOQkpY7jajAqIUODIPy0HyXvHb98 g==; X-CSE-ConnectionGUID: szoMElyQSp2nxSwMGktZ/A== X-CSE-MsgGUID: fG7VMdPLSQCL3XgcMEZCNA== X-IronPort-AV: E=Sophos;i="6.18,290,1751241600"; d="scan'208";a="2614478" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-008.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Sep 2025 14:11:08 +0000 Received: from EX19MTAEUC002.ant.amazon.com [54.240.197.228:4694] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.23.230:2525] with esmtp (Farcaster) id 8cab9ff5-ed74-4e0c-9e31-d48fcbaebf4d; Wed, 24 Sep 2025 14:11:07 +0000 (UTC) X-Farcaster-Flow-ID: 8cab9ff5-ed74-4e0c-9e31-d48fcbaebf4d Received: from EX19D039EUC004.ant.amazon.com (10.252.61.190) by EX19MTAEUC002.ant.amazon.com (10.252.51.245) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:11:07 +0000 Received: from dev-dsk-mngyadam-1c-cb3f7548.eu-west-1.amazon.com (10.253.107.175) by EX19D039EUC004.ant.amazon.com (10.252.61.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:11:04 +0000 From: Mahmoud Adam To: CC: , , , , David Woodhouse , , , Subject: [RFC PATCH 1/7] vfio/pci: refactor region dereferences for RCU. Date: Wed, 24 Sep 2025 16:09:52 +0200 Message-ID: <20250924141018.80202-2-mngyadam@amazon.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250924141018.80202-1-mngyadam@amazon.de> References: <20250924141018.80202-1-mngyadam@amazon.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D040UWA004.ant.amazon.com (10.13.139.93) To EX19D039EUC004.ant.amazon.com (10.252.61.190) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" No functional changes. These refactors multiple region array accessing into one place. This prepares for the RCU locking in the following patches. Signed-off-by: Mahmoud Adam --- drivers/vfio/pci/vfio_pci_core.c | 21 ++++++++++++--------- drivers/vfio/pci/vfio_pci_igd.c | 20 ++++++++++++++------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index 7dcf5439dedc9..ea04c1291af68 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1003,6 +1003,7 @@ static int vfio_pci_ioctl_get_region_info(struct vfio= _pci_core_device *vdev, struct pci_dev *pdev =3D vdev->pdev; struct vfio_region_info info; struct vfio_info_cap caps =3D { .buf =3D NULL, .size =3D 0 }; + struct vfio_pci_region *region; int i, ret; =20 if (copy_from_user(&info, arg, minsz)) @@ -1091,22 +1092,23 @@ static int vfio_pci_ioctl_get_region_info(struct vf= io_pci_core_device *vdev, info.index, VFIO_PCI_NUM_REGIONS + vdev->num_regions); =20 i =3D info.index - VFIO_PCI_NUM_REGIONS; + region =3D &vdev->region[i]; =20 info.offset =3D VFIO_PCI_INDEX_TO_OFFSET(info.index); - info.size =3D vdev->region[i].size; - info.flags =3D vdev->region[i].flags; + info.size =3D region->size; + info.flags =3D region->flags; =20 - cap_type.type =3D vdev->region[i].type; - cap_type.subtype =3D vdev->region[i].subtype; + cap_type.type =3D region->type; + cap_type.subtype =3D region->subtype; =20 ret =3D vfio_info_add_capability(&caps, &cap_type.header, sizeof(cap_type)); if (ret) return ret; =20 - if (vdev->region[i].ops->add_capability) { - ret =3D vdev->region[i].ops->add_capability( - vdev, &vdev->region[i], &caps); + if (region->ops->add_capability) { + ret =3D region->ops->add_capability( + vdev, region, &caps); if (ret) return ret; } @@ -1726,10 +1728,11 @@ int vfio_pci_core_mmap(struct vfio_device *core_vde= v, struct vm_area_struct *vma int regnum =3D index - VFIO_PCI_NUM_REGIONS; struct vfio_pci_region *region =3D vdev->region + regnum; =20 + ret =3D -EINVAL; if (region->ops && region->ops->mmap && (region->flags & VFIO_REGION_INFO_FLAG_MMAP)) - return region->ops->mmap(vdev, region, vma); - return -EINVAL; + ret =3D region->ops->mmap(vdev, region, vma); + return ret; } if (index >=3D VFIO_PCI_ROM_REGION_INDEX) return -EINVAL; diff --git a/drivers/vfio/pci/vfio_pci_igd.c b/drivers/vfio/pci/vfio_pci_ig= d.c index 988b6919c2c31..ac0921fdc62da 100644 --- a/drivers/vfio/pci/vfio_pci_igd.c +++ b/drivers/vfio/pci/vfio_pci_igd.c @@ -66,14 +66,18 @@ static ssize_t vfio_pci_igd_rw(struct vfio_pci_core_dev= ice *vdev, bool iswrite) { unsigned int i =3D VFIO_PCI_OFFSET_TO_INDEX(*ppos) - VFIO_PCI_NUM_REGIONS; - struct igd_opregion_vbt *opregionvbt =3D vdev->region[i].data; loff_t pos =3D *ppos & VFIO_PCI_OFFSET_MASK, off =3D 0; size_t remaining; + struct vfio_pci_region *region; + struct igd_opregion_vbt *opregionvbt; + + region =3D &vdev->region[i]; + opregionvbt =3D region->data; =20 - if (pos >=3D vdev->region[i].size || iswrite) + if (pos >=3D region->size || iswrite) return -EINVAL; =20 - count =3D min_t(size_t, count, vdev->region[i].size - pos); + count =3D min_t(size_t, count, region->size - pos); remaining =3D count; =20 /* Copy until OpRegion version */ @@ -283,15 +287,19 @@ static ssize_t vfio_pci_igd_cfg_rw(struct vfio_pci_co= re_device *vdev, bool iswrite) { unsigned int i =3D VFIO_PCI_OFFSET_TO_INDEX(*ppos) - VFIO_PCI_NUM_REGIONS; - struct pci_dev *pdev =3D vdev->region[i].data; loff_t pos =3D *ppos & VFIO_PCI_OFFSET_MASK; size_t size; int ret; + struct vfio_pci_region *region; + struct pci_dev *pdev; + + region =3D &vdev->region[i]; + pdev =3D region->data; =20 - if (pos >=3D vdev->region[i].size || iswrite) + if (pos >=3D region->size || iswrite) return -EINVAL; =20 - size =3D count =3D min(count, (size_t)(vdev->region[i].size - pos)); + size =3D count =3D min(count, (size_t)(region->size - pos)); =20 if ((pos & 1) && size) { u8 val; --=20 2.47.3 Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13 10243 Berlin Geschaeftsfuehrung: Christian Schlaeger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597 From nobody Thu Oct 2 01:01:44 2025 Received: from fra-out-001.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-001.esa.eu-central-1.outbound.mail-perimeter.amazon.com [18.156.205.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 381563054C1; Wed, 24 Sep 2025 14:11:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=18.156.205.64 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723102; cv=none; b=tLeyyk2hrFn+lcaNE77XOF74qB72p36eHjMdre016P/HCLyAqC9Zw2fFGCrUKXilcul3NW5NLKzkVQf8aspIyyT0xtBqNGMlt9+O+m6/7EXxLJIPs9WcNON8t2ymS0ztByEEmLd7v20LHlxXMCmFo0v4nMi+9fP2KHZIZVAlnUU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723102; c=relaxed/simple; bh=+G+nhgMDkUB3krElw/Y5fjO5ixQTrXyQMbOi3Ishsuo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=q53WHyXMEuujsU2dmDhYQeFBCyEH0hddQtTZeSW34dFOw0hn05y3cwaF3LdgxMBVHA+6BHEqo5pyCFz0PyXt0K9BMlr8MnftZ2ZhW/8O1LyL68bnvMJb/R1opAXb3E79oI2fe04ArUqROieKLdLoitSFuoNEOeJndI6cMU5hjL4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de; spf=pass smtp.mailfrom=amazon.de; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b=YROiuv8t; arc=none smtp.client-ip=18.156.205.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b="YROiuv8t" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazoncorp2; t=1758723100; x=1790259100; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=44sLeJ/g4ccVdWLHAH06fHNbcre1IP6QJSrawkrRk9A=; b=YROiuv8tSlTV9fn0zWA9KWX8s43RZMVVrQOYx1snDc3SxEdL6TIRpwjT 4QeIzlUIaS1f2yq1LUOgqWWXZv6B5e1/TYNsQQ1M+W2lQf3Fs3biULtD8 icUCx6JbeLX4KysEN1H5u2SW8qFLnplCooa57iK6Aw3DJxe3fSSGOTzuX spD4ujWwWruvZA2P7R3PQqY10dBDV0Ey6I/xbuxrI9spZHmH3xh6KU7tv Web5HNPPzrvO9OCVeUZveWlu2zvdGbCMLJszakujVc1zZYU4MY0sAGEPv ygXzjuUh/GI/TO3W6S5CeYG71YWHfgLBKMLfDNazhDla1emczhu2c+icN Q==; X-CSE-ConnectionGUID: BxIec1HyS62Lm99Tp+7DFw== X-CSE-MsgGUID: ACRxUN5BTo2YzKTXLD+d/A== X-IronPort-AV: E=Sophos;i="6.18,290,1751241600"; d="scan'208";a="2613010" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-001.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Sep 2025 14:11:29 +0000 Received: from EX19MTAEUC001.ant.amazon.com [54.240.197.225:5949] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.38.97:2525] with esmtp (Farcaster) id 67400613-76e3-4aa3-82f3-8f3a059ec344; Wed, 24 Sep 2025 14:11:29 +0000 (UTC) X-Farcaster-Flow-ID: 67400613-76e3-4aa3-82f3-8f3a059ec344 Received: from EX19D039EUC004.ant.amazon.com (10.252.61.190) by EX19MTAEUC001.ant.amazon.com (10.252.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:11:29 +0000 Received: from dev-dsk-mngyadam-1c-cb3f7548.eu-west-1.amazon.com (10.253.107.175) by EX19D039EUC004.ant.amazon.com (10.252.61.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:11:26 +0000 From: Mahmoud Adam To: CC: , , , , David Woodhouse , , , Subject: [RFC PATCH 2/7] vfio_pci_core: split krealloc to allow use RCU & return index Date: Wed, 24 Sep 2025 16:09:53 +0200 Message-ID: <20250924141018.80202-3-mngyadam@amazon.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250924141018.80202-1-mngyadam@amazon.de> References: <20250924141018.80202-1-mngyadam@amazon.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D040UWA004.ant.amazon.com (10.13.139.93) To EX19D039EUC004.ant.amazon.com (10.252.61.190) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Unwrap the allocation, copying, assignation & freeing part of the krealloc. This enables using RCU for picking the reference in the following patches, and synchronize before writing back to region. Use the return value for returning the region index that was created. This is helpful for the caller to know the index of the region that was created. Signed-off-by: Mahmoud Adam --- drivers/vfio/pci/vfio_pci_core.c | 34 ++++++++++++++++++++------------ drivers/vfio/pci/vfio_pci_igd.c | 6 +++--- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index ea04c1291af68..6629490c0e46f 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -881,30 +881,38 @@ static int msix_mmappable_cap(struct vfio_pci_core_de= vice *vdev, return vfio_info_add_capability(caps, &header, sizeof(header)); } =20 +/* + * Registers a new region to vfio_pci_core_device. + * Returns region index on success or a negative errno. + */ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, unsigned int type, unsigned int subtype, const struct vfio_pci_regops *ops, size_t size, u32 flags, void *data) { - struct vfio_pci_region *region; + int num_regions =3D vdev->num_regions; + struct vfio_pci_region *region, *old_region; =20 - region =3D krealloc(vdev->region, - (vdev->num_regions + 1) * sizeof(*region), - GFP_KERNEL_ACCOUNT); + region =3D kmalloc((num_regions + 1) * sizeof(*region), + GFP_KERNEL_ACCOUNT); if (!region) return -ENOMEM; =20 - vdev->region =3D region; - vdev->region[vdev->num_regions].type =3D type; - vdev->region[vdev->num_regions].subtype =3D subtype; - vdev->region[vdev->num_regions].ops =3D ops; - vdev->region[vdev->num_regions].size =3D size; - vdev->region[vdev->num_regions].flags =3D flags; - vdev->region[vdev->num_regions].data =3D data; + old_region =3D vdev->region; + if (old_region) + memcpy(region, old_region, num_regions * sizeof(*region)); =20 - vdev->num_regions++; + region[num_regions].type =3D type; + region[num_regions].subtype =3D subtype; + region[num_regions].ops =3D ops; + region[num_regions].size =3D size; + region[num_regions].flags =3D flags; + region[num_regions].data =3D data; =20 - return 0; + vdev->region =3D region; + vdev->num_regions++; + kfree(old_region); + return num_regions; } EXPORT_SYMBOL_GPL(vfio_pci_core_register_dev_region); =20 diff --git a/drivers/vfio/pci/vfio_pci_igd.c b/drivers/vfio/pci/vfio_pci_ig= d.c index ac0921fdc62da..93ddef48e4e4c 100644 --- a/drivers/vfio/pci/vfio_pci_igd.c +++ b/drivers/vfio/pci/vfio_pci_igd.c @@ -265,7 +265,7 @@ static int vfio_pci_igd_opregion_init(struct vfio_pci_c= ore_device *vdev) PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE, VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &vfio_pci_igd_regops, size, VFIO_REGION_INFO_FLAG_READ, opregionvbt); - if (ret) { + if (ret < 0) { if (opregionvbt->vbt_ex) memunmap(opregionvbt->vbt_ex); =20 @@ -415,7 +415,7 @@ static int vfio_pci_igd_cfg_init(struct vfio_pci_core_d= evice *vdev) VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG, &vfio_pci_igd_cfg_regops, host_bridge->cfg_size, VFIO_REGION_INFO_FLAG_READ, host_bridge); - if (ret) { + if (ret < 0) { pci_dev_put(host_bridge); return ret; } @@ -435,7 +435,7 @@ static int vfio_pci_igd_cfg_init(struct vfio_pci_core_d= evice *vdev) VFIO_REGION_SUBTYPE_INTEL_IGD_LPC_CFG, &vfio_pci_igd_cfg_regops, lpc_bridge->cfg_size, VFIO_REGION_INFO_FLAG_READ, lpc_bridge); - if (ret) { + if (ret < 0) { pci_dev_put(lpc_bridge); return ret; } --=20 2.47.3 Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13 10243 Berlin Geschaeftsfuehrung: Christian Schlaeger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597 From nobody Thu Oct 2 01:01:44 2025 Received: from fra-out-012.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-012.esa.eu-central-1.outbound.mail-perimeter.amazon.com [52.57.120.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D4221A841C; Wed, 24 Sep 2025 14:12:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.57.120.243 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723125; cv=none; b=gBVhPv+Jps6IfmBQXpEyGCZz4Emp1oEHhNlwa9CgyEZnaJJFfTsG36f1vPqMnO6Ge0U3GrssT2ewbVnqOVR/dreyXBqNLnsb+oQ+DNntGsWbru00ZPXK90TUAmQzpfgab0NA/Jk/HKm4RPmNXl9lHIFZdbFC4YbXgKQkDpjQMVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723125; c=relaxed/simple; bh=DPvxm+FfMOM5QjeAEmx9j5Fwj34KHgDs0wexiRvQ1U0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fGssTvvfPoyrqDXG6GlCWaTtVYlIAZjkBV6WUZXcV4QI5jSnwsFXoU0GDoO1FUgLodX6e9mIIsq2qGqFX3mxF2Pn7XERjFuskuyS/5Bu1f7z5wJ5zwnckJuUuH5LybGhrxrE5DOORbgx4cC14o7v15IeN15EkPF/7SQJyN6vXC8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de; spf=pass smtp.mailfrom=amazon.de; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b=gglMehBK; arc=none smtp.client-ip=52.57.120.243 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b="gglMehBK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazoncorp2; t=1758723123; x=1790259123; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Zfql9UowG8WIegMp+ynWapRYmAIWirTs7yiufqTrTrs=; b=gglMehBKAi2HlhqiqG9H07ALfuCX0YaTm25Qw1kHp2FPNzgJZ7gvd5FL 7RO7b00vXPD4E8LunBqXc+X7YbHfrbSuLpW77hkPtL+kAUSJK7HGWCHwD SNadWjA53hrltDaMmlhlvU53nDa+Fiu2IBJC/4t7SAr496VFn0lS/VSmG MHMHz2sN+3pCQZPbxZxcChxPDipHFSHdFnn30xjxxRasiA5YLqhgkLaCL Kyce+c+VD/uGF0I4kJ7EttJk72dkiRAoq/ElUooibiSpe02GMkEgIwY26 oKIbHAZYNwZkr3S+cqhlQ1yMXexbzQQsRzNV0/KDN6xocg71jwQcGXWdT Q==; X-CSE-ConnectionGUID: Nwrmg35MQmu+3p5o0heSag== X-CSE-MsgGUID: sz8V3zpBRwug1UcpPuxcNw== X-IronPort-AV: E=Sophos;i="6.18,290,1751241600"; d="scan'208";a="2511497" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-012.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Sep 2025 14:11:53 +0000 Received: from EX19MTAEUA001.ant.amazon.com [54.240.197.233:14147] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.23.230:2525] with esmtp (Farcaster) id e5c7181d-828d-417b-8d5b-ea01043c173b; Wed, 24 Sep 2025 14:11:53 +0000 (UTC) X-Farcaster-Flow-ID: e5c7181d-828d-417b-8d5b-ea01043c173b Received: from EX19D039EUC004.ant.amazon.com (10.252.61.190) by EX19MTAEUA001.ant.amazon.com (10.252.50.50) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:11:51 +0000 Received: from dev-dsk-mngyadam-1c-cb3f7548.eu-west-1.amazon.com (10.253.107.175) by EX19D039EUC004.ant.amazon.com (10.252.61.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:11:48 +0000 From: Mahmoud Adam To: CC: , , , , David Woodhouse , , , Subject: [RFC PATCH 3/7] vfio/pci: add RCU locking for regions access Date: Wed, 24 Sep 2025 16:09:54 +0200 Message-ID: <20250924141018.80202-4-mngyadam@amazon.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250924141018.80202-1-mngyadam@amazon.de> References: <20250924141018.80202-1-mngyadam@amazon.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D031UWC004.ant.amazon.com (10.13.139.246) To EX19D039EUC004.ant.amazon.com (10.252.61.190) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Since we could request to add more regions after initialization. We would need locking to avoid racing with readers and cause UAF. use RCU for read-write synchronization. And region_lock mutex is used to synchronize the write section. Changing the value of num_regions is done under the mutex. Since the num_regions can only increase, using READ_ONCE and WRITE_ONCE should be enough to make sure we have a valid value. On the write section, synchronize_rcu() is run before incrementing num_regions. Doing that makes sure read sections are passed before increasing num_regions to avoid causing out-of-bound access. Signed-off-by: Mahmoud Adam --- drivers/vfio/pci/vfio_pci_core.c | 59 +++++++++++++++++++++++--------- drivers/vfio/pci/vfio_pci_igd.c | 16 ++++++--- include/linux/vfio_pci_core.h | 1 + 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index 6629490c0e46f..78e18bfd973e5 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -882,7 +882,8 @@ static int msix_mmappable_cap(struct vfio_pci_core_devi= ce *vdev, } =20 /* - * Registers a new region to vfio_pci_core_device. + * Registers a new region to vfio_pci_core_device. region_lock should + * be held when multiple registers could happen. * Returns region index on success or a negative errno. */ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, @@ -890,15 +891,20 @@ int vfio_pci_core_register_dev_region(struct vfio_pci= _core_device *vdev, const struct vfio_pci_regops *ops, size_t size, u32 flags, void *data) { - int num_regions =3D vdev->num_regions; struct vfio_pci_region *region, *old_region; + int num_regions; + + mutex_lock(&vdev->region_lock); + num_regions =3D READ_ONCE(vdev->num_regions); =20 region =3D kmalloc((num_regions + 1) * sizeof(*region), GFP_KERNEL_ACCOUNT); if (!region) return -ENOMEM; =20 - old_region =3D vdev->region; + old_region =3D + rcu_dereference_protected(vdev->region, + lockdep_is_held(&vdev->region_lock)); if (old_region) memcpy(region, old_region, num_regions * sizeof(*region)); =20 @@ -909,8 +915,10 @@ int vfio_pci_core_register_dev_region(struct vfio_pci_= core_device *vdev, region[num_regions].flags =3D flags; region[num_regions].data =3D data; =20 - vdev->region =3D region; - vdev->num_regions++; + rcu_assign_pointer(vdev->region, region); + synchronize_rcu(); + WRITE_ONCE(vdev->num_regions, READ_ONCE(vdev->num_regions) + 1); + mutex_unlock(&vdev->region_lock); kfree(old_region); return num_regions; } @@ -968,7 +976,7 @@ static int vfio_pci_ioctl_get_info(struct vfio_pci_core= _device *vdev, if (vdev->reset_works) info.flags |=3D VFIO_DEVICE_FLAGS_RESET; =20 - info.num_regions =3D VFIO_PCI_NUM_REGIONS + vdev->num_regions; + info.num_regions =3D VFIO_PCI_NUM_REGIONS + READ_ONCE(vdev->num_regions); info.num_irqs =3D VFIO_PCI_NUM_IRQS; =20 ret =3D vfio_pci_info_zdev_add_caps(vdev, &caps); @@ -1094,13 +1102,16 @@ static int vfio_pci_ioctl_get_region_info(struct vf= io_pci_core_device *vdev, .header.version =3D 1 }; =20 - if (info.index >=3D VFIO_PCI_NUM_REGIONS + vdev->num_regions) + if (info.index >=3D VFIO_PCI_NUM_REGIONS + + READ_ONCE(vdev->num_regions)) return -EINVAL; - info.index =3D array_index_nospec( - info.index, VFIO_PCI_NUM_REGIONS + vdev->num_regions); + info.index =3D array_index_nospec(info.index, + VFIO_PCI_NUM_REGIONS + + READ_ONCE(vdev->num_regions)); =20 i =3D info.index - VFIO_PCI_NUM_REGIONS; - region =3D &vdev->region[i]; + rcu_read_lock(); + region =3D &rcu_dereference(vdev->region)[i]; =20 info.offset =3D VFIO_PCI_INDEX_TO_OFFSET(info.index); info.size =3D region->size; @@ -1111,15 +1122,20 @@ static int vfio_pci_ioctl_get_region_info(struct vf= io_pci_core_device *vdev, =20 ret =3D vfio_info_add_capability(&caps, &cap_type.header, sizeof(cap_type)); - if (ret) + if (ret) { + rcu_read_unlock(); return ret; + } =20 if (region->ops->add_capability) { ret =3D region->ops->add_capability( vdev, region, &caps); - if (ret) + if (ret) { + rcu_read_unlock(); return ret; + } } + rcu_read_unlock(); } } =20 @@ -1536,7 +1552,7 @@ static ssize_t vfio_pci_rw(struct vfio_pci_core_devic= e *vdev, char __user *buf, unsigned int index =3D VFIO_PCI_OFFSET_TO_INDEX(*ppos); int ret; =20 - if (index >=3D VFIO_PCI_NUM_REGIONS + vdev->num_regions) + if (index >=3D VFIO_PCI_NUM_REGIONS + READ_ONCE(vdev->num_regions)) return -EINVAL; =20 ret =3D pm_runtime_resume_and_get(&vdev->pdev->dev); @@ -1568,8 +1584,11 @@ static ssize_t vfio_pci_rw(struct vfio_pci_core_devi= ce *vdev, char __user *buf, =20 default: index -=3D VFIO_PCI_NUM_REGIONS; - ret =3D vdev->region[index].ops->rw(vdev, buf, - count, ppos, iswrite); + rcu_read_lock(); + ret =3D rcu_dereference(vdev->region)[index].ops->rw(vdev, buf, + count, ppos, + iswrite); + rcu_read_unlock(); break; } =20 @@ -1726,7 +1745,7 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev,= struct vm_area_struct *vma =20 index =3D vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); =20 - if (index >=3D VFIO_PCI_NUM_REGIONS + vdev->num_regions) + if (index >=3D VFIO_PCI_NUM_REGIONS + READ_ONCE(vdev->num_regions)) return -EINVAL; if (vma->vm_end < vma->vm_start) return -EINVAL; @@ -1734,12 +1753,16 @@ int vfio_pci_core_mmap(struct vfio_device *core_vde= v, struct vm_area_struct *vma return -EINVAL; if (index >=3D VFIO_PCI_NUM_REGIONS) { int regnum =3D index - VFIO_PCI_NUM_REGIONS; - struct vfio_pci_region *region =3D vdev->region + regnum; + struct vfio_pci_region *region; + + rcu_read_lock(); + region =3D rcu_dereference(vdev->region) + regnum; =20 ret =3D -EINVAL; if (region->ops && region->ops->mmap && (region->flags & VFIO_REGION_INFO_FLAG_MMAP)) ret =3D region->ops->mmap(vdev, region, vma); + rcu_read_unlock(); return ret; } if (index >=3D VFIO_PCI_ROM_REGION_INDEX) @@ -2107,6 +2130,7 @@ int vfio_pci_core_init_dev(struct vfio_device *core_v= dev) INIT_LIST_HEAD(&vdev->sriov_pfs_item); init_rwsem(&vdev->memory_lock); xa_init(&vdev->ctx); + mutex_init(&vdev->region_lock); =20 return 0; } @@ -2119,6 +2143,7 @@ void vfio_pci_core_release_dev(struct vfio_device *co= re_vdev) =20 mutex_destroy(&vdev->igate); mutex_destroy(&vdev->ioeventfds_lock); + mutex_destroy(&vdev->region_lock); kfree(vdev->region); kfree(vdev->pm_save); } diff --git a/drivers/vfio/pci/vfio_pci_igd.c b/drivers/vfio/pci/vfio_pci_ig= d.c index 93ddef48e4e4c..1f7e9e82ac08c 100644 --- a/drivers/vfio/pci/vfio_pci_igd.c +++ b/drivers/vfio/pci/vfio_pci_igd.c @@ -71,13 +71,17 @@ static ssize_t vfio_pci_igd_rw(struct vfio_pci_core_dev= ice *vdev, struct vfio_pci_region *region; struct igd_opregion_vbt *opregionvbt; =20 - region =3D &vdev->region[i]; + rcu_read_lock(); + region =3D &rcu_dereference(vdev->region)[i]; opregionvbt =3D region->data; =20 - if (pos >=3D region->size || iswrite) + if (pos >=3D region->size || iswrite) { + rcu_read_unlock(); return -EINVAL; + } =20 count =3D min_t(size_t, count, region->size - pos); + rcu_read_unlock(); remaining =3D count; =20 /* Copy until OpRegion version */ @@ -293,13 +297,17 @@ static ssize_t vfio_pci_igd_cfg_rw(struct vfio_pci_co= re_device *vdev, struct vfio_pci_region *region; struct pci_dev *pdev; =20 - region =3D &vdev->region[i]; + rcu_read_lock(); + region =3D &rcu_dereference(vdev->region)[i]; pdev =3D region->data; =20 - if (pos >=3D region->size || iswrite) + if (pos >=3D region->size || iswrite) { + rcu_read_unlock(); return -EINVAL; + } =20 size =3D count =3D min(count, (size_t)(region->size - pos)); + rcu_read_unlock(); =20 if ((pos & 1) && size) { u8 val; diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index f541044e42a2a..e106e58f297e9 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -63,6 +63,7 @@ struct vfio_pci_core_device { int irq_type; int num_regions; struct vfio_pci_region *region; + struct mutex region_lock; u8 msi_qmax; u8 msix_bar; u16 msix_size; --=20 2.47.3 Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13 10243 Berlin Geschaeftsfuehrung: Christian Schlaeger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597 From nobody Thu Oct 2 01:01:44 2025 Received: from fra-out-005.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-005.esa.eu-central-1.outbound.mail-perimeter.amazon.com [63.176.194.123]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A00B305078; Wed, 24 Sep 2025 14:12:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=63.176.194.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723147; cv=none; b=VvRXZGikthnw1B7CQBwm885zq7seTqARpMTkbVquHEqB5VV3Mtrw3Ly3zHZksHX0rZyJhltIztQR7FKZg5X1kjeVYIkni6f6YQFOH9Cew24Z/vXr3OoClMvZ6h3L8KYs97A86uYGuByrxUIhsVYCZrpnqIw+PNS4VPwihDMQnxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723147; c=relaxed/simple; bh=oZKQ4C5nPwEI/MmC2cxUscdgtruN1fXnU1IaTsWCpMU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Fm5O9ilm5vaIloFYArZiW027f9KgwT79rOwWzIsbQNaKeKZgnc5O225soDtmDYtXoo5niVSdyrcXo1ZJg0db9J40VQm0eW7nfILOsy6LQ4a+C2yzBOTAFa437IfRlVt+sninW20kk9Lno48YneCfGLO3ie/fQ73jgr1dbrRHnHw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de; spf=pass smtp.mailfrom=amazon.de; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b=bfqnXNMV; arc=none smtp.client-ip=63.176.194.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b="bfqnXNMV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazoncorp2; t=1758723145; x=1790259145; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dux6/g5MiCozMl06Rtbnr6VbWtIYfhLmT7gxLzOUBI4=; b=bfqnXNMV9ILIf7j/o79Da/zAP2aQrlP3ZqF+UO3jBvczvmL8IFOgebSM VVt2vT+MWZHJZCCIZJLq7yq5GLaEVAPGKbqEoD64dL6mqb1MBmwkD0VwV JwVe5KfCEMZ/aTI4an6IGW1kQ7W4VIhLgstnlygKSVi+d8Qm/V6oUleTv ERgn5gv8A0xUTHucbOBd8ClL++H2+TnevI1CNuMB9Bv/E/CETmLgxT5zk 6XzGR54w04ly4/sQ4cNi81j3w/Nwwg1b3Ct4IG/kbpXcrELD/jsv6EY7q 2t31o3ATxuELuWDhnbEBfW8bX7AidKeI+OxchmHwH5jAWo57ztb7wc4Tn g==; X-CSE-ConnectionGUID: s/7JeZw0RSqUVXT7izFP4A== X-CSE-MsgGUID: 5BIFu9IJTXmLVIjTpbN4qQ== X-IronPort-AV: E=Sophos;i="6.18,290,1751241600"; d="scan'208";a="2613315" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-005.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Sep 2025 14:12:14 +0000 Received: from EX19MTAEUA002.ant.amazon.com [54.240.197.232:28268] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.10.226:2525] with esmtp (Farcaster) id df92be04-940c-4b32-b8c2-7f001ca0db47; Wed, 24 Sep 2025 14:12:14 +0000 (UTC) X-Farcaster-Flow-ID: df92be04-940c-4b32-b8c2-7f001ca0db47 Received: from EX19D039EUC004.ant.amazon.com (10.252.61.190) by EX19MTAEUA002.ant.amazon.com (10.252.50.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:12:14 +0000 Received: from dev-dsk-mngyadam-1c-cb3f7548.eu-west-1.amazon.com (10.253.107.175) by EX19D039EUC004.ant.amazon.com (10.252.61.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:12:10 +0000 From: Mahmoud Adam To: CC: , , , , David Woodhouse , , , Subject: [RFC PATCH 4/7] vfio: add FEATURE_ALIAS_REGION uapi Date: Wed, 24 Sep 2025 16:09:55 +0200 Message-ID: <20250924141018.80202-5-mngyadam@amazon.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250924141018.80202-1-mngyadam@amazon.de> References: <20250924141018.80202-1-mngyadam@amazon.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D038UWB003.ant.amazon.com (10.13.139.157) To EX19D039EUC004.ant.amazon.com (10.252.61.190) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a new uapi DEVICE_FEATURE uapi, which allows users to create region aliases. The main usage is allowing user to request alias region with different attributes set, Like WC etc. This could be used create alias for current regions with WC or similar attributes set. Which is helpful for mmap-ing a region with WC. User can use PROBE to get the supported flags by the specified region index. Signed-off-by: Mahmoud Adam --- include/uapi/linux/vfio.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 75100bf009baf..1584409ba2fb9 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -275,6 +275,8 @@ struct vfio_region_info { #define VFIO_REGION_INFO_FLAG_WRITE (1 << 1) /* Region supports write */ #define VFIO_REGION_INFO_FLAG_MMAP (1 << 2) /* Region supports mmap */ #define VFIO_REGION_INFO_FLAG_CAPS (1 << 3) /* Info supports caps */ +#define VFIO_REGION_INFO_FLAG_ALIAS (1 << 4) /* This is an Alias Region */ +#define VFIO_REGION_INFO_FLAG_WC (1 << 5) /* Region supports write combine= */ __u32 index; /* Region index */ __u32 cap_offset; /* Offset within info struct of first cap */ __aligned_u64 size; /* Region size (bytes) */ @@ -1478,6 +1480,28 @@ struct vfio_device_feature_bus_master { }; #define VFIO_DEVICE_FEATURE_BUS_MASTER 10 =20 + +/** + * Upon VFIO_DEVICE_FEATURE_SET, creates a new region with the specified f= lags set. + * VFIO_DEVICE_FEATURE_PROBE can be used to return the supported flags for= this region. + * + * Alias a region with certain region flags set. For example this + * could be used to alias a region with Write Combine or similar + * attributes set for mmap. The new region index is returned on + * alias_index with the flags specified set. GET_REGION_INFO could then + * be used with the new index. By probing a region index the supported + * region flags are returned. + * Region flags follows the same flags from REGION_GET_REGION_INFO. + */ +struct vfio_device_feature_alias_region { + __u32 flags; /* Region flags to be used */ + __u32 index; /* Region index */ + __u32 alias_index; /* New region index */ + __u32 _resv1; + __u64 _resv2; +}; + +#define VFIO_DEVICE_FEATURE_ALIAS_REGION 11 /* -------- API for Type1 VFIO IOMMU -------- */ =20 /** --=20 2.47.3 Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13 10243 Berlin Geschaeftsfuehrung: Christian Schlaeger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597 From nobody Thu Oct 2 01:01:44 2025 Received: from fra-out-015.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-015.esa.eu-central-1.outbound.mail-perimeter.amazon.com [18.158.153.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9879F17A303; Wed, 24 Sep 2025 14:12:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=18.158.153.154 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723170; cv=none; b=kiD4iQ1ed8jCoXsxvclXS2LWbyCZJolnAWZiKmSYdcw4o4FL4OFvEvFOkpLVoRayrj4pGb7YrLg86DLpixNOp1oQ3g3vmBvSWYyW8R0WRgN5NONFPyDZhJxvI4jX4NADeuzLj1gIxc+fWHiw3NO/xKh7czQKzHFbsGajbsQFSBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723170; c=relaxed/simple; bh=H99QDTEIlY+DDELnzVOKberX3exoJMAsICZA/wPsE5s=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VcMB2UceVZAx7m6vWgR8JMVpHcEjI3ixUOkejobL/yrnzGEU6f+qHn4pH9rPCV6Dx9JB9WRrYCZaip+R6weiliPwWiuD5YVrIMqIuPwNRhgtnDfNji9RhPp/upnHuXzXhsdavR6k3ylVcVX8HnDv1gjMf/CkJ1WAbOIQFopfb9U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de; spf=pass smtp.mailfrom=amazon.de; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b=KMvi9Cx6; arc=none smtp.client-ip=18.158.153.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b="KMvi9Cx6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazoncorp2; t=1758723169; x=1790259169; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=38/mJ3wiOfJfNpSSHx+xaos5reN6f9BIE/olJW3PDIw=; b=KMvi9Cx6ivrY3OhrWJ/FRdvgRv98e5M6Ljw5cOrworE4Jad9RVDpYwyo VLv1XNvsJc1+sjIDqH0cVmI+0Id6IJ7Tq/OodU5SnM8avUBMv0rztm/OM /Eu4QRb/3lgFsB8/TVzO5U/0HXeNFslqCzc3TLRVuTz+F6UzC1SZHRX1Z FGkAeKyR/DawGA/831pPmCWct0Ul6ODeFgWTJ4utrK3BlmOQDAJzQbIhp ne1Aq4hh89ynhh/dsn9lslGAogLwyYVeh4pMZGsPoZutjWbctA4B2tKtb +L7OOtnUzIE+o44ZEmM2vyL6v00MEYNgOSrgYyQGZ+qXIXMopSJ3MiKLl w==; X-CSE-ConnectionGUID: x/TRL3pHQZ67WB6qLR3e1Q== X-CSE-MsgGUID: uA9D7gzeQn+A4SdIs9M2Xg== X-IronPort-AV: E=Sophos;i="6.18,290,1751241600"; d="scan'208";a="2498795" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-015.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Sep 2025 14:12:36 +0000 Received: from EX19MTAEUB002.ant.amazon.com [54.240.197.224:18731] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.10.226:2525] with esmtp (Farcaster) id 1eb9aafd-c834-41b1-8ff1-e0a20105f74a; Wed, 24 Sep 2025 14:12:36 +0000 (UTC) X-Farcaster-Flow-ID: 1eb9aafd-c834-41b1-8ff1-e0a20105f74a Received: from EX19D039EUC004.ant.amazon.com (10.252.61.190) by EX19MTAEUB002.ant.amazon.com (10.252.51.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:12:36 +0000 Received: from dev-dsk-mngyadam-1c-cb3f7548.eu-west-1.amazon.com (10.253.107.175) by EX19D039EUC004.ant.amazon.com (10.252.61.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:12:32 +0000 From: Mahmoud Adam To: CC: , , , , David Woodhouse , , , Subject: [RFC PATCH 5/7] vfio_pci_core: allow regions with no release op Date: Wed, 24 Sep 2025 16:09:56 +0200 Message-ID: <20250924141018.80202-6-mngyadam@amazon.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250924141018.80202-1-mngyadam@amazon.de> References: <20250924141018.80202-1-mngyadam@amazon.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D042UWB004.ant.amazon.com (10.13.139.150) To EX19D039EUC004.ant.amazon.com (10.252.61.190) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow regions to not to have a release op. This could be helpful with alias regions. These regions wouldn't need to implement release ops. Signed-off-by: Mahmoud Adam --- With the initial implementnation purposed in this RFC, there is nothing to release for the alias regions. I wasn't sure if we should force regions to implement release ops, If this is the case then an empty function might be the better solution here. drivers/vfio/pci/vfio_pci_core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index 78e18bfd973e5..04b93bd55a5c2 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -605,7 +605,8 @@ void vfio_pci_core_disable(struct vfio_pci_core_device = *vdev) vdev->virq_disabled =3D false; =20 for (i =3D 0; i < vdev->num_regions; i++) - vdev->region[i].ops->release(vdev, &vdev->region[i]); + if (vdev->region[i].ops && vdev->region[i].ops->release) + vdev->region[i].ops->release(vdev, &vdev->region[i]); =20 vdev->num_regions =3D 0; kfree(vdev->region); --=20 2.47.3 Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13 10243 Berlin Geschaeftsfuehrung: Christian Schlaeger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597 From nobody Thu Oct 2 01:01:44 2025 Received: from fra-out-012.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-012.esa.eu-central-1.outbound.mail-perimeter.amazon.com [52.57.120.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECEE3305949; Wed, 24 Sep 2025 14:13:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.57.120.243 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723185; cv=none; b=mxWyjzShg26IvVQCjsCUXU1bCdC2h5S/y4MzNQbjRPCciBPB6WhNyawkCJjyLCEVjcMmn2HW4cccvJKyR0B4hJwmNEn76E3uAYeJHyTXXOdsmupjVkVkG8tfhjsrKKaMyI5+x4UTBUfZGSjfDYyKHphyilVodeK/YgHUnd8WGaE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723185; c=relaxed/simple; bh=ViUO0IxYcuB/eb85CpxW3PyEQgoG9MD6blUfHb2Jm1k=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=I8qSHb9Rsegz8Qg906Mya9NOFlkLxNsPweMP7EGpnCY0wIdilYDqbh9+ACIfRaGp6MmnBNG/1e1BKyQ34P4CBY6nldWSI6/k2ufyMrY0oYq4HXHe02ZfPp4ibNc/zDrbHM4LGVLOPIvxpO5QfO7aTkoB+a1ddLK3hKLAfMU09lg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de; spf=pass smtp.mailfrom=amazon.de; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b=B6Rg2A6z; arc=none smtp.client-ip=52.57.120.243 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b="B6Rg2A6z" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazoncorp2; t=1758723182; x=1790259182; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=czGB2SwCKNKXAfZ9U9/T0smrUlLXryM3ssqmlBWgsFY=; b=B6Rg2A6z+Enaqi4sr2PpwrlPirar1/5uBUAgq7ye0OPnj00/c1tmuB+f Ljav9IF8e/0gWPnZzMyUpSYb2V0sxdg30r+X74MBG+Af177SXidvZRDmX XhLiUB/o5JRRZAseUaXuI0w0iJ5hDs4DfHyXIQJ8QbnPmfBTUF96/IX0I IXJpj6HUfRoOZ2xGQRe1strFsZd3RKWh/FZh/F568Xl9P9TNQM/KM4wJv QdZFbfGUhNeTwVe45XxrrHvz0VSi41rUoyZPanPCT2wQW8YWQtYIjlX+3 tpRVWv/SMtb6K18aRkZY0qN549dsYtJvQCUAk9LZgzFexi7vsh20dA7N5 A==; X-CSE-ConnectionGUID: Qlma0UydS9yj/kfl4ty7cg== X-CSE-MsgGUID: TJKkLL29SRmoHY2VHaAYHg== X-IronPort-AV: E=Sophos;i="6.18,290,1751241600"; d="scan'208";a="2511554" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-012.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Sep 2025 14:13:00 +0000 Received: from EX19MTAEUC002.ant.amazon.com [54.240.197.228:15134] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.10.226:2525] with esmtp (Farcaster) id 9dbd9be8-2ebb-4721-b94d-5e4d39397bed; Wed, 24 Sep 2025 14:13:00 +0000 (UTC) X-Farcaster-Flow-ID: 9dbd9be8-2ebb-4721-b94d-5e4d39397bed Received: from EX19D039EUC004.ant.amazon.com (10.252.61.190) by EX19MTAEUC002.ant.amazon.com (10.252.51.245) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:12:58 +0000 Received: from dev-dsk-mngyadam-1c-cb3f7548.eu-west-1.amazon.com (10.253.107.175) by EX19D039EUC004.ant.amazon.com (10.252.61.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:12:54 +0000 From: Mahmoud Adam To: CC: , , , , David Woodhouse , , , Subject: [RFC PATCH 6/7] vfio-pci: add alias_region mmap ops Date: Wed, 24 Sep 2025 16:09:57 +0200 Message-ID: <20250924141018.80202-7-mngyadam@amazon.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250924141018.80202-1-mngyadam@amazon.de> References: <20250924141018.80202-1-mngyadam@amazon.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D040UWA002.ant.amazon.com (10.13.139.113) To EX19D039EUC004.ant.amazon.com (10.252.61.190) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement struct vfio_pci_regops for alias regions. Where it implements the mmap ops. When mmap is called on these regions it translates the vm_pgoff to match the aliased region. Then it calls the required mmap for the target region. It updates the vm_page_prot afterwards with the requested flags. The call path would be: vfio_pci_core_mmap (index >=3D VFIO_PCI_NUM_REGIONS) vfio_pci_alias_region_mmap (update vm_pgoff) vfio_pci_core_mmap For now no more information is needed more than the aliased index. So we use region->data to save the aliased index number. Note: Alias regions can't alias another alias. Signed-off-by: Mahmoud Adam --- drivers/vfio/pci/vfio_pci_core.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index 04b93bd55a5c2..962d3eda1ea9f 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1528,6 +1528,33 @@ static int vfio_pci_core_feature_token(struct vfio_d= evice *device, u32 flags, return 0; } =20 +static int vfio_pci_alias_region_mmap(struct vfio_pci_core_device *vdev, + struct vfio_pci_region *region, + struct vm_area_struct *vma) +{ + unsigned int alias_index =3D (uintptr_t) region->data; + unsigned long vm_pgoff; + int ret; + + /* change the pgoff to the corresponding alias */ + vm_pgoff =3D alias_index << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); + vm_pgoff |=3D vma->vm_pgoff & + ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1); + vma->vm_pgoff =3D vm_pgoff; + + ret =3D vdev->vdev.ops->mmap(&vdev->vdev, vma); + + /* overwrite prot with the alias flags */ + if (region->flags & VFIO_REGION_INFO_FLAG_WC) + vma->vm_page_prot =3D pgprot_writecombine(vma->vm_page_prot); + + return ret; +} + +struct vfio_pci_regops vfio_pci_alias_region_ops =3D { + .mmap =3D vfio_pci_alias_region_mmap, +}; + int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, void __user *arg, size_t argsz) { --=20 2.47.3 Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13 10243 Berlin Geschaeftsfuehrung: Christian Schlaeger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597 From nobody Thu Oct 2 01:01:44 2025 Received: from fra-out-007.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-007.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.75.33.185]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E12E21A38F9; Wed, 24 Sep 2025 14:13:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.75.33.185 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723213; cv=none; b=DZ1Id6KqlzzXtAhn2FZI+68swDzybmq+ML+Yg2ePWjpt1iZ/EVJ3PHXXMjV2IhY9NUuzOuRp1ozlHzcDul0uF9ZANI1sSN2Ma5r70wVhQaEIdrY5QDrBoKIpL9/XrFL3MCwyhUKNUhUbfwMTT7a2KxmFq4dYb4o8jFtCd9bwTTM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758723213; c=relaxed/simple; bh=Psf2UiHbIfFoCKVtGR7vRnCiMvd8l30OQ4xeY9HiHrE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bnQ3/U2g7V46yor8D8eQzNsBqNuu4vw1AQ7qQ7KaVwyseOq20cAWNlqjq3yDXiDBi1LZxv5Y4URhxJ7989n9eWKwVinaegqheQ3pBgDafwB4jdYQ3tyVFTeODUFDx9ilOdsG1BpMnvNg+8fdyol2D9zUgzWtbD+k4TXNEXc94Xg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de; spf=pass smtp.mailfrom=amazon.de; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b=JxSo6cCE; arc=none smtp.client-ip=3.75.33.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.de header.i=@amazon.de header.b="JxSo6cCE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazoncorp2; t=1758723211; x=1790259211; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bhLPFvRc8Jr7ZaJZGuxccCtemmZ4vyWewvC1yxLU+O8=; b=JxSo6cCEoQ/HDNcYvYdF1tR78CJGGiuWTQ5mPOQTKOOTXwk4Xot0ZZ0N Tr1svDmMh7PaZsMhgjns60vn3iYBnJwC8uop0tFuV4E+/AQSwLUB47R2W hcsVIXoAanmkq2YAD9gEYwzuP3BaekfQ8Hz8wFU1s53TyRT3W2oe5EI4L grntKS6flk5nyxVoYuGfmhOImZod0Zj/+Exc4NcAVWMCI3sR+pFwaNqXW S7ou2dVhEKPIz0AFOXkU119LCZRAp8LaDL19tkk0cIHvcDTHyEiPs5Mfz akWuJ4iTndAald4ZuV2maBDncNVKGQbcxArjnH/bKp76zvHnjNEbQD5Kl w==; X-CSE-ConnectionGUID: AaxHro97QF2ob+ABW/zNLg== X-CSE-MsgGUID: F6Ae34Y6SDuTndUMh5RU5A== X-IronPort-AV: E=Sophos;i="6.18,290,1751241600"; d="scan'208";a="2615973" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-007.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Sep 2025 14:13:21 +0000 Received: from EX19MTAEUC001.ant.amazon.com [54.240.197.225:28822] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.10.226:2525] with esmtp (Farcaster) id e487420e-1f17-415c-8a4b-03885c61768e; Wed, 24 Sep 2025 14:13:20 +0000 (UTC) X-Farcaster-Flow-ID: e487420e-1f17-415c-8a4b-03885c61768e Received: from EX19D039EUC004.ant.amazon.com (10.252.61.190) by EX19MTAEUC001.ant.amazon.com (10.252.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:13:20 +0000 Received: from dev-dsk-mngyadam-1c-cb3f7548.eu-west-1.amazon.com (10.253.107.175) by EX19D039EUC004.ant.amazon.com (10.252.61.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Wed, 24 Sep 2025 14:13:16 +0000 From: Mahmoud Adam To: CC: , , , , David Woodhouse , , , Subject: [RFC PATCH 7/7] vfio-pci-core: implement FEATURE_ALIAS_REGION uapi Date: Wed, 24 Sep 2025 16:09:58 +0200 Message-ID: <20250924141018.80202-8-mngyadam@amazon.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250924141018.80202-1-mngyadam@amazon.de> References: <20250924141018.80202-1-mngyadam@amazon.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D041UWB002.ant.amazon.com (10.13.139.179) To EX19D039EUC004.ant.amazon.com (10.252.61.190) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This implements the new DEVICE_FEATURE_ALIAS_REGION. As of right now Alias is only needed for mmaping. So we will allow aliasing mmap supported regions only. If the user requested a similar alias (same flags and aliased index). re-use the old index instead by returning it to the user. Since creating another alias gives no extra value for the user. The region with the new flag (WC), will allow the user to mmap the aliased region with WC enabled. We also supports probing. When the user probe a region index, we return the region flags supported to be enabled for this region. Initially we are supporting WC only when the region is mmap-able. add vfio_pci_core_register_dev_region_locked to allow externally locking the mutex. So that we can check for if a similar region exists and add a new region under the same mutex lock, to avoid racing. Signed-off-by: Mahmoud Adam --- drivers/vfio/pci/vfio_pci_core.c | 173 ++++++++++++++++++++++++++++--- 1 file changed, 161 insertions(+), 12 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index 962d3eda1ea9f..3c162cf47a1eb 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -882,20 +882,14 @@ static int msix_mmappable_cap(struct vfio_pci_core_de= vice *vdev, return vfio_info_add_capability(caps, &header, sizeof(header)); } =20 -/* - * Registers a new region to vfio_pci_core_device. region_lock should - * be held when multiple registers could happen. - * Returns region index on success or a negative errno. - */ -int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, - unsigned int type, unsigned int subtype, - const struct vfio_pci_regops *ops, - size_t size, u32 flags, void *data) +static int vfio_pci_core_register_dev_region_locked( + struct vfio_pci_core_device *vdev, + unsigned int type, unsigned int subtype, + const struct vfio_pci_regops *ops, + size_t size, u32 flags, void *data) { struct vfio_pci_region *region, *old_region; int num_regions; - - mutex_lock(&vdev->region_lock); num_regions =3D READ_ONCE(vdev->num_regions); =20 region =3D kmalloc((num_regions + 1) * sizeof(*region), @@ -919,10 +913,29 @@ int vfio_pci_core_register_dev_region(struct vfio_pci= _core_device *vdev, rcu_assign_pointer(vdev->region, region); synchronize_rcu(); WRITE_ONCE(vdev->num_regions, READ_ONCE(vdev->num_regions) + 1); - mutex_unlock(&vdev->region_lock); kfree(old_region); return num_regions; } + +/* + * Registers a new region to vfio_pci_core_device. region_lock should + * be held when multiple registers could happen. + * Returns region index on success or a negative errno. + */ +int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, + unsigned int type, unsigned int subtype, + const struct vfio_pci_regops *ops, + size_t size, u32 flags, void *data) +{ + int index; + + mutex_lock(&vdev->region_lock); + index =3D vfio_pci_core_register_dev_region_locked(vdev, type, subtype, + ops, + size, flags, data); + mutex_unlock(&vdev->region_lock); + return index; +} EXPORT_SYMBOL_GPL(vfio_pci_core_register_dev_region); =20 static int vfio_pci_info_atomic_cap(struct vfio_pci_core_device *vdev, @@ -1528,6 +1541,48 @@ static int vfio_pci_core_feature_token(struct vfio_d= evice *device, u32 flags, return 0; } =20 +static bool vfio_pci_region_is_mmap_supported(struct vfio_pci_core_device = *vdev, + int index) +{ + if (index <=3D VFIO_PCI_BAR5_REGION_INDEX) + return vdev->bar_mmap_supported[index]; + + if (index >=3D VFIO_PCI_NUM_REGIONS) { + int i =3D index - VFIO_PCI_NUM_REGIONS; + bool is_mmap; + struct vfio_pci_region *region; + + rcu_read_lock(); + region =3D &rcu_dereference(vdev->region)[i]; + is_mmap =3D (region->flags & VFIO_REGION_INFO_FLAG_MMAP) && + region->ops && region->ops->mmap; + rcu_read_unlock(); + return is_mmap; + } + return false; +} + +static bool vfio_pci_region_alias_exists(struct vfio_pci_core_device *vdev, + u32 flags, int index, int *alias_index) +{ + int i; + + for (i =3D 0; i < READ_ONCE(vdev->num_regions); i++) { + struct vfio_pci_region *region; + + region =3D &rcu_dereference_protected( + vdev->region, lockdep_is_held(&vdev->region_lock))[i]; + if (!(region->flags & VFIO_REGION_INFO_FLAG_ALIAS)) + continue; + if ((int)(uintptr_t) region->data =3D=3D index && + region->flags =3D=3D flags) { + *alias_index =3D i + VFIO_PCI_NUM_REGIONS; + return true; + } + } + return false; +} + static int vfio_pci_alias_region_mmap(struct vfio_pci_core_device *vdev, struct vfio_pci_region *region, struct vm_area_struct *vma) @@ -1555,6 +1610,97 @@ struct vfio_pci_regops vfio_pci_alias_region_ops =3D= { .mmap =3D vfio_pci_alias_region_mmap, }; =20 +static int vfio_pci_core_feature_alias_region( + struct vfio_device *device, u32 flags, + struct vfio_device_feature_alias_region __user *arg, + size_t argsz) +{ + struct vfio_pci_core_device *vdev =3D + container_of(device, struct vfio_pci_core_device, vdev); + struct pci_dev *pdev =3D vdev->pdev; + bool is_probe =3D false; + u32 region_flags; + struct vfio_device_feature_alias_region request_region; + int ret, index, new_index; + size_t size; + + ret =3D vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, + sizeof(request_region)); + if (ret < 0) + return ret; + + if (ret =3D=3D 0) /* probing only */ + is_probe =3D true; + + if (copy_from_user(&request_region, arg, sizeof(request_region))) + return -EFAULT; + + if (request_region.index >=3D VFIO_PCI_NUM_REGIONS + + READ_ONCE(vdev->num_regions)) + return -EINVAL; + + index =3D array_index_nospec(request_region.index, + VFIO_PCI_NUM_REGIONS + + READ_ONCE(vdev->num_regions)); + + /* make sure we are not aliasing an alias region */ + if (index >=3D VFIO_PCI_NUM_REGIONS) { + int i; + + rcu_read_lock(); + i =3D index - VFIO_PCI_NUM_REGIONS; + if (rcu_dereference(vdev->region)[i].flags & + VFIO_REGION_INFO_FLAG_ALIAS) { + rcu_read_unlock(); + return -EINVAL; + } + rcu_read_unlock(); + } + + /* For now we only allow aliasing mmap supported regions. */ + if (!vfio_pci_region_is_mmap_supported(vdev, index)) + return -EINVAL; + + if (is_probe) { + request_region.flags =3D VFIO_REGION_INFO_FLAG_WC; + goto out_copy; + } + + if (request_region.flags & ~VFIO_REGION_INFO_FLAG_WC) + return -EINVAL; + + region_flags =3D VFIO_REGION_INFO_FLAG_ALIAS | + VFIO_REGION_INFO_FLAG_MMAP | VFIO_REGION_INFO_FLAG_WC; + + mutex_lock(&vdev->region_lock); + if (vfio_pci_region_alias_exists(vdev, region_flags, + index, &new_index)) { + request_region.alias_index =3D new_index; + goto out_copy_unlock; + } + + if (index <=3D VFIO_PCI_BAR5_REGION_INDEX) + size =3D pci_resource_len(pdev, index); + else + size =3D vdev->region[index].size; + + new_index =3D vfio_pci_core_register_dev_region_locked( + vdev, 0, 0, &vfio_pci_alias_region_ops, size, region_flags, + (void *)(uintptr_t)index); + + if (new_index < 0) { + mutex_unlock(&vdev->region_lock); + return new_index; + } + request_region.alias_index =3D new_index + VFIO_PCI_NUM_REGIONS; + +out_copy_unlock: + mutex_unlock(&vdev->region_lock); +out_copy: + ret =3D copy_to_user(arg, &request_region, sizeof(request_region)); + return ret ? -EFAULT : 0; +} + int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, void __user *arg, size_t argsz) { @@ -1568,6 +1714,9 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *d= evice, u32 flags, return vfio_pci_core_pm_exit(device, flags, arg, argsz); case VFIO_DEVICE_FEATURE_PCI_VF_TOKEN: return vfio_pci_core_feature_token(device, flags, arg, argsz); + case VFIO_DEVICE_FEATURE_ALIAS_REGION: + return vfio_pci_core_feature_alias_region(device, flags, + arg, argsz); default: return -ENOTTY; } --=20 2.47.3 Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13 10243 Berlin Geschaeftsfuehrung: Christian Schlaeger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597