From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 2F9BE365A14 for ; Wed, 19 Nov 2025 17:26:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763573222; cv=none; b=TN5BfTNkG+Jf5CmKm49LxVKnOC3qCcqyyUeg5W7bpfKjowEisJAbpg8cx+WHu792ojqtyXxDA9raKCN5P0kCoxmO3p9i7hj/xdkDWKvNM0twhvBgE38215yxW0gOyU3KVqMHeHbeMN/GYWjpLC/odDwTOx2dZQEV1WZilAsFxbk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763573222; c=relaxed/simple; bh=x8iBaiaicGAtjmD4BmGRaGt7f+JdMe3FEJcOVd+rVwc=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=iE2WYVZW2VZs61NIrcfIwOlSK7/t8potcx6dOSaWK8NYr+hnBOTR4LnV1lRMeUUtFgQj8bWfjbYFXMm9bL3QNtITbaBSJQAzhSyRclZrJGXbDLvLvLbRWmreejFGIJJ0aeEgM1HHK5fE6heBlYK+D1kQEZaSZ4jFg4wuFxYaKxw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=1/KOjNlD; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=HcKujRIW; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1/KOjNlD"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="HcKujRIW" Message-ID: <20251119172549.385208276@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1763573216; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=p7hyel86UBVfl9zWGClTa0FqOcfHkwxsnIzR8LtL0OU=; b=1/KOjNlDNN+LbASp/E7yAIifoz8sVbNN6hdKEroG4FkPzUu9etD8/gu0Wgb/4xyhtBiikt x448Gqv0cg/Mn52C12U5fOSDCzWF4TM4RsQvgH1gOLWbxn5GofyrYXxqRYNDt+bPfQWMho e5+Jef2u6AY359L8rwGl87W573D5I1umxjrCIoKVaqntl0g1ga5DtOdGqdywidbyVa3Ihb /8CG/Rg9Ds6Wk0IwuvpmZOI5ra0rA//obvXRRP9O9ALtg9cIF9JuvzlZgDooot6NYYYDv6 g3R7hm37kS4+uyURLQavZcprgMvOrFntGSMePkYUcIaIk65hDhiRiATjY5POQA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1763573216; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=p7hyel86UBVfl9zWGClTa0FqOcfHkwxsnIzR8LtL0OU=; b=HcKujRIWtGG2/YU05gCb0roM8ErpAREp3iZxiMRLJty5CENlpsgUOVF545X+6aLqBBOCqv 3I9E5KtOmRqNFeCw== From: Thomas Gleixner To: LKML Cc: Peter Zijlstra , Gabriele Monaco , Mathieu Desnoyers , Michael Jeanson , Jens Axboe , "Paul E. McKenney" , "Gautham R. Shenoy" , Florian Weimer , Tim Chen , Yury Norov , Shrikanth Hegde Subject: [patch V5 06/20] sched/mmcid: Prevent pointless work in mm_update_cpus_allowed() References: <20251119171016.815482037@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Wed, 19 Nov 2025 18:26:55 +0100 (CET) mm_update_cpus_allowed() is not required to be invoked for affinity changes due to migrate_disable() and migrate_enable(). migrate_disable() restricts the task temporarily to a CPU on which the task was already allowed to run, so nothing changes. migrate_enable() restores the actual task affinity mask. If that mask changed between migrate_disable() and migrate_enable() then that change was already accounted for. Move the invocation to the proper place to avoid that. Signed-off-by: Thomas Gleixner Reviewed-by: Mathieu Desnoyers --- V2: Remove the nr_cpu_ids optimization which does not really work - PeterZ --- kernel/sched/core.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2684,6 +2684,7 @@ void set_cpus_allowed_common(struct task cpumask_copy(&p->cpus_mask, ctx->new_mask); p->nr_cpus_allowed = cpumask_weight(ctx->new_mask); + mm_update_cpus_allowed(p->mm, ctx->new_mask); /* * Swap in a new user_cpus_ptr if SCA_USER flag set @@ -2730,7 +2731,6 @@ static void put_prev_task(rq, p); p->sched_class->set_cpus_allowed(p, ctx); - mm_update_cpus_allowed(p->mm, ctx->new_mask); if (queued) enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); @@ -10376,12 +10376,17 @@ void call_trace_sched_update_nr_running( */ static inline void mm_update_cpus_allowed(struct mm_struct *mm, const struct cpumask *affmsk) { - struct cpumask *mm_allowed = mm_cpus_allowed(mm); + struct cpumask *mm_allowed; if (!mm) return; - /* The mm_cpus_allowed is the union of each thread allowed CPUs masks. */ + + /* + * mm::mm_cid::mm_cpus_allowed is the superset of each threads + * allowed CPUs mask which means it can only grow. + */ guard(raw_spinlock)(&mm->mm_cid.lock); + mm_allowed = mm_cpus_allowed(mm); cpumask_or(mm_allowed, mm_allowed, affmsk); WRITE_ONCE(mm->mm_cid.nr_cpus_allowed, cpumask_weight(mm_allowed)); }