Skip to contentSkip to conversation

xen/arm: ffa: Harden notifications and enable VM-to-VM delivery

PATCH Superseded Bertrand Marquis 2 participants 21 messages Apr 17 – Apr 23

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover xen/arm: ffa: Harden notifications and enable VM-to-VM delivery

This series hardens FF-A notification handling in the Arm FF-A mediator and completes local delivery for non-secure VM-to-VM notifications.

Hardening and state handling (Patches 1-4):
1) Fix notification pending interrupt delivery when vcpu0 is offline by reusing a common global NPI injection helper.
2) Replace the single hypervisor notification boolean with a protected HYP bitmap and keep bitmap lifecycle tied to the cached endpoint ID.
3) Tighten notification parameter validation so malformed BIND, UNBIND, GET, and SET requests are rejected consistently before reaching cached state or the SPMC.
4) Preserve the secure pending indication until secure notifications are retrieved, protect the secure pending latch with notif_lock, serialize SPMC INFO_GET polling, and keep INFO_GET return width consistent with the caller.

Local VM notification delivery (Patches 5-6):
1) Track non-secure VM notification bindings locally, promote pending state to a per-bit bitmap, and validate BIND/UNBIND requests against that state.
2) Deliver non-secure VM-to-VM notifications locally, track whether a local NPI is already armed, and only advertise notification support when firmware capabilities or CONFIG_FFA_VM_TO_VM actually provide it.

Backward compatibility: v1.0/v1.1 guests remain compatible. Valid guest-visible notification behavior is preserved; the series only tightens malformed-request handling and enables local non-secure VM-to-VM delivery when CONFIG_FFA_VM_TO_VM is enabled.

Gitlab branch with patches:
https://gitlab.com/xen-project/people/bmarquis/xen-ffa/-/tree/vm-notif/v1?ref_type=heads
CI pass result:
https://gitlab.com/xen-project/people/bmarquis/xen-ffa/-/pipelines/2460589353

Bertrand Marquis (6):
xen/arm: ffa: Fix NPI injection when vcpu0 is offline xen/arm: ffa: Track hypervisor notifications in a bitmap xen/arm: ffa: Tighten notification parameter validation xen/arm: ffa: Preserve secure notification state when polling SPMC xen/arm: ffa: Track VM notification bindings locally xen/arm: ffa: Deliver VM-to-VM notifications locally

 xen/arch/arm/tee/ffa.c         |  24 +-
 xen/arch/arm/tee/ffa_notif.c   | 407 +++++++++++++++++++++++++++------
 xen/arch/arm/tee/ffa_private.h |  29 ++-
 3 files changed, 385 insertions(+), 75 deletions(-)
Signature · 1 line
2.53.0
#3 lore raw

PATCH 2/6 xen/arm: ffa: Track hypervisor notifications in a bitmap

Hypervisor notifications are currently tracked with a dedicated buff_full_pending boolean. That state only represents a single HYP notification bit and keeps HYP bitmap handling tied to single-purpose bookkeeping.

Replace the boolean with a hypervisor notification bitmap protected by notif_lock. INFO_GET reports pending when the bitmap is non-zero, GET returns and clears the HYP bitmap under the lock, and RX-buffer-full sets FFA_NOTIF_RX_BUFFER_FULL in the bitmap instead of updating separate state.

Initialize and clear the bitmap during domain lifecycle handling, and use ctx->ffa_id for bitmap create and destroy so the notification state stays tied to the cached FF-A endpoint ID.

No functional changes.

---
 xen/arch/arm/tee/ffa_notif.c   | 46 ++++++++++++++++++++++++++--------
 xen/arch/arm/tee/ffa_private.h |  9 +++++--
 2 files changed, 43 insertions(+), 12 deletions(-)
xen/arch/arm/tee/ffa_notif.c +36 -10
index 07bc5cb3a430..d15119409a25 100644
@@ -94,8 +94,15 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 
     notif_pending = test_and_clear_bool(ctx->notif.secure_pending);
     if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+    {
         notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
 
+        spin_lock(&ctx->notif.notif_lock);
+        if ( ctx->notif.hyp_pending )
+            notif_pending = true;
+        spin_unlock(&ctx->notif.notif_lock);
+    }
+
     if ( notif_pending )
     {
         /* A pending global notification for the guest */
@@ -174,12 +181,17 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
             w6 = resp.a6;
     }
 
-    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) &&
-          flags & FFA_NOTIF_FLAG_BITMAP_HYP &&
-          test_and_clear_bool(ctx->notif.buff_full_pending) )
+    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
-        ACCESS_ONCE(ctx->notif.vm_pending) = false;
-        w7 = FFA_NOTIF_RX_BUFFER_FULL;
+        spin_lock(&ctx->notif.notif_lock);
+
+        if ( (flags & FFA_NOTIF_FLAG_BITMAP_HYP) && ctx->notif.hyp_pending )
+        {
+            w7 = ctx->notif.hyp_pending;
+            ctx->notif.hyp_pending = 0;
+        }
+
+        spin_unlock(&ctx->notif.notif_lock);
     }
 
     ffa_set_regs(regs, FFA_SUCCESS_32, 0, w2, w3, w4, w5, w6, w7);
@@ -207,12 +219,17 @@ int32_t ffa_handle_notification_set(struct cpu_user_regs *regs)
 void ffa_raise_rx_buffer_full(struct domain *d)
 {
     struct ffa_ctx *ctx = d->arch.tee;
+    uint32_t prev_bitmap;
 
     if ( !ctx )
         return;
 
-    ACCESS_ONCE(ctx->notif.buff_full_pending) = true;
-    if ( !test_and_set_bool(ctx->notif.vm_pending) )
+    spin_lock(&ctx->notif.notif_lock);
+    prev_bitmap = ctx->notif.hyp_pending;
+    ctx->notif.hyp_pending |= FFA_NOTIF_RX_BUFFER_FULL;
+    spin_unlock(&ctx->notif.notif_lock);
+
+    if ( !(prev_bitmap & FFA_NOTIF_RX_BUFFER_FULL) )
         inject_notif_pending(d);
 }
 #endif
@@ -426,12 +443,15 @@ void ffa_notif_init(void)
 
 int ffa_notif_domain_init(struct domain *d)
 {
+    struct ffa_ctx *ctx = d->arch.tee;
     int32_t res;
 
+    spin_lock_init(&ctx->notif.notif_lock);
+    ctx->notif.hyp_pending = 0;
+
     if ( fw_notif_enabled )
     {
-
-        res = ffa_notification_bitmap_create(ffa_get_vm_id(d), d->max_vcpus);
+        res = ffa_notification_bitmap_create(ctx->ffa_id, d->max_vcpus);
         if ( res )
             return -ENOMEM;
     }
@@ -441,10 +461,16 @@ int ffa_notif_domain_init(struct domain *d)
 
 void ffa_notif_domain_destroy(struct domain *d)
 {
+    struct ffa_ctx *ctx = d->arch.tee;
+
+    spin_lock(&ctx->notif.notif_lock);
+    ctx->notif.hyp_pending = 0;
+    spin_unlock(&ctx->notif.notif_lock);
+
     /*
      * Call bitmap_destroy even if bitmap create failed as the SPMC will
      * return a DENIED error that we will ignore.
      */
     if ( fw_notif_enabled )
-        ffa_notification_bitmap_destroy(ffa_get_vm_id(d));
+        ffa_notification_bitmap_destroy(ctx->ffa_id);
 }
xen/arch/arm/tee/ffa_private.h +7 -2
index c291f32b56ff..5693772481ed 100644
@@ -340,9 +340,14 @@ struct ffa_ctx_notif {
     bool vm_pending;
 
     /*
-     * True if domain has buffer full notification pending
+     * Lock protecting the hypervisor-managed notification state.
      */
-    bool buff_full_pending;
+    spinlock_t notif_lock;
+
+    /*
+     * Bitmap of pending hypervisor notifications (for HYP bitmap queries).
+     */
+    uint32_t hyp_pending;
 };
 
 struct ffa_ctx {
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/6 xen/arm: ffa: Fix NPI injection when vcpu0 is offline

In reply to Bertrand Marquis

RX-buffer-full notifications currently inject the notification pending interrupt through vcpu0 only. Secure notification delivery already walks the domain's online vCPUs, but the RX-buffer-full path does not. When vcpu0 is offline, the notification remains pending and the guest never receives it.

Extract the common notification injection path and reuse it from ffa_raise_rx_buffer_full(). The shared helper delivers the global notification to the first online vCPU and keeps the existing ratelimited debug message when none are online.

Functional impact: RX-buffer-full notifications are delivered even when vcpu0 is offline.

Fixes 3935c705688e ("xen/arm: ffa: Add buffer full notification support")
---
 xen/arch/arm/tee/ffa_notif.c | 45 ++++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 20 deletions(-)
xen/arch/arm/tee/ffa_notif.c +25 -20
index 186e72641237..07bc5cb3a430 100644
@@ -19,6 +19,29 @@
 static bool __ro_after_init fw_notif_enabled;
 static unsigned int __ro_after_init notif_sri_irq;
 
+static void inject_notif_pending(struct domain *d)
+{
+    struct vcpu *v;
+
+    /*
+     * Since we're only delivering global notification, always
+     * deliver to the first online vCPU. It doesn't matter
+     * which we chose, as long as it's available.
+     */
+    for_each_vcpu(d, v)
+    {
+        if ( is_vcpu_online(v) )
+        {
+            vgic_inject_irq(d, v, GUEST_FFA_NOTIF_PEND_INTR_ID, true);
+            return;
+        }
+    }
+
+    if ( printk_ratelimit() )
+        printk(XENLOG_G_DEBUG "%pd: ffa: can't inject NPI, all vCPUs offline\n",
+               d);
+}
+
 int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
@@ -190,7 +213,7 @@ void ffa_raise_rx_buffer_full(struct domain *d)
 
     ACCESS_ONCE(ctx->notif.buff_full_pending) = true;
     if ( !test_and_set_bool(ctx->notif.vm_pending) )
-        vgic_inject_irq(d, d->vcpu[0], GUEST_FFA_NOTIF_PEND_INTR_ID, true);
+        inject_notif_pending(d);
 }
 #endif
 
@@ -238,7 +261,6 @@ static void notif_vm_pend_intr(uint16_t vm_id)
 {
     struct ffa_ctx *ctx;
     struct domain *d;
-    struct vcpu *v;
 
     /*
      * vm_id == 0 means a notifications pending for Xen itself, but
@@ -277,24 +299,7 @@ static void notif_vm_pend_intr(uint16_t vm_id)
      * it.
      */
     ACCESS_ONCE(ctx->notif.secure_pending) = true;
-
-    /*
-     * Since we're only delivering global notification, always
-     * deliver to the first online vCPU. It doesn't matter
-     * which we chose, as long as it's available.
-     */
-    for_each_vcpu(d, v)
-    {
-        if ( is_vcpu_online(v) )
-        {
-            vgic_inject_irq(d, v, GUEST_FFA_NOTIF_PEND_INTR_ID,
-                            true);
-            break;
-        }
-    }
-    if ( !v && printk_ratelimit() )
-        printk(XENLOG_G_DEBUG "%pd: ffa: can't inject NPI, all vCPUs offline\n",
-               d);
+    inject_notif_pending(d);
 
 out_unlock:
     rcu_unlock_domain(d);
Signature · 1 line
2.53.0
#11 lore raw

PATCH 4/6 xen/arm: ffa: Preserve secure notification state when polling SPMC

In reply to Bertrand Marquis

Secure pending state is latched when the SPMC raises the schedule receiver interrupt, but Xen currently clears that latch too aggressively. Guest FFA_NOTIFICATION_INFO_GET consumes secure_pending even though it only reports pending state, and secure FFA_NOTIFICATION_GET only clears the latch when both SP and SPM bitmaps are requested together. This can drop a pending indication before the receiver retrieves secure notifications, or keep INFO_GET reporting stale secure pending state after a successful GET.

Keep secure_pending as a latched indication until secure notifications are actually retrieved. Guest FFA_NOTIFICATION_INFO_GET now reports the latched state without clearing it, while a successful secure FFA_NOTIFICATION_GET clears the latch regardless of which secure bitmap flags were requested. Also protect secure_pending with notif_lock, serialize SPMC INFO_GET polling behind notif_info_lock, and preserve the caller-visible INFO_GET success width.

Functional impact: guest INFO_GET preserves the secure pending indication until secure notifications are retrieved, and successful secure GET clears the guest-visible pending latch.

---
 xen/arch/arm/tee/ffa_notif.c | 54 +++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 19 deletions(-)
xen/arch/arm/tee/ffa_notif.c +35 -19
index 491db3b04df5..fff00ca2baec 100644
@@ -18,6 +18,7 @@
 
 static bool __ro_after_init fw_notif_enabled;
 static unsigned int __ro_after_init notif_sri_irq;
+static DEFINE_SPINLOCK(notif_info_lock);
 
 static void inject_notif_pending(struct domain *d)
 {
@@ -109,6 +110,7 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
+    uint32_t fid = get_user_reg(regs, 0);
     bool notif_pending;
 
     if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) && !fw_notif_enabled )
@@ -117,7 +119,10 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
         return;
     }
 
-    notif_pending = test_and_clear_bool(ctx->notif.secure_pending);
+    spin_lock(&ctx->notif.notif_lock);
+    notif_pending = ctx->notif.secure_pending;
+    spin_unlock(&ctx->notif.notif_lock);
+
     if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
         notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
@@ -131,7 +136,9 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
     if ( notif_pending )
     {
         /* A pending global notification for the guest */
-        ffa_set_regs(regs, FFA_SUCCESS_64, 0,
+        ffa_set_regs(regs,
+                     smccc_is_conv_64(fid) ? FFA_SUCCESS_64 : FFA_SUCCESS_32,
+                     0,
                      1U << FFA_NOTIF_INFO_GET_ID_COUNT_SHIFT, ffa_get_vm_id(d),
                      0, 0, 0, 0);
     }
@@ -154,6 +161,8 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
     uint32_t w5 = 0;
     uint32_t w6 = 0;
     uint32_t w7 = 0;
+    uint32_t secure_flags = flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
+                                      FFA_NOTIF_FLAG_BITMAP_SPM );
 
     if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) && !fw_notif_enabled )
     {
@@ -173,27 +182,16 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
         return;
     }
 
-    if ( fw_notif_enabled && (flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
-                                        FFA_NOTIF_FLAG_BITMAP_SPM )) )
+    if ( fw_notif_enabled && secure_flags )
     {
         struct arm_smccc_1_2_regs arg = {
             .a0 = FFA_NOTIFICATION_GET,
             .a1 = recv,
-            .a2 = flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
-                            FFA_NOTIF_FLAG_BITMAP_SPM ),
+            .a2 = secure_flags,
         };
         struct arm_smccc_1_2_regs resp;
         int32_t e;
 
-        /*
-         * Clear secure pending if both FFA_NOTIF_FLAG_BITMAP_SP and
-         * FFA_NOTIF_FLAG_BITMAP_SPM are set since secure world can't have
-         * any more pending notifications.
-         */
-        if ( ( flags  & FFA_NOTIF_FLAG_BITMAP_SP ) &&
-             ( flags & FFA_NOTIF_FLAG_BITMAP_SPM ) )
-            ACCESS_ONCE(ctx->notif.secure_pending) = false;
-
         arm_smccc_1_2_smc(&arg, &resp);
         e = ffa_get_ret_code(&resp);
         if ( e )
@@ -210,6 +208,10 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
 
         if ( flags & FFA_NOTIF_FLAG_BITMAP_SPM )
             w6 = resp.a6;
+
+        spin_lock(&ctx->notif.notif_lock);
+        ctx->notif.secure_pending = false;
+        spin_unlock(&ctx->notif.notif_lock);
     }
 
     if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
@@ -354,7 +356,10 @@ static void notif_vm_pend_intr(uint16_t vm_id)
      * guarantees that the data structure isn't freed while we're accessing
      * it.
      */
-    ACCESS_ONCE(ctx->notif.secure_pending) = true;
+    spin_lock(&ctx->notif.notif_lock);
+    ctx->notif.secure_pending = true;
+    spin_unlock(&ctx->notif.notif_lock);
+
     inject_notif_pending(d);
 
 out_unlock:
@@ -373,11 +378,18 @@ static void notif_sri_action(void *unused)
     unsigned int n;
     int32_t res;
 
