From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=222.66.158.135 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796297168; b=BKr8Xo+0LKHV+xg+Wrok6SQJydUqPsD5sAcmgVblNQc7qAFe72G19waXKS9fMKm/zXT3 /aETiW0dXQFt8Qskmzwl+tHxRMGVX7D4uxt4mgNjRj4DvskSA5rbYnU0Gz004CMb7c6LT e4Kw7rA9aCGIoK+Q47/F9ZK8adOqeWskVouuqb8RcIPv2O+XI0LSmMOk92Mrn4CvKC53p uAv66jeyvZOaEO1WEz+xHE0vglcMCZL48+6qhxl7+Vj1zutvMlb+nBNpUX0WnnBaRN3VH OQx5+fXak0ec2rcUXAPry2Gt4l6dJ+1PrpYxPJGeIhEVA/4G3UuOadNmB+JE6BNuffg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796297168; h=DMARC-Filter:Received:Received:Received:From:To:CC:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding:X-Originating-IP:X-ClientProxiedBy:X-MAIL: DKIM-Signature; bh=yqLqzivNZ0VWv6U6Ae9KZwjqenZZJs0yklhu0PolsG8=; b=MtwPT9tgfl6f82usJQw4Z+yUJFDmqugmex24XAbzc4OozShscQWyZ3+aY5umOxjY11Qf c9qx2BMC1WzaeAS1QZnE9UHz2Rbm/kJZvW/P7xst+3w/gaFJo6GtG4cOkSj+pqkFfXcnQ dqgQ4wYyET2ydHDO1rcc7X3E4GYz7KBv86ZKS3NYny6g7xr4Mm8l3tPxipHARPOjJjH2z LnLKMK8Bbp9zjn/lHbOsern/vocrOeYDwuXmfYkMA96pL503svcjzwE4qv/N6WvwivphW vR9SeRZt11n8VZTRhjqvRT3pcBL4sPOg+4hU7+JzTpE9o/nOwAiAfExMKEVaopoUoXA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; dkim=pass header.d=unisoc.com header.i=@unisoc.com header.a=rsa-sha256 header.s=default header.b=ra5SOqrj; arc=none smtp.remote-ip=222.66.158.135 Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E80A606A322 for ; Thu, 3 Dec 2026 11:26:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6E80A606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=unisoc.com header.i=@unisoc.com header.a=rsa-sha256 header.s=default header.b=ra5SOqrj Received: from dlp.unisoc.com ([10.29.3.83]) by SHSQR01.spreadtrum.com with ESMTPS id 6B3BPN2H042443 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Thu, 3 Dec 2026 19:25:23 +0800 (+08) (envelope-from zhen.wang1@unisoc.com) Received: from SHDLP.spreadtrum.com (BJMBX01.spreadtrum.com [10.0.64.7]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4jgC3C3JR3z1VVX; Thu, 3 Dec 2026 19:25:11 +0800 (CST) Received: from zebjkernups01.spreadtrum.com (10.0.93.153) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Thu, 3 Dec 2026 19:25:21 +0800 From: Zhen Wang To: , , CC: , , , , , , , , , , , , , , , Subject: Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop() Date: Thu, 3 Dec 2026 19:25:14 +0800 Message-ID: <20261203112514.686980-1-zhen.wang1@unisoc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261120171658.Cf9qIU-Y@linutronix.de> References: <20261120171658.Cf9qIU-Y@linutronix.de> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL:SHSQR01.spreadtrum.com 6B3BPN2H042443 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unisoc.com; s=default; t=1796297149; bh=yqLqzivNZ0VWv6U6Ae9KZwjqenZZJs0yklhu0PolsG8=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=ra5SOqrjrVkiTPXfj24SEGQ39+RH0WeO82AAKVSbtlsCvi0ZfBUG5bE7ksh/8D8sc qZm7iJvEuaZYm/UO0COFLwCes+37guaoLOYNEvVnC6dC9GmDP23pn4+d063pPK4k9/ ikbH0gxvuAgBILdtPwf1Pkc0o1+0OSNamUq531wSqFhXp6JRaiygpSbuTvTAvRn5R5 1thnCnvXkF4CMoA+bBcN1tK4hRFKnfZLQ10m2VPGBnS1VijtV8Fpt9Ehc8fyMb39MM uw5VhSBS5id5l1wtg6HKDZK23cc/gNlhrG0cUnnIqojks2tm0XL9+bL0j+stICwSmV XXF/UO5wwaSyA== On Thu, Nov 26, 2026 at 11:40:49AM +0100, Peter Zijlstra wrote: > Fun though; if we did hit this condition then > migrate_enable() would have silently failed. Also not a good thing. > > We should probably have ___migrate_enable() warn on this. We hit exactly this condition in testing. The difference from the syzbot trace: the CPU stays offline, so the task's cpus_mask contains no online CPU at all, and we *do* take the -EINVAL exit even with the migration_disabled() mitigation switching to cpu_online_mask. Two extra consequences we see, beyond the silently ignored affinity: - __migrate_enable() only calls ___migrate_enable() when p->cpus_ptr !=3D &p->cpus_mask. With a stale cpus_ptr that branch is taken on every enable: each later migrate_enable() re-enters __set_cpus_allowed_ptr(), re-takes task_rq_lock() and fails again. A healthy task returns early without taking any lock. - That breaks migrate_enable() in constrained contexts. We see it from a BPF program attached to trace_sched_switch that calls migrate_enable(): the call takes task_rq_lock() on the runqueue __schedule() already holds and deadlocks. With an intact cpus_ptr the enable takes no lock, so the same program is fine. -- >8 -- From: Zhen Wang Date: Wed, 2 Dec 2026 16:47:03 +0800 Subject: [RFC PATCH] sched/core: Fix cpus_ptr leak leading to spinlock recursion on CPU hotplug MIME-Version: 1.0 Content-Type: text/plain; charset=3DUTF-8 Content-Transfer-Encoding: 8bit When a task changes its affinity to a CPU that is dying, __set_cpus_allowed_ptr can succeed in updating p->cpus_mask, but affine_move_task / migration_cpu_stop may fail to migrate the task, leaving it running on an out-of-affinity CPU. If the task then enters migrate_disable(), migrate_disable_switch() sets p->cpus_ptr to the per-CPU cpumask of the current CPU. When migrate_enable() subsequently calls __set_cpus_allowed_ptr(), cpumask_any_and_distribute() on p->cpus_mask returns an empty result because the original target CPU is already offline, causing __set_cpus_allowed_ptr_locked() to bail out without restoring p->cpus_ptr back to &p->cpus_mask. The stale cpus_ptr then causes any later migrate_enable() call =E2=80=94 including those invoked from BPF programs attached to trace_sched_switch =E2=80=94 to enter __set_cpus_allowed_ptr() and attempt task_rq_lock() on a runqueue already locked by __schedule(), triggering spinlock recursion. The comment in __set_cpus_allowed_ptr_locked() already warns about this: "Specifically, migration_disabled() tasks must not fail the cpumask_any_and_distribute() pick below, esp. so on SCA_MIGRATE_ENABLE, otherwise we'll not call set_cpus_allowed_common() and actually reset p->cpus_ptr." However the existing mitigation (using cpu_online_mask for migration_disabled tasks) is insufficient when the cpumask contains only offline CPUs. Fix this by restoring p->cpus_ptr to ctx->new_mask even when a valid destination CPU cannot be found, when called under SCA_MIGRATE_ENABLE. This is done under rq->lock and p->pi_lock, satisfying the locking requirement for cpus_ptr updates. Fixes: af449901b84c9 ("sched: Add migrate_disable()") Signed-off-by: Zhen Wang --- kernel/sched/core.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 8f5e15af3c4e..222dad4b73e3 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3245,6 +3245,8 @@ static int __set_cpus_allowed_ptr_locked(struct task_= struct *p, */ dest_cpu =3D cpumask_any_and_distribute(cpu_valid_mask, ctx->new_ma= sk); if (dest_cpu >=3D nr_cpu_ids) { + if (ctx->flags & SCA_MIGRATE_ENABLE) + p->cpus_ptr =3D ctx->new_mask; ret =3D -EINVAL; goto out; } -- 2.34.1 ________________________________ This email (including its attachments) is intended only for the person or = entity to which it is addressed and may contain information that is privile= ged, confidential or otherwise protected from disclosure. Unauthorized use,= dissemination, distribution or copying of this email or the information he= rein or taking any action in reliance on the contents of this email or the = information herein, by anyone other than the intended recipient, or an empl= oyee or agent responsible for delivering the message to the intended recipi= ent, is strictly prohibited. If you are not the intended recipient, please = do not read, copy, use or disclose any part of this e-mail to others. Pleas= e notify the sender immediately and permanently delete this e-mail and any = attachments if you received it in error. Internet communications cannot be = guaranteed to be timely, secure, error-free or virus-free. The sender does = not accept liability for any errors or omissions. =E6=9C=AC=E9=82=AE=E4=BB=B6=E5=8F=8A=E5=85=B6=E9=99=84=E4=BB=B6=E5=85=B7=E6= =9C=89=E4=BF=9D=E5=AF=86=E6=80=A7=E8=B4=A8=EF=BC=8C=E5=8F=97=E6=B3=95=E5=BE= =8B=E4=BF=9D=E6=8A=A4=E4=B8=8D=E5=BE=97=E6=B3=84=E9=9C=B2=EF=BC=8C=E4=BB=85= =E5=8F=91=E9=80=81=E7=BB=99=E6=9C=AC=E9=82=AE=E4=BB=B6=E6=89=80=E6=8C=87=E7= =89=B9=E5=AE=9A=E6=94=B6=E4=BB=B6=E4=BA=BA=E3=80=82=E4=B8=A5=E7=A6=81=E9=9D= =9E=E7=BB=8F=E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