futex/sched: introduce FUTEX_SWAP operation
v3 cover futex/sched: introduce FUTEX_SWAP operation
From: Peter Oskolkov <[email protected]>
This is an RFC!
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 RFC 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.
v2: fix undefined symbol error ifndef CONFIG_SMP.
v3: rebased onto the latest tip/locking/core.
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 | 96 ++++++-- 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, 322 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/futex/functional/futex_swap.c
--
2.25.1
RFC PATCH v3 1/3 futex: introduce FUTEX_SWAP operation
From: Peter Oskolkov <[email protected]>
This is an RFC!
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 RFC 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 | 97 +++++++++++++++++++++++++++++++------- 2 files changed, 83 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 +81 -16
207 lines not on this page · raw message
--
2.25.1
RFC PATCH v3 2/3 futex/sched: add wake_up_process_prefer_current_cpu, use in FUTEX_SWAP
In reply to Peter OskolkovFrom: Peter Oskolkov <[email protected]>
This is an RFC!
v2: fix undefined symbol error ifndef CONFIG_SMP, as
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(). This can potentially interfere with load balancing but, on the other hand, the code is concise and simple and is easy to understand to start the discussion.
If this approach is OK, then 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
@@ -1719,6 +1719,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
@@ -2649,12 +2649,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
@@ -6253,6 +6253,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_RQ 0x08/* Wakee is on_rq */ +#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
--
2.25.1
RFC PATCH v3 3/3 selftests/futex: add futex_swap selftest
In reply to Peter OskolkovFrom: Peter Oskolkov <[email protected]>
This is the final RFC 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
Hi Thomas, Ingo!
Do you have any comments/suggestions/objections here? FUTEX_SWAP seems to be quite useful for fast task context switching, and several teams at Google would like to see this capability upstreamed.
Thanks,
Peter
Quoted 68 lines · Peter Oskolkov
From: Peter Oskolkov <[email protected]>
This is an RFC!
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 RFC 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.Another common use case for
FUTEX_SWAPis 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 isa. T1: futex-wake T2, futex-wait
b. T2: wakes, does what it has been woken to do
c. T2: futex-wake T1, futex-waitWith
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_SWAPfutex operation that,
internally, does wake + wait. The purpose of this patch is to work out the API.
Patch 2: a first rough attempt to makeFUTEX_SWAPfaster than
what wake + wait can do.
Patch 3: a selftest that can also be used to benchmarkFUTEX_SWAPvs
FUTEX_WAKE+FUTEX_WAIT.v2: fix undefined symbol error ifndef
CONFIG_SMP.
v3: rebased onto the latest tip/locking/core.Peter Oskolkov (3):
futex: introduceFUTEX_SWAPoperation
futex/sched: addwake_up_process_prefer_current_cpu, use inFUTEX_SWAPselftests/futex: addfutex_swapselftestinclude/linux/sched.h | 1 + include/uapi/linux/futex.h | 2 + kernel/futex.c | 96 ++++++-- 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, 322 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/futex/functional/futex_swap.c--
2.25.1