-    do {
+    if ( !fw_notif_enabled )
+        return;
+
+    spin_lock(&notif_info_lock);
+
+    do
+    {
         arm_smccc_1_2_smc(&arg, &resp);
         res = ffa_get_ret_code(&resp);
         if ( res )
         {
+            spin_unlock(&notif_info_lock);
             if ( res != FFA_RET_NO_DATA && printk_ratelimit() )
                 printk(XENLOG_WARNING
                        "ffa: notification info get failed: error %d\n", res);
@@ -391,7 +403,7 @@ static void notif_sri_action(void *unused)
         id_pos = 0;
         for ( n = 0; n < list_count; n++ )
         {
-            unsigned int count = ((ids_count >> 2 * n) & 0x3) + 1;
+            unsigned int count = ((ids_count >> (2 * n)) & 0x3) + 1;
             uint16_t vm_id = get_id_from_resp(&resp, id_pos);
 
             notif_vm_pend_intr(vm_id);
@@ -399,7 +411,9 @@ static void notif_sri_action(void *unused)
             id_pos += count;
         }
 
-    } while (resp.a2 & FFA_NOTIF_INFO_GET_MORE_FLAG);
+    } while ( resp.a2 & FFA_NOTIF_INFO_GET_MORE_FLAG );
+
+    spin_unlock(&notif_info_lock);
 }
 
 static DECLARE_TASKLET(notif_sri_tasklet, notif_sri_action, NULL);
@@ -486,6 +500,7 @@ int ffa_notif_domain_init(struct domain *d)
     int32_t res;
 
     spin_lock_init(&ctx->notif.notif_lock);
+    ctx->notif.secure_pending = false;
     ctx->notif.hyp_pending = 0;
 
     if ( fw_notif_enabled )
@@ -503,6 +518,7 @@ void ffa_notif_domain_destroy(struct domain *d)
     struct ffa_ctx *ctx = d->arch.tee;
 
     spin_lock(&ctx->notif.notif_lock);
+    ctx->notif.secure_pending = false;
     ctx->notif.hyp_pending = 0;
     spin_unlock(&ctx->notif.notif_lock);
 
Signature · 1 line
2.53.0
#17 lore raw

PATCH 6/6 xen/arm: ffa: Deliver VM-to-VM notifications locally

In reply to Bertrand Marquis

VM notification binding and pending tracking exist for non-secure endpoints, but FFA_NOTIFICATION_SET still only forwards secure destinations to the SPMC. Non-secure VMs therefore cannot receive notifications from other VMs. Local NPI delivery also needs explicit re-arm tracking so repeated raises are not lost while the interrupt is already pending.

Add a local VM notification delivery path for non-secure destinations. notification_set_vm() resolves the destination endpoint, verifies that every requested bit is bound to the sender, sets the receiver's vm_pending bitmap under notif_lock, and raises an NPI only when the receiver transitions from no local pending notifications to some.

Track whether a local NPI is already armed with notif_irq_raised, clear that state once both VM and hypervisor pending bitmaps are drained, and roll back newly-added VM pending bits if no destination vCPU is online. Also expose firmware notification availability so FFA_FEATURES only advertises notification support when it is actually provided by the firmware or by CONFIG_FFA_VM_TO_VM.

Functional impact: when CONFIG_FFA_VM_TO_VM is enabled, non-secure FFA_NOTIFICATION_SET delivers VM-to-VM notifications locally and keeps NPI delivery reliable across repeated raises.

---
 xen/arch/arm/tee/ffa.c         |  24 +++++--
 xen/arch/arm/tee/ffa_notif.c   | 126 +++++++++++++++++++++++++++++++--
 xen/arch/arm/tee/ffa_private.h |  11 ++-
 3 files changed, 147 insertions(+), 14 deletions(-)
xen/arch/arm/tee/ffa.c +20 -4
index 1fe33f26454a..7fe021049cba 100644
@@ -39,8 +39,13 @@
  * o FFA_MSG_SEND_DIRECT_REQ:
  *   - only supported from a VM to an SP
  * o FFA_NOTIFICATION_*:
+ *   - only supported when firmware notifications are enabled or VM-to-VM
+ *     support is built in
  *   - only supports global notifications, that is, per vCPU notifications
- *     are not supported
+ *     are not supported and secure per-vCPU notification information is
+ *     not forwarded
+ *   - the source endpoint ID reported for a notification may no longer
+ *     exist by the time the receiver consumes it
  *   - doesn't support signalling the secondary scheduler of pending
  *     notification for secure partitions
  *   - doesn't support notifications for Xen itself
@@ -245,6 +250,8 @@ static void handle_features(struct cpu_user_regs *regs)
     uint32_t a1 = get_user_reg(regs, 1);
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
+    bool notif_supported = IS_ENABLED(CONFIG_FFA_VM_TO_VM) ||
+                           ffa_notif_fw_enabled();
 
     /*
      * FFA_FEATURES defines w2 as input properties only for specific
@@ -343,10 +350,16 @@ static void handle_features(struct cpu_user_regs *regs)
 
         break;
     case FFA_FEATURE_NOTIF_PEND_INTR:
-        ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         break;
     case FFA_FEATURE_SCHEDULE_RECV_INTR:
-        ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         break;
     case FFA_PARTITION_INFO_GET_REGS:
         if ( ACCESS_ONCE(ctx->guest_vers) >= FFA_VERSION_1_2 )
@@ -361,7 +374,10 @@ static void handle_features(struct cpu_user_regs *regs)
     case FFA_NOTIFICATION_SET:
     case FFA_NOTIFICATION_INFO_GET_32:
     case FFA_NOTIFICATION_INFO_GET_64:
-        ffa_set_regs_success(regs, 0, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         break;
     default:
         ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
xen/arch/arm/tee/ffa_notif.c +119 -7

194 lines not on this page · raw message

xen/arch/arm/tee/ffa_private.h +8 -3
index 6d83afb3d00a..5bb19bd11dd0 100644
@@ -344,13 +344,17 @@ struct ffa_ctx_notif {
     uint64_t vm_pending;
 
     /*
-     * Source endpoint bound to each VM notification ID (0 means unbound).
+     * Tracks whether an NPI has been raised for local pending notifications.
+     * Protected by notif_lock.
      */
-    uint16_t vm_bind[FFA_NUM_VM_NOTIF];
+    bool notif_irq_raised;
 
     /*
-     * Lock protecting the hypervisor-managed notification state.
+     * Source endpoint bound to each VM notification ID (0 means unbound).
      */
+    uint16_t vm_bind[FFA_NUM_VM_NOTIF];
+
+    /* Lock protecting local notification state. */
     spinlock_t notif_lock;
 
     /*
@@ -493,6 +497,7 @@ void ffa_notif_init(void);
 void ffa_notif_init_interrupt(void);
 int ffa_notif_domain_init(struct domain *d);
 void ffa_notif_domain_destroy(struct domain *d);
+bool ffa_notif_fw_enabled(void);
 
 int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs);
 int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs);
Signature · 1 line
2.53.0
#8 lore raw

PATCH 3/6 xen/arm: ffa: Tighten notification parameter validation

In reply to Bertrand Marquis

The notification handlers still validate overlapping subsets of their inputs. BIND, UNBIND, and SET each decode caller and destination IDs locally, GET still accepts a non-zero receiver vCPU ID and reserved flag bits, and SET still accepts non-zero NS-virtual flags. BIND also treats unsupported non-zero flag encodings as a supported-feature failure instead of as malformed input.

Add ffa_notif_parse_params() and use it to centralize the common caller/destination and non-zero bitmap checks for BIND, UNBIND, and SET. Also reject malformed GET and SET requests locally before touching cached state or forwarding anything to the SPMC. Keep BIND limited to global notifications and reject unsupported non-zero flag encodings with INVALID_PARAMETERS.

- add a shared parameter parser for notification caller/destination validation
- wire BIND and UNBIND through the shared parser and reject unsupported bind flag encodings with INVALID_PARAMETERS
- reject non-zero receiver vCPU and reserved flag bits in FFA_NOTIFICATION_GET
- reject non-zero flags in the NS-virtual FFA_NOTIFICATION_SET path

Functional impact: malformed notification requests are rejected consistently earlier in the mediator.

---
 xen/arch/arm/tee/ffa_notif.c | 61 +++++++++++++++++++++++++++++-------
 1 file changed, 50 insertions(+), 11 deletions(-)
xen/arch/arm/tee/ffa_notif.c +50 -11
index d15119409a25..491db3b04df5 100644
@@ -42,21 +42,40 @@ static void inject_notif_pending(struct domain *d)
                d);
 }
 
+static int32_t ffa_notif_parse_params(uint16_t dom_id, uint16_t caller_id,
+                                      uint16_t dest_id, uint32_t bitmap_lo,
+                                      uint32_t bitmap_hi)
+{
+    if ( caller_id != dom_id || dest_id == dom_id || !dest_id )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    if ( !bitmap_lo && !bitmap_hi )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    return FFA_RET_OK;
+}
+
 int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
+    struct ffa_ctx *ctx = d->arch.tee;
+    int32_t ret;
     uint32_t src_dst = get_user_reg(regs, 1);
     uint32_t flags = get_user_reg(regs, 2);
     uint32_t bitmap_lo = get_user_reg(regs, 3);
     uint32_t bitmap_hi = get_user_reg(regs, 4);
+    uint16_t caller_id = src_dst & GENMASK(15, 0);
+    uint16_t dest_id = src_dst >> 16;
 
-    if ( (src_dst & GENMASK(15, 0)) != ffa_get_vm_id(d) )
+    if ( flags )    /* Only global notifications are supported */
         return FFA_RET_INVALID_PARAMETERS;
 
-    if ( flags )    /* Only global notifications are supported */
-        return FFA_RET_DENIED;
+    ret = ffa_notif_parse_params(ctx->ffa_id, caller_id, dest_id, bitmap_lo,
+                                 bitmap_hi);
+    if ( ret )
+        return ret;
 
-    if ( FFA_ID_IS_SECURE(src_dst >> 16) && fw_notif_enabled )
+    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
         return ffa_simple_call(FFA_NOTIFICATION_BIND, src_dst, flags,
                                bitmap_lo, bitmap_hi);
 
@@ -66,16 +85,22 @@ int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
+    struct ffa_ctx *ctx = d->arch.tee;
+    int32_t ret;
     uint32_t src_dst = get_user_reg(regs, 1);
     uint32_t bitmap_lo = get_user_reg(regs, 3);
     uint32_t bitmap_hi = get_user_reg(regs, 4);
+    uint16_t caller_id = src_dst & GENMASK(15, 0);
+    uint16_t dest_id = src_dst >> 16;
 
-    if ( (src_dst & GENMASK(15, 0)) != ffa_get_vm_id(d) )
-        return FFA_RET_INVALID_PARAMETERS;
+    ret = ffa_notif_parse_params(ctx->ffa_id, caller_id, dest_id, bitmap_lo,
+                                 bitmap_hi);
+    if ( ret )
+        return ret;
 
-    if ( FFA_ID_IS_SECURE(src_dst >> 16) && fw_notif_enabled )
-        return  ffa_simple_call(FFA_NOTIFICATION_UNBIND, src_dst, 0, bitmap_lo,
-                                bitmap_hi);
+    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
+        return ffa_simple_call(FFA_NOTIFICATION_UNBIND, src_dst, 0, bitmap_lo,
+                               bitmap_hi);
 
     return FFA_RET_NOT_SUPPORTED;
 }
@@ -142,6 +167,12 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
         return;
     }
 
+    if ( recv >> 16 || (flags & GENMASK(31, 4)) )
+    {
+        ffa_set_regs_error(regs, FFA_RET_INVALID_PARAMETERS);
+        return;
+    }
+
     if ( fw_notif_enabled && (flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
                                         FFA_NOTIF_FLAG_BITMAP_SPM )) )
     {
@@ -204,11 +235,19 @@ int32_t ffa_handle_notification_set(struct cpu_user_regs *regs)
     uint32_t flags = get_user_reg(regs, 2);
     uint32_t bitmap_lo = get_user_reg(regs, 3);
     uint32_t bitmap_hi = get_user_reg(regs, 4);
+    uint16_t caller_id = src_dst >> 16;
+    uint16_t dest_id = src_dst & GENMASK(15, 0);
+    int32_t ret;
+
+    ret = ffa_notif_parse_params(ffa_get_vm_id(d), caller_id, dest_id,
+                                 bitmap_lo, bitmap_hi);
+    if ( ret )
+        return ret;
 
-    if ( (src_dst >> 16) != ffa_get_vm_id(d) )
+    if ( flags )
         return FFA_RET_INVALID_PARAMETERS;
 
-    if ( FFA_ID_IS_SECURE(src_dst & GENMASK(15, 0)) && fw_notif_enabled )
+    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
         return ffa_simple_call(FFA_NOTIFICATION_SET, src_dst, flags, bitmap_lo,
                                bitmap_hi);
 
Signature · 1 line
2.53.0
#14 lore raw

PATCH 5/6 xen/arm: ffa: Track VM notification bindings locally

In reply to Bertrand Marquis

VM-to-VM notifications need receiver-side bind state so Xen can validate which sender owns each notification bit. Non-secure BIND and UNBIND requests currently have no local state and cannot enforce that contract.

Add per-bit VM notification binding state to struct ffa_ctx_notif and use it to handle non-secure BIND and UNBIND requests when CONFIG_FFA_VM_TO_VM is enabled. The update helper validates the whole request under notif_lock before mutating anything, denies bind or unbind when a bit is pending, rejects rebinding to a different sender, and keeps rebinding to the same sender idempotent.

Promote vm_pending to a bitmap so the bind logic can reason per notification ID, use that bitmap directly when reporting pending state, and initialize and clear the new VM notification state during domain init and teardown.

Functional impact: when CONFIG_FFA_VM_TO_VM is enabled, Xen tracks VM notification bindings locally and validates non-secure bind and unbind requests against that state.

---
 xen/arch/arm/tee/ffa_notif.c   | 97 ++++++++++++++++++++++++++++++----
 xen/arch/arm/tee/ffa_private.h | 15 ++++--
 2 files changed, 99 insertions(+), 13 deletions(-)
xen/arch/arm/tee/ffa_notif.c +87 -10
index fff00ca2baec..4def701f0130 100644
@@ -56,6 +56,54 @@ static int32_t ffa_notif_parse_params(uint16_t dom_id, uint16_t caller_id,
     return FFA_RET_OK;
 }
 
+static int32_t ffa_notif_update_vm_binding(struct ffa_ctx *ctx,
+                                           uint16_t dest_id, uint64_t bitmap,
+                                           bool bind)
+{
+    unsigned int id;
+    int32_t ret = FFA_RET_OK;
+
+    spin_lock(&ctx->notif.notif_lock);
+
+    for ( id = 0; id < FFA_NUM_VM_NOTIF; id++ )
+    {
+        if ( !(bitmap & BIT(id, ULL)) )
+            continue;
+
+        if ( ctx->notif.vm_pending & BIT(id, ULL) )
+        {
+            ret = FFA_RET_DENIED;
+            goto out_unlock;
+        }
+
+        if ( bind )
+        {
+            if ( ctx->notif.vm_bind[id] != 0 &&
+                 ctx->notif.vm_bind[id] != dest_id )
+            {
+                ret = FFA_RET_DENIED;
+                goto out_unlock;
+            }
+        }
+        else if ( ctx->notif.vm_bind[id] != dest_id )
+        {
+            ret = FFA_RET_DENIED;
+            goto out_unlock;
+        }
+    }
+
+    for ( id = 0; id < FFA_NUM_VM_NOTIF; id++ )
+    {
+        if ( bitmap & BIT(id, ULL) )
+            ctx->notif.vm_bind[id] = bind ? dest_id : 0;
+    }
+
+out_unlock:
+    spin_unlock(&ctx->notif.notif_lock);
+
+    return ret;
+}
+
 int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
@@ -76,11 +124,21 @@ int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
     if ( ret )
         return ret;
 
-    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
-        return ffa_simple_call(FFA_NOTIFICATION_BIND, src_dst, flags,
-                               bitmap_lo, bitmap_hi);
+    if ( FFA_ID_IS_SECURE(dest_id) )
+    {
+        if ( fw_notif_enabled )
+            return ffa_simple_call(FFA_NOTIFICATION_BIND, src_dst, flags,
+                                   bitmap_lo, bitmap_hi);
 
-    return FFA_RET_NOT_SUPPORTED;
+        return FFA_RET_NOT_SUPPORTED;
+    }
+
+    if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+        return FFA_RET_NOT_SUPPORTED;
+
+    return ffa_notif_update_vm_binding(ctx, dest_id,
+                                       ((uint64_t)bitmap_hi << 32) | bitmap_lo,
+                                       true);
 }
 
 int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs)
