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 9D20727B34D for ; Sun, 16 Nov 2025 20:48:47 +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=1763326129; cv=none; b=cRH4D1reN8sGWYYDeXGXCksTiXr2r0zA0mzohw3I5ugwGeUGLXRBdMfLasZGWF+skg/gjFcnQyyF4/eq9/GLgN++htek3o3PhQ9168C+mUAUDrDUJsixEDJJqWMYU2n8j0UyiETPqG0A7CWloAXI5h7c9URd9tS4Xw1vvO2AX3c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763326129; c=relaxed/simple; bh=mLARz8eOD6cANZ+D67/eKif5/sO/GjjSQEW4ngnAZTo=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=QWdtXb6lQaq+2jf+XvNZlcA8QQUmq6Xj2JZRVHyaZkR16Sf7VRc9S9TXZ4EvxSPSXVuTK7cV6v2EfOkikbZtZrVF7pNFGKV6IaBGy3acCe+YmNEujEb5ABAyCNOxd8ZrA4aVmBMm8ADl7DyDoYzwVUiQmsXJZY1Rwc8Tc1unC+I= 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=z/3U96j+; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=wz2+Q370; 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="z/3U96j+"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="wz2+Q370" Message-ID: <20251104075427.015769425@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1763326126; 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=tXxS/buarlh+mvFVSZjGYRW/JDWR27Mw6LNRTr34ibY=; b=z/3U96j+waGEOgzZrUGnzOIquqNc/Weret+fge/QV9g33qU8bvDCG7Oj7ntvsgt/DwqWAA z4Ck36IbVD21Mzw1TyrqChyN4AcHd3YT45mt6onIdeskp7v1pnSkbPJClnzxAvPH1EucZU IhjFnyJtg7XPGYHtwz1NHe2oskqU8m0UQ/BodrDzzG6qGZY58NkJ1uOKVVhJmAwotaMc2x 2IAIfaeVERpmu9E/52yr8Wf+3mzPm8sPlJ00x6UWa+tT4FTIwS0FuWNiAqk2by6CbhNIFu kYn6/GS+YadI+eO8ihr8SbM0J5ks7GAsgsPXxgjNpaG6ZmmNx5lgvQUtkKHbNQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1763326126; 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=tXxS/buarlh+mvFVSZjGYRW/JDWR27Mw6LNRTr34ibY=; b=wz2+Q370vyuQnXSdTlyWyKFunSu2dlKZ1CLtRcjLCTbJ572zsio9WyQf03J5boh5+wd13s xTVP4mmhEykaUoBg== 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 V4 06/20] sched/mmcid: Prevent pointless work in mm_update_cpus_allowed() References: <20251104075053.700034556@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: Sun, 16 Nov 2025 21:48:45 +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)); }