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 47BC92FD1B3 for ; Thu, 18 Jun 2026 05:15:42 +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=1781759744; cv=none; b=JLipjPAN+DtEVTgzm0k8QET6GJUqFo1Re5VpZeY4+8jkKHfVKBCFw03BfcHO8njWaXUwvXfhBpd4aopds9F6CmVCZGWk7LGJopDKQJmBEX98vdJLdohu8FksKTVSgF9HLcJt50/yYdv9Zi1aNB4zmcBkAotrZfook+xoZ4mJINo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781759744; c=relaxed/simple; bh=2B4rE50XpYwGdTpr6baEFMbuq/cvFmSzxEZJzdwPIhg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ExxO+Hy92YMub7VBxa9XINNFuF4VOTSyxTfNm8g7dHsQFZqLWLA0ma1/rYuaL/W4pyeHfooPG5s4sXLXZwVu2igmU43EgJCu6S74ri6RyIrhC5jzX08GshzZMGhAkQ29pIGIz4OnNUSRa6hbuGvAkEl68AzbL7dlX4Smj4HebVg= 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=DFAKQfKX; 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="DFAKQfKX" Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 65HHmGqV1000243; Thu, 18 Jun 2026 05:14:42 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=vCJPZE lE1ld6qipBpE4/xAu1MIIx3KTm7CWsxG2qtio=; b=DFAKQfKXhmJh9G+NvjhO21 MIJCbM8pSFJCpY4GreUmTe/ZQFveErWvQDhiwDkXxy9RxqA458PcmAPqs2bTC1ZU xSFP3RJOzBI/BbuoWZQCeDh18FV8OJhuU0mj1g5NryNzLzZkG7UOz0xrdNxymROW we/z96B1iSAEFNYVFe7PhiF1/bNbmt2UDsz6Hf6rkCVJFEEe+KLbxRFig440y3zO zigt3WXG+D7aUJGz4XPnpZr9mG5h+oLUUnVeqBI6o/HOIPATcCn6YGXjHTF9+vjX NnL7OnUhzWv8gFuFeCmskO33fPYI61+/lTabnHbM5FWAz78hfFcUiSo6Tx6UuSZQ == Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4eueqx6c04-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 18 Jun 2026 05:14:41 +0000 (GMT) Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.18.1.7/8.18.1.7) with ESMTP id 65I54l7U031755; Thu, 18 Jun 2026 05:14:41 GMT Received: from smtprelay04.fra02v.mail.ibm.com ([9.218.2.228]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4ev172a403-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 18 Jun 2026 05:14:41 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay04.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 65I5Ebv631851032 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 18 Jun 2026 05:14:37 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E726E20043; Thu, 18 Jun 2026 05:14:36 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B5E6220040; Thu, 18 Jun 2026 05:14:30 +0000 (GMT) Received: from [9.123.5.233] (unknown [9.123.5.233]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 18 Jun 2026 05:14:30 +0000 (GMT) Message-ID: <0088b89f-d18b-4f36-863f-888c93177f76@linux.ibm.com> Date: Thu, 18 Jun 2026 10:44:29 +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 To: Yury Norov Cc: linux-kernel@vger.kernel.org, mingo@kernel.org, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, kprateek.nayak@amd.com, iii@linux.ibm.com, 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 References: <20260617174139.155540-1-sshegde@linux.ibm.com> <20260617174139.155540-7-sshegde@linux.ibm.com> From: Shrikanth Hegde Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-Reinject: loops=2 maxloops=12 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNjE4MDA0MCBTYWx0ZWRfX8zNVvU/rBx49 ry4Od33ksIyC9INbKUhp9frFt2PKPc/NUhwvDrCcFSbAGjjhsjkiJ2vHi2UC/5r64ApGO26aRbO RlftRdZlUogKuLVMLz2iZpJiIqpyaPmVNs8odikkLm85CbP1dAV2OMF2fAdbGMl2yXNOvRqnstn CVHMQwZjlVtk8XOSgWizGn0i8s62nxtAjMCAx8/7QRwyo6BKvfyaQSwbJu/nTVUqIYUzWvswAmJ GMq/2TMy2//2wHgJfAgjTjRyFepNtH9NWuS8wABqRIsNzb29UKq5RPwiG0eqxGLZozboLnccof5 j7EZPCXrbSGPL0aAq+grk38WFQSVbEBKfemktlFnVpH7oT8uqxeIr2CMglUFM/u4CwBOj5a3+wu 46j3+dWiY3xRjzTE+VeBv4k13nrkk+/f7HGcyIRQQgaqhuDCEEPl0FLdPBxFCJAbsTkjAE765sy n2xKAAnQ9Z8W1wswDLg== X-Proofpoint-GUID: Xon7GI4xjofHOl92mD2T4ELlvQF2Ut-l X-Proofpoint-Spam-Info: AW1haW4tMjYwNjE4MDA0MCBTYWx0ZWRfX43TXoOP1SNYY dYqDdGS8xu8xNoy75zBRpMjrTauGAqIL2k9sUW1jQOV5M/pReIhTFKt880bH1997xdqXkRKXd57 yA8c1C8ztofhMcf0ed7SIlcuiklLdl8= X-Proofpoint-ORIG-GUID: 4vp-A4Jdl-nE_XHs3DdgaylUMFKEdwtK X-Authority-Analysis: v=2.4 cv=auGCzyZV c=1 sm=1 tr=0 ts=6a337ec2 cx=c_pps a=GFwsV6G8L6GxiO2Y/PsHdQ==:117 a=GFwsV6G8L6GxiO2Y/PsHdQ==:17 a=IkcTkHD0fZMA:10 a=FelO9ux0wxsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=Y2IxJ9c9Rs8Kov3niI8_:22 a=DzHEmww-3viXP9ocNJ4A:9 a=QEXdDO2ut3YA:10 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-17_02,2026-06-17_03,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 clxscore=1015 priorityscore=1501 suspectscore=0 impostorscore=0 spamscore=0 lowpriorityscore=0 adultscore=0 malwarescore=0 bulkscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2606180040 On 6/18/26 10:19 AM, Yury Norov wrote: > On Thu, Jun 18, 2026 at 09:47:49AM +0530, Shrikanth Hegde wrote: >> >> It is reset to indicate that any subsequent direct calls to is_cpu_allowed can't use the >> old cached value of select_fallback_rq. >> >> So events could be, >> >> - cpu marked as non preferred - select_fallback_rq (sets the p->has_preferred_cpu_state) >> Lets say CPU(300-450) are marked as non-preferred and Task affinity is (200-350) >> - task moved out. Now either task's affinity changed or preferred_mask has changed. >> while CPU(400) maybe still marked as non-preferred but CPU(340) is marked as preferred. >> - Subsequent call to is_cpu_allowed (CPU=340) can't assume the old value. > > Please, no top-posting. > > My point is: out of the scope of the select_fallback_rq(), the > p->has_preferred_cpu_state is always 0 because of the line above. It > means, it doesn't belong to the task_struct, it belongs the current > scope. > > So either make this caching really surviving the scope exit, or make > it a local variable. > > Not sure I understood the passage above about the possible events, but > variables that are always zero out of the function scope should not be > placed in global structures. That was the other way. add one more variable to is_cpu_allowed. > >>> What for resetting it here? I think it should be zeroed only on update >>> of preferred cpumask. In other words, to properly implement caching, >>> you need to have a global counter incremented on each >>> cpu_preferred_mask update, and in task_has_preferred_cpus() you do: >>> >>> { >>> if (p->preferred_cpu_updates == atomic_read(preferred_cpumask_updates)) >>> return p->has_preferred_cpus; >>> >>> p->preferred_cpu_updates = atomic_read(preferred_cpumask_updates); >>> p->has_preferred_cpus = cpumask_intersects(...); >>> } >>> >>> Do you have any numbers that justify this caching? The best practice >>> is to put performance optimizations at the end of the series and >>> provide some sort of benchmark supporting it. >>> >> >> This was to avoid N**2 aspect that was there in select_fallback_rq. >> Its more of the functional aspect which i mentioned above which this needs >> to take care as well. > > Please, collect the performance data first, then optimize your code, > not vice-versa. It did make sense to cache it since select_fallback_rq does repeated calculations with pi_lock held and irq disabled. > >>>> return dest_cpu; >>>> } >>>> @@ -4612,6 +4641,7 @@ static void __sched_fork(u64 clone_flags, struct task_struct *p) >>>> init_numa_balancing(clone_flags, p); >>>> p->wake_entry.u_flags = CSD_TYPE_TTWU; >>>> p->migration_pending = NULL; >>>> + p->has_preferred_cpu_state = 0; >>>> init_sched_mm(p); >>>> } >>>> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h >>>> index c7c2dea65edd..38fd84b0b8f8 100644 >>>> --- a/kernel/sched/sched.h >>>> +++ b/kernel/sched/sched.h >>>> @@ -4213,4 +4213,22 @@ 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. >>>> + * >>>> + * 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. >>>> + */ >>>> +static inline bool task_has_preferred_cpus(struct task_struct *p) >>> >>> This function should be void because you change the task state. >>> >> >> It doesn't alter p->has_preferred_cpu_state. No? > > It doesn't, but it should. Not sure. i think this can be just a wrapper to indicate the info to inform if this cpu should be used or not based on task affinity. > >>>> +{ >>>> + int cached_value = p->has_preferred_cpu_state; >>>> + >>>> + if (cached_value & 0x1) >>>> + return p->has_preferred_cpu_state >> 8; >>>> + else >>>> + return cpumask_intersects(p->cpus_ptr, cpu_preferred_mask); >>>> +} >>>> #endif /* _KERNEL_SCHED_SCHED_H */ >>>> -- >>>> 2.47.3