@@ -99,11 +157,21 @@ int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs)
     if ( ret )
         return ret;
 
-    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
-        return ffa_simple_call(FFA_NOTIFICATION_UNBIND, src_dst, 0, bitmap_lo,
-                               bitmap_hi);
+    if ( FFA_ID_IS_SECURE(dest_id) )
+    {
+        if ( fw_notif_enabled )
+            return ffa_simple_call(FFA_NOTIFICATION_UNBIND, src_dst, 0,
+                                   bitmap_lo, bitmap_hi);
 
-    return FFA_RET_NOT_SUPPORTED;
+        return FFA_RET_NOT_SUPPORTED;
+    }
+
+    if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+        return FFA_RET_NOT_SUPPORTED;
+
+    return ffa_notif_update_vm_binding(ctx, dest_id,
+                                       ((uint64_t)bitmap_hi << 32) | bitmap_lo,
+                                       false);
 }
 
 void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
@@ -125,9 +193,10 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 
     if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
-        notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
-
         spin_lock(&ctx->notif.notif_lock);
+        if ( ctx->notif.vm_pending )
+            notif_pending = true;
+
         if ( ctx->notif.hyp_pending )
             notif_pending = true;
         spin_unlock(&ctx->notif.notif_lock);
@@ -497,10 +566,14 @@ void ffa_notif_init(void)
 int ffa_notif_domain_init(struct domain *d)
 {
     struct ffa_ctx *ctx = d->arch.tee;
+    unsigned int i;
     int32_t res;
 
     spin_lock_init(&ctx->notif.notif_lock);
     ctx->notif.secure_pending = false;
+    ctx->notif.vm_pending = 0;
+    for ( i = 0; i < FFA_NUM_VM_NOTIF; i++ )
+        ctx->notif.vm_bind[i] = 0;
     ctx->notif.hyp_pending = 0;
 
     if ( fw_notif_enabled )
@@ -516,9 +589,13 @@ int ffa_notif_domain_init(struct domain *d)
 void ffa_notif_domain_destroy(struct domain *d)
 {
     struct ffa_ctx *ctx = d->arch.tee;
+    unsigned int i;
 
     spin_lock(&ctx->notif.notif_lock);
     ctx->notif.secure_pending = false;
+    ctx->notif.vm_pending = 0;
+    for ( i = 0; i < FFA_NUM_VM_NOTIF; i++ )
+        ctx->notif.vm_bind[i] = 0;
     ctx->notif.hyp_pending = 0;
     spin_unlock(&ctx->notif.notif_lock);
 
xen/arch/arm/tee/ffa_private.h +12 -3
index 5693772481ed..6d83afb3d00a 100644
@@ -236,6 +236,11 @@
 #define FFA_NOTIF_INFO_GET_ID_COUNT_MASK    0x1F
 
 #define FFA_NOTIF_RX_BUFFER_FULL        BIT(0, U)
+#define FFA_NUM_VM_NOTIF                64U
+
+#if FFA_NUM_VM_NOTIF > 64
+#error "FFA_NUM_VM_NOTIF must be <= 64"
+#endif
 
 /* Feature IDs used with FFA_FEATURES */
 #define FFA_FEATURE_NOTIF_PEND_INTR     0x1U
@@ -334,10 +339,14 @@ struct ffa_ctx_notif {
     bool secure_pending;
 
     /*
-     * True if domain is reported by FFA_NOTIFICATION_INFO_GET to have
-     * pending notifications from VMs (including framework ones).
+     * Bitmap of pending notifications from VMs (including framework ones).
+     */
+    uint64_t vm_pending;
+
+    /*
+     * Source endpoint bound to each VM notification ID (0 means unbound).
      */
-    bool vm_pending;
+    uint16_t vm_bind[FFA_NUM_VM_NOTIF];
 
     /*
      * Lock protecting the hypervisor-managed notification state.
Signature · 1 line
2.53.0
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] xen/arm: ffa: Fix NPI injection when vcpu0 is offline

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 15 lines · Bertrand Marquis

RX-buffer-full notifications currently inject the notification pending interrupt through vcpu0 only. Secure notification delivery already walks the domain's online vCPUs, but the RX-buffer-full path does not. When vcpu0 is offline, the notification remains pending and the guest never receives it.

Extract the common notification injection path and reuse it from ffa_raise_rx_buffer_full(). The shared helper delivers the global notification to the first online vCPU and keeps the existing ratelimited debug message when none are online.

Functional impact: RX-buffer-full notifications are delivered even when vcpu0 is offline.

Quoted 6 lines
Fixes 3935c705688e ("xen/arm: ffa: Add buffer full notification support")
---
 xen/arch/arm/tee/ffa_notif.c | 45 ++++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 20 deletions(-)

Looks good:

Cheers,
Jens

Quoted 81 lines
xen/arch/arm/tee/ffa_notif.c +25 -21
index 186e72641237..07bc5cb3a430 100644
@@ -19,6 +19,29 @@
 static bool __ro_after_init fw_notif_enabled;
 static unsigned int __ro_after_init notif_sri_irq;
 
+static void inject_notif_pending(struct domain *d)
+{
+    struct vcpu *v;
+
+    /*
+     * Since we're only delivering global notification, always
+     * deliver to the first online vCPU. It doesn't matter
+     * which we chose, as long as it's available.
+     */
+    for_each_vcpu(d, v)
+    {
+        if ( is_vcpu_online(v) )
+        {
+            vgic_inject_irq(d, v, GUEST_FFA_NOTIF_PEND_INTR_ID, true);
+            return;
+        }
+    }
+
+    if ( printk_ratelimit() )
+        printk(XENLOG_G_DEBUG "%pd: ffa: can't inject NPI, all vCPUs offline\n",
+               d);
+}
+
 int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
@@ -190,7 +213,7 @@ void ffa_raise_rx_buffer_full(struct domain *d)
 
     ACCESS_ONCE(ctx->notif.buff_full_pending) = true;
     if ( !test_and_set_bool(ctx->notif.vm_pending) )
-        vgic_inject_irq(d, d->vcpu[0], GUEST_FFA_NOTIF_PEND_INTR_ID, true);
+        inject_notif_pending(d);
 }
 #endif
 
@@ -238,7 +261,6 @@ static void notif_vm_pend_intr(uint16_t vm_id)
 {
     struct ffa_ctx *ctx;
     struct domain *d;
-    struct vcpu *v;
 
     /*
      * vm_id == 0 means a notifications pending for Xen itself, but
@@ -277,24 +299,7 @@ static void notif_vm_pend_intr(uint16_t vm_id)
      * it.
      */
     ACCESS_ONCE(ctx->notif.secure_pending) = true;
-
-    /*
-     * Since we're only delivering global notification, always
-     * deliver to the first online vCPU. It doesn't matter
-     * which we chose, as long as it's available.
-     */
-    for_each_vcpu(d, v)
-    {
-        if ( is_vcpu_online(v) )
-        {
-            vgic_inject_irq(d, v, GUEST_FFA_NOTIF_PEND_INTR_ID,
-                            true);
-            break;
-        }
-    }
-    if ( !v && printk_ratelimit() )
-        printk(XENLOG_G_DEBUG "%pd: ffa: can't inject NPI, all vCPUs offline\n",
-               d);
+    inject_notif_pending(d);
 
 out_unlock:
     rcu_unlock_domain(d);
--

2.53.0

#4 lore raw

Re: [PATCH 2/6] xen/arm: ffa: Track hypervisor notifications in a bitmap

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 29 lines · Bertrand Marquis

Hypervisor notifications are currently tracked with a dedicated buff_full_pending boolean. That state only represents a single HYP notification bit and keeps HYP bitmap handling tied to single-purpose bookkeeping.

Replace the boolean with a hypervisor notification bitmap protected by notif_lock. INFO_GET reports pending when the bitmap is non-zero, GET returns and clears the HYP bitmap under the lock, and RX-buffer-full sets FFA_NOTIF_RX_BUFFER_FULL in the bitmap instead of updating separate state.

Initialize and clear the bitmap during domain lifecycle handling, and use ctx->ffa_id for bitmap create and destroy so the notification state stays tied to the cached FF-A endpoint ID.

No functional changes.

---
 xen/arch/arm/tee/ffa_notif.c   | 46 ++++++++++++++++++++++++++--------
 xen/arch/arm/tee/ffa_private.h |  9 +++++--
 2 files changed, 43 insertions(+), 12 deletions(-)
xen/arch/arm/tee/ffa_notif.c +0 -0
index 07bc5cb3a430..d15119409a25 100644

xen/arch/arm/tee/ffa_notif.c void ffa_handle_notification_info_get(struct cpu_user_regs *regs) · line 103

Quoted 10 lines
xen/arch/arm/tee/ffa_notif.c +5 -0
@@ -94,8 +94,15 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 
     notif_pending = test_and_clear_bool(ctx->notif.secure_pending);
     if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+    {
         notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
 
+        spin_lock(&ctx->notif.notif_lock);
+        if ( ctx->notif.hyp_pending )
+            notif_pending = true;
+        spin_unlock(&ctx->notif.notif_lock);

Isn't this a functional change? Before this patch, we didn't consider ctx->notif.buff_full_pending here. Am I missing something?

Quoted 35 lines
xen/arch/arm/tee/ffa_notif.c +13 -5
+    }
+
     if ( notif_pending )
     {
         /* A pending global notification for the guest */
@@ -174,12 +181,17 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
             w6 = resp.a6;
     }
 
-    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) &&
-          flags & FFA_NOTIF_FLAG_BITMAP_HYP &&
-          test_and_clear_bool(ctx->notif.buff_full_pending) )
+    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
-        ACCESS_ONCE(ctx->notif.vm_pending) = false;
-        w7 = FFA_NOTIF_RX_BUFFER_FULL;
+        spin_lock(&ctx->notif.notif_lock);
+
+        if ( (flags & FFA_NOTIF_FLAG_BITMAP_HYP) && ctx->notif.hyp_pending )
+        {
+            w7 = ctx->notif.hyp_pending;
+            ctx->notif.hyp_pending = 0;
+        }
+
+        spin_unlock(&ctx->notif.notif_lock);
     }
 
     ffa_set_regs(regs, FFA_SUCCESS_32, 0, w2, w3, w4, w5, w6, w7);
@@ -207,12 +219,17 @@ int32_t ffa_handle_notification_set(struct cpu_user_regs *regs)
 void ffa_raise_rx_buffer_full(struct domain *d)
 {
     struct ffa_ctx *ctx = d->arch.tee;
+    uint32_t prev_bitmap;
 
     if ( !ctx )

xen/arch/arm/tee/ffa_notif.c int32_t ffa_handle_notification_set(struct cpu_user_regs *regs) · line 232

Quoted 10 lines
xen/arch/arm/tee/ffa_notif.c +6 -2
         return;
 
-    ACCESS_ONCE(ctx->notif.buff_full_pending) = true;
-    if ( !test_and_set_bool(ctx->notif.vm_pending) )
+    spin_lock(&ctx->notif.notif_lock);
+    prev_bitmap = ctx->notif.hyp_pending;
+    ctx->notif.hyp_pending |= FFA_NOTIF_RX_BUFFER_FULL;
+    spin_unlock(&ctx->notif.notif_lock);
+
+    if ( !(prev_bitmap & FFA_NOTIF_RX_BUFFER_FULL) )

Do we need to check for FFA_NOTIF_RX_BUFFER_FULL? Isn't !prev_bitmap more accurate, if any other bit would ever be used in the bitmap?

Cheers,
Jens

Quoted 63 lines
xen/arch/arm/tee/ffa_notif.c +12 -3
         inject_notif_pending(d);
 }
 #endif
@@ -426,12 +443,15 @@ void ffa_notif_init(void)
 
 int ffa_notif_domain_init(struct domain *d)
 {
+    struct ffa_ctx *ctx = d->arch.tee;
     int32_t res;
 
+    spin_lock_init(&ctx->notif.notif_lock);
+    ctx->notif.hyp_pending = 0;
+
     if ( fw_notif_enabled )
     {
-
-        res = ffa_notification_bitmap_create(ffa_get_vm_id(d), d->max_vcpus);
+        res = ffa_notification_bitmap_create(ctx->ffa_id, d->max_vcpus);
         if ( res )
             return -ENOMEM;
     }
@@ -441,10 +461,16 @@ int ffa_notif_domain_init(struct domain *d)
 
 void ffa_notif_domain_destroy(struct domain *d)
 {
+    struct ffa_ctx *ctx = d->arch.tee;
+
+    spin_lock(&ctx->notif.notif_lock);
+    ctx->notif.hyp_pending = 0;
+    spin_unlock(&ctx->notif.notif_lock);
+
     /*
      * Call bitmap_destroy even if bitmap create failed as the SPMC will
      * return a DENIED error that we will ignore.
      */
     if ( fw_notif_enabled )
-        ffa_notification_bitmap_destroy(ffa_get_vm_id(d));
+        ffa_notification_bitmap_destroy(ctx->ffa_id);
 }
xen/arch/arm/tee/ffa_private.h +7 -3
index c291f32b56ff..5693772481ed 100644
@@ -340,9 +340,14 @@ struct ffa_ctx_notif {
     bool vm_pending;
 
     /*
-     * True if domain has buffer full notification pending
+     * Lock protecting the hypervisor-managed notification state.
      */
-    bool buff_full_pending;
+    spinlock_t notif_lock;
+
+    /*
+     * Bitmap of pending hypervisor notifications (for HYP bitmap queries).
+     */
+    uint32_t hyp_pending;
 };
 
 struct ffa_ctx {
--

2.53.0

Reviewed-by on v1 3/6
#9 lore raw

Re: [PATCH 3/6] xen/arm: ffa: Tighten notification parameter validation

In reply to Bertrand Marquis

Hi Bertand,

Quoted 36 lines · Bertrand Marquis

The notification handlers still validate overlapping subsets of their inputs. BIND, UNBIND, and SET each decode caller and destination IDs locally, GET still accepts a non-zero receiver vCPU ID and reserved flag bits, and SET still accepts non-zero NS-virtual flags. BIND also treats unsupported non-zero flag encodings as a supported-feature failure instead of as malformed input.

Add ffa_notif_parse_params() and use it to centralize the common caller/destination and non-zero bitmap checks for BIND, UNBIND, and SET. Also reject malformed GET and SET requests locally before touching cached state or forwarding anything to the SPMC. Keep BIND limited to global notifications and reject unsupported non-zero flag encodings with INVALID_PARAMETERS.

- add a shared parameter parser for notification caller/destination validation
- wire BIND and UNBIND through the shared parser and reject unsupported bind flag encodings with INVALID_PARAMETERS
- reject non-zero receiver vCPU and reserved flag bits in FFA_NOTIFICATION_GET
- reject non-zero flags in the NS-virtual FFA_NOTIFICATION_SET path

Functional impact: malformed notification requests are rejected consistently earlier in the mediator.

---
 xen/arch/arm/tee/ffa_notif.c | 61 +++++++++++++++++++++++++++++-------
 1 file changed, 50 insertions(+), 11 deletions(-)
xen/arch/arm/tee/ffa_notif.c +0 -0
index d15119409a25..491db3b04df5 100644

xen/arch/arm/tee/ffa_notif.c static void inject_notif_pending(struct domain *d) · line 47

Quoted 6 lines
xen/arch/arm/tee/ffa_notif.c +3 -0
@@ -42,21 +42,40 @@ static void inject_notif_pending(struct domain *d)
                d);
 }
 
+static int32_t ffa_notif_parse_params(uint16_t dom_id, uint16_t caller_id,
+                                      uint16_t dest_id, uint32_t bitmap_lo,
+                                      uint32_t bitmap_hi)

Nit: I would have picked ffa_notif_validate_params() or ffa_notif_check_params(), but that might be more a matter of taste. Anyway, looks good:

Cheers,
Jens

Quoted 104 lines
xen/arch/arm/tee/ffa_notif.c +47 -12
+{
+    if ( caller_id != dom_id || dest_id == dom_id || !dest_id )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    if ( !bitmap_lo && !bitmap_hi )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    return FFA_RET_OK;
+}
+
 int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
