[XEN PATCH v4] xen/arinc653: fix delay in the start of major frame

Anderson Choi posted 1 patch 3 months, 1 week ago
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/88a17c994f85270982e03912c9c9db916ca69d4e.1753395268.git.anderson.choi@boeing.com
xen/common/sched/arinc653.c | 40 +++++++++++++++++++++----------------
1 file changed, 23 insertions(+), 17 deletions(-)
[XEN PATCH v4] xen/arinc653: fix delay in the start of major frame
Posted by Anderson Choi 3 months, 1 week ago
ARINC653 specification requires partition scheduling to be deterministic
and periodic over time.

However, the use of current timestamp (now) as the baseline to calculate
next_major_frame and next_switch_time introduces a delay in the start of
major frame at every period, which breaks determinism and periodicity in
partition scheduling.

For example, we observe 3.5 msec of accumulated delay at the 21st major
frame with the following configuration.

Target : qemuarm64
xen version : 4.19 (43aeacff86, x86/IRQ: constrain creator-domain-ID assertion)
dom1 : 10 msec runtime
dom2 : 10 msec runtime

$ a653_sched -p Pool-arinc dom1:10 dom2:10

0.014553536 ---x d?v? runstate_change d1v0 runnable->running //1st major
frame
0.034629712 ---x d?v? runstate_change d1v0 runnable->running //2nd major
frame
<snip>
0.397747008 |||x d?v? runstate_change d1v0 runnable->running //20th
major frame
0.418066096 -||x d?v? runstate_change d1v0 runnable->running //21st
major frame

This is due to an inherent delta between the time value the scheduler timer
is programmed to be fired with and the time value the schedule function
is executed.

Another observation that breaks the deterministic behavior of partition
scheduling is a delayed execution of schedule(); It was called 14 msec
later than programmed.

1.530603952 ---x d?v? runstate_change d1v0 runnable->running
1.564956784 --|x d?v? runstate_change d1v0 runnable->running

Enforce the periodic behavior of partition scheduling by using the value
next_major_frame as the base to calculate the start of major frame and
the next domain switch time.

Per discussion with Nathan Studer, there are odd cases the first minor
frame can be also missed. In that scenario, iterate through the schedule
after resyncing the expected next major frame.

Per suggestion from Stewart Hildebrand, use a modulo operation to
calculate the start of next major frame.

Fixes: 22787f2e107c ("ARINC 653 scheduler")
Suggested-by: Stewart Hildebrand <stewart.hildebrand@amd.com>
Suggested-by: Nathan Studer <nathan.studer@dornerworks.com>
Signed-off-by: Anderson Choi <anderson.choi@boeing.com>

---
Changes in v4:
- Check if sched_priv->major_frame is non-zero before calculating the
  start of next major frame
- Update commit message to be consistent with the code change
- Update multi-line comments to comply with CODING_STYLE
- Fix typos

Changes in v3:
- Replace the while loop with the modulo operation to calculate the
  start of next major frame.
- Initialize major_frame and runtime of zeroth schedule entry to
  DEFAULT_TIMESLICE not to use "if" branch in the calculation of next
major frame.

Changes in v2:
- Changed the logic to resync major frame and to find correct
  minor frame after a miss suggested by Nathan
---
---
 xen/common/sched/arinc653.c | 40 +++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 17 deletions(-)

diff --git a/xen/common/sched/arinc653.c b/xen/common/sched/arinc653.c
index 930361fa5c..246ca9b742 100644
--- a/xen/common/sched/arinc653.c
+++ b/xen/common/sched/arinc653.c
@@ -361,6 +361,8 @@ a653sched_init(struct scheduler *ops)
     ops->sched_data = prv;
 
     prv->next_major_frame = 0;
+    prv->major_frame = DEFAULT_TIMESLICE;
+    prv->schedule[0].runtime = DEFAULT_TIMESLICE;
     spin_lock_init(&prv->lock);
     INIT_LIST_HEAD(&prv->unit_list);
 
