[PATCH v9 02/13] virt: coco: tsm: Remove the device from lookup before PCI teardown

Aneesh Kumar K.V aneesh.kumar at kernel.org
Mon Nov 23 19:57:04 PST 2026


Xu Yilun <yilun.xu at linux.intel.com> writes:

> On Mon, Nov 16, 2026 at 01:49:24PM +0530, Aneesh Kumar K.V (Arm) wrote:
>> PCI connect looks up a TSM by ID under pci_tsm_rwsem. Previously,
>> pci_tsm_unregister() released that lock after tearing down PCI state
>> while the TSM was still in the class lookup. A racing connect could then
>> attach a new PCI context that the teardown would never see.
>> 
>> Remove the device from the class lookup first, then take the PCI write
>> lock to drain in-flight operations and destroy their contexts. Drop the
>> device reference only after PCI teardown completes.
>> 
>> Reviewed-by: Kevin Tian <kevin.tian at intel.com>
>> Cc: Alexey Kardashevskiy <aik at amd.com>
>> Cc: Jason Gunthorpe <jgg at ziepe.ca>
>> Cc: Lukas Wunner <lukas at wunner.de>
>> Cc: linux-coco at lists.linux.dev
>> Fixes: 3225f52cde56 ("PCI/TSM: Establish Secure Sessions and Link Encryption")
>> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar at kernel.org>
>> ---
>>  drivers/virt/coco/tsm-core.c | 20 +++++++++++++-------
>>  1 file changed, 13 insertions(+), 7 deletions(-)
>> 
>> diff --git a/drivers/virt/coco/tsm-core.c b/drivers/virt/coco/tsm-core.c
>> index 03894c5663f1..57db3f14cf46 100644
>> --- a/drivers/virt/coco/tsm-core.c
>> +++ b/drivers/virt/coco/tsm-core.c
>> @@ -56,23 +56,22 @@ static struct tsm_dev *alloc_tsm_dev(struct device *parent)
>>  	return no_free_ptr(tsm_dev);
>>  }
>>  
>> -static struct tsm_dev *tsm_register_pci_or_reset(struct tsm_dev *tsm_dev,
>> -						 struct pci_tsm_ops *pci_ops)
>> +static int __tsm_register(struct tsm_dev *tsm_dev, struct pci_tsm_ops *pci_ops)
>>  {
>>  	int rc;
>>  
>>  	tsm_dev->pci_ops = pci_ops;
>>  	rc = pci_tsm_register(tsm_dev);
>>  	if (rc) {
>> +		tsm_dev->pci_ops = NULL;
>
> Previous comment shows this can't solve any racing connect problem.
> And with your next patch, the racing connect is solved. So I think
> remove it.
>


I don't follow. This only undoes the changes made before
pci_tsm_register() on the error path.

>
>>  		dev_err(tsm_dev->dev.parent,
>>  			"PCI/TSM registration failure: %d\n", rc);
>> -		device_unregister(&tsm_dev->dev);
>> -		return ERR_PTR(rc);
>> +		return rc;
>>  	}
>>  
>>  	/* Notify TSM userspace that PCI/TSM operations are now possible */
>>  	kobject_uevent(&tsm_dev->dev.kobj, KOBJ_CHANGE);
>> -	return tsm_dev;
>> +	return 0;
>
> I really don't think we need this wrapper. In next patch we move uevent
> out, so this wrapper is just:
>
> 	tsm_dev->pci_ops = pci_ops;
> 	pci_tsm_register();
>
> Open code it, as Alexey and I suggest before. All kinds of xxx_tsm_register()
> are really confusing.
>

I do that in the next patch, after moving the kobject_uevent() call out
of the helper.

>
>>  }
>>  
>>  struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *pci_ops)
>> @@ -96,15 +95,22 @@ struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *pci_ops)
>>  	if (rc)
>>  		return ERR_PTR(rc);
>>  
>> -	return tsm_register_pci_or_reset(no_free_ptr(tsm_dev), pci_ops);
>> +	rc = __tsm_register(tsm_dev, pci_ops);
>> +	if (rc) {
>> +		device_del(dev);
>> +		return ERR_PTR(rc);
>> +	}
>> +	return no_free_ptr(tsm_dev);
>
> I think none of the above changes belongs to this patch. They don't
> solve any problem mentioned in the changelog.
>
> They belong to the next patch.
>


We are replacing device_unregister() with device_del() followed by
put_device(), so the error path should follow the same pattern.

>
>>  }
>>  EXPORT_SYMBOL_GPL(tsm_register);
>>  
>>  void tsm_unregister(struct tsm_dev *tsm_dev)
>>  {
>> +	/* Remove the class lookup first. */
>> +	device_del(&tsm_dev->dev);
>>  	if (tsm_dev->pci_ops)
>
> The patch 1 rejects NULL pci_ops, so is this check still needed? Can we
> remove this check in your Patch 1?
>


Sure, I will remove the check in the next revision.

>
>>  		pci_tsm_unregister(tsm_dev);
>> -	device_unregister(&tsm_dev->dev);
>> +	put_device(&tsm_dev->dev);
>>  }
>>  EXPORT_SYMBOL_GPL(tsm_unregister);
>>  

-aneesh



More information about the linux-arm-kernel mailing list