FUTEX_SWAP (tip/locking/core)
cover FUTEX_SWAP (tip/locking/core)
From: Peter Oskolkov <[email protected]>
As Paul Turner presented at LPC in 2013 ...
- pdf: http://pdxplumbers.osuosl.org/2013/ocw//system/presentations/1653/original/LPC%20-%20User%20Threading.pdf
- video: https://www.youtube.com/watch?v=KXuZi9aeGTw
... Google has developed an M:N userspace threading subsystem backed by Google-private SwitchTo Linux Kernel API (page 17 in the pdf referenced above). This subsystem provides latency-sensitive services at Google with fine-grained user-space control/scheduling over what is running when, and this subsystem is used widely internally (called schedulers or fibers).
This patchset is the first step to open-source this work. As explained in the linked pdf and video, SwitchTo API has three core operations: wait, resume, and swap (=switch). So this patchset adds a FUTEX_SWAP operation that, in addition to FUTEX_WAIT and FUTEX_WAKE, will provide a foundation on top of which user-space threading libraries can be built.
Another common use case for FUTEX_SWAP is message passing a-la RPC between tasks: task/thread T1 prepares a message,
wakes T2 to work on it, and waits for the results; when T2 is done, it wakes T1 and waits for more work to arrive. Currently the simplest way to implement this is
a. T1: futex-wake T2, futex-wait
b. T2: wakes, does what it has been woken to do
c. T2: futex-wake T1, futex-wait
With FUTEX_SWAP, steps a and c above can be reduced to one futex operation that runs 5-10 times faster.
Patches in this patchset:
Patch 1: introduce FUTEX_SWAP futex operation that,
internally, does wake + wait. The purpose of this patch is to work out the API.
Patch 2: a first rough attempt to make FUTEX_SWAP faster than
what wake + wait can do.
Patch 3: a selftest that can also be used to benchmark FUTEX_SWAP vs
FUTEX_WAKE + FUTEX_WAIT.
Peter Oskolkov (3):
futex: introduce FUTEX_SWAP operation
futex/sched: add wake_up_process_prefer_current_cpu, use in FUTEX_SWAP selftests/futex: add futex_swap selftest
include/linux/sched.h | 1 + include/uapi/linux/futex.h | 2 + kernel/futex.c | 89 ++++++-- kernel/sched/core.c | 5 + kernel/sched/fair.c | 3 + kernel/sched/sched.h | 1 + .../selftests/futex/functional/.gitignore | 1 + .../selftests/futex/functional/Makefile | 1 + .../selftests/futex/functional/futex_swap.c | 209 ++++++++++++++++++ .../selftests/futex/include/futextest.h | 19 ++ 10 files changed, 315 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/futex/functional/futex_swap.c
Signature · 1 line
2.25.1
PATCH 1/3 futex: introduce FUTEX_SWAP operation
From: Peter Oskolkov <[email protected]>
As Paul Turner presented at LPC in 2013 ...
- pdf: http://pdxplumbers.osuosl.org/2013/ocw//system/presentations/1653/original/LPC%20-%20User%20Threading.pdf
- video: https://www.youtube.com/watch?v=KXuZi9aeGTw
... Google has developed an M:N userspace threading subsystem backed by Google-private SwitchTo Linux Kernel API (page 17 in the pdf referenced above). This subsystem provides latency-sensitive services at Google with fine-grained user-space control/scheduling over what is running when, and this subsystem is used widely internally (called schedulers or fibers).
This patchset is the first step to open-source this work. As explained in the linked pdf and video, SwitchTo API has three core operations: wait, resume, and swap (=switch). So this patchset adds a FUTEX_SWAP operation that, in addition to FUTEX_WAIT and FUTEX_WAKE, will provide a foundation on top of which user-space threading libraries can be built.
Another common use case for FUTEX_SWAP is message passing a-la RPC between tasks: task/thread T1 prepares a message,
wakes T2 to work on it, and waits for the results; when T2 is done, it wakes T1 and waits for more work to arrive. Currently the simplest way to implement this is
a. T1: futex-wake T2, futex-wait
b. T2: wakes, does what it has been woken to do
c. T2: futex-wake T1, futex-wait
With FUTEX_SWAP, steps a and c above can be reduced to one futex operation that runs 5-10 times faster.
Patches in this patchset:
Patch 1: (this patch) introduce FUTEX_SWAP futex operation that,
internally, does wake + wait. The purpose of this patch is to work out the API.
Patch 2: a first rough attempt to make FUTEX_SWAP faster than
what wake + wait can do.
Patch 3: a selftest that can also be used to benchmark FUTEX_SWAP vs
FUTEX_WAKE + FUTEX_WAIT.
Tested: see patch 3 in this patchset.
--- include/uapi/linux/futex.h | 2 + kernel/futex.c | 90 +++++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 16 deletions(-)
include/uapi/linux/futex.h +2 -0
@@ -21,6 +21,7 @@#define FUTEX_WAKE_BITSET 10#define FUTEX_WAIT_REQUEUE_PI 11#define FUTEX_CMP_REQUEUE_PI 12 +#define FUTEX_SWAP 13#define FUTEX_PRIVATE_FLAG 128#define FUTEX_CLOCK_REALTIME 256
@@ -40,6 +41,7 @@ FUTEX_PRIVATE_FLAG)#define FUTEX_CMP_REQUEUE_PI_PRIVATE (FUTEX_CMP_REQUEUE_PI | \ FUTEX_PRIVATE_FLAG) +#define FUTEX_SWAP_PRIVATE (FUTEX_SWAP | FUTEX_PRIVATE_FLAG)/* * Support for robust futexes: the kernel cleans up held futexes at
kernel/futex.c +74 -16
201 lines not on this page · raw message
Signature · 1 line
2.25.1
Peter Zijlstra
maintainerRe: [PATCH for 5.9 1/3] futex: introduce FUTEX_SWAP operation
Quoted 5 lines · Peter Oskolkov
This patchset is the first step to open-source this work. As explained in the linked pdf and video, SwitchTo API has three core operations: wait, resume, and swap (=switch). So this patchset adds a
FUTEX_SWAPoperation that, in addition toFUTEX_WAITandFUTEX_WAKE, will provide a foundation on top of which user-space threading libraries can be built.
The PDF and video can go pound sand; you get to fully explain things here.
What worries me is how FUTEX_SWAP would interact with the future FUTEX_LOCK / FUTEX_UNLOCK. When we implement pthread_mutex with those, there's very few WAIT/WAKE left.
Also, why would we commit to an ABI without ever having seen the rest?
On another note: wake_up_process_prefer_current_cpu() is a horrific function name :/ That's half to a third of the line limit.
Re: [PATCH for 5.9 1/3] futex: introduce FUTEX_SWAP operation
On Thu, Jul 23, 2020 at 4:28 AM Peter Zijlstra <[email protected]> wrote:
Thanks a lot for your comments, Peter! My answers below.
Quoted 10 lines · Peter Oskolkov
Quoted 5 lines
This patchset is the first step to open-source this work. As explained in the linked pdf and video, SwitchTo API has three core operations: wait, resume, and swap (=switch). So this patchset adds a
FUTEX_SWAPoperation that, in addition toFUTEX_WAITandFUTEX_WAKE, will provide a foundation on top of which user-space threading libraries can be built.The PDF and video can go pound sand; you get to fully explain things here.
Will do. Should I expand the cover letter or the commit message? (I'll probably split the first patch into two in the latter case).
Quoted 4 lines
What worries me is how
FUTEX_SWAPwould interact with the futureFUTEX_LOCK/FUTEX_UNLOCK. When we implementpthread_mutexwith those, there's very few WAIT/WAKE left.
[+cc Waiman Long]
I've looked through the latest FUTEX_LOCK patchset I could find (
https://lore.kernel.org/patchwork/cover/772643/ and related), and it seems that FUTEX_SWAP and FUTEX_LOCK/FUTEX_UNLOCK patchsets
address the same issue (slow wakeups) but for different use cases:
FUTEX_LOCK/FUTEX_UNLOCK uses spinning and lock stealing to improve futex wake/wait performance in high contention situations; FUTEX_SWAP is designed to be used for fast context switching with _no_ contention by design: the waker that is going to sleep, and the wakee are using different futexes; the userspace will have a futex per thread/task, and when needed the thread/task will either simply sleep on its futex, or context switch (=FUTEX_SWAP) into a different thread/task.
I can also imagine that instead of combining WAIT/WAKE for fast context switching, a variant of FUTEX_SWAP can use LOCK/UNLOCK operations in the future, when these are available; but again, I fully expect that
a single "FUTEX_LOCK the current task on futex A, FUTEX_UNLOCK futex B, context switch into the wakee" futex op will be much faster than doing the same thing
in two syscalls, as FUTEX_LOCK/FUTEX_UNLOCK does not seem to be concerned with fast waking of a sleeping task, but more with minimizing sleeping in the first place.
What will be faster: FUTEX_SWAP that does
FUTEX_WAKE (futex A) + FUTEX_WAIT (current, futex B),
or FUTEX_SWAP that does
FUTEX_UNLOCK (futex A) + FUTEX_LOCK (current, futex B)?
As wake+wait will always put the waker to sleep, it means that there will be a true context switch on the same CPU on the fast path; on the other hand, unlock+lock will potentially evade sleeping, so the wakee will often run on a different CPU (with the waker spinning instead of sleeping?), thus not benefitting from cache locality that fast context switching on the same CPU is meant to use...
I'll add some of the considerations above to the expanded cover letter (or a commit message).
Quoted 2 lines
Also, why would we commit to an ABI without ever having seen the rest?
I'm not completely sure what you mean here. We do not envision any expansion/changes to the ABI proposed here, only further performance improvements. On these, we currently think that marking the wakee as the preferred next task to run on the current CPU (by storing "struct task_struct *preferred_next_tast" either in a per-CPU pointer, or in the current task_struct) and then having schedule() determine whether to follow the hint or ignore it would be the simplest way to speed up the context switch.
Quoted 3 lines
On another note:
wake_up_process_prefer_current_cpu()is a horrific function name :/ That's half to a third of the line limit.
I fully agree. I considered wake_up_on_current_cpu() first, but this name does not indicate that the wakeup is a "strong wish", but "current cpu" is a weak
one... Do you have any suggestions? Maybe
wake_up_on_cpu(struct task_struct *next, int cpu_hint)?
But this seems too broad in scope, as we are interested here in only migrating the task to the current CPU...
Thanks again for your comments!
Re: [PATCH for 5.9 1/3] futex: introduce FUTEX_SWAP operation
Quoted 10 lines · Peter Oskolkov
On Thu, Jul 23, 2020 at 4:28 AM Peter Zijlstra <[email protected]> wrote:
Thanks a lot for your comments, Peter! My answers below.
Quoted 6 lines · Peter Oskolkov
Quoted 5 lines
This patchset is the first step to open-source this work. As explained in the linked pdf and video, SwitchTo API has three core operations: wait, resume, and swap (=switch). So this patchset adds a
FUTEX_SWAPoperation that, in addition toFUTEX_WAITandFUTEX_WAKE, will provide a foundation on top of which user-space threading libraries can be built.
Quoted 4 lines
Quoted 2 lines
The PDF and video can go pound sand; you get to fully explain things here.
Will do. Should I expand the cover letter or the commit message? (I'll probably split the first patch into two in the latter case).
You should put it mostly in the commit message which will be part of the git log history. The cover letter, on the other hand, is not part of the git log.
Quoted 11 lines
Quoted 4 lines
What worries me is how
FUTEX_SWAPwould interact with the futureFUTEX_LOCK/FUTEX_UNLOCK. When we implementpthread_mutexwith those, there's very few WAIT/WAKE left.[+cc Waiman Long]
I've looked through the latest
FUTEX_LOCKpatchset I could find (
https://lore.kernel.org/patchwork/cover/772643/ and related), and it seems thatFUTEX_SWAPand FUTEX_LOCK/FUTEX_UNLOCK patchsets
address the same issue (slow wakeups) but for different use cases:
Quoted 7 lines
FUTEX_LOCK/FUTEX_UNLOCK uses spinning and lock stealing to improve futex wake/wait performance in high contention situations;
FUTEX_SWAPis designed to be used for fast context switching with _no_ contention by design: the waker that is going to sleep, and the wakee are using different futexes; the userspace will have a futex per thread/task, and when needed the thread/task will either simply sleep on its futex, or context switch (=FUTEX_SWAP) into a different thread/task.
I agree that it is a different use case. I just hope that you keep the possible future extension to support FUTEX_LOCK/UNLOCK in mind so that they won't conflict with each other.
Cheers,
Longman
Re: [PATCH for 5.9 1/3] futex: introduce FUTEX_SWAP operation
Quoted 12 lines · Waiman Long
Quoted 10 lines · Peter Oskolkov
On Thu, Jul 23, 2020 at 4:28 AM Peter Zijlstra <[email protected]> wrote:
Thanks a lot for your comments, Peter! My answers below.
Quoted 6 lines · Peter Oskolkov
Quoted 5 lines
This patchset is the first step to open-source this work. As explained in the linked pdf and video, SwitchTo API has three core operations: wait, resume, and swap (=switch). So this patchset adds a
FUTEX_SWAPoperation that, in addition toFUTEX_WAITandFUTEX_WAKE, will provide a foundation on top of which user-space threading libraries can be built.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
The PDF and video can go pound sand; you get to fully explain things here.
Will do. Should I expand the cover letter or the commit message? (I'll probably split the first patch into two in the latter case).
You should put it mostly in the commit message which will be part of the git log history. The cover letter, on the other hand, is not part of the git log.
Ack. (Networking/David Miller usually includes the cover letter in the git log, so this
is context dependent, I guess).
Quoted 21 lines
Quoted 13 lines
Quoted 6 lines
What worries me is how
FUTEX_SWAPwould interact with the futureFUTEX_LOCK/FUTEX_UNLOCK. When we implementpthread_mutexwith those, there's very few WAIT/WAKE left.[+cc Waiman Long]
I've looked through the latest
FUTEX_LOCKpatchset I could find (
https://lore.kernel.org/patchwork/cover/772643/ and related), and it seems thatFUTEX_SWAPand FUTEX_LOCK/FUTEX_UNLOCK patchsets
address the same issue (slow wakeups) but for different use cases:Quoted 7 lines
FUTEX_LOCK/FUTEX_UNLOCK uses spinning and lock stealing to improve futex wake/wait performance in high contention situations;
FUTEX_SWAPis designed to be used for fast context switching with _no_ contention by design: the waker that is going to sleep, and the wakee are using different futexes; the userspace will have a futex per thread/task, and when needed the thread/task will either simply sleep on its futex, or context switch (=FUTEX_SWAP) into a different thread/task.
Quoted 3 lines
I agree that it is a different use case. I just hope that you keep the possible future extension to support FUTEX_LOCK/UNLOCK in mind so that they won't conflict with each other.
Ack. Will do. :)
Thanks,
Peter
Quoted 4 lines
Cheers,
Longman
[email protected]
maintainerRe: [PATCH for 5.9 1/3] futex: introduce FUTEX_SWAP operation
In reply to Peter OskolkovQuoted 8 lines · Peter Oskolkov
Quoted 3 lines · Peter Zijlstra
What worries me is how
FUTEX_SWAPwould interact with the futureFUTEX_LOCK/FUTEX_UNLOCK. When we implementpthread_mutexwith those, there's very few WAIT/WAKE left.[+cc Waiman Long]
Quoted 7 lines
I've looked through the latest
FUTEX_LOCKpatchset I could find (
https://lore.kernel.org/patchwork/cover/772643/ and related), and it seems thatFUTEX_SWAPand FUTEX_LOCK/FUTEX_UNLOCK patchsets
address the same issue (slow wakeups) but for different use cases:FUTEX_LOCK/FUTEX_UNLOCK uses spinning and lock stealing to improve futex wake/wait performance in high contention situations;
The spinning is best for light contention.
Quoted 5 lines
FUTEX_SWAPis designed to be used for fast context switching with _no_ contention by design: the waker that is going to sleep, and the wakee are using different futexes; the userspace will have a futex per thread/task, and when needed the thread/task will either simply sleep on its futex, or context switch (=FUTEX_SWAP) into a different thread/task.
Right, but how can you tell what the next thing after UNLOCK is going to be.. that's going to be tricky.
Quoted 10 lines
I can also imagine that instead of combining WAIT/WAKE for fast context switching, a variant of
FUTEX_SWAPcan use LOCK/UNLOCK operations in the future, when these are available; but again, I fully expect that
a single "FUTEX_LOCKthe current task on futex A,FUTEX_UNLOCKfutex B, context switch into the wakee" futex op will be much faster than doing the same thing
in two syscalls, as FUTEX_LOCK/FUTEX_UNLOCK does not seem to be concerned with fast waking of a sleeping task, but more with minimizing sleeping in the first place.
Correct; however the reason I like LOCK/UNLOCK is that it exposes the blocking relations to the kernel -- and that ties into yet another unfinished patch-set :-/
https://lkml.kernel.org/r/[email protected]
Quoted 4 lines
What will be faster:
FUTEX_SWAPthat does
FUTEX_WAKE(futex A) +FUTEX_WAIT(current, futex B),
orFUTEX_SWAPthat does
FUTEX_UNLOCK(futex A) +FUTEX_LOCK(current, futex B)?
Well, perhaps both argue against having SWAP but instead having compound futex ops. Something I think we're already starting to see with the new futex API patches posted here:
https://lkml.kernel.org/r/[email protected]
sys_futex_waitv() is effectively a whole bunch of WAIT ops combined.
Quoted 9 lines
As wake+wait will always put the waker to sleep, it means that there will be a true context switch on the same CPU on the fast path; on the other hand, unlock+lock will potentially evade sleeping, so the wakee will often run on a different CPU (with the waker spinning instead of sleeping?), thus not benefitting from cache locality that fast context switching on the same CPU is meant to use...
I'll add some of the considerations above to the expanded cover letter (or a commit message).
It's Monday morning, so perhaps I'm making a mess of things, but something like the below, where our thread t2 issues synchronous work to t1:
t1 t2 LOCK A LOCK B
1: LOCK A
... UNLOCK A LOCK B ... UNLOCK B UNLOCK A LOCK B GOTO 1 UNLOCK B LOCK A ...
Is an abuse of mutexes, that is, it implements completions using a (fair) mutex. A guards the work-queue, B is the 'completion'.
Then, if you teach it that a compound UNLOCK-A + LOCK-B, where the lock owner of B is on the wait list of A is a 'SWAP', should get you the desired semantics, I think.
You can do SWAP and you get to have an exposed blocking relation.
Is this exactly what we want, I don't know. Because I'm not entirely sure what problem we're solving. Why would you be setting things up like that in the first place. IIRC you're using this to implement coroutines in golang, and I'm not sure I have a firm enough grasp of all that to make a cogent suggestion one way or the other.
Quoted 4 lines
Quoted 1 line
Also, why would we commit to an ABI without ever having seen the rest?
I'm not completely sure what you mean here. We do not envision any expansion/changes to the ABI proposed here,
Well, you do not, but how can we verify this without having a complete picture. Also, IIRC, there's a bunch of scheduler patches that goes on top to implement the fast switch.
Also, how does this compare to some of the glorious hacks found in GNU Pth? You can implement M:N threading using those as well.
Re: [PATCH for 5.9 1/3] futex: introduce FUTEX_SWAP operation
[+cc BPF maintainers]
I added a lot of details below... Maybe too much? Let me know...
Quoted 6 lines · On Mon, Jul 27, 2020 at 2:51 AM <[email protected]> wrote:
Quoted 5 lines · Peter Oskolkov
Quoted 3 lines · Peter Zijlstra
What worries me is how
FUTEX_SWAPwould interact with the futureFUTEX_LOCK/FUTEX_UNLOCK. When we implementpthread_mutexwith those, there's very few WAIT/WAKE left.
[,,,]
Quoted 4 lines
Quoted 2 lines
FUTEX_LOCK/FUTEX_UNLOCK uses spinning and lock stealing to improve futex wake/wait performance in high contention situations;
The spinning is best for light contention.
Right. But the goal here is to enable fast context switching, not deal with any kind of contention, light or heavy... See more on that below.
Quoted 9 lines
Quoted 6 lines
FUTEX_SWAPis designed to be used for fast context switching with _no_ contention by design: the waker that is going to sleep, and the wakee are using different futexes; the userspace will have a futex per thread/task, and when needed the thread/task will either simply sleep on its futex, or context switch (=FUTEX_SWAP) into a different thread/task.Right, but how can you tell what the next thing after UNLOCK is going to be.. that's going to be tricky.
Yes, but it is somewhat irrelevant here: if task A wants task B to run in its place, and task B is "cooperating", let them do it... Again, see below on what we would like to achieve here.
[,,,]
Quoted 5 lines
Correct; however the reason I like LOCK/UNLOCK is that it exposes the blocking relations to the kernel -- and that ties into yet another unfinished patch-set :-/
Futexes were initially introduced, I believe, as barebone kernel primitives for the userspace to build various synchronization mechanisms on top of. Explicitly keeping the kernel agnostic to blocking relations.
While I understand and fully support the desire to have more sophisticated machinery in the kernel (like the LOCK/UNLOCK work, and the proxy execution patchset referenced above), FUTEX_SWAP is more aligned with the initial "keep it simple" approach, i.e. enable a fast context switch, let the userspace deal with complexity.
Again, see more below.
Quoted 5 lines
Quoted 4 lines
What will be faster:
FUTEX_SWAPthat does
FUTEX_WAKE(futex A) +FUTEX_WAIT(current, futex B),
orFUTEX_SWAPthat does
FUTEX_UNLOCK(futex A) +FUTEX_LOCK(current, futex B)?
Quoted 7 lines
Well, perhaps both argue against having SWAP but instead having compound futex ops. Something I think we're already starting to see with the new futex API patches posted here:
https://lkml.kernel.org/r/[email protected]
sys_futex_waitv()is effectively a whole bunch of WAIT ops combined.
I'm not sure how to build a fast context switch out of multiple related wait ops...
Quoted 36 lines
Quoted 10 lines
As wake+wait will always put the waker to sleep, it means that there will be a true context switch on the same CPU on the fast path; on the other hand, unlock+lock will potentially evade sleeping, so the wakee will often run on a different CPU (with the waker spinning instead of sleeping?), thus not benefitting from cache locality that fast context switching on the same CPU is meant to use...
I'll add some of the considerations above to the expanded cover letter (or a commit message).
It's Monday morning, so perhaps I'm making a mess of things, but something like the below, where our thread t2 issues synchronous work to t1:
t1 t2
LOCK A
LOCK B
1: LOCK A... UNLOCK A LOCK B ... UNLOCK B UNLOCK A LOCK B GOTO 1 UNLOCK B LOCK A ...
Quoted 8 lines
Is an abuse of mutexes, that is, it implements completions using a (fair) mutex. A guards the work-queue, B is the 'completion'.
Then, if you teach it that a compound UNLOCK-A + LOCK-B, where the lock owner of B is on the wait list of A is a 'SWAP', should get you the desired semantics, I think.
You can do SWAP and you get to have an exposed blocking relation.
This will work for two threads. However, if a process has an arbitrary number of threads (tasks), I'm not sure how to construct something similar that would allow any task to swap into (= context switch into) any other task (and have the tasks dynamically come and go).
With FUTEX_SWAP, each task has its own futex, and futexes are locked (= waited on) only by their owners, so no cross-locking is needed as in above.
Quoted 5 lines
Is this exactly what we want, I don't know. Because I'm not entirely sure what problem we're solving. Why would you be setting things up like that in the first place. IIRC you're using this to implement coroutines in golang, and I'm not sure I have a firm enough grasp of all that to make a cogent suggestion one way or the other.
OK, here is the main wall of text in this message... :)
A simplified/idealized use case: imagine a multi-user service application (e.g. a DBMS) that has to implement the following user CPU quota policy:
- each user (these are DBMS users, not Linux users) can purchase certain amounts of expensive, but guaranteed, low-latency
CPU quota (as a % of total CPUs available to the service), and a certain amount of cheap high-latency CPU quota;
- quotas are enforced per second;
- each user RPC/request to the service can specify whether this is a latency-critical request that should use the user's low-latency quota, and be immediately rejected if the quota for this second is exhausted;
- requests can be high-latency-tolerant: should only use the high-latency quota;
- a request can also be latency-tolerant: it should use the low-latency quota if available, or the high-latency quota if the low-latency quota is exhausted;
- all "sold" (= allocated) low-latency quotas can go up to, but not exceed, 70% of all available CPUs (i.e. no over-subscription);
- high-latency quotas are oversubscribed;
- user isolation: misbehaving users should not affect the serving latency of users with available low-latency quotas;
- soft deadlines/timeouts: each request/RPC can specify that it must be served within a certain deadline (let's say the minimum deadline is 10 milliseconds) or dropped if the deadline is exceeded;
- each user request can potentially spawn several processing threads/tasks, and do child RPCs to remote services; these threads/tasks should also be covered by this quota/policy;
- user requests should be served somewhat in-order: requests that use the same quota tiers that arrive earlier should be granted CPU before requests that arrive later ("arrival order scheduling").
There are many services at Google that implement a variant of the scheduling policy outlined above. In reality there are more priorities/quota tiers, there is service-internal maintenance work that can be either high or low priority, sometimes FIFO/LIFO/round robin scheduling is used in addition to arrival order scheduling, etc. (for example, LIFO scheduling is better at cache-locality in certain scenarios). User isolation within a process, as well as latency improvements are the main benefits (on top of the actual ability to implement complex quota/scheduling policies).
What is important is that these scheduling policies are driven by userspace schedulers built on top of these basic kernel primitives:
- block: block the current thread/task (with a timeout);
- resume: resume some previously blocked task (should commutate with block, i.e. racing block/resume pairs should behave exactly as if wake arrived after block);
- switch_to: block the current thread, resume some previously blocked task (behaves exactly as wake(remote), block(current), but optimized to do a fast context switch on the fast path);
- block detection: when a task blocks in the kernel (on a network read, for example), the userspace scheduler is notified and schedules (resumes or swaps into) a pending task in the newly available CPU slot;
- wake detection: when a task wakes from a previously blocking kernel operation (e.g. can now process some data on a network socket), the userspace scheduler is notified and can now schedule the task to run on a CPU when a CPU is available and the task can use it according to its scheduling policy.
(Technically, block/wake detection is still experimental and not used widely: as we control the userspace, we can actually determine blocking/waking syscalls without kernel support).
Internally we currently use kernel patches that are too "intrusive" to be included in a general-purpose Linux kernel, so we are exploring ways to upstream this functionality.
The easiest/least intrusive approach that we have come up with is this:
- block/resume map perfectly to futex wait/wake;
- switch_to thus maps to FUTEX_SWAP;
- block and wake detection can be done either through tracing or by introducing new BPF attach points (when a task blocks or wakes, a BPF program is triggered that then communicates with the userspace);
- the BPF attach points are per task, and the task needs to "opt in" (i.e. all other tasks suffer just an additional pointer comparison on block/wake);
- the BPF programs triggered on block/wake should be able to perform futex ops (e.g. wake a designated userspace scheduling task) - this probably indicates that tracing is not enough, and a new BPF prog type is needed.
[+cc BPF maintainers for the above].
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Also, why would we commit to an ABI without ever having seen the rest?
I'm not completely sure what you mean here. We do not envision any expansion/changes to the ABI proposed here,
Well, you do not, but how can we verify this without having a complete picture. Also, IIRC, there's a bunch of scheduler patches that goes on top to implement the fast switch.
I hope my wall of text above clarifies things a bit.
Quoted 2 lines
Also, how does this compare to some of the glorious hacks found in GNU Pth? You can implement M:N threading using those as well.
It seems GNU Pth tries to emulate kernel scheduling in the userspace, while we would like to expose basic scheduling building blocks to the userspace and let it do anything it feels like...
PATCH 2/3 futex/sched: add wake_up_process_prefer_current_cpu, use in FUTEX_SWAP
In reply to Peter OskolkovFrom: Peter Oskolkov <[email protected]>
As described in the previous patch in this patchset ("futex: introduce FUTEX_SWAP operation"), it is often beneficial to wake a task and run it on the same CPU where the current going to sleep task it running.
Internally at Google, switchto_switch sycall not only migrates the wakee to the current CPU, but also moves the waker's load stats to the wakee, thus ensuring that the migration to the current CPU does not interfere with load balancing. switchto_switch also does the context switch into the wakee, bypassing schedule().
This patchset does not go that far yet, it simply migrates the wakee to the current CPU and calls schedule().
In follow-up patches I will try to fune-tune the behavior by adjusting load stats and schedule(): our internal switchto_switch
is still about 2x faster than FUTEX_SWAP (see numbers below).
And now about performance: futex_swap benchmark from the last patch in this patchset produces this typical output:
$ ./futex_swap -i 100000
------- running SWAP_WAKE_WAIT -----------
completed 100000 swap and back iterations in 820683263 ns: 4103 ns per swap PASS
------- running SWAP_SWAP -----------
completed 100000 swap and back iterations in 124034476 ns: 620 ns per swap PASS
In the above, the first benchmark (SWAP_WAKE_WAIT) calls FUTEX_WAKE, then FUTEX_WAIT; the second benchmark (SWAP_SWAP) calls FUTEX_SWAP.
If the benchmark is restricted to a single cpu:
$ taskset -c 1 ./futex_swap -i 1000000
The numbers are very similar, as expected (with wake+wait being a bit slower than swap due to two vs one syscalls).
Please also note that switchto_switch is about 2x faster than FUTEX_SWAP because it does a contex switch to the wakee immediately, bypassing schedule(), so this is one of the options I'll explore in further patches (if/when this initial patchset is accepted).
Tested: see the last patch is this patchset.
--- include/linux/sched.h | 1 + kernel/futex.c | 9 ++++----- kernel/sched/core.c | 5 +++++ kernel/sched/fair.c | 3 +++ kernel/sched/sched.h | 1 + 5 files changed, 14 insertions(+), 5 deletions(-)
include/linux/sched.h +1 -0
@@ -1716,6 +1716,7 @@ extern struct task_struct *find_get_task_by_vpid(pid_t nr);extern int wake_up_state (struct task_struct *tsk,unsigned int state);extern int wake_up_process (struct task_struct *tsk); +extern int wake_up_process_prefer_current_cpu (struct task_struct *tsk);extern void wake_up_new_task (struct task_struct *tsk);#ifdef CONFIG_SMP
kernel/futex.c +4 -5
@@ -2603,12 +2603,11 @@ static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,*/ if (!timeout || timeout->task) {if (next) { -/* -* wake_up_process() below will be replaced -* in the next patch with -* wake_up_process_prefer_current_cpu(). -*/ +#ifdef CONFIG_SMP +wake_up_process_prefer_current_cpu (next); +#else wake_up_process (next); +#endif put_task_struct (next); next =NULL ; }
kernel/sched/core.c +5 -0
@@ -6277,6 +6277,11 @@ void sched_setnuma(struct task_struct *p, int nid) }#endif /* CONFIG_NUMA_BALANCING */ +int wake_up_process_prefer_current_cpu (struct task_struct *next) +{ +return try_to_wake_up (next,TASK_NORMAL ,WF_CURRENT_CPU ); +} +#ifdef CONFIG_HOTPLUG_CPU /* * Ensure that the idle task is using init_mm right before its CPU goes
kernel/sched/fair.c +3 -0
@@ -6664,6 +6664,9 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_fint want_affine =0 ;int sync = (wake_flags &WF_SYNC ) && !(current->flags &PF_EXITING ); +if ((wake_flags &WF_CURRENT_CPU ) &&cpumask_test_cpu (cpu, p->cpus_ptr)) +return cpu; +if (sd_flag &SD_BALANCE_WAKE ) {record_wakee (p);
kernel/sched/sched.h +1 -0
@@ -1683,6 +1683,7 @@ static inline int task_on_rq_migrating(struct task_struct *p)#define WF_FORK 0x02/* Child wakeup after fork */ #define WF_MIGRATED 0x04/* Internal use, task got migrated */ #define WF_ON_CPU 0x08/* Wakee is on_cpu */ +#define WF_CURRENT_CPU 0x10/* Prefer to move wakee to the current CPU */ /* * To aid in avoiding the subversion of "niceness" due to uneven distribution
Signature · 1 line
2.25.1
PATCH 3/3 selftests/futex: add futex_swap selftest
In reply to Peter OskolkovFrom: Peter Oskolkov <[email protected]>
This is the final patch in FUTEX_SWAP patchset. It adds a test/benchmark to validate behavior and compare performance of a new FUTEX_SWAP futex operation.
Detailed API design and behavior considerations are provided in the commit messages of the previous two patches.
--- .../selftests/futex/functional/.gitignore | 1 + .../selftests/futex/functional/Makefile | 1 + .../selftests/futex/functional/futex_swap.c | 209 ++++++++++++++++++ .../selftests/futex/include/futextest.h | 19 ++ 4 files changed, 230 insertions(+) create mode 100644 tools/testing/selftests/futex/functional/futex_swap.c
tools/testing/selftests/futex/functional/.gitignore +1 -0
@@ -2,6 +2,7 @@ futex_requeue_pi futex_requeue_pi_mismatched_ops futex_requeue_pi_signal_restart +futex_swap futex_wait_private_mapped_file futex_wait_timeout futex_wait_uninitialized_heap
tools/testing/selftests/futex/functional/Makefile +1 -0
@@ -13,6 +13,7 @@ TEST_GEN_FILES := \ futex_requeue_pi \ futex_requeue_pi_signal_restart \ futex_requeue_pi_mismatched_ops \ + futex_swap \ futex_wait_uninitialized_heap \ futex_wait_private_mapped_file
tools/testing/selftests/futex/functional/futex_swap.c new file +209 -0
209 lines not on this page · raw message
tools/testing/selftests/futex/include/futextest.h +19 -0
@@ -38,6 +38,9 @@ typedef volatile u_int32_t futex_t;#ifndef FUTEX_CMP_REQUEUE_PI #define FUTEX_CMP_REQUEUE_PI 12#endif +#ifndef FUTEX_SWAP +#define FUTEX_SWAP 13 +#endif #ifndef FUTEX_WAIT_REQUEUE_PI_PRIVATE #define FUTEX_WAIT_REQUEUE_PI_PRIVATE (FUTEX_WAIT_REQUEUE_PI | \ FUTEX_PRIVATE_FLAG)
@@ -46,6 +49,9 @@ typedef volatile u_int32_t futex_t;#define FUTEX_CMP_REQUEUE_PI_PRIVATE (FUTEX_CMP_REQUEUE_PI | \ FUTEX_PRIVATE_FLAG)#endif +#ifndef FUTEX_SWAP_PRIVATE +#define FUTEX_SWAP_PRIVATE (FUTEX_WAIT_WAKE | FUTEX_PRIVATE_FLAG) +#endif /** * futex() - SYS_futex syscall wrapper
@@ -204,6 +210,19 @@ futex_cmp_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake, val, opflags); } +/** +* futex_swap() - block on uaddr and wake one task blocked on uaddr2. +* @uaddr: futex to block the current task on +* @timeout: relative timeout for the current task block +* @uaddr2: futex to wake tasks at (can be the same as uaddr) +*/ +static inline int +futex_swap (futex_t *uaddr,futex_t val,struct timespec *timeout, +futex_t *uaddr2,int opflags) +{ +return futex (uaddr,FUTEX_SWAP , val, timeout, uaddr2,0 , opflags); +} +/** * futex_cmpxchg() - atomic compare and exchange * @uaddr: The address of the futex to be modified
Signature · 1 line
2.25.1