Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2834718pxv; Mon, 12 Jul 2021 03:06:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJxi4tkEJTAtgoL+azCJ3rpYpXTeIVDAJFyL8UtR6sEG5Ujy2CRAfQeB6LmZsJAQbO9HJv X-Received: by 2002:a05:6602:179e:: with SMTP id y30mr25578374iox.0.1626084404093; Mon, 12 Jul 2021 03:06:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626084404; cv=none; d=google.com; s=arc-20160816; b=OiWrWUymXiGoI4fdLQJPRZAz6vCT1OrGzZH+9bjEoNXM7Rpokk6Xo9zdAypbcTVDuK /XlW44TfxYRkniRHBQ9weHPf/4BoflmLG9sZCOoy0kuxWcWoRAXXHniJ08wQ2D5shaB/ iwurCT//z5rreTySDKvUw8E0dD3mlmo+nvbfW6GALaRjKQe6coe7GVyEVsB8YiD0Rttj 9cfF6uohdEAc7eF0jNK8a+PvOI7MbtGQw/thVxJVcHqjNyYxDZv5Y2TFvIU97+hSCn1Y eBLnR3u3ihsoJCzaf0TW4+VrhjWJXnvN4ldJJ7pFrNjAnDqeT7yvQJgBN9+ZQApNKaVA zXBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=DJtMmuQqQjEXjjJMLvc3nns2b+O7jqddpJbtq1SAaZg=; b=aiAumyG+JNYeJ6gRyT8Zxjrp9A6OsrbqTUCTEq6PJ2THmTDo4naRShy0jx0Tu4cwaA FGoGi6g9JnvAqq9uVaflsRrQYAnO3+8Fjs2pn15PNjZMchGGVbP69V/T/ax2cO/6YmrB AhsXmjE3ry0kJxjWbrqspu+9o4pvZtDSXZqogXYKEI8iGEp+A106U4sLDrNFvBTn3LcK xANq7mIrlShutPQntquQwNvQ8rT0TFvnvK/8sB2t7Q9/kMfPRIV3Cbz3DlA7zn4YDeY5 AOH3RBMpl4U2iM+2aHR6KdXeE2LGFmAtARobjpdE/V35HcqUdl2kFqMnXHLAp8n3lDel jGBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aUDoiVho; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t3si17188051ile.109.2021.07.12.03.06.32; Mon, 12 Jul 2021 03:06:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aUDoiVho; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243371AbhGLHgc (ORCPT + 99 others); Mon, 12 Jul 2021 03:36:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:42946 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243932AbhGLHKN (ORCPT ); Mon, 12 Jul 2021 03:10:13 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7E3A161351; Mon, 12 Jul 2021 07:05:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626073524; bh=IEo8snFy3HtpvzPHtquWZOolpEjDD5J5/6/kQK4Wytg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aUDoiVhoKpyxw2vHysVus6BuPcsnkgFT1JUmn1/6rFt03jNohFU6kuTi3q4FXkAi9 DYUwqrcIEvPS62zoJlwm+fBRYVwkMYRGTCbbHb2nNBTNuAxz9voMsiOwF+psyYw/Lz jEy6XGb+1F8wAP69qwuKtQPIG4sELF/q3TXdKVgQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Will Deacon , Valentin Schneider , "Peter Zijlstra (Intel)" , Sasha Levin Subject: [PATCH 5.12 272/700] sched: Dont defer CPU pick to migration_cpu_stop() Date: Mon, 12 Jul 2021 08:05:55 +0200 Message-Id: <20210712061005.202907029@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060924.797321836@linuxfoundation.org> References: <20210712060924.797321836@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Valentin Schneider [ Upstream commit 475ea6c60279e9f2ddf7e4cf2648cd8ae0608361 ] Will reported that the 'XXX __migrate_task() can fail' in migration_cpu_stop() can happen, and it *is* sort of a big deal. Looking at it some more, one will note there is a glaring hole in the deferred CPU selection: (w/ CONFIG_CPUSET=n, so that the affinity mask passed via taskset doesn't get AND'd with cpu_online_mask) $ taskset -pc 0-2 $PID # offline CPUs 3-4 $ taskset -pc 3-5 $PID `\ $PID may stay on 0-2 due to the cpumask_any_distribute() picking an offline CPU and __migrate_task() refusing to do anything due to cpu_is_allowed(). set_cpus_allowed_ptr() goes to some length to pick a dest_cpu that matches the right constraints vs affinity and the online/active state of the CPUs. Reuse that instead of discarding it in the affine_move_task() case. Fixes: 6d337eab041d ("sched: Fix migrate_disable() vs set_cpus_allowed_ptr()") Reported-by: Will Deacon Signed-off-by: Valentin Schneider Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20210526205751.842360-2-valentin.schneider@arm.com Signed-off-by: Sasha Levin --- kernel/sched/core.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index f59166fe499a..fe5da692dd7a 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1919,7 +1919,6 @@ static int migration_cpu_stop(void *data) struct migration_arg *arg = data; struct set_affinity_pending *pending = arg->pending; struct task_struct *p = arg->task; - int dest_cpu = arg->dest_cpu; struct rq *rq = this_rq(); bool complete = false; struct rq_flags rf; @@ -1952,19 +1951,15 @@ static int migration_cpu_stop(void *data) if (p->migration_pending == pending) p->migration_pending = NULL; complete = true; - } - if (dest_cpu < 0) { if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) goto out; - - dest_cpu = cpumask_any_distribute(&p->cpus_mask); } if (task_on_rq_queued(p)) - rq = __migrate_task(rq, &rf, p, dest_cpu); + rq = __migrate_task(rq, &rf, p, arg->dest_cpu); else - p->wake_cpu = dest_cpu; + p->wake_cpu = arg->dest_cpu; /* * XXX __migrate_task() can fail, at which point we might end @@ -2243,7 +2238,7 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag init_completion(&my_pending.done); my_pending.arg = (struct migration_arg) { .task = p, - .dest_cpu = -1, /* any */ + .dest_cpu = dest_cpu, .pending = &my_pending, }; @@ -2251,6 +2246,15 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag } else { pending = p->migration_pending; refcount_inc(&pending->refs); + /* + * Affinity has changed, but we've already installed a + * pending. migration_cpu_stop() *must* see this, else + * we risk a completion of the pending despite having a + * task on a disallowed CPU. + * + * Serialized by p->pi_lock, so this is safe. + */ + pending->arg.dest_cpu = dest_cpu; } } pending = p->migration_pending; -- 2.30.2