@@ -526,27 +528,31 @@ a653sched_do_schedule(
 
     spin_lock_irqsave(&sched_priv->lock, flags);
 
-    if ( sched_priv->num_schedule_entries < 1 )
-        sched_priv->next_major_frame = now + DEFAULT_TIMESLICE;
-    else if ( now >= sched_priv->next_major_frame )
+    ASSERT(sched_priv->major_frame > 0);
+
+    /* Switch to next major frame using a modulo operation */
+    if ( now >= sched_priv->next_major_frame )
     {
-        /* time to enter a new major frame
-         * the first time this function is called, this will be true */
-        /* start with the first domain in the schedule */
+        s_time_t major_frame = sched_priv->major_frame;
+        s_time_t remainder = (now - sched_priv->next_major_frame) % major_frame;
+
+        /*
+         * major_frame and schedule[0].runtime contain DEFAULT_TIMESLICE
+         * if num_schedule_entries is zero.
+         */
         sched_priv->sched_index = 0;
-        sched_priv->next_major_frame = now + sched_priv->major_frame;
-        sched_priv->next_switch_time = now + sched_priv->schedule[0].runtime;
+        sched_priv->next_major_frame = now - remainder + major_frame;
+        sched_priv->next_switch_time = now - remainder +
+            sched_priv->schedule[0].runtime;
     }
-    else
+
+    /* Switch minor frame or find correct minor frame after a miss */
+    while ( (now >= sched_priv->next_switch_time) &&
+        (sched_priv->sched_index < sched_priv->num_schedule_entries) )
     {
-        while ( (now >= sched_priv->next_switch_time) &&
-                (sched_priv->sched_index < sched_priv->num_schedule_entries) )
-        {
-            /* time to switch to the next domain in this major frame */
-            sched_priv->sched_index++;
-            sched_priv->next_switch_time +=
-                sched_priv->schedule[sched_priv->sched_index].runtime;
-        }
+        sched_priv->sched_index++;
+        sched_priv->next_switch_time +=
+            sched_priv->schedule[sched_priv->sched_index].runtime;
     }
 
     /*
-- 
2.43.0
Re: [XEN PATCH v4] xen/arinc653: fix delay in the start of major frame
Posted by Stewart Hildebrand 3 months, 1 week ago
On 7/24/25 18:33, Anderson Choi wrote:
> ARINC653 specification requires partition scheduling to be deterministic
> and periodic over time.
> 
> However, the use of current timestamp (now) as the baseline to calculate
> next_major_frame and next_switch_time introduces a delay in the start of
> major frame at every period, which breaks determinism and periodicity in
> partition scheduling.
> 
> For example, we observe 3.5 msec of accumulated delay at the 21st major
> frame with the following configuration.
> 
> Target : qemuarm64
> xen version : 4.19 (43aeacff86, x86/IRQ: constrain creator-domain-ID assertion)
> dom1 : 10 msec runtime
> dom2 : 10 msec runtime
> 
> $ a653_sched -p Pool-arinc dom1:10 dom2:10
> 
> 0.014553536 ---x d?v? runstate_change d1v0 runnable->running //1st major
> frame
> 0.034629712 ---x d?v? runstate_change d1v0 runnable->running //2nd major
> frame
> <snip>
> 0.397747008 |||x d?v? runstate_change d1v0 runnable->running //20th
> major frame
> 0.418066096 -||x d?v? runstate_change d1v0 runnable->running //21st
> major frame
> 
> This is due to an inherent delta between the time value the scheduler timer
> is programmed to be fired with and the time value the schedule function
> is executed.
> 
> Another observation that breaks the deterministic behavior of partition
> scheduling is a delayed execution of schedule(); It was called 14 msec
> later than programmed.
> 
> 1.530603952 ---x d?v? runstate_change d1v0 runnable->running
> 1.564956784 --|x d?v? runstate_change d1v0 runnable->running
> 
> Enforce the periodic behavior of partition scheduling by using the value
> next_major_frame as the base to calculate the start of major frame and
> the next domain switch time.
> 
> Per discussion with Nathan Studer, there are odd cases the first minor
> frame can be also missed. In that scenario, iterate through the schedule
> after resyncing the expected next major frame.
> 
> Per suggestion from Stewart Hildebrand, use a modulo operation to
> calculate the start of next major frame.
> 
> Fixes: 22787f2e107c ("ARINC 653 scheduler")
> Suggested-by: Stewart Hildebrand <stewart.hildebrand@amd.com>
> Suggested-by: Nathan Studer <nathan.studer@dornerworks.com>
> Signed-off-by: Anderson Choi <anderson.choi@boeing.com>

Reviewed-by: Stewart Hildebrand <stewart@stew.dk>
Re: [XEN PATCH v4] xen/arinc653: fix delay in the start of major frame
Posted by Dario Faggioli 3 months ago
On Thu, 2025-07-24 at 21:28 -0400, Stewart Hildebrand wrote:
> On 7/24/25 18:33, Anderson Choi wrote:
> > 
> > Fixes: 22787f2e107c ("ARINC 653 scheduler")
> > Suggested-by: Stewart Hildebrand <stewart.hildebrand@amd.com>
> > Suggested-by: Nathan Studer <nathan.studer@dornerworks.com>
> > Signed-off-by: Anderson Choi <anderson.choi@boeing.com>
> 
> Reviewed-by: Stewart Hildebrand <stewart@stew.dk>
>
Ok, cool, there were versions of these series that my filters did miss,
and that's why I replied to v1. :-/

Well, scratch that email, and sorry for the noise.

Regards,
-- 
Dario Faggioli, Ph.D
http://about.me/dario.faggioli
Virtualization Software Engineer
SUSE Labs, SUSE https://www.suse.com/
-------------------------------------------------------------------
<<This happens because _I_ choose it to happen!>> (Raistlin Majere)
RE: [XEN PATCH v4] xen/arinc653: fix delay in the start of major frame
Posted by Choi, Anderson 3 months ago
Dario,

> On Thu, 2025-07-24 at 21:28 -0400, Stewart Hildebrand wrote:
>> On 7/24/25 18:33, Anderson Choi wrote:
>>> 
>>> Fixes: 22787f2e107c ("ARINC 653 scheduler")
>>> Suggested-by: Stewart Hildebrand <stewart.hildebrand@amd.com>
>>> Suggested-by: Nathan Studer <nathan.studer@dornerworks.com>
>>> Signed-off-by: Anderson Choi <anderson.choi@boeing.com>
>> 
>> Reviewed-by: Stewart Hildebrand <stewart@stew.dk>
>> 
> Ok, cool, there were versions of these series that my filters did 
> miss, and that's why I replied to v1. :-/
> 
> Well, scratch that email, and sorry for the noise.
> 
> Regards,

I appreciate your feedback.

Could I get your ACK for v4 patch as well as for v1?

Thanks,
Anderson


Re: [XEN PATCH v4] xen/arinc653: fix delay in the start of major frame
Posted by Andrew Cooper 3 months ago
On 31/07/2025 11:52 pm, Choi, Anderson wrote:
> Dario,
>
>> On Thu, 2025-07-24 at 21:28 -0400, Stewart Hildebrand wrote:
>>> On 7/24/25 18:33, Anderson Choi wrote:
>>>> Fixes: 22787f2e107c ("ARINC 653 scheduler")
>>>> Suggested-by: Stewart Hildebrand <stewart.hildebrand@amd.com>
>>>> Suggested-by: Nathan Studer <nathan.studer@dornerworks.com>
>>>> Signed-off-by: Anderson Choi <anderson.choi@boeing.com>
>>> Reviewed-by: Stewart Hildebrand <stewart@stew.dk>
>>>
>> Ok, cool, there were versions of these series that my filters did 
>> miss, and that's why I replied to v1. :-/
>>
>> Well, scratch that email, and sorry for the noise.
>>
>> Regards,
> I appreciate your feedback.
>
> Could I get your ACK for v4 patch as well as for v1?

It was committed on Monday

https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=f2927d8343aef714339fa455074bf56d2b878b3e

and backported to 4.20 and 4.19 already.

~Andrew
RE: [EXTERNAL] Re: [XEN PATCH v4] xen/arinc653: fix delay in the start of major frame
Posted by Choi, Anderson 3 months ago
Andrew,

> EXT email: be mindful of links/attachments.
> 
> On 31/07/2025 11:52 pm, Choi, Anderson wrote:
>> Dario,
>> 
>>> On Thu, 2025-07-24 at 21:28 -0400, Stewart Hildebrand wrote:
>>>> On 7/24/25 18:33, Anderson Choi wrote:
>>>>> Fixes: 22787f2e107c ("ARINC 653 scheduler")
>>>>> Suggested-by: Stewart Hildebrand <stewart.hildebrand@amd.com>
>>>>> Suggested-by: Nathan Studer <nathan.studer@dornerworks.com>
>>>>> Signed-off-by: Anderson Choi <anderson.choi@boeing.com>
>>>> Reviewed-by: Stewart Hildebrand <stewart@stew.dk>
>>>> 
>>> Ok, cool, there were versions of these series that my filters did
>>> miss, and that's why I replied to v1. :-/
>>> 
>>> Well, scratch that email, and sorry for the noise.
>>> 
>>> Regards,
>> I appreciate your feedback.
>> 
>> Could I get your ACK for v4 patch as well as for v1?
> 
> It was committed on Monday
> 
> https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=f2927d8343aef714339
> fa455074bf56d2b878b3e
> 
> and backported to 4.20 and 4.19 already.
> 
> ~Andrew

Oh, thanks for sharing the latest status! I haven't noticed that.

Have a great day!

Thanks,
Anderson