+    struct ffa_ctx *ctx = d->arch.tee;
+    int32_t ret;
     uint32_t src_dst = get_user_reg(regs, 1);
     uint32_t flags = get_user_reg(regs, 2);
     uint32_t bitmap_lo = get_user_reg(regs, 3);
     uint32_t bitmap_hi = get_user_reg(regs, 4);
+    uint16_t caller_id = src_dst & GENMASK(15, 0);
+    uint16_t dest_id = src_dst >> 16;
 
-    if ( (src_dst & GENMASK(15, 0)) != ffa_get_vm_id(d) )
+    if ( flags )    /* Only global notifications are supported */
         return FFA_RET_INVALID_PARAMETERS;
 
-    if ( flags )    /* Only global notifications are supported */
-        return FFA_RET_DENIED;
+    ret = ffa_notif_parse_params(ctx->ffa_id, caller_id, dest_id, bitmap_lo,
+                                 bitmap_hi);
+    if ( ret )
+        return ret;
 
-    if ( FFA_ID_IS_SECURE(src_dst >> 16) && fw_notif_enabled )
+    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
         return ffa_simple_call(FFA_NOTIFICATION_BIND, src_dst, flags,
                                bitmap_lo, bitmap_hi);
 
@@ -66,16 +85,22 @@ int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
+    struct ffa_ctx *ctx = d->arch.tee;
+    int32_t ret;
     uint32_t src_dst = get_user_reg(regs, 1);
     uint32_t bitmap_lo = get_user_reg(regs, 3);
     uint32_t bitmap_hi = get_user_reg(regs, 4);
+    uint16_t caller_id = src_dst & GENMASK(15, 0);
+    uint16_t dest_id = src_dst >> 16;
 
-    if ( (src_dst & GENMASK(15, 0)) != ffa_get_vm_id(d) )
-        return FFA_RET_INVALID_PARAMETERS;
+    ret = ffa_notif_parse_params(ctx->ffa_id, caller_id, dest_id, bitmap_lo,
+                                 bitmap_hi);
+    if ( ret )
+        return ret;
 
-    if ( FFA_ID_IS_SECURE(src_dst >> 16) && fw_notif_enabled )
-        return  ffa_simple_call(FFA_NOTIFICATION_UNBIND, src_dst, 0, bitmap_lo,
-                                bitmap_hi);
+    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
+        return ffa_simple_call(FFA_NOTIFICATION_UNBIND, src_dst, 0, bitmap_lo,
+                               bitmap_hi);
 
     return FFA_RET_NOT_SUPPORTED;
 }
@@ -142,6 +167,12 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
         return;
     }
 
+    if ( recv >> 16 || (flags & GENMASK(31, 4)) )
+    {
+        ffa_set_regs_error(regs, FFA_RET_INVALID_PARAMETERS);
+        return;
+    }
+
     if ( fw_notif_enabled && (flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
                                         FFA_NOTIF_FLAG_BITMAP_SPM )) )
     {
@@ -204,11 +235,19 @@ int32_t ffa_handle_notification_set(struct cpu_user_regs *regs)
     uint32_t flags = get_user_reg(regs, 2);
     uint32_t bitmap_lo = get_user_reg(regs, 3);
     uint32_t bitmap_hi = get_user_reg(regs, 4);
+    uint16_t caller_id = src_dst >> 16;
+    uint16_t dest_id = src_dst & GENMASK(15, 0);
+    int32_t ret;
+
+    ret = ffa_notif_parse_params(ffa_get_vm_id(d), caller_id, dest_id,
+                                 bitmap_lo, bitmap_hi);
+    if ( ret )
+        return ret;
 
-    if ( (src_dst >> 16) != ffa_get_vm_id(d) )
+    if ( flags )
         return FFA_RET_INVALID_PARAMETERS;
 
-    if ( FFA_ID_IS_SECURE(src_dst & GENMASK(15, 0)) && fw_notif_enabled )
+    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
         return ffa_simple_call(FFA_NOTIFICATION_SET, src_dst, flags, bitmap_lo,
                                bitmap_hi);
 
--

2.53.0

#12 lore raw

Re: [PATCH 4/6] xen/arm: ffa: Preserve secure notification state when polling SPMC

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 133 lines · Bertrand Marquis

Secure pending state is latched when the SPMC raises the schedule receiver interrupt, but Xen currently clears that latch too aggressively. Guest FFA_NOTIFICATION_INFO_GET consumes secure_pending even though it only reports pending state, and secure FFA_NOTIFICATION_GET only clears the latch when both SP and SPM bitmaps are requested together. This can drop a pending indication before the receiver retrieves secure notifications, or keep INFO_GET reporting stale secure pending state after a successful GET.

Keep secure_pending as a latched indication until secure notifications are actually retrieved. Guest FFA_NOTIFICATION_INFO_GET now reports the latched state without clearing it, while a successful secure FFA_NOTIFICATION_GET clears the latch regardless of which secure bitmap flags were requested. Also protect secure_pending with notif_lock, serialize SPMC INFO_GET polling behind notif_info_lock, and preserve the caller-visible INFO_GET success width.

Functional impact: guest INFO_GET preserves the secure pending indication until secure notifications are retrieved, and successful secure GET clears the guest-visible pending latch.

---
 xen/arch/arm/tee/ffa_notif.c | 54 +++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 19 deletions(-)
xen/arch/arm/tee/ffa_notif.c +21 -16
index 491db3b04df5..fff00ca2baec 100644
@@ -18,6 +18,7 @@
 
 static bool __ro_after_init fw_notif_enabled;
 static unsigned int __ro_after_init notif_sri_irq;
+static DEFINE_SPINLOCK(notif_info_lock);
 
 static void inject_notif_pending(struct domain *d)
 {
@@ -109,6 +110,7 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
+    uint32_t fid = get_user_reg(regs, 0);
     bool notif_pending;
 
     if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) && !fw_notif_enabled )
@@ -117,7 +119,10 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
         return;
     }
 
-    notif_pending = test_and_clear_bool(ctx->notif.secure_pending);
+    spin_lock(&ctx->notif.notif_lock);
+    notif_pending = ctx->notif.secure_pending;
+    spin_unlock(&ctx->notif.notif_lock);
+
     if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
         notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
@@ -131,7 +136,9 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
     if ( notif_pending )
     {
         /* A pending global notification for the guest */
-        ffa_set_regs(regs, FFA_SUCCESS_64, 0,
+        ffa_set_regs(regs,
+                     smccc_is_conv_64(fid) ? FFA_SUCCESS_64 : FFA_SUCCESS_32,
+                     0,
                      1U << FFA_NOTIF_INFO_GET_ID_COUNT_SHIFT, ffa_get_vm_id(d),
                      0, 0, 0, 0);
     }
@@ -154,6 +161,8 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
     uint32_t w5 = 0;
     uint32_t w6 = 0;
     uint32_t w7 = 0;
+    uint32_t secure_flags = flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
+                                      FFA_NOTIF_FLAG_BITMAP_SPM );
 
     if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) && !fw_notif_enabled )
     {
@@ -173,27 +182,16 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
         return;
     }
 
-    if ( fw_notif_enabled && (flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
-                                        FFA_NOTIF_FLAG_BITMAP_SPM )) )
+    if ( fw_notif_enabled && secure_flags )
     {
         struct arm_smccc_1_2_regs arg = {
             .a0 = FFA_NOTIFICATION_GET,
             .a1 = recv,
-            .a2 = flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
-                            FFA_NOTIF_FLAG_BITMAP_SPM ),
+            .a2 = secure_flags,
         };
         struct arm_smccc_1_2_regs resp;
         int32_t e;
 
-        /*
-         * Clear secure pending if both FFA_NOTIF_FLAG_BITMAP_SP and
-         * FFA_NOTIF_FLAG_BITMAP_SPM are set since secure world can't have
-         * any more pending notifications.
-         */
-        if ( ( flags  & FFA_NOTIF_FLAG_BITMAP_SP ) &&
-             ( flags & FFA_NOTIF_FLAG_BITMAP_SPM ) )
-            ACCESS_ONCE(ctx->notif.secure_pending) = false;
-
         arm_smccc_1_2_smc(&arg, &resp);
         e = ffa_get_ret_code(&resp);
         if ( e )
@@ -210,6 +208,10 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
 
         if ( flags & FFA_NOTIF_FLAG_BITMAP_SPM )
             w6 = resp.a6;
+
+        spin_lock(&ctx->notif.notif_lock);
+        ctx->notif.secure_pending = false;
+        spin_unlock(&ctx->notif.notif_lock);
     }
 
     if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
@@ -354,7 +356,10 @@ static void notif_vm_pend_intr(uint16_t vm_id)
      * guarantees that the data structure isn't freed while we're accessing
      * it.
      */
-    ACCESS_ONCE(ctx->notif.secure_pending) = true;
+    spin_lock(&ctx->notif.notif_lock);
+    ctx->notif.secure_pending = true;
+    spin_unlock(&ctx->notif.notif_lock);
+
     inject_notif_pending(d);
 
 out_unlock:

xen/arch/arm/tee/ffa_notif.c static void notif_sri_action(void *unused) · line 382

Quoted 6 lines
xen/arch/arm/tee/ffa_notif.c +2 -1
@@ -373,11 +378,18 @@ static void notif_sri_action(void *unused)
     unsigned int n;
     int32_t res;
 
-    do {
+    if ( !fw_notif_enabled )
+        return;

Can this ever happen? Am I missing something?

Cheers,
Jens

Quoted 52 lines
xen/arch/arm/tee/ffa_notif.c +12 -3
+
+    spin_lock(&notif_info_lock);
+
+    do
+    {
         arm_smccc_1_2_smc(&arg, &resp);
         res = ffa_get_ret_code(&resp);
         if ( res )
         {
+            spin_unlock(&notif_info_lock);
             if ( res != FFA_RET_NO_DATA && printk_ratelimit() )
                 printk(XENLOG_WARNING
                        "ffa: notification info get failed: error %d\n", res);
@@ -391,7 +403,7 @@ static void notif_sri_action(void *unused)
         id_pos = 0;
         for ( n = 0; n < list_count; n++ )
         {
-            unsigned int count = ((ids_count >> 2 * n) & 0x3) + 1;
+            unsigned int count = ((ids_count >> (2 * n)) & 0x3) + 1;
             uint16_t vm_id = get_id_from_resp(&resp, id_pos);
 
             notif_vm_pend_intr(vm_id);
@@ -399,7 +411,9 @@ static void notif_sri_action(void *unused)
             id_pos += count;
         }
 
-    } while (resp.a2 & FFA_NOTIF_INFO_GET_MORE_FLAG);
+    } while ( resp.a2 & FFA_NOTIF_INFO_GET_MORE_FLAG );
+
+    spin_unlock(&notif_info_lock);
 }
 
 static DECLARE_TASKLET(notif_sri_tasklet, notif_sri_action, NULL);
@@ -486,6 +500,7 @@ int ffa_notif_domain_init(struct domain *d)
     int32_t res;
 
     spin_lock_init(&ctx->notif.notif_lock);
+    ctx->notif.secure_pending = false;
     ctx->notif.hyp_pending = 0;
 
     if ( fw_notif_enabled )
@@ -503,6 +518,7 @@ void ffa_notif_domain_destroy(struct domain *d)
     struct ffa_ctx *ctx = d->arch.tee;
 
     spin_lock(&ctx->notif.notif_lock);
+    ctx->notif.secure_pending = false;
     ctx->notif.hyp_pending = 0;
     spin_unlock(&ctx->notif.notif_lock);
 
--

2.53.0

#15 lore raw

Re: [PATCH 5/6] xen/arm: ffa: Track VM notification bindings locally

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 167 lines · Bertrand Marquis

VM-to-VM notifications need receiver-side bind state so Xen can validate which sender owns each notification bit. Non-secure BIND and UNBIND requests currently have no local state and cannot enforce that contract.

Add per-bit VM notification binding state to struct ffa_ctx_notif and use it to handle non-secure BIND and UNBIND requests when CONFIG_FFA_VM_TO_VM is enabled. The update helper validates the whole request under notif_lock before mutating anything, denies bind or unbind when a bit is pending, rejects rebinding to a different sender, and keeps rebinding to the same sender idempotent.

Promote vm_pending to a bitmap so the bind logic can reason per notification ID, use that bitmap directly when reporting pending state, and initialize and clear the new VM notification state during domain init and teardown.

Functional impact: when CONFIG_FFA_VM_TO_VM is enabled, Xen tracks VM notification bindings locally and validates non-secure bind and unbind requests against that state.

---
 xen/arch/arm/tee/ffa_notif.c   | 97 ++++++++++++++++++++++++++++++----
 xen/arch/arm/tee/ffa_private.h | 15 ++++--
 2 files changed, 99 insertions(+), 13 deletions(-)
xen/arch/arm/tee/ffa_notif.c +83 -10
index fff00ca2baec..4def701f0130 100644
@@ -56,6 +56,54 @@ static int32_t ffa_notif_parse_params(uint16_t dom_id, uint16_t caller_id,
     return FFA_RET_OK;
 }
 
+static int32_t ffa_notif_update_vm_binding(struct ffa_ctx *ctx,
+                                           uint16_t dest_id, uint64_t bitmap,
+                                           bool bind)
+{
+    unsigned int id;
+    int32_t ret = FFA_RET_OK;
+
+    spin_lock(&ctx->notif.notif_lock);
+
+    for ( id = 0; id < FFA_NUM_VM_NOTIF; id++ )
+    {
+        if ( !(bitmap & BIT(id, ULL)) )
+            continue;
+
+        if ( ctx->notif.vm_pending & BIT(id, ULL) )
+        {
+            ret = FFA_RET_DENIED;
+            goto out_unlock;
+        }
+
+        if ( bind )
+        {
+            if ( ctx->notif.vm_bind[id] != 0 &&
+                 ctx->notif.vm_bind[id] != dest_id )
+            {
+                ret = FFA_RET_DENIED;
+                goto out_unlock;
+            }
+        }
+        else if ( ctx->notif.vm_bind[id] != dest_id )
+        {
+            ret = FFA_RET_DENIED;
+            goto out_unlock;
+        }
+    }
+
+    for ( id = 0; id < FFA_NUM_VM_NOTIF; id++ )
+    {
+        if ( bitmap & BIT(id, ULL) )
+            ctx->notif.vm_bind[id] = bind ? dest_id : 0;
+    }
+
+out_unlock:
+    spin_unlock(&ctx->notif.notif_lock);
+
+    return ret;
+}
+
 int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
@@ -76,11 +124,21 @@ int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
     if ( ret )
         return ret;
 
-    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
-        return ffa_simple_call(FFA_NOTIFICATION_BIND, src_dst, flags,
-                               bitmap_lo, bitmap_hi);
+    if ( FFA_ID_IS_SECURE(dest_id) )
+    {
+        if ( fw_notif_enabled )
+            return ffa_simple_call(FFA_NOTIFICATION_BIND, src_dst, flags,
+                                   bitmap_lo, bitmap_hi);
 
-    return FFA_RET_NOT_SUPPORTED;
+        return FFA_RET_NOT_SUPPORTED;
+    }
+
+    if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+        return FFA_RET_NOT_SUPPORTED;
+
+    return ffa_notif_update_vm_binding(ctx, dest_id,
+                                       ((uint64_t)bitmap_hi << 32) | bitmap_lo,
+                                       true);
 }
 
 int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs)
@@ -99,11 +157,21 @@ int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs)
     if ( ret )
         return ret;
 
-    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
-        return ffa_simple_call(FFA_NOTIFICATION_UNBIND, src_dst, 0, bitmap_lo,
-                               bitmap_hi);
+    if ( FFA_ID_IS_SECURE(dest_id) )
+    {
+        if ( fw_notif_enabled )
+            return ffa_simple_call(FFA_NOTIFICATION_UNBIND, src_dst, 0,
+                                   bitmap_lo, bitmap_hi);
 
-    return FFA_RET_NOT_SUPPORTED;
+        return FFA_RET_NOT_SUPPORTED;
+    }
+
+    if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+        return FFA_RET_NOT_SUPPORTED;
+
+    return ffa_notif_update_vm_binding(ctx, dest_id,
+                                       ((uint64_t)bitmap_hi << 32) | bitmap_lo,
+                                       false);
 }
 
 void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
@@ -125,9 +193,10 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 
     if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
-        notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
-
         spin_lock(&ctx->notif.notif_lock);
