From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 08C053EC825 for ; Wed, 9 Sep 2026 22:27:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788992870; cv=none; b=iwCNwSebaCsPP0/9iXEUdE7kbPBSqjU58RnQgkT2lhCDbLEKtEz19beSj9sCpiMySMscSQ38FYVg3rO3x8uxRPc6ntBldvZohCfIP6KlQmTehfZAwtTkcHrVgwLU4hb0I40FIORAxa8ni6tJy9HsnsTcWG2HzdQ3iCWRM7bkIfE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788992870; c=relaxed/simple; bh=mqKexKRlmNDFLQEIvT7Y9z7FUD+hpM8xK3PiP7gpeMA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=peWWj+asuyaEpApOIre5LexpJGerAmoskMar4l3sSgoZS26hqTt6gQdqxgNzGUhqDFQo+po5UJfnG8QMl8G19Fzm/NUS0j6DvpPYc7z+4IHwtuwTCEd1phpO2s+AFAFpZgLFXFVHxoetC/lm0y/7B2um2WzngFrdqm7uGJI0tm4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=kh99ibga; arc=none smtp.client-ip=209.85.216.46 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=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kh99ibga" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-38dfe7eb825so5397839a91.0 for ; Wed, 09 Sep 2026 15:27:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1788992867; x=1789597667; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=R5cPdTAC3boYVm13Avr5IgzIz8D8z9d9W6rfo2545Nc=; b=kh99ibgak5G4QcwI18d3owe7OazqpalonXOzIn1u7ixcsAtGPBuMJCV+wPTfPW93Zx 5JksmleUaj1HYW/wzEHI2D7v9CmTZtfkIiSlyT80XG9h2rxjfb52L4g/WatVXsHrBLWG 1WVAi7zbp75wITpZVE11ectc+xUDt80kn7K7i59MnZd04nCFZ14rkLjUqTV2MzN/a+WU E4ehGWy/30sShjczlgoS/41orH8pVxldORcj2zpMcG+Z69cojlCKBlwpbn8Hta4j598P nvSc/iOw2dto3nnFLL26kQY/IebWj9RYNDPGaHYsksBBDaIdjlVn9qTnJdhfn29zUrxP E2Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788992867; x=1789597667; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=R5cPdTAC3boYVm13Avr5IgzIz8D8z9d9W6rfo2545Nc=; b=gK0SDDqq2NlpSTPdvYzdAaPuLHf2IBlE3PnO2unuUeybRSX/fVG+LBks1ACuOda0X2 GoQFlnO4RHeTilLingGFPxnWWbBimVGmlgSgyksvvvKTmzv/9G3v440bhkwmtMDDy7YD hCMbUG3curKahqi+X7Wi5bHMvItt9OSDZ+zOql48kpCz098ISdWTjBF5SFVxQ/XAm7Fc 6pD4F0/mZHQn8raOZtXFljXM8G3kA+CNmku7m+gMbyg89QIDiBDjLoj4yaBoqlCnFm+7 rBfHQf79A5Hx1eA+35uJyBoWHX4W6+3IsrVrPvi5Mds4Wa+vj6V8/9qn9WUorH+7jWav En7w== X-Forwarded-Encrypted: i=1; AKwUvByHA/LPx58hz3BwqZ4LhyJqpYO2L7xCS4grfVInXcIs0IR0VAHTN7EHbK+D2JQJ3dTNYBudXZ2L+Y/qeJE=@vger.kernel.org X-Gm-Message-State: AFuF++myIXipO2l9Zmx2LJeAgIfveADV+p62XNPqunRVTToNtLBORIHV r/7AgMllSHAkoN2+UH6UG8QHwPHYtkSDhU6daKdpKHh7zgAeUEaUBnZ+/aXlnmZQQg== X-Gm-Gg: AYBFou1MY+rvuS7KQ6Yu5G26E+w0prXk3dGKMdX0kL6jsI6TGAniX1BKhTdIdA0v2+2 RjXytr714eiHENQmtDSRlbmASK8UndQthnL87YM89sDJpjrOCZPVHVbwbAKIP6Wtr6iJPG4jP4H 4yFecYXHvwQ6RVR827cjAi348fFE38q7xh4jdznvzUPpX/o02uWlMHYsP6K1tZxNWujc1gGvn+i Ob689e6JiIKSVfWQvNn00pRayY9XVSOJxfKUBL0XWInFjQ7hB7DvK5lsx9M9W3BvGpFcfQmWP3w V/9n6ICiiYeaXH2pZXBeAaYAfuXIDuHAycHvOmovYFDiaeMj5qMWvB0b8A5XHuHhCEY0ogUoPEj d4xlN/w2lvtVLK2dlpHiZz8AwLYOZ9g9XAmYrYLO+bpg/SRJ6ur7dNjaAgUfE8v+b5JQKhAneVg Tm4AQpTAD1LnnojyouCq8MKH1WdksDJIhfwDU2oHlhZqT49SLdYssiVDQ3W/g4T6saeX2X1tHnv 4k4+rvFyYipBMBI1saH2gfMg3jroGgFAvhhEWN/ X-Received: by 2002:a17:90b:4cce:b0:38f:240d:b857 with SMTP id 98e67ed59e1d1-39b260dd4e9mr60322664a91.2.1788992866367; Wed, 09 Sep 2026 15:27:46 -0700 (PDT) Received: from google.com (192.150.203.35.bc.googleusercontent.com. [35.203.150.192]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-39d7738ce41sm1598219a91.3.2026.09.09.15.27.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Sep 2026 15:27:45 -0700 (PDT) Date: Wed, 9 Sep 2026 22:27:42 +0000 From: David Matlack To: Alex Williamson Cc: Alex Williamson , kvm , linux-kernel , Jason Gunthorpe , Kevin Tian , Yi Liu Subject: Re: [PATCH 1/4] vfio: Reject a second cdev open before mutating shared device state Message-ID: References: <20260901215358.2421359-1-alex.williamson@nvidia.com> <20260901215358.2421359-2-alex.williamson@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260901215358.2421359-2-alex.williamson@nvidia.com> On 2026-09-01 03:53 PM, Alex Williamson wrote: > The cdev single-open check lives in vfio_df_open(), which runs at the > end of the bind ioctl, after vfio_df_ioctl_bind_iommufd() has already > updated state shared across all opens: vfio_df_check_token() can set > the PF vf_token and vfio_df_get_kvm_safe() records the caller's KVM > pointer in device->kvm and takes a reference. > > A second cdev bind of an already-open device runs both, only to be > rejected in vfio_df_open(). The error path clears device->kvm and > drops the reference, tearing down the current opener's KVM association > and potentially resulting in an unbalanced reference on close or > premature release, while the vf_token remains clobbered. > > Move the single-open check into vfio_df_ioctl_bind_iommufd() ahead of > both mutations, so a bind that cannot complete leaves the current > opener's state untouched. df->group is NULL on this path, so a > non-zero open_count is exactly what vfio_df_open() rejected. The test > in vfio_df_open() becomes redundant and is removed. > > Return -EBUSY rather than -EINVAL here. The arguments are not invalid, > the device is in use, which could be a transient condition due to a > delayed fput if the prior user is terminated. This provides > compatibility with the group path, where a group open returns -EBUSY, > and users may choose bounded polling to detect such a transient > condition. > > Fixes: 839e692fa4eb ("vfio: Make vfio_df_open() single open for device cdev path") > Fixes: 5fcc26969a16 ("vfio: Add VFIO_DEVICE_BIND_IOMMUFD") > Fixes: 86624ba3b522 ("vfio/pci: Do vf_token checks for VFIO_DEVICE_BIND_IOMMUFD") > Assisted-by: claude-opus-4-8 > Signed-off-by: Alex Williamson Can you add a regression test for this? The VF token clobbering can be reproduced in vfio_pci_sriov_uapi_test: diff --git a/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c b/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c index 19d657d00b75..de8408b90a25 100644 --- a/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c +++ b/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c @@ -157,6 +157,42 @@ TEST_F(vfio_pci_sriov_uapi_test, override_token) ASSERT_COND_VF_CREATION(ret); } +TEST(failed_second_open_does_not_clobber_token) +{ + struct vfio_pci_device *pf = NULL, *pf_second_fd = NULL, *vf = NULL; + struct iommu *iommu; + int ret; + + iommu = iommu_init("iommufd"); + if (!iommu) + SKIP(return, "iommufd mode not supported"); + + /* Create and bind PF using UUID_1 */ + ret = device_init(pf_bdf, iommu, UUID_1, &pf); + ASSERT_EQ(ret, 0); + + /* + * Attempt to open the same PF again and bind it with a *different* + * token (UUID_2). This must fail with EBUSY because it's a second open. + */ + ret = device_init(pf_bdf, iommu, UUID_2, &pf_second_fd); + ASSERT_EQ(ret, -EBUSY); + + /* + * Attempt to initialize a VF using the original PF token (UUID_1). + * If the failed open above clobbered the PF's token (i.e. updated it to + * UUID_2), this VF initialization will fail. + */ + ret = device_init(vf_bdf, iommu, UUID_1, &vf); + ASSERT_EQ(ret, 0); + + device_cleanup(vf); + if (pf_second_fd) + device_cleanup(pf_second_fd); + device_cleanup(pf); + iommu_cleanup(iommu); +} + static void vf_teardown(void) { /*