Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp2077427ybp; Thu, 10 Oct 2019 01:46:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqyQt8uDnYCZKqEuHDHwB8Rjb4y/aWZ0dtpw5X8x2/gDS6CQetOxWlozJIbCjDMu8t2ToGiw X-Received: by 2002:a50:afa1:: with SMTP id h30mr6982441edd.126.1570697214378; Thu, 10 Oct 2019 01:46:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570697214; cv=none; d=google.com; s=arc-20160816; b=0ypoBUyPL/ZPWdtEKjQ4rcwbQVPwPPg0JrnQkrM9hMWSlv19h8ILd1m5rKZao9kY6R TSljZDO/vvv4Z/O5vssy+5TIMRABiU6gqgNbyQW8wNNMi5ShIZTUIlTr6C/FVSlur62o nePbRkyTHJun+onPRXQkM+6+3n6SlZU8wJ1AHoxat1kK92ojBZJmLj2SnQ3op4VyOvH/ tnuTmd8VegtEmTISsGfccMkNqaF484Zs1WHqLn7DcNErBxnCn6K8C/sov7bbXqSJVMsQ Ac40oPLgOFguYhulbOfxSGvpnC8BoB4M2IoIJk7MjnCY4DBhpc00FOx3JX0dQMN3DX60 9HQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=XWN18Jeg6TcyHF+J3KiimvgMzh1SrFKOc4UIttLmu80=; b=duMhM4++rm6nfixg4KRvsAr3+HUXylvahKQMG83czc1j0+RNeAyqnVsON92jt9GlZe Br3RcSKbD1XCGVMPze/GPW1rfmTU5KlokGyf/ucIoQ0aTrApY9qKCWQzz9Z72w9IIQ3G b5fOeRPqKPX+NdVOxK0MyJD/Pp9LRoDeBD2udTuuOGk/14ecNyvyLIBoKZk7KtBbxOJc cjNwKGC1c2CkQb4DPZZN9Xk9NtR7aYciGGTgTsRACeVynppy+GmZw3oms/lMEokLFZk9 yAm3zUmaZHTPI71UcjxBTzDJ9ljthYzTGP6W/cV1Y1VERthRMCSBmLm3HK7kDjrznkBB OnQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AedSd62L; 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 c48si3170867edc.169.2019.10.10.01.46.31; Thu, 10 Oct 2019 01:46:54 -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=@kernel.org header.s=default header.b=AedSd62L; 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 S2388673AbfJJInH (ORCPT + 99 others); Thu, 10 Oct 2019 04:43:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:47766 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387600AbfJJInF (ORCPT ); Thu, 10 Oct 2019 04:43:05 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 27C2C21A4A; Thu, 10 Oct 2019 08:43:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570696982; bh=yGD/7nzqg5tDde1KXThcePa5G7U72K7pfa0Y2PXDuWk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AedSd62L3Bns+W5hrcblZ63jwJMJGk5bL03kMEux+cxFDF7yWdXWl0WFN8JxURI/T nqzrYE5OqCanzorX8CQToTNZrK05LqsJ3trqNO8dDUBlyDSUcraDmxxnfXgOoVAcn4 /gBD+CKJz95y3PYe1Aw9+IXezl3FoNdV9xw/exDc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, KeMeng Shi , "Peter Zijlstra (Intel)" , Valentin Schneider , Linus Torvalds , Thomas Gleixner , Ingo Molnar , Sasha Levin Subject: [PATCH 5.3 122/148] sched/core: Fix migration to invalid CPU in __set_cpus_allowed_ptr() Date: Thu, 10 Oct 2019 10:36:23 +0200 Message-Id: <20191010083618.460925281@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191010083609.660878383@linuxfoundation.org> References: <20191010083609.660878383@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KeMeng Shi [ Upstream commit 714e501e16cd473538b609b3e351b2cc9f7f09ed ] An oops can be triggered in the scheduler when running qemu on arm64: Unable to handle kernel paging request at virtual address ffff000008effe40 Internal error: Oops: 96000007 [#1] SMP Process migration/0 (pid: 12, stack limit = 0x00000000084e3736) pstate: 20000085 (nzCv daIf -PAN -UAO) pc : __ll_sc___cmpxchg_case_acq_4+0x4/0x20 lr : move_queued_task.isra.21+0x124/0x298 ... Call trace: __ll_sc___cmpxchg_case_acq_4+0x4/0x20 __migrate_task+0xc8/0xe0 migration_cpu_stop+0x170/0x180 cpu_stopper_thread+0xec/0x178 smpboot_thread_fn+0x1ac/0x1e8 kthread+0x134/0x138 ret_from_fork+0x10/0x18 __set_cpus_allowed_ptr() will choose an active dest_cpu in affinity mask to migrage the process if process is not currently running on any one of the CPUs specified in affinity mask. __set_cpus_allowed_ptr() will choose an invalid dest_cpu (dest_cpu >= nr_cpu_ids, 1024 in my virtual machine) if CPUS in an affinity mask are deactived by cpu_down after cpumask_intersects check. cpumask_test_cpu() of dest_cpu afterwards is overflown and may pass if corresponding bit is coincidentally set. As a consequence, kernel will access an invalid rq address associate with the invalid CPU in migration_cpu_stop->__migrate_task->move_queued_task and the Oops occurs. The reproduce the crash: 1) A process repeatedly binds itself to cpu0 and cpu1 in turn by calling sched_setaffinity. 2) A shell script repeatedly does "echo 0 > /sys/devices/system/cpu/cpu1/online" and "echo 1 > /sys/devices/system/cpu/cpu1/online" in turn. 3) Oops appears if the invalid CPU is set in memory after tested cpumask. Signed-off-by: KeMeng Shi Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Valentin Schneider Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Link: https://lkml.kernel.org/r/1568616808-16808-1-git-send-email-shikemeng@huawei.com Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- kernel/sched/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index d38f007afea74..fffe790d98bb2 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1537,7 +1537,8 @@ static int __set_cpus_allowed_ptr(struct task_struct *p, if (cpumask_equal(p->cpus_ptr, new_mask)) goto out; - if (!cpumask_intersects(new_mask, cpu_valid_mask)) { + dest_cpu = cpumask_any_and(cpu_valid_mask, new_mask); + if (dest_cpu >= nr_cpu_ids) { ret = -EINVAL; goto out; } @@ -1558,7 +1559,6 @@ static int __set_cpus_allowed_ptr(struct task_struct *p, if (cpumask_test_cpu(task_cpu(p), new_mask)) goto out; - dest_cpu = cpumask_any_and(cpu_valid_mask, new_mask); if (task_running(rq, p) || p->state == TASK_WAKING) { struct migration_arg arg = { p, dest_cpu }; /* Need help from migration thread: drop lock and wait. */ -- 2.20.1