+        if ( ctx->notif.vm_pending )
+            notif_pending = true;
+
         if ( ctx->notif.hyp_pending )
             notif_pending = true;
         spin_unlock(&ctx->notif.notif_lock);
@@ -497,10 +566,14 @@ void ffa_notif_init(void)
 int ffa_notif_domain_init(struct domain *d)
 {
     struct ffa_ctx *ctx = d->arch.tee;
+    unsigned int i;
     int32_t res;
 
     spin_lock_init(&ctx->notif.notif_lock);
     ctx->notif.secure_pending = false;
+    ctx->notif.vm_pending = 0;
+    for ( i = 0; i < FFA_NUM_VM_NOTIF; i++ )
+        ctx->notif.vm_bind[i] = 0;
     ctx->notif.hyp_pending = 0;
 
     if ( fw_notif_enabled )

xen/arch/arm/tee/ffa_notif.c int ffa_notif_domain_init(struct domain *d) · line 598

Quoted 10 lines
xen/arch/arm/tee/ffa_notif.c +4 -0
@@ -516,9 +589,13 @@ int ffa_notif_domain_init(struct domain *d)
 void ffa_notif_domain_destroy(struct domain *d)
 {
     struct ffa_ctx *ctx = d->arch.tee;
+    unsigned int i;
 
     spin_lock(&ctx->notif.notif_lock);
     ctx->notif.secure_pending = false;
+    ctx->notif.vm_pending = 0;
+    for ( i = 0; i < FFA_NUM_VM_NOTIF; i++ )
+        ctx->notif.vm_bind[i] = 0;

Why not memset(ctx->notif.vm_bind, 0, sizeof(ctx->notif.vm_bind)?

Quoted 8 lines
xen/arch/arm/tee/ffa_notif.c +0 -0
     ctx->notif.hyp_pending = 0;
     spin_unlock(&ctx->notif.notif_lock);
 
xen/arch/arm/tee/ffa_private.h +0 -0
index 5693772481ed..6d83afb3d00a 100644

xen/arch/arm/tee/ffa_private.h line 243

Quoted 8 lines
xen/arch/arm/tee/ffa_private.h +5 -0
@@ -236,6 +236,11 @@
 #define FFA_NOTIF_INFO_GET_ID_COUNT_MASK    0x1F
 
 #define FFA_NOTIF_RX_BUFFER_FULL        BIT(0, U)
+#define FFA_NUM_VM_NOTIF                64U
+
+#if FFA_NUM_VM_NOTIF > 64
+#error "FFA_NUM_VM_NOTIF must be <= 64"
+#endif

BUILD_BUG_ON(FFA_NUM_VM_NOTIF > 64) ?

Cheers,
Jens

Quoted 24 lines
xen/arch/arm/tee/ffa_private.h +7 -4
 /* Feature IDs used with FFA_FEATURES */
 #define FFA_FEATURE_NOTIF_PEND_INTR     0x1U
@@ -334,10 +339,14 @@ struct ffa_ctx_notif {
     bool secure_pending;
 
     /*
-     * True if domain is reported by FFA_NOTIFICATION_INFO_GET to have
-     * pending notifications from VMs (including framework ones).
+     * Bitmap of pending notifications from VMs (including framework ones).
+     */
+    uint64_t vm_pending;
+
+    /*
+     * Source endpoint bound to each VM notification ID (0 means unbound).
      */
-    bool vm_pending;
+    uint16_t vm_bind[FFA_NUM_VM_NOTIF];
 
     /*
      * Lock protecting the hypervisor-managed notification state.
--

2.53.0

#18 lore raw

Re: [PATCH 6/6] xen/arm: ffa: Deliver VM-to-VM notifications locally

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 176 lines · Bertrand Marquis

VM notification binding and pending tracking exist for non-secure endpoints, but FFA_NOTIFICATION_SET still only forwards secure destinations to the SPMC. Non-secure VMs therefore cannot receive notifications from other VMs. Local NPI delivery also needs explicit re-arm tracking so repeated raises are not lost while the interrupt is already pending.

Add a local VM notification delivery path for non-secure destinations. notification_set_vm() resolves the destination endpoint, verifies that every requested bit is bound to the sender, sets the receiver's vm_pending bitmap under notif_lock, and raises an NPI only when the receiver transitions from no local pending notifications to some.

Track whether a local NPI is already armed with notif_irq_raised, clear that state once both VM and hypervisor pending bitmaps are drained, and roll back newly-added VM pending bits if no destination vCPU is online. Also expose firmware notification availability so FFA_FEATURES only advertises notification support when it is actually provided by the firmware or by CONFIG_FFA_VM_TO_VM.

Functional impact: when CONFIG_FFA_VM_TO_VM is enabled, non-secure FFA_NOTIFICATION_SET delivers VM-to-VM notifications locally and keeps NPI delivery reliable across repeated raises.

---
 xen/arch/arm/tee/ffa.c         |  24 +++++--
 xen/arch/arm/tee/ffa_notif.c   | 126 +++++++++++++++++++++++++++++++--
 xen/arch/arm/tee/ffa_private.h |  11 ++-
 3 files changed, 147 insertions(+), 14 deletions(-)
xen/arch/arm/tee/ffa.c +20 -4
index 1fe33f26454a..7fe021049cba 100644
@@ -39,8 +39,13 @@
  * o FFA_MSG_SEND_DIRECT_REQ:
  *   - only supported from a VM to an SP
  * o FFA_NOTIFICATION_*:
+ *   - only supported when firmware notifications are enabled or VM-to-VM
+ *     support is built in
  *   - only supports global notifications, that is, per vCPU notifications
- *     are not supported
+ *     are not supported and secure per-vCPU notification information is
+ *     not forwarded
+ *   - the source endpoint ID reported for a notification may no longer
+ *     exist by the time the receiver consumes it
  *   - doesn't support signalling the secondary scheduler of pending
  *     notification for secure partitions
  *   - doesn't support notifications for Xen itself
@@ -245,6 +250,8 @@ static void handle_features(struct cpu_user_regs *regs)
     uint32_t a1 = get_user_reg(regs, 1);
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
+    bool notif_supported = IS_ENABLED(CONFIG_FFA_VM_TO_VM) ||
+                           ffa_notif_fw_enabled();
 
     /*
      * FFA_FEATURES defines w2 as input properties only for specific
@@ -343,10 +350,16 @@ static void handle_features(struct cpu_user_regs *regs)
 
         break;
     case FFA_FEATURE_NOTIF_PEND_INTR:
-        ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         break;
     case FFA_FEATURE_SCHEDULE_RECV_INTR:
-        ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         break;
     case FFA_PARTITION_INFO_GET_REGS:
         if ( ACCESS_ONCE(ctx->guest_vers) >= FFA_VERSION_1_2 )
@@ -361,7 +374,10 @@ static void handle_features(struct cpu_user_regs *regs)
     case FFA_NOTIFICATION_SET:
     case FFA_NOTIFICATION_INFO_GET_32:
     case FFA_NOTIFICATION_INFO_GET_64:
-        ffa_set_regs_success(regs, 0, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         break;
     default:
         ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
xen/arch/arm/tee/ffa_notif.c +55 -2
index 4def701f0130..e77321244926 100644
@@ -20,7 +20,12 @@ static bool __ro_after_init fw_notif_enabled;
 static unsigned int __ro_after_init notif_sri_irq;
 static DEFINE_SPINLOCK(notif_info_lock);
 
-static void inject_notif_pending(struct domain *d)
+bool ffa_notif_fw_enabled(void)
+{
+    return fw_notif_enabled;
+}
+
+static bool inject_notif_pending(struct domain *d)
 {
     struct vcpu *v;
 
@@ -34,13 +39,15 @@ static void inject_notif_pending(struct domain *d)
         if ( is_vcpu_online(v) )
         {
             vgic_inject_irq(d, v, GUEST_FFA_NOTIF_PEND_INTR_ID, true);
-            return;
+            return true;
         }
     }
 
     if ( printk_ratelimit() )
         printk(XENLOG_G_DEBUG "%pd: ffa: can't inject NPI, all vCPUs offline\n",
                d);
+
+    return false;
 }
 
 static int32_t ffa_notif_parse_params(uint16_t dom_id, uint16_t caller_id,
@@ -104,6 +111,73 @@ out_unlock:
     return ret;
 }
 
+/*
+ * Deliver a VM-to-VM notification. ctx->notif.notif_lock protects
+ * vm_bind/vm_pending so callers must not hold it already.
+ */
+static int32_t notification_set_vm(uint16_t dst_id, uint16_t src_id,
+                                   uint32_t flags, uint64_t bitmap)
+{
+    struct domain *dst_d;
+    struct ffa_ctx *dst_ctx;
+    unsigned int id;
+    int32_t ret;
+    uint64_t prev_bitmap = 0;
+    uint64_t new_bitmap;
+    bool inject = false;
+
+    if ( flags )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    ret = ffa_endpoint_domain_lookup(dst_id, &dst_d, &dst_ctx);
+    if ( ret )
+        return ret;
+
+    ret = FFA_RET_OK;
+
+    spin_lock(&dst_ctx->notif.notif_lock);
+
+    for ( id = 0; id < FFA_NUM_VM_NOTIF; id++ )
+    {
+        if ( !(bitmap & BIT(id, ULL)) )
+            continue;
+
+        if ( dst_ctx->notif.vm_bind[id] != src_id )
+        {
+            ret = FFA_RET_DENIED;
+            goto out_unlock;
+        }
+    }
+
+    prev_bitmap = dst_ctx->notif.vm_pending;
+    dst_ctx->notif.vm_pending |= bitmap;
+    if ( !dst_ctx->notif.notif_irq_raised &&
+         (dst_ctx->notif.vm_pending || dst_ctx->notif.hyp_pending) )
+    {
+        dst_ctx->notif.notif_irq_raised = true;
+        inject = true;
+    }

xen/arch/arm/tee/ffa_notif.c out_unlock: · line 169

Quoted 10 lines
xen/arch/arm/tee/ffa_notif.c +10 -0
+
+out_unlock:
+    spin_unlock(&dst_ctx->notif.notif_lock);
+
+    new_bitmap = bitmap & ~prev_bitmap;
+    if ( ret == FFA_RET_OK && inject && new_bitmap &&
+         !inject_notif_pending(dst_d) )
+    {
+        spin_lock(&dst_ctx->notif.notif_lock);
+        dst_ctx->notif.vm_pending &= ~new_bitmap;

There's a window above when dst_ctx->notif.notif_lock is unlocked. What if another CPU has modified dst_ctx->notif.vm_pending during that window?

Cheers,
Jens

Quoted 149 lines
xen/arch/arm/tee/ffa_notif.c +54 -5
+        if ( !(dst_ctx->notif.vm_pending || dst_ctx->notif.hyp_pending) )
+            dst_ctx->notif.notif_irq_raised = false;
+        spin_unlock(&dst_ctx->notif.notif_lock);
+        ret = FFA_RET_DENIED;
+    }
+
+    rcu_unlock_domain(dst_d);
+
+    return ret;
+}
+
 int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
@@ -285,6 +359,8 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
 
     if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
+        bool pending;
+
         spin_lock(&ctx->notif.notif_lock);
 
         if ( (flags & FFA_NOTIF_FLAG_BITMAP_HYP) && ctx->notif.hyp_pending )
@@ -293,6 +369,18 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
             ctx->notif.hyp_pending = 0;
         }
 
+        if ( (flags & FFA_NOTIF_FLAG_BITMAP_VM) && ctx->notif.vm_pending )
+        {
+            w4 = (uint32_t)(ctx->notif.vm_pending & GENMASK(31, 0));
+            w5 = (uint32_t)((ctx->notif.vm_pending >> 32) & GENMASK(31, 0));
+            ctx->notif.vm_pending = 0;
+        }
+
+        pending = (ctx->notif.hyp_pending != 0) ||
+                  (ctx->notif.vm_pending != 0);
+        if ( !pending )
+            ctx->notif.notif_irq_raised = false;
+
         spin_unlock(&ctx->notif.notif_lock);
     }
 
@@ -318,9 +406,17 @@ int32_t ffa_handle_notification_set(struct cpu_user_regs *regs)
     if ( flags )
         return FFA_RET_INVALID_PARAMETERS;
 
-    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
-        return ffa_simple_call(FFA_NOTIFICATION_SET, src_dst, flags, bitmap_lo,
-                               bitmap_hi);
+    if ( FFA_ID_IS_SECURE(dest_id) )
+    {
+        if ( fw_notif_enabled )
+            return ffa_simple_call(FFA_NOTIFICATION_SET, src_dst, flags,
+                                   bitmap_lo, bitmap_hi);
+    }
+    else if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+    {
+        return notification_set_vm(dest_id, caller_id, flags,
+                                   ((uint64_t)bitmap_hi << 32) | bitmap_lo);
+    }
 
     return FFA_RET_NOT_SUPPORTED;
 }
@@ -330,6 +426,7 @@ void ffa_raise_rx_buffer_full(struct domain *d)
 {
     struct ffa_ctx *ctx = d->arch.tee;
     uint32_t prev_bitmap;
+    bool inject = false;
 
     if ( !ctx )
         return;
@@ -337,10 +434,23 @@ void ffa_raise_rx_buffer_full(struct domain *d)
     spin_lock(&ctx->notif.notif_lock);
     prev_bitmap = ctx->notif.hyp_pending;
     ctx->notif.hyp_pending |= FFA_NOTIF_RX_BUFFER_FULL;
+    if ( !ctx->notif.notif_irq_raised &&
+         (ctx->notif.vm_pending || ctx->notif.hyp_pending) )
+    {
+        ctx->notif.notif_irq_raised = true;
+        inject = true;
+    }
     spin_unlock(&ctx->notif.notif_lock);
 
-    if ( !(prev_bitmap & FFA_NOTIF_RX_BUFFER_FULL) )
-        inject_notif_pending(d);
+    if ( inject && !(prev_bitmap & FFA_NOTIF_RX_BUFFER_FULL) &&
+         !inject_notif_pending(d) )
+    {
+        spin_lock(&ctx->notif.notif_lock);
+        ctx->notif.hyp_pending &= ~FFA_NOTIF_RX_BUFFER_FULL;
+        if ( !(ctx->notif.vm_pending || ctx->notif.hyp_pending) )
+            ctx->notif.notif_irq_raised = false;
+        spin_unlock(&ctx->notif.notif_lock);
+    }
 }
 #endif
 
@@ -572,6 +682,7 @@ int ffa_notif_domain_init(struct domain *d)
     spin_lock_init(&ctx->notif.notif_lock);
     ctx->notif.secure_pending = false;
     ctx->notif.vm_pending = 0;
+    ctx->notif.notif_irq_raised = false;
     for ( i = 0; i < FFA_NUM_VM_NOTIF; i++ )
         ctx->notif.vm_bind[i] = 0;
     ctx->notif.hyp_pending = 0;
@@ -594,6 +705,7 @@ void ffa_notif_domain_destroy(struct domain *d)
     spin_lock(&ctx->notif.notif_lock);
     ctx->notif.secure_pending = false;
     ctx->notif.vm_pending = 0;
+    ctx->notif.notif_irq_raised = false;
     for ( i = 0; i < FFA_NUM_VM_NOTIF; i++ )
         ctx->notif.vm_bind[i] = 0;
     ctx->notif.hyp_pending = 0;
xen/arch/arm/tee/ffa_private.h +8 -4
index 6d83afb3d00a..5bb19bd11dd0 100644
@@ -344,13 +344,17 @@ struct ffa_ctx_notif {
     uint64_t vm_pending;
 
     /*
-     * Source endpoint bound to each VM notification ID (0 means unbound).
+     * Tracks whether an NPI has been raised for local pending notifications.
+     * Protected by notif_lock.
      */
-    uint16_t vm_bind[FFA_NUM_VM_NOTIF];
+    bool notif_irq_raised;
 
     /*
-     * Lock protecting the hypervisor-managed notification state.
+     * Source endpoint bound to each VM notification ID (0 means unbound).
      */
+    uint16_t vm_bind[FFA_NUM_VM_NOTIF];
+
+    /* Lock protecting local notification state. */
     spinlock_t notif_lock;
 
     /*
@@ -493,6 +497,7 @@ void ffa_notif_init(void);
 void ffa_notif_init_interrupt(void);
 int ffa_notif_domain_init(struct domain *d);
 void ffa_notif_domain_destroy(struct domain *d);
+bool ffa_notif_fw_enabled(void);
 
 int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs);
 int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs);
--

2.53.0

#5 lore raw

Re: [PATCH 2/6] xen/arm: ffa: Track hypervisor notifications in a bitmap

In reply to Jens Wiklander

Hi Jens,

Thanks a lot for the review.

Quoted 41 lines · Jens Wiklander

Hi Bertrand,

Quoted 35 lines · Bertrand Marquis

Hypervisor notifications are currently tracked with a dedicated buff_full_pending boolean. That state only represents a single HYP notification bit and keeps HYP bitmap handling tied to single-purpose bookkeeping.

Replace the boolean with a hypervisor notification bitmap protected by notif_lock. INFO_GET reports pending when the bitmap is non-zero, GET returns and clears the HYP bitmap under the lock, and RX-buffer-full sets FFA_NOTIF_RX_BUFFER_FULL in the bitmap instead of updating separate state.

Initialize and clear the bitmap during domain lifecycle handling, and use ctx->ffa_id for bitmap create and destroy so the notification state stays tied to the cached FF-A endpoint ID.

No functional changes.

---
xen/arch/arm/tee/ffa_notif.c | 46 ++++++++++++++++++++++++++--------
xen/arch/arm/tee/ffa_private.h | 9 +++++--

2 files changed, 43 insertions(+), 12 deletions(-)
xen/arch/arm/tee/ffa_notif.c +1 -0
index 07bc5cb3a430..d15119409a25 100644
@@ -94,8 +94,15 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 
    notif_pending = test_and_clear_bool(ctx->notif.secure_pending);
    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+    {
        notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
 
Quoted 7 lines

xen/arch/arm/tee/ffa_notif.c void ffa_handle_notification_info_get(struct cpu_user_regs *regs) · line 103

Quoted 4 lines
xen/arch/arm/tee/ffa_notif.c +4 -0
+        spin_lock(&ctx->notif.notif_lock);
+        if ( ctx->notif.hyp_pending )
+            notif_pending = true;
+        spin_unlock(&ctx->notif.notif_lock);

Isn't this a functional change? Before this patch, we didn't consider ctx->notif.buff_full_pending here. Am I missing something?

We did consider it implicitly through vm_pending.

This patch makes that cleaner by using hyp_pending for the Hypervisor framework notification itself. Previously, RX-buffer-full was made visible indirectly via vm_pending, and FFA_NOTIFICATION_INFO_GET cleared that summary state.

As a result, the guest-visible pending indication could be lost before the Hypervisor notification was actually retrieved with FFA_NOTIFICATION_GET.

With this change, the pending state is tracked in hyp_pending and is only cleared when the Hypervisor notifications are retrieved through FFA_NOTIFICATION_GET.

I will reword the commit message to make that clearer.

Quoted 38 lines
Quoted 38 lines
xen/arch/arm/tee/ffa_notif.c +13 -5
+    }
+
    if ( notif_pending )
    {
        /* A pending global notification for the guest */
@@ -174,12 +181,17 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
            w6 = resp.a6;
    }
 
-    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) &&
-          flags & FFA_NOTIF_FLAG_BITMAP_HYP &&
-          test_and_clear_bool(ctx->notif.buff_full_pending) )
+    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
    {
-        ACCESS_ONCE(ctx->notif.vm_pending) = false;
-        w7 = FFA_NOTIF_RX_BUFFER_FULL;
+        spin_lock(&ctx->notif.notif_lock);
+
+        if ( (flags & FFA_NOTIF_FLAG_BITMAP_HYP) && ctx->notif.hyp_pending )
+        {
+            w7 = ctx->notif.hyp_pending;
+            ctx->notif.hyp_pending = 0;
+        }
+
+        spin_unlock(&ctx->notif.notif_lock);
    }
 
    ffa_set_regs(regs, FFA_SUCCESS_32, 0, w2, w3, w4, w5, w6, w7);
