[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 04/10] xen/arm: ffa: rework SPMC RX/TX buffer management


  • To: Jens Wiklander <jens.wiklander@xxxxxxxxxx>
  • From: Bertrand Marquis <Bertrand.Marquis@xxxxxxx>
  • Date: Wed, 3 Dec 2025 12:15:06 +0000
  • Accept-language: en-GB, en-US
  • Arc-authentication-results: i=2; mx.microsoft.com 1; spf=pass (sender ip is 4.158.2.129) smtp.rcpttodomain=linaro.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=pass (signature was verified) header.d=arm.com; arc=pass (0 oda=1 ltdi=1 spf=[1,1,smtp.mailfrom=arm.com] dkim=[1,1,header.d=arm.com] dmarc=[1,1,header.from=arm.com])
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none
  • Arc-message-signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=VB7UskeVf7o5DRGGKZLrZbXEy/iwTjAcQ0qV6s8fwfI=; b=OHRU/5bJnSdyr2eE1WvUQ/GkuxIozN+VU9Eg8Ebb3LWwYtyDqPFR678EGTY2rEMmdUuSchzVa2gsGWeF6TkfiFxYusVbzyhRrH8zll2NkbhSW8Af8muWP/Q7r1vamFe9+hPQS2Ux8LV1jjQ8TcQSGcPT51PZpe5ge4k96g3jxX3fiRSKq8TruU3Fv6jAq9BVR+B7zN+EoauxZsYSRvpyRlmpncB9hvi9XKhXPMoIP/MnvuvN4dSz28HW9ZcmDqvdBC69bSBMXjQKtHS2Hfl9vZtHqRk3XxMt/MspwBXQZ+DLiKpFO6bftWmqEUh6F75Mx602U5sRtBtqx6s6apowWg==
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=VB7UskeVf7o5DRGGKZLrZbXEy/iwTjAcQ0qV6s8fwfI=; b=Jeve7LKw0+xtFxQ6fqzRysUszS0MvR9NZXcKt21X+JqDgnFRjf7hpuoWQ02hnpGtJLypZq2tY8UJjI4kTq6ra3Qday29UZUdydFUo2yS/ZFAXjuku0uE5tl61wr92ips720Tf9rhRHMiDagVq4HfK7Vus0u9+9OrJ8RQEVDwu41xU77JaSil93ruTJ5nn5BfG6Ctw8doXVp4P9FcKMpbTJUXwDSGkzI3KVWN3BCYRj/IkqOwQ9Bs0zSTxxJbCH6nU1sBFwAFosRPHvw7wttKOdrmOWvk4+Bx9xi6ph1o+w54kc7Bz+8I9hqUvjK+EC2JxY1dKB2csR+Tvtk+OENgcQ==
  • Arc-seal: i=2; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=pass; b=P9bd6RMf6CJWWTb/K3WSObgs/u4nBU/sfaNGpwqq+/pBqeBaNPPF4+CA0LUm/u77gTzP2X1EcDJQqQG1+LXPO6Zwv1teVmBj9yXxyPmQTQyU9Gw2aGBGn9Oa5pDbrnF3T8y2bUpIf+5/L2vouqaSPZlk3P7UO2OvF7P+KMaa2zyvbqUvlqvopZSiKheK9zd+bTCgH/k/TyJqXYe3byPYKs58NQHfUYjm44p28HVBzYfbddLq+ewAgsMThihaMUhPH+pMwroyRDXlzOUfqTFdujNNya6wvXojCQl+8801NgSFOQSEThZPmDo9AaZqdIALUUFOO18MQQvEutMgFwUvYw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=MSaP4j6V2Q6cbrP+eRYD6Hgh1LnqJ4YSyUOvzKoFYXNZy7D+VWlXDEuNJYz+9pVtEV6+/k/jiycdxxIpq+YnQDFElqvRTanp44/MHdDTJSgHMuHQnBmSTUIWB30wJOmPrqPJRnY6U4n1cJ1RB30gw0ONK5TOXQ9aUxt/Rex7WArv9QFGaRONDghW/a7x0+CdjOnfc2bA7oFV2YGLN6nPM8j7Fy/ygHEhGOzA88E05kG6l4C8N4ONDBqe+nD993YVmNE/L93vQf047lPPbJeocbYNRDvF+yr/Vs2Pv9jT5SPHc9wXeCSIKlousGWkaFt7MHhrwMxjGQ5QpgIfQTmv4Q==
  • Authentication-results-original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com;
  • Cc: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>, Volodymyr Babchuk <volodymyr_babchuk@xxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>
  • Delivery-date: Wed, 03 Dec 2025 12:16:24 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Nodisclaimer: true
  • Thread-index: AQHcX7XuY/AW54zALEuMn1h4iIrvHbUOavEAgAAsagCAAQzjgIAADJGAgAAQAoCAAByhgA==
  • Thread-topic: [PATCH 04/10] xen/arm: ffa: rework SPMC RX/TX buffer management

Hi Jens,

