# \[seL4 kernel\]\[MCS\]\[schedule context donate\]senario of example need your help!

**URL:** <https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632>\
**Category:** seL4 kernel\
**Created:** [December 16, 2022, 7:35am UTC](https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632 "2022-12-16T07:35:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![TPS28225](https://yyz2.discourse-cdn.com/free1/user_avatar/sel4.discourse.group/tps28225/32/208_2.png) [@TPS28225](https://sel4.discourse.group/u/TPS28225)\
**Post date:** [December 16, 2022, 7:35am UTC](https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632/1 "2022-12-16T07:35:03Z")

</div>

I have studied mcs code in seL4 kernel for last three weeks, And I can’t understand the senario as mentiond in seL4-manual-latest.pdf as flollow:  
Could you please give some hint to me, to help me understand the schedule context donation?

 ![image](https://global.discourse-cdn.com/free1/uploads/sel4/original/1X/5e1d49c0da869f2a7a5e1f2ae0e041a84342d78b.jpeg)

In intial state: A have a SC which is a postive thread;B and C are passive threads without SC.  
Then A execute syscall seL4\_call-\>enter seL4 kernel-\>sendIPC  
**Question: Current endpoint state is idle, we can’t donate A’s SC to B with the code show as below, and B can’t execute any user code, such as seL4\_Recv for don’t have a SC. Which means, B have no chance to get A’s SC! \*\*  
\*\* Could you please give some exapmle user code to show me how to make SC donate from A to B to C to be come true?**

 ![image](https://global.discourse-cdn.com/free1/uploads/sel4/original/1X/37066f15b1e4f098ad8100566c15cf9d3d479618.jpeg)

---

<div class="post-metadata">

**Author:** ![TPS28225](https://yyz2.discourse-cdn.com/free1/user_avatar/sel4.discourse.group/tps28225/32/208_2.png) [@TPS28225](https://sel4.discourse.group/u/TPS28225)\
**Post date:** [December 20, 2022, 2:54am UTC](https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632/2 "2022-12-20T02:54:06Z")

</div>

I find the example code at tutorail at mcs

 ![Snipaste_2022-12-20_10-53-33](https://global.discourse-cdn.com/free1/uploads/sel4/original/1X/06eb9083e05c64c0a60b3edc8001335ea48dfc13.png)

---

<div class="post-metadata">

**Author:** ![gerwin.klein](https://yyz2.discourse-cdn.com/free1/user_avatar/sel4.discourse.group/gerwin.klein/32/46_2.png) [@gerwin.klein](https://sel4.discourse.group/u/gerwin.klein)\
**Post date:** [December 20, 2022, 3:38am UTC](https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632/3 "2022-12-20T03:38:03Z")

</div>

Thanks for posting the reply as well!

To leave an explanation here in text form: for scheduling context donation to happen, the server must be waiting on an endpoint (i.e. the endpoint state must be waiting to receive, not idle).

To achieve that, you would usually make the server first active for initialisation, then let the server send a message that it is finished initialising, using `SendRecv` so that the server is now waiting for its first message to handle in “normal” passive operation. To make the server passive, you unbind the scheduling context it had during initialisation. It is now a passive server that with an endpoint in waiting-to-receive state and scheduling context donation can happen.

---

<div class="post-metadata">

**Author:** ![TPS28225](https://yyz2.discourse-cdn.com/free1/user_avatar/sel4.discourse.group/tps28225/32/208_2.png) [@TPS28225](https://sel4.discourse.group/u/TPS28225)\
**Post date:** [December 20, 2022, 6:21am UTC](https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632/4 "2022-12-20T06:21:22Z")

</div>

but I still have doubt at call chain A-\>B-\>C when B is revoked,what will happen?  
As far as I can concer,in hte last seL4\_ReplyRecv(B\_endpoint, , , B\_reply) in task B, it will directly retune in function doReplyTransfer.  
And I can not find the call chain bettwen A and C, It seem like SC in C can’t return back to A after B’s reply cap is revoked?

---

<div class="post-metadata">

**Author:** ![TPS28225](https://yyz2.discourse-cdn.com/free1/user_avatar/sel4.discourse.group/tps28225/32/208_2.png) [@TPS28225](https://sel4.discourse.group/u/TPS28225)\
**Post date:** [December 20, 2022, 6:22am UTC](https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632/5 "2022-12-20T06:22:00Z")

</div>

![Snipaste_2022-12-20_13-48-29](https://global.discourse-cdn.com/free1/uploads/sel4/original/1X/253e5c0e8b22aed0bf7d3df3c93f3c38c3887e93.png)

---

<div class="post-metadata">

**Author:** ![TPS28225](https://yyz2.discourse-cdn.com/free1/user_avatar/sel4.discourse.group/tps28225/32/208_2.png) [@TPS28225](https://sel4.discourse.group/u/TPS28225)\
**Post date:** [December 20, 2022, 6:23am UTC](https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632/6 "2022-12-20T06:23:24Z")

</div>

![Snipaste_2022-12-20_14-06-57](https://global.discourse-cdn.com/free1/uploads/sel4/original/1X/23f7acabf8e55537e0b9559c94f58873c766f8c9.png)

---

<div class="post-metadata">

**Author:** ![TPS28225](https://yyz2.discourse-cdn.com/free1/user_avatar/sel4.discourse.group/tps28225/32/208_2.png) [@TPS28225](https://sel4.discourse.group/u/TPS28225)\
**Post date:** [December 20, 2022, 11:36am UTC](https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632/7 "2022-12-20T11:36:29Z")

</div>

and the word ‘revoke’ which means seL4\_Delete? as seL4\_Revoke only delete child cap of the target cap

---

<div class="post-metadata">

**Author:** ![gerwin.klein](https://yyz2.discourse-cdn.com/free1/user_avatar/sel4.discourse.group/gerwin.klein/32/46_2.png) [@gerwin.klein](https://sel4.discourse.group/u/gerwin.klein)\
**Post date:** [December 20, 2022, 9:58pm UTC](https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632/8 "2022-12-20T21:58:00Z")

</div>

> [@TPS28225](#):
>
> and the word ‘revoke’ which means seL4\_Delete? as seL4\_Revoke only delete child cap of the target cap

To answer the easier one first: revoking a cap means deleting all descendants of a cap, and deleting a cap deletes the cap itself (children of the cap may continue existing).

`Revoke` is used to re-use authority to a resource you have delegated, but still retain control over. `Delete` is usually only useful for managing your own cspace.

To really fully re-use a resource (not just remove authority you have delegated), you would usually revoke the untyped cap that the resource was retyped from. That will clean up all references to this resource any other component in the system might have and reset its memory.

> [@TPS28225](#):
>
> but I still have doubt at call chain A-\>B-\>C when B is revoked,what will happen?  
> […]  
> And I can not find the call chain bettwen A and C, It seem like SC in C can’t return back to A after B’s reply cap is revoked?

I think you are correct, if an object in the call chain is removed (which can happen when the last cap to that object is deleted, e.g. when the untyped cap it was created from is revoked), the reply chain is broken and the SC cannot travel back. You have to have sufficient trust in the system to know the communication structure will not be disturbed for A to reclaim its scheduling context in this scenario.

@mbrcknl @corey.lewis I remember discussions about this during the invariant proofs for MCS. Can you confirm that what I describe above is true?

If you have a dynamic system like that, you’d have to make sure that the components that have sufficient authority to delete things either have the ability to reset A or otherwise restore its operation. To make sense of this behaviour: if you think of IPC with scheduling context donation as a procedure call, this behaviour is similar to the client A calling a procedure B which calls a procedure C. If the memory for B disappears while C is running, you wouldn’t expect execution to return to A – the reply from C wouldn’t make sense to A. You’d instead have some kind of exception and/or recovery mechanism.

---

<div class="post-metadata">

**Author:** ![anna.lyons](https://yyz2.discourse-cdn.com/free1/user_avatar/sel4.discourse.group/anna.lyons/32/4_2.png) [@anna.lyons](https://sel4.discourse.group/u/anna.lyons)\
**Post date:** [December 20, 2022, 10:08pm UTC](https://sel4.discourse.group/t/sel4-kernel-mcs-schedule-context-donate-senario-of-example-need-your-help/632/9 "2022-12-20T22:08:29Z")

</div>

Yes, if a reply object in the middle of the call chain is revoked, the chain is broken and the scheduling context can never go back to the originator.

I originally toyed with designs where we would preserve the chain, but this added a lot of complexity to the kernel for gains we couldn’t find a decent use case for - tearing down a server in the middle of a nested RPC doesn’t seem like something you would want to work at all.
