Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp858560imm; Wed, 25 Jul 2018 07:23:38 -0700 (PDT) X-Google-Smtp-Source: AAOMgpel5TdqruiBZlovHHgy19OAzHcD/LxjUZfm1uwqTXnejoC5VyslyjlT0ItSm/obxOSFbOnZ X-Received: by 2002:a62:2b4c:: with SMTP id r73-v6mr22348283pfr.134.1532528618104; Wed, 25 Jul 2018 07:23:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532528618; cv=none; d=google.com; s=arc-20160816; b=c/9Y5y2jQIPzbvprC1Q7NE+9/eT1IrjwfGj5IBD4p0rtSH+3OTEN4tco+rqkSe23Ch d06F3OYWnDLeW9vJ6A9b9XCrtPGgVzgdgucd4/7nhp+3hUXmsqO1c+KWHj/90zGkcozP 3vHh3Z3CTZaX+9lAHkqUSwC2caBBlyqNgaxGijEQmf7NqmyPheT4SFASkzraCX1EXqki kyZ1TF+UbOAOZhqKw8saOK57BHb6Nkgqloh1PSSCsQVPaFtrcrLQ61iukVfgULSXoQc/ 2APpLwyzayF6Csu46WMm4KtI7WpO+LVISIXWnfMWc7M9DI6M9QAJ0MWTYCtCPhWL6nce AK1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:arc-authentication-results; bh=WCLIV9/3Z2ZbONA8VDiDRr8GCeoiRGFbuwK8rMM4YXU=; b=puTFZQNndAy+NubR9LmkmBr2zEnGhhonpFcDtOh5Nw6+nB9ceuSrLkRwNts4I/q8M4 RKcDDbeQJs3zDYtknerLLuqcmS0yNr+Op6Xs9fJpLZgbmGEKwn+SA5LF/y3bI8NqvGVJ WoBD/wpKsqQ06j57AvzKydlJG6bf9LwN9lgN58t99UpCuXy66eIZreFAlk0GXWL3fRy7 /GZ03OXnpd47TQyoqvOxQAE3wJ78KjpcwSUG5+y/jI5PVDGkoOsAHcpqNnhf04D54RB5 b3aCo3K6x59L3WlA1ZSlzyYonqOkk86xRd7hRV3rhcWcyriV5gM2AbZ0MSwujKt3J9MJ LH9A== 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 l14-v6si13805740pgk.562.2018.07.25.07.23.23; Wed, 25 Jul 2018 07:23:38 -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 S1729146AbeGYPdY (ORCPT + 99 others); Wed, 25 Jul 2018 11:33:24 -0400 Received: from terminus.zytor.com ([198.137.202.136]:58063 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727881AbeGYPdX (ORCPT ); Wed, 25 Jul 2018 11:33:23 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id w6PELAle400159 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 25 Jul 2018 07:21:10 -0700 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w6PEL91X400156; Wed, 25 Jul 2018 07:21:09 -0700 Date: Wed, 25 Jul 2018 07:21:09 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: "tip-bot for Isaac J. Manjarres" Message-ID: Cc: psodagud@codeaurora.org, linux-kernel@vger.kernel.org, tglx@linutronix.de, hpa@zytor.com, mingo@kernel.org, isaacm@codeaurora.org, torvalds@linux-foundation.org, pkondeti@codeaurora.org, peterz@infradead.org Reply-To: mingo@kernel.org, isaacm@codeaurora.org, hpa@zytor.com, pkondeti@codeaurora.org, torvalds@linux-foundation.org, peterz@infradead.org, psodagud@codeaurora.org, tglx@linutronix.de, linux-kernel@vger.kernel.org In-Reply-To: <1531856129-9871-1-git-send-email-isaacm@codeaurora.org> References: <1531856129-9871-1-git-send-email-isaacm@codeaurora.org> To: linux-tip-commits@vger.kernel.org Subject: [tip:sched/core] stop_machine: Disable preemption after queueing stopper threads Git-Commit-ID: 2610e88946632afb78aa58e61f11368ac4c0af7b X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-0.7 required=5.0 tests=ALL_TRUSTED,BAYES_00, DATE_IN_FUTURE_48_96 autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 2610e88946632afb78aa58e61f11368ac4c0af7b Gitweb: https://git.kernel.org/tip/2610e88946632afb78aa58e61f11368ac4c0af7b Author: Isaac J. Manjarres AuthorDate: Tue, 17 Jul 2018 12:35:29 -0700 Committer: Ingo Molnar CommitDate: Wed, 25 Jul 2018 11:25:08 +0200 stop_machine: Disable preemption after queueing stopper threads This commit: 9fb8d5dc4b64 ("stop_machine, Disable preemption when waking two stopper threads") does not fully address the race condition that can occur as follows: On one CPU, call it CPU 3, thread 1 invokes cpu_stop_queue_two_works(2, 3,...), and the execution is such that thread 1 queues the works for migration/2 and migration/3, and is preempted after releasing the locks for migration/2 and migration/3, but before waking the threads. Then, On CPU 2, a kworker, call it thread 2, is running, and it invokes cpu_stop_queue_two_works(1, 2,...), such that thread 2 queues the works for migration/1 and migration/2. Meanwhile, on CPU 3, thread 1 resumes execution, and wakes migration/2 and migration/3. This means that when CPU 2 releases the locks for migration/1 and migration/2, but before it wakes those threads, it can be preempted by migration/2. If thread 2 is preempted by migration/2, then migration/2 will execute the first work item successfully, since migration/3 was woken up by CPU 3, but when it goes to execute the second work item, it disables preemption, calls multi_cpu_stop(), and thus, CPU 2 will wait forever for migration/1, which should have been woken up by thread 2. However migration/1 cannot be woken up by thread 2, since it is a kworker, so it is affine to CPU 2, but CPU 2 is running migration/2 with preemption disabled, so thread 2 will never run. Disable preemption after queueing works for stopper threads to ensure that the operation of queueing the works and waking the stopper threads is atomic. Co-Developed-by: Prasad Sodagudi Co-Developed-by: Pavankumar Kondeti Signed-off-by: Isaac J. Manjarres Signed-off-by: Prasad Sodagudi Signed-off-by: Pavankumar Kondeti Signed-off-by: Peter Zijlstra (Intel) Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: bigeasy@linutronix.de Cc: gregkh@linuxfoundation.org Cc: matt@codeblueprint.co.uk Fixes: 9fb8d5dc4b64 ("stop_machine, Disable preemption when waking two stopper threads") Link: http://lkml.kernel.org/r/1531856129-9871-1-git-send-email-isaacm@codeaurora.org Signed-off-by: Ingo Molnar --- kernel/stop_machine.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c index 1ff523dae6e2..e190d1ef3a23 100644 --- a/kernel/stop_machine.c +++ b/kernel/stop_machine.c @@ -260,6 +260,15 @@ retry: err = 0; __cpu_stop_queue_work(stopper1, work1, &wakeq); __cpu_stop_queue_work(stopper2, work2, &wakeq); + /* + * The waking up of stopper threads has to happen + * in the same scheduling context as the queueing. + * Otherwise, there is a possibility of one of the + * above stoppers being woken up by another CPU, + * and preempting us. This will cause us to n ot + * wake up the other stopper forever. + */ + preempt_disable(); unlock: raw_spin_unlock(&stopper2->lock); raw_spin_unlock_irq(&stopper1->lock); @@ -271,7 +280,6 @@ unlock: } if (!err) { - preempt_disable(); wake_up_q(&wakeq); preempt_enable(); }