Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp693286imm; Sat, 26 May 2018 08:48:31 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqTgzZ3+dZ6vMdwBcn/FG49TZ0A3chUv6Hl6I47Oi5tXUwae6E5DDkvs4AHliyprrSNsk5b X-Received: by 2002:a17:902:8a82:: with SMTP id p2-v6mr6964162plo.244.1527349711500; Sat, 26 May 2018 08:48:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527349711; cv=none; d=google.com; s=arc-20160816; b=h3y6BnJgnu8lQxNIACqBh+LUWywHNhfr0lhVxAkJrUJ0ligmM0mqczOSpRYeLhCxvr xj16dbpfpgZ3AnJhor9/nFyXqu+3eBKJZuFAy9EOGYtgBtItjIPbuM6YxQDaZjmtOCr+ B6VHmUTuSsnIgL+PwolWGqiVU0ump1d26Pk4VZWDiuzd5ha7yGx6Yj9Kij8CF/g5PLxS GqOHx/p0WIiQ7pfRIhU0Q62R9LL9fQpcWGh9NB+SxosmNEzJfqLwNIOf2Xdzh5ohLszn ojlTBFczQv0h2ur153L09EdNmdqCswoXFeFvCId40hPq4FGR7ysmp51MB+ePGgL4ubQI IjRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=DvlbuQV0LnCRXKyx6CwSf4lIzxfEpogNDdE0wcbDFRE=; b=yGfTuXMZzkuKyBBAMAKZ6GWD0hN69sSUGhYnjYFF8z5v95/OoZiqHSfwejHhKa+NxC vtX9e/h978Eip4UrHDIbK2nVWXQUqWsWHeRJjhj4GiOFmL3RX2KzeuzhG40GDlU6uLoe DyhNRgWe1GBFfpgQdw5ClwTWBPQPtDeaZzh78EQPyGvNN7B1yerYJlerAIksxMWrAFQs ixEUWYc8YMxxYFuQXoboRIRkMU78IlTQXXYAOXUk5fXck1f4SH2/Gu082G0twFeSl48y /6P9W5g5jV62TfUW4bNYTDabpIQboRKvpNeHoZIT4kgw5taXuyBl9kQdFOV8Nz16/SKF zCnw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o4-v6si25208746plk.321.2018.05.26.08.48.17; Sat, 26 May 2018 08:48:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1032095AbeEZPrr (ORCPT + 99 others); Sat, 26 May 2018 11:47:47 -0400 Received: from 9pmail.ess.barracuda.com ([64.235.154.211]:37311 "EHLO 9pmail.ess.barracuda.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1031908AbeEZPro (ORCPT ); Sat, 26 May 2018 11:47:44 -0400 Received: from mipsdag01.mipstec.com (mail1.mips.com [12.201.5.31]) by mx1403.ess.rzc.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA256 bits=128 verify=NO); Sat, 26 May 2018 15:47:24 +0000 Received: from mipsdag02.mipstec.com (10.20.40.47) by mipsdag01.mipstec.com (10.20.40.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1415.2; Sat, 26 May 2018 08:46:58 -0700 Received: from pburton-laptop.ba.imgtec.org (10.20.78.205) by mipsdag02.mipstec.com (10.20.40.47) with Microsoft SMTP Server id 15.1.1415.2 via Frontend Transport; Sat, 26 May 2018 08:46:58 -0700 From: Paul Burton To: CC: Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Paul Burton Subject: [PATCH 1/2] sched: Make select_task_rq() require cpu_active() for user tasks Date: Sat, 26 May 2018 08:46:47 -0700 Message-ID: <20180526154648.11635-2-paul.burton@mips.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180526154648.11635-1-paul.burton@mips.com> References: <20180526154648.11635-1-paul.burton@mips.com> MIME-Version: 1.0 Content-Type: text/plain X-BESS-ID: 1527349614-321459-6793-23952-2 X-BESS-VER: 2018.6-r1805181819 X-BESS-Apparent-Source-IP: 12.201.5.31 X-BESS-Outbound-Spam-Score: 0.00 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.193394 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------- 0.00 BSF_BESS_OUTBOUND META: BESS Outbound X-BESS-Outbound-Spam-Status: SCORE=0.00 using account:ESS59374 scores of KILL_LEVEL=7.0 tests=BSF_BESS_OUTBOUND X-BESS-BRTS-Status: 1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org select_task_rq() is used in a few paths to select the CPU upon which a thread should be run - for example it is used by try_to_wake_up() & by fork or exec balancing. As-is it allows use of any online CPU that is present in the task's cpus_allowed mask. This presents a problem because there is a period whilst CPUs are brought online where a CPU is marked online, but is not yet fully initialized - ie. the period where CPUHP_AP_ONLINE_IDLE <= state < CPUHP_ONLINE. Usually we don't run any user tasks during this window, but there are corner cases where this can happen. An example observed is: - Some user task A, running on CPU X, forks to create task B. - sched_fork() calls __set_task_cpu() with cpu=X, setting task B's struct task_struct cpu field to X. - CPU X is offlined. - Task A, currently somewhere between the __set_task_cpu() in copy_process() and the call to wake_up_new_task(), is migrated to CPU Y by migrate_tasks() when CPU X is offlined. - CPU X is onlined, but still in the CPUHP_AP_ONLINE_IDLE state. The scheduler is now active on CPU X, but there are no user tasks on the runqueue. - Task A runs on CPU Y & reaches wake_up_new_task(). This calls select_task_rq() with cpu=X, taken from task B's struct task_struct, and select_task_rq() allows cpu X to be returned. - Task A enqueues task B on CPU X's runqueue, via activate_task() & enqueue_task(). - CPU X now has a user task on its runqueue before it has reached the CPUHP_ONLINE state. In most cases, the user tasks that schedule on the newly onlined CPU have no idea that anything went wrong, but one case observed to be problematic is if the task goes on to invoke the sched_setaffinity syscall. The newly onlined CPU reaches the CPUHP_AP_ONLINE_IDLE state before the CPU that brought it online calls stop_machine_unpark(). This means that for a portion of the window of time between CPUHP_AP_ONLINE_IDLE & CPUHP_ONLINE the newly onlined CPU's struct cpu_stopper has its enabled field set to false. If a user thread is executed on the CPU during this window and it invokes sched_setaffinity with a CPU mask that does not include the CPU it's running on, then when __set_cpus_allowed_ptr() calls stop_one_cpu() intending to invoke migration_cpu_stop() and perform the actual migration away from the CPU it will simply return -ENOENT rather than calling migration_cpu_stop(). We then return from the sched_setaffinity syscall back to the user task that is now running on a CPU which it just asked not to run on, and which is not present in its cpus_allowed mask. This patch resolves the problem by having select_task_rq() enforce that user tasks run on CPUs that are active - the same requirement that select_fallback_rq() already enforces. This should ensure that newly onlined CPUs reach the CPUHP_AP_ACTIVE state before being able to schedule user tasks, and also implies that bringup_wait_for_ap() will have called stop_machine_unpark() which resolves the sched_setaffinity issue above. I haven't yet investigated them, but it may be of interest to review whether any of the actions performed by hotplug states between CPUHP_AP_ONLINE_IDLE & CPUHP_AP_ACTIVE could have similar unintended effects on user tasks that might schedule before they are reached, which might widen the scope of the problem from just affecting the behaviour of sched_setaffinity. Signed-off-by: Paul Burton Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: linux-kernel@vger.kernel.org --- As described in the series' cover letter I've created a small test case for the observed problem here: https://gist.github.com/paulburton/25187c4f537263a6be9c8aac67bd33bf The issue was first observed on a MIPS system running a test program that was sensitive to CPU affinity, but has been reproduced on x86_64 using the above program too. On my laptop with an Intel i7-5600U CPU the test tends to fail within 10 minutes, but with this series applied runs cleanly overnight. kernel/sched/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index a7bf32aabfda..2380bc228dd0 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1565,7 +1565,8 @@ int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags) * not worry about this generic constraint ] */ if (unlikely(!cpumask_test_cpu(cpu, &p->cpus_allowed) || - !cpu_online(cpu))) + !cpu_online(cpu) || + (!cpu_active(cpu) && !(p->flags & PF_KTHREAD)))) cpu = select_fallback_rq(task_cpu(p), p); return cpu; -- 2.17.0