Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp3741493imm; Tue, 17 Jul 2018 09:31:24 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfmlDjuQ4x/tTttLDRpUgbO9hWFg8NDUc//k9Pk8TF3SXsccwScWLel5hgoQ97R2RX+EMMW X-Received: by 2002:a17:902:e85:: with SMTP id 5-v6mr2304000plx.318.1531845083907; Tue, 17 Jul 2018 09:31:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531845083; cv=none; d=google.com; s=arc-20160816; b=EHbtO7M1QX74MBEE3vDHZwa+UviA+l9qu55kCRnNeheBQMlmE5wYFIwcGGouP95LR0 2Rmwe4K30ACtwGSN2eQPyEBKh6MLtpnRnl+1dneBxA0ilVvLIfiZM5b6toTkaqIhUbYU FMxjWJ1QD4YzAnBsvIl3kxqorR86Ts0IB9aMlkXWukiX7umPG/gRg6/M+ZRU3/9d9SFs Sl6O+544MB+qavNjjQTnvaXojarmIXUlopsxozCdxi5VtzC94Io4/66KQhO/bUniyC2B eb1PtGz7ceXtxqETMwI+hiEAHHMkIYg32R8fGJ6i/7JPSXoqgDCtj/pkQqEJGxoELQ93 Rbag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dmarc-filter:dkim-signature:dkim-signature :arc-authentication-results; bh=EyLA+YaTz5dHc2+giVvjzdua74N5fwEPqwfukXzcbiw=; b=OiLpm2Hvm7AwVU45bqF44vI3pb1NmNty9JKkAJP4XBdxl8ncqxZO9Xonc1FWpM0DM2 wvMF7s30h/SfawGda1KLiBGaOCM71BBvE5bWQHoiQdmytasFcoqORSsX+o6K/EW5OMXd TlWbbKQcmBfagtojdW2+5bYVGlN6z9B4yJ5oS775fCGQy/uGgqHIqf4dsMLkX9iZCkOL VSfBcUAHrlO47UrBXCQOGzQPhLctX/vD/BRNJNeyQTDgJcq1geEBS4CWDleOH08j7e9W LePOeyyhK3anD2o+d7um2eDhK2r3FJ2YpzerRp9RuVw/pM+Ff92eP0usfkJx1MNOYiu8 z36Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=QwLtm8Ch; dkim=pass header.i=@codeaurora.org header.s=default header.b=QC0MWs+4; 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 j8-v6si1239051pgi.575.2018.07.17.09.31.08; Tue, 17 Jul 2018 09:31:23 -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; dkim=pass header.i=@codeaurora.org header.s=default header.b=QwLtm8Ch; dkim=pass header.i=@codeaurora.org header.s=default header.b=QC0MWs+4; 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 S1730341AbeGQRD7 (ORCPT + 99 others); Tue, 17 Jul 2018 13:03:59 -0400 Received: from smtp.codeaurora.org ([198.145.29.96]:60280 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729641AbeGQRD7 (ORCPT ); Tue, 17 Jul 2018 13:03:59 -0400 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 632C0607C6; Tue, 17 Jul 2018 16:30:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1531845031; bh=o/MmxlsRLDcZX/3ufmC5YLiCfFkKOCiNtV0e2YnFQEk=; h=From:To:Cc:Subject:Date:From; b=QwLtm8Ch1fIEwiKsC/xuc/uVzCSjJxP+8hTWrNUcjXBa71BvzzRCM6iaqwBrpeEpi GF77z+7evjST5WtKMgQFAPB8kijWXPT24dIe7LGiRRhKcO3XCxjAigOBIrZLczfAi1 xBjPq2cjIQfQgSgi6ZV3yYNvpQ66Jv/Q5CCPjq80= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,T_DKIM_INVALID autolearn=no autolearn_force=no version=3.4.0 Received: from isaacm-linux.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: isaacm@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 44E0760481; Tue, 17 Jul 2018 16:30:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1531845030; bh=o/MmxlsRLDcZX/3ufmC5YLiCfFkKOCiNtV0e2YnFQEk=; h=From:To:Cc:Subject:Date:From; b=QC0MWs+4oy46Bx5xea9p1Rdrn6OwowLeXCR8VZDJGI1dspzc+l6aRfvOeFztSd7xD dfUmXcd0gz3Yx4iVrv+6XAe2Ep7k8kra8DizR6yh2UPn5nEvENqg4+EVUgGgG1lt63 35tlZmSvxjbWB+t6gfOPJnvq0uVKH3Q7wkyZ2UNM= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 44E0760481 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=isaacm@codeaurora.org From: "Isaac J. Manjarres" To: peterz@infradead.org, matt@codeblueprint.co.uk, mingo@kernel.org, tglx@linutronix.de, bigeasy@linutronix.de Cc: "Isaac J. Manjarres" , linux-kernel@vger.kernel.org, psodagud@codeaurora.org, gregkh@linuxfoundation.org, pkondeti@codeaurora.org, stable@vger.kernel.org Subject: [PATCH v4] stop_machine: Disable preemption after queueing stopper threads Date: Tue, 17 Jul 2018 09:30:17 -0700 Message-Id: <1531845017-19935-1-git-send-email-isaacm@codeaurora.org> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org After cpu_stop_queue_two_works() queues the cpu_stop works for the stopper threads, it releases the locks held for both threads, which enables preemption, which allows the following race condition to occur: 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. Fixes: 0b26351b910f ("stop_machine, sched: Fix migrate_swap() vs. active_balance() deadlock") 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 Cc: stable@vger.kernel.org --- kernel/stop_machine.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c index f89014a..e190d1e 100644 --- a/kernel/stop_machine.c +++ b/kernel/stop_machine.c @@ -260,6 +260,15 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1, 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); @@ -270,7 +279,10 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1, goto retry; } - wake_up_q(&wakeq); + if (!err) { + wake_up_q(&wakeq); + preempt_enable(); + } return err; } -- The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project