From nobody Mon Oct 6 17:04:07 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD9EC2E5B1A for ; Fri, 18 Jul 2025 16:49:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752857371; cv=none; b=azL5Nnufxc8bP6q4giJsZeO0Vdw5O0Eorg+gJpGKxIcrNtsdEflRMrb1U1sDCLnTJeO+EJ3I1qrvubkgPCDbncSV1CvcSEoObJy3Ye/Eh9aYeZg9o6xft2AzfvBg8Pc0/vBb6YUHfEfjhCGUeaMtVpk1OX0KD8VKNuzPluCaMyY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752857371; c=relaxed/simple; bh=4uoXBBfWbqYL3O4e8u7GIuSRK9OMIEk0Tri9CKXPXgo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YZOIAyc7GW5cOcI6grt1fLNIEMKaU2gTjy1Yzqdsb9frmTUQMpS0l6NpsImsL+S/v9MtZ4h/1oobgRWwlCjHR7NLpOGgPyV1WWO9KoACXX96sFSqCPF+UZ37Rtl/dLaaT4AD1/WnujW7eMa/h80UFSHTlZcQsHkze4Nn6FWa8eM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=J14utVha; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="J14utVha" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1752857368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aZcCp9t+rgA1nQ2OVuDyJEw0PACPvSc+gXRhn6+fay0=; b=J14utVhaOHdKtRu8wCqU87YJR/ZaR9REAm3d+HnwH6aNL4YC+ERFxWMBuFbzGm71IC8sPg c5cTyw6tzkqXax/uhcx7Fht76h4s8mbvDqi+NHEeD9JdOuztezp3W2mkOUaEhkcyjEiQ1Z JTIdtNwsRnLxIikFRSKhBimiDzj5MDE= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-628-ep-vzKe8N7ei0CH1Pn3d3A-1; Fri, 18 Jul 2025 12:49:25 -0400 X-MC-Unique: ep-vzKe8N7ei0CH1Pn3d3A-1 X-Mimecast-MFC-AGG-ID: ep-vzKe8N7ei0CH1Pn3d3A_1752857363 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F280C180034E; Fri, 18 Jul 2025 16:49:22 +0000 (UTC) Received: from llong-thinkpadp16vgen1.westford.csb (unknown [10.22.90.55]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C6F78180049D; Fri, 18 Jul 2025 16:49:19 +0000 (UTC) From: Waiman Long To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Chen Ridong , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Waiman Long Subject: [PATCH v3] sched/core: Skip user_cpus_ptr masking if no online CPU left Date: Fri, 18 Jul 2025 12:48:56 -0400 Message-ID: <20250718164857.31963-1-longman@redhat.com> In-Reply-To: <20250718164143.31338-1-longman@redhat.com> References: <20250718164143.31338-1-longman@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Chen Ridong reported that cpuset could report a kernel warning for a task due to set_cpus_allowed_ptr() returning failure in the corner case that: 1) the task used sched_setaffinity(2) to set its CPU affinity mask to be the same as the cpuset.cpus of its cpuset, 2) all the CPUs assigned to that cpuset were taken offline, and 3) cpuset v1 is in use and the task had to be migrated to the top cpuset. Due to the fact that CPU affinity of the tasks in the top cpuset are not updated when a CPU hotplug online/offline event happens, offline CPUs are included in CPU affinity of those tasks. It is possible that further masking with user_cpus_ptr set by sched_setaffinity(2) in __set_cpus_allowed_ptr() will leave only offline CPUs in the new mask causing the subsequent call to __set_cpus_allowed_ptr_locked() to return failure with an empty CPU affinity. Fix this failure by skipping user_cpus_ptr masking if there is no online CPU left. Reported-by: Chen Ridong Closes: https://lore.kernel.org/lkml/20250714032311.3570157-1-chenridong@hu= aweicloud.com/ Fixes: da019032819a ("sched: Enforce user requested affinity") Signed-off-by: Waiman Long Tested-by: Chen Ridong --- kernel/sched/core.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 81c6df746df1..208f8af73134 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3170,12 +3170,13 @@ int __set_cpus_allowed_ptr(struct task_struct *p, s= truct affinity_context *ctx) =20 rq =3D task_rq_lock(p, &rf); /* - * Masking should be skipped if SCA_USER or any of the SCA_MIGRATE_* - * flags are set. + * Masking should be skipped if SCA_USER, any of the SCA_MIGRATE_* + * flags are set or no online CPU left. */ if (p->user_cpus_ptr && !(ctx->flags & (SCA_USER | SCA_MIGRATE_ENABLE | SCA_MIGRATE_DISABLE))= && - cpumask_and(rq->scratch_mask, ctx->new_mask, p->user_cpus_ptr)) + cpumask_and(rq->scratch_mask, ctx->new_mask, p->user_cpus_ptr) && + cpumask_intersects(rq->scratch_mask, cpu_active_mask)) ctx->new_mask =3D rq->scratch_mask; =20 return __set_cpus_allowed_ptr_locked(p, ctx, rq, &rf); --=20 2.50.0