[PATCH v10 10/18] PCI/TSM: Add helpers to prevent disconnect while in use

Aneesh Kumar K.V aneesh.kumar at kernel.org
Thu Dec 3 00:23:22 PST 2026


Baolu Lu <baolu.lu at linux.intel.com> writes:

> On 11/25/2026 4:11 PM, Aneesh Kumar K.V (Arm) wrote:
>> Add PCI TEE Security Manager (TSM) helpers that prevent PF0 disconnect
>> while a function is in use. Validate the function's TSM association and
>> increment PF0's dsm_context_users count on get. Find PF0 through the
>> same function and decrement the count on put. Reject disconnect with
>> -EBUSY until all users have released it.
>> 
>> The caller must prevent removal of the function between get and put, for
>> example by keeping its VFIO device open. No per-call allocation is
>> needed.
>> 
>> Cc: Bjorn Helgaas <bhelgaas at google.com>
>> Cc: linux-pci at vger.kernel.org
>> Cc: Alexey Kardashevskiy <aik at amd.com>
>> Cc: Jason Gunthorpe <jgg at ziepe.ca>
>> Cc: Lukas Wunner <lukas at wunner.de>
>> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar at kernel.org>
>> ---
>>   Documentation/ABI/testing/sysfs-bus-pci |  1 +
>>   drivers/pci/tsm.c                       | 79 ++++++++++++++++++++++---
>>   include/linux/pci-tsm.h                 | 14 +++++
>>   3 files changed, 85 insertions(+), 9 deletions(-)
>> 
>> diff --git a/Documentation/ABI/testing/sysfs-bus-pci b/Documentation/ABI/testing/sysfs-bus-pci
>> index be9126e56894..2906257eb610 100644
>> --- a/Documentation/ABI/testing/sysfs-bus-pci
>> +++ b/Documentation/ABI/testing/sysfs-bus-pci
>> @@ -660,6 +660,7 @@ Contact:	linux-coco at lists.linux.dev
>>   Description:
>>   		(WO) Write the name of the TSM device that was specified
>>   		to 'connect' to teardown the connection.
>> +		The write fails with EBUSY if the TSM is in use.
>
> "In use" is vague for an ABI document. Something like "while any
> function managed by this DSM is assigned to a TVM" would tell userspace
> what to undo before retrying.
>

Sure, I'll update the ABI documentation.

>
>>   
>>   What:		/sys/bus/pci/devices/.../tsm/dsm
>>   Contact:	linux-coco at lists.linux.dev
>> diff --git a/drivers/pci/tsm.c b/drivers/pci/tsm.c
>> index 1423c64dc9c8..73f16a9a78ef 100644
>> --- a/drivers/pci/tsm.c
>> +++ b/drivers/pci/tsm.c
>> @@ -292,6 +292,51 @@ static int remove_fn(struct pci_dev *pdev, void *data)

 [ ... 109 lines skipped ... ] 

>>   
>> -	if (is_link_tsm(tsm_dev) && is_pci_tsm_pf0(pdev))
>> -		pci_tsm_disconnect(pdev);
>> -	else
>> +	if (is_link_tsm(tsm_dev) && is_pci_tsm_pf0(pdev)) {
>> +		if (pci_tsm_disconnect(pdev))
>> +			pci_warn(pdev, "TSM connection is still in use\n");
>
> This will cause leaks and use-after-free. A warning is not a good
> solution for such BUSY case in a cannot-fail path.
>

The full series makes both the destroy and disconnect paths wait,
similar to how VFIO makes PCI device destruction wait. The final two
patches implement this.

>
>> +	} else {
>>   		pci_tsm_fn_exit(pdev);
>> +	}
>>   }
>>   

-aneesh



More information about the linux-arm-kernel mailing list