@@ -207,12 +219,17 @@ int32_t ffa_handle_notification_set(struct cpu_user_regs *regs)
 void ffa_raise_rx_buffer_full(struct domain *d)
 {
    struct ffa_ctx *ctx = d->arch.tee;
+    uint32_t prev_bitmap;
 
    if ( !ctx )
        return;
 
Quoted 11 lines

xen/arch/arm/tee/ffa_notif.c int32_t ffa_handle_notification_set(struct cpu_user_regs *regs) · line 232

Quoted 8 lines
xen/arch/arm/tee/ffa_notif.c +6 -2
-    ACCESS_ONCE(ctx->notif.buff_full_pending) = true;
-    if ( !test_and_set_bool(ctx->notif.vm_pending) )
+    spin_lock(&ctx->notif.notif_lock);
+    prev_bitmap = ctx->notif.hyp_pending;
+    ctx->notif.hyp_pending |= FFA_NOTIF_RX_BUFFER_FULL;
+    spin_unlock(&ctx->notif.notif_lock);
+
+    if ( !(prev_bitmap & FFA_NOTIF_RX_BUFFER_FULL) )

Do we need to check for FFA_NOTIF_RX_BUFFER_FULL? Isn't !prev_bitmap more accurate, if any other bit would ever be used in the bitmap?

I would keep the bit-specific check here, if that is OK with you.

This function is about raising the RX buffer full notification, so I think it is clearer to check whether that bit was newly pended rather than whether the whole Hypervisor bitmap was previously empty.

Using !prev_bitmap would also make the condition depend on unrelated Hypervisor notifications being pending.

Tell me if you are ok with that.

Cheers
Bertrand

#10 lore raw

Re: [PATCH 3/6] xen/arm: ffa: Tighten notification parameter validation

In reply to Jens Wiklander

Hi Jens,

Quoted 45 lines · Jens Wiklander

Hi Bertand,

Quoted 39 lines · Bertrand Marquis

The notification handlers still validate overlapping subsets of their inputs. BIND, UNBIND, and SET each decode caller and destination IDs locally, GET still accepts a non-zero receiver vCPU ID and reserved flag bits, and SET still accepts non-zero NS-virtual flags. BIND also treats unsupported non-zero flag encodings as a supported-feature failure instead of as malformed input.

Add ffa_notif_parse_params() and use it to centralize the common caller/destination and non-zero bitmap checks for BIND, UNBIND, and SET. Also reject malformed GET and SET requests locally before touching cached state or forwarding anything to the SPMC. Keep BIND limited to global notifications and reject unsupported non-zero flag encodings with INVALID_PARAMETERS.

- add a shared parameter parser for notification caller/destination validation
- wire BIND and UNBIND through the shared parser and reject unsupported bind flag encodings with INVALID_PARAMETERS
- reject non-zero receiver vCPU and reserved flag bits in FFA_NOTIFICATION_GET
- reject non-zero flags in the NS-virtual FFA_NOTIFICATION_SET path

Functional impact: malformed notification requests are rejected consistently earlier in the mediator.

---
xen/arch/arm/tee/ffa_notif.c | 61 +++++++++++++++++++++++++++++-------

1 file changed, 50 insertions(+), 11 deletions(-)
xen/arch/arm/tee/ffa_notif.c +0 -0
index d15119409a25..491db3b04df5 100644
@@ -42,21 +42,40 @@ static void inject_notif_pending(struct domain *d)
               d);
 }
 
Quoted 6 lines

xen/arch/arm/tee/ffa_notif.c static void inject_notif_pending(struct domain *d) · line 47

Quoted 3 lines
xen/arch/arm/tee/ffa_notif.c +3 -0
+static int32_t ffa_notif_parse_params(uint16_t dom_id, uint16_t caller_id,
+                                      uint16_t dest_id, uint32_t bitmap_lo,
+                                      uint32_t bitmap_hi)

Nit: I would have picked ffa_notif_validate_params() or ffa_notif_check_params(), but that might be more a matter of taste.

Agree, I will rename it to validate_params.

Quoted 2 lines

Anyway, looks good:

Thanks
Bertrand

#13 lore raw

Re: [PATCH 4/6] xen/arm: ffa: Preserve secure notification state when polling SPMC

In reply to Jens Wiklander

HI Jens,

Quoted 142 lines · Jens Wiklander

Hi Bertrand,

Quoted 136 lines · Bertrand Marquis

Secure pending state is latched when the SPMC raises the schedule receiver interrupt, but Xen currently clears that latch too aggressively. Guest FFA_NOTIFICATION_INFO_GET consumes secure_pending even though it only reports pending state, and secure FFA_NOTIFICATION_GET only clears the latch when both SP and SPM bitmaps are requested together. This can drop a pending indication before the receiver retrieves secure notifications, or keep INFO_GET reporting stale secure pending state after a successful GET.

Keep secure_pending as a latched indication until secure notifications are actually retrieved. Guest FFA_NOTIFICATION_INFO_GET now reports the latched state without clearing it, while a successful secure FFA_NOTIFICATION_GET clears the latch regardless of which secure bitmap flags were requested. Also protect secure_pending with notif_lock, serialize SPMC INFO_GET polling behind notif_info_lock, and preserve the caller-visible INFO_GET success width.

Functional impact: guest INFO_GET preserves the secure pending indication until secure notifications are retrieved, and successful secure GET clears the guest-visible pending latch.

---
xen/arch/arm/tee/ffa_notif.c | 54 +++++++++++++++++++++++-------------

1 file changed, 35 insertions(+), 19 deletions(-)
xen/arch/arm/tee/ffa_notif.c +21 -16
index 491db3b04df5..fff00ca2baec 100644
@@ -18,6 +18,7 @@
 
 static bool __ro_after_init fw_notif_enabled;
 static unsigned int __ro_after_init notif_sri_irq;
+static DEFINE_SPINLOCK(notif_info_lock);
 
 static void inject_notif_pending(struct domain *d)
 {
@@ -109,6 +110,7 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 {
    struct domain *d = current->domain;
    struct ffa_ctx *ctx = d->arch.tee;
+    uint32_t fid = get_user_reg(regs, 0);
    bool notif_pending;
 
    if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) && !fw_notif_enabled )
@@ -117,7 +119,10 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
        return;
    }
 
-    notif_pending = test_and_clear_bool(ctx->notif.secure_pending);
+    spin_lock(&ctx->notif.notif_lock);
+    notif_pending = ctx->notif.secure_pending;
+    spin_unlock(&ctx->notif.notif_lock);
+
    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
    {
        notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
@@ -131,7 +136,9 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
    if ( notif_pending )
    {
        /* A pending global notification for the guest */
-        ffa_set_regs(regs, FFA_SUCCESS_64, 0,
+        ffa_set_regs(regs,
+                     smccc_is_conv_64(fid) ? FFA_SUCCESS_64 : FFA_SUCCESS_32,
+                     0,
                     1U << FFA_NOTIF_INFO_GET_ID_COUNT_SHIFT, ffa_get_vm_id(d),
                     0, 0, 0, 0);
    }
@@ -154,6 +161,8 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
    uint32_t w5 = 0;
    uint32_t w6 = 0;
    uint32_t w7 = 0;
+    uint32_t secure_flags = flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
+                                      FFA_NOTIF_FLAG_BITMAP_SPM );
 
    if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) && !fw_notif_enabled )
    {
@@ -173,27 +182,16 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
        return;
    }
 
-    if ( fw_notif_enabled && (flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
-                                        FFA_NOTIF_FLAG_BITMAP_SPM )) )
+    if ( fw_notif_enabled && secure_flags )
    {
        struct arm_smccc_1_2_regs arg = {
            .a0 = FFA_NOTIFICATION_GET,
            .a1 = recv,
-            .a2 = flags & ( FFA_NOTIF_FLAG_BITMAP_SP |
-                            FFA_NOTIF_FLAG_BITMAP_SPM ),
+            .a2 = secure_flags,
        };
        struct arm_smccc_1_2_regs resp;
        int32_t e;
 
-        /*
-         * Clear secure pending if both FFA_NOTIF_FLAG_BITMAP_SP and
-         * FFA_NOTIF_FLAG_BITMAP_SPM are set since secure world can't have
-         * any more pending notifications.
-         */
-        if ( ( flags  & FFA_NOTIF_FLAG_BITMAP_SP ) &&
-             ( flags & FFA_NOTIF_FLAG_BITMAP_SPM ) )
-            ACCESS_ONCE(ctx->notif.secure_pending) = false;
-
        arm_smccc_1_2_smc(&arg, &resp);
        e = ffa_get_ret_code(&resp);
        if ( e )
@@ -210,6 +208,10 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
 
        if ( flags & FFA_NOTIF_FLAG_BITMAP_SPM )
            w6 = resp.a6;
+
+        spin_lock(&ctx->notif.notif_lock);
+        ctx->notif.secure_pending = false;
+        spin_unlock(&ctx->notif.notif_lock);
    }
 
    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
@@ -354,7 +356,10 @@ static void notif_vm_pend_intr(uint16_t vm_id)
     * guarantees that the data structure isn't freed while we're accessing
     * it.
     */
-    ACCESS_ONCE(ctx->notif.secure_pending) = true;
+    spin_lock(&ctx->notif.notif_lock);
+    ctx->notif.secure_pending = true;
+    spin_unlock(&ctx->notif.notif_lock);
+
    inject_notif_pending(d);
 

out_unlock:

xen/arch/arm/tee/ffa_notif.c +0 -0
@@ -373,11 +378,18 @@ static void notif_sri_action(void *unused)
    unsigned int n;
    int32_t res;
 
Quoted 5 lines

xen/arch/arm/tee/ffa_notif.c static void notif_sri_action(void *unused) · line 382

Quoted 3 lines
xen/arch/arm/tee/ffa_notif.c +2 -1
-    do {
+    if ( !fw_notif_enabled )
+        return;

Can this ever happen? Am I missing something?

No in fact it cannot you are right, we will never get an SRI if firmware notifications are not enabled so I will remove that.

Cheers
Bertrand

#16 lore raw

Re: [PATCH 5/6] xen/arm: ffa: Track VM notification bindings locally

In reply to Jens Wiklander

Hi Jens,

Quoted 178 lines · Jens Wiklander

Hi Bertrand,

Quoted 172 lines · Bertrand Marquis

VM-to-VM notifications need receiver-side bind state so Xen can validate which sender owns each notification bit. Non-secure BIND and UNBIND requests currently have no local state and cannot enforce that contract.

Add per-bit VM notification binding state to struct ffa_ctx_notif and use it to handle non-secure BIND and UNBIND requests when CONFIG_FFA_VM_TO_VM is enabled. The update helper validates the whole request under notif_lock before mutating anything, denies bind or unbind when a bit is pending, rejects rebinding to a different sender, and keeps rebinding to the same sender idempotent.

Promote vm_pending to a bitmap so the bind logic can reason per notification ID, use that bitmap directly when reporting pending state, and initialize and clear the new VM notification state during domain init and teardown.

Functional impact: when CONFIG_FFA_VM_TO_VM is enabled, Xen tracks VM notification bindings locally and validates non-secure bind and unbind requests against that state.

---
xen/arch/arm/tee/ffa_notif.c | 97 ++++++++++++++++++++++++++++++----
xen/arch/arm/tee/ffa_private.h | 15 ++++--

2 files changed, 99 insertions(+), 13 deletions(-)
xen/arch/arm/tee/ffa_notif.c +84 -10
index fff00ca2baec..4def701f0130 100644
@@ -56,6 +56,54 @@ static int32_t ffa_notif_parse_params(uint16_t dom_id, uint16_t caller_id,
    return FFA_RET_OK;
 }
 
+static int32_t ffa_notif_update_vm_binding(struct ffa_ctx *ctx,
+                                           uint16_t dest_id, uint64_t bitmap,
+                                           bool bind)
+{
+    unsigned int id;
+    int32_t ret = FFA_RET_OK;
+
+    spin_lock(&ctx->notif.notif_lock);
+
+    for ( id = 0; id < FFA_NUM_VM_NOTIF; id++ )
+    {
+        if ( !(bitmap & BIT(id, ULL)) )
+            continue;
+
+        if ( ctx->notif.vm_pending & BIT(id, ULL) )
+        {
+            ret = FFA_RET_DENIED;
+            goto out_unlock;
+        }
+
+        if ( bind )
+        {
+            if ( ctx->notif.vm_bind[id] != 0 &&
+                 ctx->notif.vm_bind[id] != dest_id )
+            {
+                ret = FFA_RET_DENIED;
+                goto out_unlock;
+            }
+        }
+        else if ( ctx->notif.vm_bind[id] != dest_id )
+        {
+            ret = FFA_RET_DENIED;
+            goto out_unlock;
+        }
+    }
+
+    for ( id = 0; id < FFA_NUM_VM_NOTIF; id++ )
+    {
+        if ( bitmap & BIT(id, ULL) )
+            ctx->notif.vm_bind[id] = bind ? dest_id : 0;
+    }
+
+out_unlock:
+    spin_unlock(&ctx->notif.notif_lock);
+
+    return ret;
+}
+
 int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 {
    struct domain *d = current->domain;
@@ -76,11 +124,21 @@ int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
    if ( ret )
        return ret;
 
-    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
-        return ffa_simple_call(FFA_NOTIFICATION_BIND, src_dst, flags,
-                               bitmap_lo, bitmap_hi);
+    if ( FFA_ID_IS_SECURE(dest_id) )
+    {
+        if ( fw_notif_enabled )
+            return ffa_simple_call(FFA_NOTIFICATION_BIND, src_dst, flags,
+                                   bitmap_lo, bitmap_hi);
 
-    return FFA_RET_NOT_SUPPORTED;
+        return FFA_RET_NOT_SUPPORTED;
+    }
+
+    if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+        return FFA_RET_NOT_SUPPORTED;
+
+    return ffa_notif_update_vm_binding(ctx, dest_id,
+                                       ((uint64_t)bitmap_hi << 32) | bitmap_lo,
+                                       true);
 }
 
 int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs)