> On 3 Dec 2025, at 11:32, Jens Wiklander <jens.wiklander@xxxxxxxxxx> wrote:
> 
> Hi,
> 
> On Wed, Dec 3, 2025 at 10:36 AM Bertrand Marquis
> <Bertrand.Marquis@xxxxxxx> wrote:
>> 
>> Hi Jens,
>> 
>>> On 3 Dec 2025, at 09:50, Jens Wiklander <jens.wiklander@xxxxxxxxxx> wrote:
>>> 
>>> Hi Bertrand,
>>> 
>>> On Tue, Dec 2, 2025 at 5:50 PM Bertrand Marquis
>>> <Bertrand.Marquis@xxxxxxx> wrote:
>>>> 
>>>> Hi Jens,
>>>> 
>>>>> On 2 Dec 2025, at 15:08, Jens Wiklander <jens.wiklander@xxxxxxxxxx> wrote:
>>>>> 
>>>>> Hi Bertrand,
>>>>> 
>>>>> On Thu, Nov 27, 2025 at 4:52 PM Bertrand Marquis
>>>>> <bertrand.marquis@xxxxxxx> wrote:
>>>>>> 
>>>>>> Rework how Xen accesses the RX/TX buffers shared with the SPMC so that
>>>>>> ownership and locking are handled centrally.
>>>>>> 
>>>>>> Move the SPMC RX/TX buffer bases into ffa_rxtx.c as 
>>>>>> ffa_spmc_rx/ffa_spmc_tx,
>>>>>> protect them with dedicated ffa_spmc_{rx,tx}_lock spinlocks and expose
>>>>>> ffa_rxtx_spmc_{rx,tx}_{acquire,release}() helpers instead of the global
>>>>>> ffa_rx/ffa_tx pointers and ffa_{rx,tx}_buffer_lock.
>>>>>> 
>>>>>> The RX helpers now always issue FFA_RX_RELEASE when we are done
>>>>>> consuming data from the SPMC, so partition-info enumeration and shared
>>>>>> memory paths release the RX buffer on all exit paths. The RX/TX mapping
>>>>>> code is updated to use the descriptor offsets (rx_region_offs and
>>>>>> tx_region_offs) rather than hard-coded structure layout, and to use the
>>>>>> TX acquire/release helpers instead of touching the TX buffer directly.
>>>>>> 
>>>>>> Signed-off-by: Bertrand Marquis <bertrand.marquis@xxxxxxx>
>>>>>> ---
>>>>>> xen/arch/arm/tee/ffa.c          |  22 +-----
>>>>>> xen/arch/arm/tee/ffa_partinfo.c |  40 +++++-----
>>>>>> xen/arch/arm/tee/ffa_private.h  |  18 ++---
>>>>>> xen/arch/arm/tee/ffa_rxtx.c     | 132 +++++++++++++++++++++++++-------
>>>>>> xen/arch/arm/tee/ffa_shm.c      |  26 ++++---
>>> 
>>> [snip]
>>> 
>>>>>> 
>>>>>> -void ffa_rxtx_destroy(void)
>>>>>> +void *ffa_rxtx_spmc_rx_acquire(void)
>>>>>> +{
>>>>>> +    ASSERT(!spin_is_locked(&ffa_spmc_rx_lock));
>>>>> 
>>>>> Is it invalid for two CPUs to concurrently try to acquire the RX buffer?
>>>> 
>>>> No the RX buffer or the TX buffer with the SPMC should only be used by
>>>> one CPU at a time as there cannot be any concurrent operations using it.
>>> 
>>> What if two guests call FFA_PARTITION_INFO_GET concurrently? Both can
>>> succeed in acquiring their RX buffer so they can call
>>> ffa_get_sp_partinfo() concurrently, and the assert might be triggered.
>>> We have a similar problem with FFA_RXTX_MAP_64 and
>>> ffa_rxtx_spmc_tx_acquire(). Contention on the spinlocks for the rx and
>>> tx buffers should be valid. If we can't allow a guest to block here,
>>> we should return an error and let the guest retry.
>> 
>> i am not sure i am following anymore.
>> The assert is just there to ensure that the lock is not already taken.
> 
> But it could already be taken by another CPU.
> 
>> The function is then taking the lock and not releasing it until release
>> is called which is ensuring that only one vcpu at a time is using the
>> rx buffer. Did i miss something here ?
> 
> Only one CPU at a time should use the spmc rx buffer, but others might
> try and should be blocked in spin_lock() rather than ASSERT.
> 
>> 
>> for rxtx map we do call tx_acquire so we lock the buffer.
>> 
>> Now we might have a race condition between in rxtx_map and unmap
>> where i should take the rx_lock and the tx_lock of the guest to prevent
>> concurrent usage of the vm rxtx buffer. I will fix that one.
> 
> Yes, you're right, good catch.
> 
>> 
>> Please tell me for the spmc rxtx buffers as i am not sure i am following
>> what you mean there :-)
> 
> Each guest has its own rxtx buffer, so the spinlock here is just in
> case the guest didn't synchronize its CPUs before calling. But the
> SPMC rxtx buffers are for Xen, so a guest can't be sure that no other
> guest is holding the spinlock.

In fact i just saw why this is wrong. I must not unlock at the end of 
spmc_rx/tx_acquire so that only one at a time is taking the rxtx buffers
with the spmc and they become available only when current user is done.

> 
> Two guests can independently call FFA_RXTX_MAP_64 and then call
> ffa_rxtx_spmc_tx_acquire() more or less at the same time.
> 
> I you remove the "ASSERT(!spin_is_locked(...));" from
> ffa_rxtx_spmc_tx_acquire() and ffa_rxtx_spmc_rx_acquire() it should be
> OK.

My current conclusion was that i need to remove the unlock in acquire so
that i spin_lock in acquire and spin_unlock in release.

In fact to prevent issues with rxtx vm buffers, I will add tx_acquire and 
tx_release
functions for the vm tx buffer and give the buffer address and size as return to
the caller.
This will prevent any access to tx or page_count outside of a locked section and
remove potential race conditions.

I will rework this patch and the next one to end up with a cleaner handling of 
vm
and spmc rxtx buffers.

Cheers
Bertrand


> 
> Cheers,
> Jens



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.