From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (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 0B3503B2FC2 for ; Thu, 18 Jun 2026 14:12:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781791972; cv=none; b=I4ax8mxTy/KxfIE+FdNfC+PsoSSlF92TZQg0hWqFkZpk/quGfD3s6VpVdz1ktM7s8+muerDipH2AA32fnJTTuKYoSTzg0l8wxZkxp3jCYK81bPeS/AJQoCfYKp/TbRZEcNYljHSpD2o3aWKctof3yfOgcCJ+ZT3T+hAoqDFkXOg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781791972; c=relaxed/simple; bh=w4EhW+0i5Sjo5qnRA9M7Zc/XoZ+f0b7Sq9QRE+yk9Vw=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=rtd1LxC9HiCC1aLrcBVijQWM03vHQ9efi2KTKASYHDZ9ucm8ydDxYaO6pfr5j6Ph/aITg8sh73xb5arP63+Iji7ZkF0A8N5uxs8rapvZz7Jz2lzXvUXNAUkvlgY+l7+B1dk24ji6ri09rpsTFiodSk887EseOX3n+sJx4HCK62Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=D7jNPLNS; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="D7jNPLNS" Received: from pps.filterd (m0360072.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 65IDIO0B3428249; Thu, 18 Jun 2026 14:12:24 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=pp1; bh=QQo7MS Z/i/GWqT2PWbLdBUfVaQLCgGXuBNXc+H7Lqss=; b=D7jNPLNSSgJHQLvB4GP5bB aRhXJZd9bmP1bujY8eTOukQjMJSBI7q9er+kRm0Fbq0cx1diKN3ZzJCFWS17tFtf XlKBAiLMIERGAwEIn1hqG9cxAJfR+cQgVSm1g2KGTRIqct7U/Aq/4GfYD8nxne36 PkNXNQaFJxBWpxCWX1YjD240CunnWQMIrrqepz5EYaSy0CTEaWm88jDapiJXQEMt 7c37Zq83A2K6kgGU7ULKouJ0AvdrTEsBa0X8YL0+1T1DSOWcHJFC92Mm05LEqOV4 WYwIMVjT0jhW7NhcsJC1cNr9Nlp89+k5m+iVJiLyKJzbauZOsPxqg+9dfu9//pDg == Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4eueqx8ef2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 18 Jun 2026 14:12:23 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.7/8.18.1.7) with ESMTP id 65IE56aJ002547; Thu, 18 Jun 2026 14:12:23 GMT Received: from smtprelay07.fra02v.mail.ibm.com ([9.218.2.229]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4ev172c3ku-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 18 Jun 2026 14:12:22 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay07.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 65IECIYi37814772 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 18 Jun 2026 14:12:19 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DF52A2004B; Thu, 18 Jun 2026 14:12:18 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 308CF20040; Thu, 18 Jun 2026 14:12:10 +0000 (GMT) Received: from [9.39.16.238] (unknown [9.39.16.238]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 18 Jun 2026 14:12:09 +0000 (GMT) Message-ID: Date: Thu, 18 Jun 2026 19:42:08 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 06/20] sched/core: allow only preferred CPUs in is_cpu_allowed From: Shrikanth Hegde To: K Prateek Nayak , yury.norov@gmail.com Cc: tglx@kernel.org, gregkh@linuxfoundation.org, pbonzini@redhat.com, seanjc@google.com, vschneid@redhat.com, huschle@linux.ibm.com, rostedt@goodmis.org, dietmar.eggemann@arm.com, mgorman@suse.de, bsegall@google.com, maddy@linux.ibm.com, srikar@linux.ibm.com, hdanton@sina.com, chleroy@kernel.org, vineeth@bitbyteword.org, frederic@kernel.org, arighi@nvidia.com, pauld@redhat.com, christian.loehle@arm.com, tj@kernel.org, tommaso.cucinotta@gmail.com, maz@kernel.org, rafael@kernel.org, linux-kernel@vger.kernel.org, mingo@kernel.org, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, iii@linux.ibm.com References: <20260617174139.155540-1-sshegde@linux.ibm.com> <20260617174139.155540-7-sshegde@linux.ibm.com> <4b74ad30-9e49-4ff3-b242-23a495e84265@amd.com> <2e21726e-e82f-45d2-a015-917695b49253@linux.ibm.com> Content-Language: en-US In-Reply-To: <2e21726e-e82f-45d2-a015-917695b49253@linux.ibm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-Reinject: loops=2 maxloops=12 X-Proofpoint-GUID: SzgwdCzBla29MBuUDnU5bK-UPw98Xt44 X-Authority-Analysis: v=2.4 cv=Le0MLDfi c=1 sm=1 tr=0 ts=6a33fcc8 cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=IkcTkHD0fZMA:10 a=FelO9ux0wxsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=RzCfie-kr_QcCd8fBx8p:22 a=DRkM-UOMXAxtyBhp-T0A:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 X-Proofpoint-Spam-Info: AW1haW4tMjYwNjE4MDEyOSBTYWx0ZWRfX0RUDshsVvtet EKD4VeIOeaAGdPpI9i5+P53hNdS0dMxQTZoOFnR3WoUy/PmenWpf3jbd0AQ+sNtwyl0PsnuTy75 QJNJXZPq1WfmPCeGMEL6txgxx8arGuU= X-Proofpoint-ORIG-GUID: xm9z5cwH0k48YrS6Bi0YnhEEd1wE0Z-j X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNjE4MDEyOSBTYWx0ZWRfXxl3rIPuYF/tu omwYyTBBhKGL0Wy/hw9Axmz8FoLtXzMDtupAM5lbjuniMJrxtKSpBzbhPVgvKXL8TF3+XT8eSpf w4nmx9YiTWfm80rgcUNAy6yNp0LbuXDP6lJNwQCDJMhx23uzuPzjpwexqpKZplo3TwOoIHBXEkf vzp254dfaXZyG2gASS4/hfgiL/dL3eWm9k0gM0Mrpkj3gKAhC2PGPmFZ178tMdGVQRnWKYjre6j V4hQkjSQ0NXm4vmZxsnTXfg71+1e9jGONVluqW6j2E0q0HfgX8zCxgSk5f4li+sEQQQVKd/AKJ8 Uci2YmrocMxO+59zzsflPZT//TrVJ5K6EnRbC0f5SyavmPDFHRbCWc3E5cVK/bAAQkS5clfGKVy 0o9ni3dk3gVuWR6dzzLDMoX8bBK6l+hEuRl7sNVXDrrpfPdXs6NKkcTaaXcaJ9qfcqSqdKt7SUx aj1lh9/KtObFS6/DKnA== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.125,FMLib:17.12.100.49 definitions=2026-06-18_01,2026-06-18_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 priorityscore=1501 malwarescore=0 spamscore=0 suspectscore=0 impostorscore=0 phishscore=0 lowpriorityscore=0 bulkscore=0 adultscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2606180129 Hi Prateek, Yury, On 6/18/26 9:52 AM, Shrikanth Hegde wrote: > Hi Prateek. > > On 6/18/26 9:19 AM, K Prateek Nayak wrote: >> Hello Shrikanth, >> >> On 6/17/2026 11:11 PM, Shrikanth Hegde wrote: >>> --- a/include/linux/sched.h >>> +++ b/include/linux/sched.h >>> @@ -1657,6 +1657,7 @@ struct task_struct { >>>   #ifdef CONFIG_UNWIND_USER >>>       struct unwind_task_info        unwind_info; >>>   #endif >>> +    int                has_preferred_cpu_state; >> >> Since this only really needs 2 bits, perhaps you can use the "u8 __pad" >> in the task struct? ... >> >>>       /* CPU-specific state of this task: */ >>>       struct thread_struct        thread; >> >> [...snip...] >> >>> @@ -3549,6 +3568,14 @@ static int select_fallback_rq(int cpu, struct >>> task_struct *p) >>>       enum { cpuset, possible, fail } state = cpuset; >>>       int dest_cpu; >>> +    /* >>> +     * Cache value whether task's affinity spans preferred CPUs. >>> +     * This helps to avoid repeating the same for each CPU >>> +     * later in the loop. Encode call to is_cpu_allowed coming >>> +     * via select_fallback_rq. >>> +     */ >>> +    p->has_preferred_cpu_state = task_has_preferred_cpus(p) << 8 | 0x1; >> >> ... Or maybe it can be an s8 and this indicator can be a tri-state like: >> >>   -1:  Cached; preferred CPUs exists >>    0:  preferred CPUs not cached >>    1:  Cached; preferred CPUs don't exist >> > > Yes. Current encoding has. > > 1. call came from select_fallback_rq and preferred CPUs exists > 2. call came from select_fallback_rq and preferred CPUs doesn't exists > 3. Call didn't come from select_fallback_rq and evaluate the > cpumask_interesect. > >> and then the comparison can simply be: >> >>      if (cached) >>          return cached < 0; >> >> ppc64le and arm64 seems to generates slightly smaller code for "< 0" >> check compared to the current scheme of (& + >>) in >> task_has_preferred_cpus(). >> > > Yes, this seems better. I will give it a try. I guess below captures the discussion on this patch. Other way is pass it as local variable to is_cpu_allowed. --- diff --git a/include/linux/sched.h b/include/linux/sched.h index e435f3073ffc..8c2fe953f19b 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1658,7 +1658,7 @@ struct task_struct { #ifdef CONFIG_UNWIND_USER struct unwind_task_info unwind_info; #endif - int has_preferred_cpu_state; + s8 has_preferred_cpu_state; /* CPU-specific state of this task: */ struct thread_struct thread; diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 9342aae315ca..26c82cd0bcec 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2504,7 +2504,7 @@ static inline bool rq_has_pinned_tasks(struct rq *rq) */ static inline bool is_cpu_allowed(struct task_struct *p, int cpu) { - bool task_check_preferred_cpu = false; + bool task_check_preferred_cpu; /* When not in the task's cpumask, no point in looking further. */ if (!task_allowed_on_cpu(p, cpu)) @@ -3573,12 +3573,12 @@ static int select_fallback_rq(int cpu, struct task_struct *p) int dest_cpu; /* - * Cache value whether task's affinity spans preferred CPUs. + * Cache the value whether task's affinity spans preferred CPUs. * This helps to avoid repeating the same for each CPU * later in the loop. Encode call to is_cpu_allowed coming * via select_fallback_rq. */ - p->has_preferred_cpu_state = task_has_preferred_cpus(p) << 8 | 0x1; + p->has_preferred_cpu_state = task_has_preferred_cpus(p) ? 1 : -1; /* * If the node that the CPU is on has been offlined, cpu_to_node() diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index f3814099cc0b..da8134568229 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -4220,20 +4220,20 @@ DEFINE_CLASS_IS_UNCONDITIONAL(sched_change) #include "ext.h" /* - * has_preferred_cpu_state is encoding two bits of information. - * First Byte is to encode where the call to is_cpu_allowed coming from. - * Second Byte is to encode the intersection of task affinity - * and cpu_preferred_mask. + * has_preferred_cpu_state could have the value cached from + * select_fallback_rq. It is set/cleared while holding pi_lock + * and irq disabled. * - * If 1st Byte is set, call to is_cpu_allowed coming from select_fallback_rq. - * That helps to avoid repeated calculation keeping time complexity same. + * 1: Cached and preferred CPUs exists in task's affinity. + * 0: Not cached and need to evaluate. + * -1: Cached and preferred CPU doesn't exits task's affinity */ static inline bool task_has_preferred_cpus(struct task_struct *p) { - int cached_value = p->has_preferred_cpu_state; + int cached = p->has_preferred_cpu_state; - if (cached_value & 0x1) - return p->has_preferred_cpu_state >> 8; + if (cached) + return cached > 0; else return cpumask_intersects(p->cpus_ptr, cpu_preferred_mask); }