@@ -99,11 +157,21 @@ int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs)
    if ( ret )
        return ret;
 
-    if ( FFA_ID_IS_SECURE(dest_id) && fw_notif_enabled )
-        return ffa_simple_call(FFA_NOTIFICATION_UNBIND, src_dst, 0, bitmap_lo,
-                               bitmap_hi);
+    if ( FFA_ID_IS_SECURE(dest_id) )
+    {
+        if ( fw_notif_enabled )
+            return ffa_simple_call(FFA_NOTIFICATION_UNBIND, src_dst, 0,
+                                   bitmap_lo, bitmap_hi);
 
-    return FFA_RET_NOT_SUPPORTED;
+        return FFA_RET_NOT_SUPPORTED;
+    }
+
+    if ( !IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+        return FFA_RET_NOT_SUPPORTED;
+
+    return ffa_notif_update_vm_binding(ctx, dest_id,
+                                       ((uint64_t)bitmap_hi << 32) | bitmap_lo,
+                                       false);
 }
 
 void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
@@ -125,9 +193,10 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 
    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
    {
-        notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
-
        spin_lock(&ctx->notif.notif_lock);
+        if ( ctx->notif.vm_pending )
+            notif_pending = true;
+
        if ( ctx->notif.hyp_pending )
            notif_pending = true;
        spin_unlock(&ctx->notif.notif_lock);
@@ -497,10 +566,14 @@ void ffa_notif_init(void)
 int ffa_notif_domain_init(struct domain *d)
 {
    struct ffa_ctx *ctx = d->arch.tee;
+    unsigned int i;
    int32_t res;
 
    spin_lock_init(&ctx->notif.notif_lock);
    ctx->notif.secure_pending = false;
+    ctx->notif.vm_pending = 0;
+    for ( i = 0; i < FFA_NUM_VM_NOTIF; i++ )
+        ctx->notif.vm_bind[i] = 0;
    ctx->notif.hyp_pending = 0;
 
    if ( fw_notif_enabled )
@@ -516,9 +589,13 @@ int ffa_notif_domain_init(struct domain *d)
 void ffa_notif_domain_destroy(struct domain *d)
 {
    struct ffa_ctx *ctx = d->arch.tee;
+    unsigned int i;
 
Quoted 7 lines

xen/arch/arm/tee/ffa_notif.c int ffa_notif_domain_init(struct domain *d) · line 598

Quoted 5 lines
xen/arch/arm/tee/ffa_notif.c +3 -0
    spin_lock(&ctx->notif.notif_lock);
    ctx->notif.secure_pending = false;
+    ctx->notif.vm_pending = 0;
+    for ( i = 0; i < FFA_NUM_VM_NOTIF; i++ )
+        ctx->notif.vm_bind[i] = 0;

Why not memset(ctx->notif.vm_bind, 0, sizeof(ctx->notif.vm_bind)?

Definitely better to use memset, I will use it here and in domain_init instead of an explicit loop.

Quoted 11 lines
Quoted 11 lines
xen/arch/arm/tee/ffa_notif.c +0 -0
    ctx->notif.hyp_pending = 0;
    spin_unlock(&ctx->notif.notif_lock);
 
xen/arch/arm/tee/ffa_private.h +0 -0
index 5693772481ed..6d83afb3d00a 100644
@@ -236,6 +236,11 @@
 #define FFA_NOTIF_INFO_GET_ID_COUNT_MASK    0x1F
 
Quoted 8 lines

xen/arch/arm/tee/ffa_private.h line 243

Quoted 6 lines
xen/arch/arm/tee/ffa_private.h +5 -0
 #define FFA_NOTIF_RX_BUFFER_FULL        BIT(0, U)
+#define FFA_NUM_VM_NOTIF                64U
+
+#if FFA_NUM_VM_NOTIF > 64
+#error "FFA_NUM_VM_NOTIF must be <= 64"
+#endif

BUILD_BUG_ON(FFA_NUM_VM_NOTIF > 64) ?

Yes that would be cleaner.
I will put it in ffa_notif_domain_init and remove that from the header.

Cheers
Bertrand

#19 lore raw

Re: [PATCH 6/6] xen/arm: ffa: Deliver VM-to-VM notifications locally

In reply to Jens Wiklander

Hi Jens,

Quoted 188 lines · Jens Wiklander

Hi Bertrand,

Quoted 182 lines · Bertrand Marquis

VM notification binding and pending tracking exist for non-secure endpoints, but FFA_NOTIFICATION_SET still only forwards secure destinations to the SPMC. Non-secure VMs therefore cannot receive notifications from other VMs. Local NPI delivery also needs explicit re-arm tracking so repeated raises are not lost while the interrupt is already pending.

Add a local VM notification delivery path for non-secure destinations. notification_set_vm() resolves the destination endpoint, verifies that every requested bit is bound to the sender, sets the receiver's vm_pending bitmap under notif_lock, and raises an NPI only when the receiver transitions from no local pending notifications to some.

Track whether a local NPI is already armed with notif_irq_raised, clear that state once both VM and hypervisor pending bitmaps are drained, and roll back newly-added VM pending bits if no destination vCPU is online. Also expose firmware notification availability so FFA_FEATURES only advertises notification support when it is actually provided by the firmware or by CONFIG_FFA_VM_TO_VM.

Functional impact: when CONFIG_FFA_VM_TO_VM is enabled, non-secure FFA_NOTIFICATION_SET delivers VM-to-VM notifications locally and keeps NPI delivery reliable across repeated raises.

---

xen/arch/arm/tee/ffa.c         |  24 +++++--
xen/arch/arm/tee/ffa_notif.c   | 126 +++++++++++++++++++++++++++++++--
xen/arch/arm/tee/ffa_private.h |  11 ++-
3 files changed, 147 insertions(+), 14 deletions(-)
xen/arch/arm/tee/ffa.c +20 -4
index 1fe33f26454a..7fe021049cba 100644
@@ -39,8 +39,13 @@
 * o FFA_MSG_SEND_DIRECT_REQ:
 *   - only supported from a VM to an SP
 * o FFA_NOTIFICATION_*:
+ *   - only supported when firmware notifications are enabled or VM-to-VM
+ *     support is built in
 *   - only supports global notifications, that is, per vCPU notifications
- *     are not supported
+ *     are not supported and secure per-vCPU notification information is
+ *     not forwarded
+ *   - the source endpoint ID reported for a notification may no longer
+ *     exist by the time the receiver consumes it
 *   - doesn't support signalling the secondary scheduler of pending
 *     notification for secure partitions
 *   - doesn't support notifications for Xen itself
@@ -245,6 +250,8 @@ static void handle_features(struct cpu_user_regs *regs)
    uint32_t a1 = get_user_reg(regs, 1);
    struct domain *d = current->domain;
    struct ffa_ctx *ctx = d->arch.tee;
+    bool notif_supported = IS_ENABLED(CONFIG_FFA_VM_TO_VM) ||
+                           ffa_notif_fw_enabled();
 
    /*
     * FFA_FEATURES defines w2 as input properties only for specific
@@ -343,10 +350,16 @@ static void handle_features(struct cpu_user_regs *regs)
 
        break;
    case FFA_FEATURE_NOTIF_PEND_INTR:
-        ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
        break;
    case FFA_FEATURE_SCHEDULE_RECV_INTR:
-        ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
        break;
    case FFA_PARTITION_INFO_GET_REGS:
        if ( ACCESS_ONCE(ctx->guest_vers) >= FFA_VERSION_1_2 )
@@ -361,7 +374,10 @@ static void handle_features(struct cpu_user_regs *regs)
    case FFA_NOTIFICATION_SET:
    case FFA_NOTIFICATION_INFO_GET_32:
    case FFA_NOTIFICATION_INFO_GET_64:
-        ffa_set_regs_success(regs, 0, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
        break;
    default:
        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
xen/arch/arm/tee/ffa_notif.c +61 -2
index 4def701f0130..e77321244926 100644
@@ -20,7 +20,12 @@ static bool __ro_after_init fw_notif_enabled;
 static unsigned int __ro_after_init notif_sri_irq;
 static DEFINE_SPINLOCK(notif_info_lock);
 
-static void inject_notif_pending(struct domain *d)
+bool ffa_notif_fw_enabled(void)
+{
+    return fw_notif_enabled;
+}
+
+static bool inject_notif_pending(struct domain *d)
 {
    struct vcpu *v;
 
@@ -34,13 +39,15 @@ static void inject_notif_pending(struct domain *d)
        if ( is_vcpu_online(v) )
        {
            vgic_inject_irq(d, v, GUEST_FFA_NOTIF_PEND_INTR_ID, true);
-            return;
+            return true;
        }
    }
 
    if ( printk_ratelimit() )
        printk(XENLOG_G_DEBUG "%pd: ffa: can't inject NPI, all vCPUs offline\n",
               d);
+
+    return false;
 }
 
 static int32_t ffa_notif_parse_params(uint16_t dom_id, uint16_t caller_id,
@@ -104,6 +111,73 @@ out_unlock:
    return ret;
 }
 
+/*
+ * Deliver a VM-to-VM notification. ctx->notif.notif_lock protects
+ * vm_bind/vm_pending so callers must not hold it already.
+ */
+static int32_t notification_set_vm(uint16_t dst_id, uint16_t src_id,
+                                   uint32_t flags, uint64_t bitmap)
+{
+    struct domain *dst_d;
+    struct ffa_ctx *dst_ctx;
+    unsigned int id;
+    int32_t ret;
+    uint64_t prev_bitmap = 0;
+    uint64_t new_bitmap;
+    bool inject = false;
+
+    if ( flags )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    ret = ffa_endpoint_domain_lookup(dst_id, &dst_d, &dst_ctx);
+    if ( ret )
+        return ret;
+
+    ret = FFA_RET_OK;
+
+    spin_lock(&dst_ctx->notif.notif_lock);
+
+    for ( id = 0; id < FFA_NUM_VM_NOTIF; id++ )
+    {
+        if ( !(bitmap & BIT(id, ULL)) )
+            continue;
+
+        if ( dst_ctx->notif.vm_bind[id] != src_id )
+        {
+            ret = FFA_RET_DENIED;
+            goto out_unlock;
+        }
+    }
+
+    prev_bitmap = dst_ctx->notif.vm_pending;
+    dst_ctx->notif.vm_pending |= bitmap;
+    if ( !dst_ctx->notif.notif_irq_raised &&
+         (dst_ctx->notif.vm_pending || dst_ctx->notif.hyp_pending) )
+    {
+        dst_ctx->notif.notif_irq_raised = true;
+        inject = true;
+    }
+
+out_unlock:
+    spin_unlock(&dst_ctx->notif.notif_lock);
+
+    new_bitmap = bitmap & ~prev_bitmap;
+    if ( ret == FFA_RET_OK && inject && new_bitmap &&
Quoted 8 lines

xen/arch/arm/tee/ffa_notif.c out_unlock: · line 169

Quoted 4 lines
xen/arch/arm/tee/ffa_notif.c +4 -0
+         !inject_notif_pending(dst_d) )
+    {
+        spin_lock(&dst_ctx->notif.notif_lock);
+        dst_ctx->notif.vm_pending &= ~new_bitmap;

There's a window above when dst_ctx->notif.notif_lock is unlocked. What if another CPU has modified dst_ctx->notif.vm_pending during that window?

You are right, there is a race in that window. Thanks for the finding :-)

I think the simplest fix is to keep notif_lock held across the injection attempt, so we do not expose partially updated notification state while the interrupt delivery is still in progress.

I do not think we should roll the pending bits back on injection failure, though. In practice the cases where we fail to inject are cases where the target cannot currently take the notification anyway, and dropping the pending state would be worse than keeping it pending and not marking the IRQ as raised.

So my plan would be to keep the pending state, only mark notif_irq_raised when we actually inject, and keep the existing debug print for the cases we can detect.

If that approach looks OK to you, I will update this patch accordingly.

The same issue exists in patch 2 for the RX-buffer-full path, so I will fix it there as well for the same reason.

Cheers
Bertrand

#6 lore raw

Re: [PATCH 2/6] xen/arm: ffa: Track hypervisor notifications in a bitmap

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 65 lines · Bertrand Marquis

Hi Jens,

Thanks a lot for the review.

Quoted 41 lines · Jens Wiklander

Hi Bertrand,

Quoted 35 lines · Bertrand Marquis

Hypervisor notifications are currently tracked with a dedicated buff_full_pending boolean. That state only represents a single HYP notification bit and keeps HYP bitmap handling tied to single-purpose bookkeeping.

Replace the boolean with a hypervisor notification bitmap protected by notif_lock. INFO_GET reports pending when the bitmap is non-zero, GET returns and clears the HYP bitmap under the lock, and RX-buffer-full sets FFA_NOTIF_RX_BUFFER_FULL in the bitmap instead of updating separate state.

Initialize and clear the bitmap during domain lifecycle handling, and use ctx->ffa_id for bitmap create and destroy so the notification state stays tied to the cached FF-A endpoint ID.

No functional changes.

---
xen/arch/arm/tee/ffa_notif.c | 46 ++++++++++++++++++++++++++--------
xen/arch/arm/tee/ffa_private.h | 9 +++++--

2 files changed, 43 insertions(+), 12 deletions(-)
xen/arch/arm/tee/ffa_notif.c +1 -0
index 07bc5cb3a430..d15119409a25 100644
@@ -94,8 +94,15 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 
    notif_pending = test_and_clear_bool(ctx->notif.secure_pending);
    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+    {
        notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
 
Quoted 7 lines

xen/arch/arm/tee/ffa_notif.c void ffa_handle_notification_info_get(struct cpu_user_regs *regs) · line 103

Quoted 4 lines
xen/arch/arm/tee/ffa_notif.c +4 -0
+        spin_lock(&ctx->notif.notif_lock);
+        if ( ctx->notif.hyp_pending )
+            notif_pending = true;
+        spin_unlock(&ctx->notif.notif_lock);

Isn't this a functional change? Before this patch, we didn't consider ctx->notif.buff_full_pending here. Am I missing something?

We did consider it implicitly through vm_pending.

This patch makes that cleaner by using hyp_pending for the Hypervisor framework notification itself. Previously, RX-buffer-full was made visible indirectly via vm_pending, and FFA_NOTIFICATION_INFO_GET cleared that summary state.

As a result, the guest-visible pending indication could be lost before the Hypervisor notification was actually retrieved with FFA_NOTIFICATION_GET.

Quoted 5 lines

With this change, the pending state is tracked in hyp_pending and is only cleared when the Hypervisor notifications are retrieved through FFA_NOTIFICATION_GET.

I will reword the commit message to make that clearer.

Thanks

Quoted 58 lines
Quoted 39 lines
Quoted 39 lines
xen/arch/arm/tee/ffa_notif.c +13 -5
+    }
+
    if ( notif_pending )
    {
        /* A pending global notification for the guest */
@@ -174,12 +181,17 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
            w6 = resp.a6;
    }
 
-    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) &&
-          flags & FFA_NOTIF_FLAG_BITMAP_HYP &&
-          test_and_clear_bool(ctx->notif.buff_full_pending) )
+    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
    {
-        ACCESS_ONCE(ctx->notif.vm_pending) = false;
-        w7 = FFA_NOTIF_RX_BUFFER_FULL;
+        spin_lock(&ctx->notif.notif_lock);
+
+        if ( (flags & FFA_NOTIF_FLAG_BITMAP_HYP) && ctx->notif.hyp_pending )
+        {
+            w7 = ctx->notif.hyp_pending;
+            ctx->notif.hyp_pending = 0;
+        }
+
+        spin_unlock(&ctx->notif.notif_lock);
    }
 
    ffa_set_regs(regs, FFA_SUCCESS_32, 0, w2, w3, w4, w5, w6, w7);
@@ -207,12 +219,17 @@ int32_t ffa_handle_notification_set(struct cpu_user_regs *regs)
 void ffa_raise_rx_buffer_full(struct domain *d)
 {
    struct ffa_ctx *ctx = d->arch.tee;
+    uint32_t prev_bitmap;
 
    if ( !ctx )
        return;
 
Quoted 11 lines

xen/arch/arm/tee/ffa_notif.c int32_t ffa_handle_notification_set(struct cpu_user_regs *regs) · line 232

Quoted 8 lines
xen/arch/arm/tee/ffa_notif.c +6 -2
-    ACCESS_ONCE(ctx->notif.buff_full_pending) = true;
-    if ( !test_and_set_bool(ctx->notif.vm_pending) )
+    spin_lock(&ctx->notif.notif_lock);
+    prev_bitmap = ctx->notif.hyp_pending;
+    ctx->notif.hyp_pending |= FFA_NOTIF_RX_BUFFER_FULL;
+    spin_unlock(&ctx->notif.notif_lock);
+
+    if ( !(prev_bitmap & FFA_NOTIF_RX_BUFFER_FULL) )

Do we need to check for FFA_NOTIF_RX_BUFFER_FULL? Isn't !prev_bitmap more accurate, if any other bit would ever be used in the bitmap?

I would keep the bit-specific check here, if that is OK with you.

This function is about raising the RX buffer full notification, so I think it is clearer to check whether that bit was newly pended rather than whether the whole Hypervisor bitmap was previously empty.

Quoted 4 lines

Using !prev_bitmap would also make the condition depend on unrelated Hypervisor notifications being pending.

Tell me if you are ok with that.

I see your point, it's fine.

Cheers,
Jens

Quoted 4 lines

Cheers
Bertrand

#7 lore raw

Re: [PATCH 2/6] xen/arm: ffa: Track hypervisor notifications in a bitmap

Hi Jens,

Quoted 137 lines · Jens Wiklander

Hi Bertrand,

Quoted 65 lines · Bertrand Marquis

Hi Jens,

Thanks a lot for the review.

Quoted 41 lines · Jens Wiklander

Hi Bertrand,

Quoted 35 lines · Bertrand Marquis

Hypervisor notifications are currently tracked with a dedicated buff_full_pending boolean. That state only represents a single HYP notification bit and keeps HYP bitmap handling tied to single-purpose bookkeeping.

Replace the boolean with a hypervisor notification bitmap protected by notif_lock. INFO_GET reports pending when the bitmap is non-zero, GET returns and clears the HYP bitmap under the lock, and RX-buffer-full sets FFA_NOTIF_RX_BUFFER_FULL in the bitmap instead of updating separate state.

Initialize and clear the bitmap during domain lifecycle handling, and use ctx->ffa_id for bitmap create and destroy so the notification state stays tied to the cached FF-A endpoint ID.

No functional changes.

---
xen/arch/arm/tee/ffa_notif.c | 46 ++++++++++++++++++++++++++--------
xen/arch/arm/tee/ffa_private.h | 9 +++++--

2 files changed, 43 insertions(+), 12 deletions(-)
xen/arch/arm/tee/ffa_notif.c +1 -0
index 07bc5cb3a430..d15119409a25 100644
@@ -94,8 +94,15 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs)
 
   notif_pending = test_and_clear_bool(ctx->notif.secure_pending);
   if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+    {
       notif_pending |= test_and_clear_bool(ctx->notif.vm_pending);
 
Quoted 7 lines

xen/arch/arm/tee/ffa_notif.c void ffa_handle_notification_info_get(struct cpu_user_regs *regs) · line 103

Quoted 4 lines
xen/arch/arm/tee/ffa_notif.c +4 -0
+        spin_lock(&ctx->notif.notif_lock);
+        if ( ctx->notif.hyp_pending )
+            notif_pending = true;
+        spin_unlock(&ctx->notif.notif_lock);

Isn't this a functional change? Before this patch, we didn't consider ctx->notif.buff_full_pending here. Am I missing something?

We did consider it implicitly through vm_pending.

This patch makes that cleaner by using hyp_pending for the Hypervisor framework notification itself. Previously, RX-buffer-full was made visible indirectly via vm_pending, and FFA_NOTIFICATION_INFO_GET cleared that summary state.

As a result, the guest-visible pending indication could be lost before the Hypervisor notification was actually retrieved with FFA_NOTIFICATION_GET.

Quoted 5 lines

With this change, the pending state is tracked in hyp_pending and is only cleared when the Hypervisor notifications are retrieved through FFA_NOTIFICATION_GET.

I will reword the commit message to make that clearer.

Thanks

Quoted 58 lines
Quoted 39 lines
Quoted 39 lines
xen/arch/arm/tee/ffa_notif.c +13 -5
+    }
+
   if ( notif_pending )
   {
       /* A pending global notification for the guest */
@@ -174,12 +181,17 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
           w6 = resp.a6;
   }
 
-    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) &&
-          flags & FFA_NOTIF_FLAG_BITMAP_HYP &&
-          test_and_clear_bool(ctx->notif.buff_full_pending) )
+    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
   {
-        ACCESS_ONCE(ctx->notif.vm_pending) = false;
-        w7 = FFA_NOTIF_RX_BUFFER_FULL;
+        spin_lock(&ctx->notif.notif_lock);
+
+        if ( (flags & FFA_NOTIF_FLAG_BITMAP_HYP) && ctx->notif.hyp_pending )
+        {
+            w7 = ctx->notif.hyp_pending;
+            ctx->notif.hyp_pending = 0;
+        }
+
+        spin_unlock(&ctx->notif.notif_lock);
   }
 
   ffa_set_regs(regs, FFA_SUCCESS_32, 0, w2, w3, w4, w5, w6, w7);
@@ -207,12 +219,17 @@ int32_t ffa_handle_notification_set(struct cpu_user_regs *regs)
 void ffa_raise_rx_buffer_full(struct domain *d)
 {
   struct ffa_ctx *ctx = d->arch.tee;
+    uint32_t prev_bitmap;
 
   if ( !ctx )
       return;
 
Quoted 11 lines

xen/arch/arm/tee/ffa_notif.c int32_t ffa_handle_notification_set(struct cpu_user_regs *regs) · line 232

Quoted 8 lines
xen/arch/arm/tee/ffa_notif.c +6 -2
-    ACCESS_ONCE(ctx->notif.buff_full_pending) = true;
-    if ( !test_and_set_bool(ctx->notif.vm_pending) )
+    spin_lock(&ctx->notif.notif_lock);
+    prev_bitmap = ctx->notif.hyp_pending;
+    ctx->notif.hyp_pending |= FFA_NOTIF_RX_BUFFER_FULL;
+    spin_unlock(&ctx->notif.notif_lock);
+
+    if ( !(prev_bitmap & FFA_NOTIF_RX_BUFFER_FULL) )

Do we need to check for FFA_NOTIF_RX_BUFFER_FULL? Isn't !prev_bitmap more accurate, if any other bit would ever be used in the bitmap?

I would keep the bit-specific check here, if that is OK with you.

This function is about raising the RX buffer full notification, so I think it is clearer to check whether that bit was newly pended rather than whether the whole Hypervisor bitmap was previously empty.

Quoted 6 lines
Quoted 4 lines

Using !prev_bitmap would also make the condition depend on unrelated Hypervisor notifications being pending.

Tell me if you are ok with that.

I see your point, it's fine.

In fact while working on this i ended up in something a bit more generic due to your patch 6 findings and at the end i do not raise depending on prev_bitmap anymore but just on if a notification was raised already or not since something was changed which does not depend on hyp_pending anymore.

I am working on v2, might be more clear then :-)

Cheers
Bertrand

Quoted 7 lines

Cheers,
Jens

Quoted 3 lines

Cheers
Bertrand

#20 lore raw

Re: [PATCH 6/6] xen/arm: ffa: Deliver VM-to-VM notifications locally

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 212 lines · Bertrand Marquis

Hi Jens,

Quoted 188 lines · Jens Wiklander

Hi Bertrand,

Quoted 182 lines · Bertrand Marquis

VM notification binding and pending tracking exist for non-secure endpoints, but FFA_NOTIFICATION_SET still only forwards secure destinations to the SPMC. Non-secure VMs therefore cannot receive notifications from other VMs. Local NPI delivery also needs explicit re-arm tracking so repeated raises are not lost while the interrupt is already pending.

Add a local VM notification delivery path for non-secure destinations. notification_set_vm() resolves the destination endpoint, verifies that every requested bit is bound to the sender, sets the receiver's vm_pending bitmap under notif_lock, and raises an NPI only when the receiver transitions from no local pending notifications to some.

Track whether a local NPI is already armed with notif_irq_raised, clear that state once both VM and hypervisor pending bitmaps are drained, and roll back newly-added VM pending bits if no destination vCPU is online. Also expose firmware notification availability so FFA_FEATURES only advertises notification support when it is actually provided by the firmware or by CONFIG_FFA_VM_TO_VM.

Functional impact: when CONFIG_FFA_VM_TO_VM is enabled, non-secure FFA_NOTIFICATION_SET delivers VM-to-VM notifications locally and keeps NPI delivery reliable across repeated raises.

---

xen/arch/arm/tee/ffa.c         |  24 +++++--
xen/arch/arm/tee/ffa_notif.c   | 126 +++++++++++++++++++++++++++++++--
xen/arch/arm/tee/ffa_private.h |  11 ++-
3 files changed, 147 insertions(+), 14 deletions(-)
xen/arch/arm/tee/ffa.c +20 -4
index 1fe33f26454a..7fe021049cba 100644
@@ -39,8 +39,13 @@
 * o FFA_MSG_SEND_DIRECT_REQ:
 *   - only supported from a VM to an SP
 * o FFA_NOTIFICATION_*:
+ *   - only supported when firmware notifications are enabled or VM-to-VM
+ *     support is built in
 *   - only supports global notifications, that is, per vCPU notifications
- *     are not supported
+ *     are not supported and secure per-vCPU notification information is
+ *     not forwarded
+ *   - the source endpoint ID reported for a notification may no longer
+ *     exist by the time the receiver consumes it
 *   - doesn't support signalling the secondary scheduler of pending
 *     notification for secure partitions
 *   - doesn't support notifications for Xen itself
@@ -245,6 +250,8 @@ static void handle_features(struct cpu_user_regs *regs)
    uint32_t a1 = get_user_reg(regs, 1);
    struct domain *d = current->domain;
    struct ffa_ctx *ctx = d->arch.tee;
+    bool notif_supported = IS_ENABLED(CONFIG_FFA_VM_TO_VM) ||
+                           ffa_notif_fw_enabled();
 
    /*
     * FFA_FEATURES defines w2 as input properties only for specific
@@ -343,10 +350,16 @@ static void handle_features(struct cpu_user_regs *regs)
 
        break;
    case FFA_FEATURE_NOTIF_PEND_INTR:
-        ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
        break;
    case FFA_FEATURE_SCHEDULE_RECV_INTR:
-        ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
        break;
    case FFA_PARTITION_INFO_GET_REGS:
        if ( ACCESS_ONCE(ctx->guest_vers) >= FFA_VERSION_1_2 )
@@ -361,7 +374,10 @@ static void handle_features(struct cpu_user_regs *regs)
    case FFA_NOTIFICATION_SET:
    case FFA_NOTIFICATION_INFO_GET_32:
    case FFA_NOTIFICATION_INFO_GET_64:
-        ffa_set_regs_success(regs, 0, 0);
+        if ( notif_supported )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
        break;
    default:
        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
xen/arch/arm/tee/ffa_notif.c +61 -2
index 4def701f0130..e77321244926 100644
@@ -20,7 +20,12 @@ static bool __ro_after_init fw_notif_enabled;
 static unsigned int __ro_after_init notif_sri_irq;
 static DEFINE_SPINLOCK(notif_info_lock);
 
-static void inject_notif_pending(struct domain *d)
+bool ffa_notif_fw_enabled(void)
+{
+    return fw_notif_enabled;
+}
+
+static bool inject_notif_pending(struct domain *d)
 {
    struct vcpu *v;
 
@@ -34,13 +39,15 @@ static void inject_notif_pending(struct domain *d)
        if ( is_vcpu_online(v) )
        {
            vgic_inject_irq(d, v, GUEST_FFA_NOTIF_PEND_INTR_ID, true);
-            return;
+            return true;
        }
    }
 
    if ( printk_ratelimit() )
        printk(XENLOG_G_DEBUG "%pd: ffa: can't inject NPI, all vCPUs offline\n",
               d);
+
+    return false;
 }
 
 static int32_t ffa_notif_parse_params(uint16_t dom_id, uint16_t caller_id,
@@ -104,6 +111,73 @@ out_unlock:
    return ret;
 }
 
+/*
+ * Deliver a VM-to-VM notification. ctx->notif.notif_lock protects
+ * vm_bind/vm_pending so callers must not hold it already.
+ */
+static int32_t notification_set_vm(uint16_t dst_id, uint16_t src_id,
+                                   uint32_t flags, uint64_t bitmap)
+{
+    struct domain *dst_d;
+    struct ffa_ctx *dst_ctx;
+    unsigned int id;
+    int32_t ret;
+    uint64_t prev_bitmap = 0;
+    uint64_t new_bitmap;
+    bool inject = false;
+
+    if ( flags )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    ret = ffa_endpoint_domain_lookup(dst_id, &dst_d, &dst_ctx);
+    if ( ret )
+        return ret;
+
+    ret = FFA_RET_OK;
+
+    spin_lock(&dst_ctx->notif.notif_lock);
+
+    for ( id = 0; id < FFA_NUM_VM_NOTIF; id++ )
+    {
+        if ( !(bitmap & BIT(id, ULL)) )
+            continue;
+
+        if ( dst_ctx->notif.vm_bind[id] != src_id )
+        {
+            ret = FFA_RET_DENIED;
+            goto out_unlock;
+        }
+    }
+
+    prev_bitmap = dst_ctx->notif.vm_pending;
+    dst_ctx->notif.vm_pending |= bitmap;
+    if ( !dst_ctx->notif.notif_irq_raised &&
+         (dst_ctx->notif.vm_pending || dst_ctx->notif.hyp_pending) )
+    {
+        dst_ctx->notif.notif_irq_raised = true;
+        inject = true;
+    }
+
+out_unlock:
+    spin_unlock(&dst_ctx->notif.notif_lock);
+
+    new_bitmap = bitmap & ~prev_bitmap;
+    if ( ret == FFA_RET_OK && inject && new_bitmap &&
Quoted 8 lines

xen/arch/arm/tee/ffa_notif.c out_unlock: · line 169

Quoted 4 lines
xen/arch/arm/tee/ffa_notif.c +4 -0
+         !inject_notif_pending(dst_d) )
+    {
+        spin_lock(&dst_ctx->notif.notif_lock);
+        dst_ctx->notif.vm_pending &= ~new_bitmap;

There's a window above when dst_ctx->notif.notif_lock is unlocked. What if another CPU has modified dst_ctx->notif.vm_pending during that window?

You are right, there is a race in that window. Thanks for the finding :-)

I think the simplest fix is to keep notif_lock held across the injection attempt, so we do not expose partially updated notification state while the interrupt delivery is still in progress.

I do not think we should roll the pending bits back on injection failure, though. In practice the cases where we fail to inject are cases where the target cannot currently take the notification anyway, and dropping the pending state would be worse than keeping it pending and not marking the IRQ as raised.

Quoted 8 lines

So my plan would be to keep the pending state, only mark notif_irq_raised when we actually inject, and keep the existing debug print for the cases we can detect.

If that approach looks OK to you, I will update this patch accordingly.

The same issue exists in patch 2 for the RX-buffer-full path, so I will fix it there as well for the same reason.

Sounds good.

Cheers,
Jens