Skip to contentSkip to conversation

futex, sched: add wake_up_swap, use in FUTEX_SWAP

RFC PATCH Superseded Peter Oskolkov 2 participants 3 messages Jun 15, 2020 – Jun 16, 2020 Scheduler

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 2/3 futex, sched: add wake_up_swap, use in FUTEX_SWAP

From 2e5a6e670f4d5f5ab94893fe0bff17150b56c142 Mon Sep 17 00:00:00 2001
From: Peter Oskolkov <[email protected]>
Date: Sun, 14 Jun 2020 17:08:00 -0700
Subject: [RFC PATCH 2/3] futex, sched: add wake_up_swap, use in FUTEX_SWAP

This is an RFC!

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        | 7 +------
 kernel/sched/core.c   | 5 +++++
 kernel/sched/fair.c   | 3 +++
 kernel/sched/sched.h  | 3 ++-
 5 files changed, 12 insertions(+), 7 deletions(-)
include/linux/sched.h +1 -0
index 4418f5cb8324..4b177ac6f9be 100644
@@ -1693,6 +1693,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 +1 -6
index f3833190886f..a426671e4bbb 100644
@@ -2646,12 +2646,7 @@ 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().
-				 */
-				wake_up_process(next);
+				wake_up_process_prefer_current_cpu(next);
 				put_task_struct(next);
 				next = NULL;
 			}
kernel/sched/core.c +5 -0
index 9a2fbf98fd6f..f894b3e6c9ed 100644
@@ -6180,6 +6180,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
index 538ba5d94e99..80f927bb62eb 100644
@@ -6656,6 +6656,9 @@ select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_f
 	int 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 +2 -1
index db3a57675ccf..5dbe202a4103 100644
@@ -1688,7 +1688,8 @@ static inline int task_on_rq_migrating(struct task_struct *p)
  */
 #define WF_SYNC			0x01		/* Waker goes to sleep after wakeup */
 #define WF_FORK			0x02		/* Child wakeup after fork */
-#define WF_MIGRATED		0x4		/* Internal use, task got migrated */
+#define WF_MIGRATED		0x04		/* Internal use, task got migrated */
+#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
#1 lore raw
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…

Hi Peter,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on tip/locking/core]
[cannot apply to tip/sched/core kselftest/next tip/auto-latest v5.8-rc1 next-20200615] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Peter-Oskolkov/futex-sched-introduce-FUTEX_SWAP-operation/20200616-013237
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 19f545b6e07f753c4dc639c2f0ab52345733b6a8
config: nds32-defconfig (attached as .config)
compiler: nds32le-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=nds32

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>, old ones prefixed by <<):

nds32le-linux-ld: kernel/futex.o: in function `futex_wait_queue_me':

>> futex.c:(.text+0xf9c): undefined reference to `wake_up_process_prefer_current_cpu'
>> nds32le-linux-ld: futex.c:(.text+0xfa0): undefined reference to `wake_up_process_prefer_current_cpu'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#2 lore raw
In reply to Peter Oskolkov
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…

Hi Peter,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on tip/locking/core]
[cannot apply to tip/sched/core kselftest/next tip/auto-latest v5.8-rc1 next-20200615] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Peter-Oskolkov/futex-sched-introduce-FUTEX_SWAP-operation/20200616-013237
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 19f545b6e07f753c4dc639c2f0ab52345733b6a8
config: microblaze-randconfig-c022-20200616 (attached as .config)
compiler: microblaze-linux-gcc (GCC) 9.3.0

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>, old ones prefixed by <<):

microblaze-linux-ld: kernel/futex.o: in function `futex_wait_queue_me':

>> kernel/futex.c:2649: undefined reference to `wake_up_process_prefer_current_cpu'

vim +2649 kernel/futex.c

2611
2612 /**
2613 * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
2614 * @hb: the futex hash bucket, must be locked by the caller
2615 * @q: the futex_q to queue up on
2616 * @timeout: the prepared hrtimer_sleeper, or null for no timeout
2617 * @next: if present, wake next and hint to the scheduler that we'd
2618 * prefer to execute it locally.
2619 */
2620 static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
2621 struct hrtimer_sleeper *timeout,
2622 struct task_struct *next)
2623 {
2624 /*
2625 * The task state is guaranteed to be set before another task can
2626 * wake it. set_current_state() is implemented using smp_store_mb() and
2627 * queue_me() calls spin_unlock() upon completion, both serializing
2628 * access to the hash list and forcing another memory barrier.
2629 */
2630 set_current_state(TASK_INTERRUPTIBLE);
2631 queue_me(q, hb);
2632
2633 /* Arm the timer */
2634 if (timeout)
2635 hrtimer_sleeper_start_expires(timeout, HRTIMER_MODE_ABS);
2636
2637 /*
2638 * If we have been removed from the hash list, then another task
2639 * has tried to wake us, and we can skip the call to schedule().
2640 */
2641 if (likely(!plist_node_empty(&q->list))) {
2642 /*
2643 * If the timer has already expired, current will already be
2644 * flagged for rescheduling. Only call schedule if there
2645 * is no timeout, or if it has yet to expire.
2646 */
2647 if (!timeout || timeout->task) {
2648 if (next) {

> 2649					wake_up_process_prefer_current_cpu(next);

2650 put_task_struct(next);
2651 next = NULL;
2652 }
2653 freezable_schedule();
2654 }
2655 }
2656 __set_current_state(TASK_RUNNING);
2657
2658 if (next) {
2659 /* Maybe call wake_up_process_prefer_current_cpu()? */
2660 wake_up_process(next);
2661 put_task_struct(next);
2662 }
2663 }
2664

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org