Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp217461rdb; Thu, 19 Oct 2023 02:15:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFIfq+lbTqK5HhcxYe1/iB2TBQOjwfhACKQdrUx87R7gWruS+Y8smWipPuw/cPn48b4tBcq X-Received: by 2002:a17:902:db03:b0:1c2:218c:3754 with SMTP id m3-20020a170902db0300b001c2218c3754mr2148999plx.53.1697706931043; Thu, 19 Oct 2023 02:15:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697706931; cv=none; d=google.com; s=arc-20160816; b=qYExNLnXQgdYSWaenh88u9TpTChL+ZfN5oYWpNYpG+GDrWh/jSEjL+l209RY94ZMoR vWlmCvrRgfevEwrvbbW5HFjAtZwWMxk5qkq8MwTtTQogxfChvtWF45d9GV17jDq+oVfB FmUPA4TO3fPLGpP2sT8QKwSY54vqKgvEAVDndgJn5dyyyHvOjLDB1V9u3gyRzGozXjUO YE26i8eV8AbXzCHPxI97t4RShHaCQkg8uYHM6AeJVE46uPc3kXsGe3EkWmCyqPS9wh14 Z05EvjuKaicQSeWAHyctvMBwp3RPmgXxKG0tODAMQfq3TPmlU8LMGTaz+Bx42CGygFct peOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :message-id:date:subject:cc:to:from:dkim-signature; bh=ABt7ADKK8tsXYCvzjlIR2O1T/6z1I1a3pW7ZzNN+dV0=; fh=rjPffiPYtCU2NTuWGUqdkg+pDut/PgpFxRGySpJWweQ=; b=ALD+plQqSknzCgW0Kgy66XjF07t5K6rvb4PwCV+b6UP3icIa+QnifIdaZi2Pf3U585 7DX5IcgFQiSQ+sSvoEhryodoAiUwLv9Vjo0X+hgAhxQca1A/kDE8yPZkpY8OVOJ0KCJG 9fGvcC6rI+bton0XH5Rmr320SiuzzCIcV+/JzfQr3IdLp6FssBZ5PlywxU1xm2HRvatW +dz4t4epklWy3wsPMLc2up/2J7sKbULSHDu1XY66VW6M+JnK8O2Y9a1OkYWYbzOBBBpF V5sOP+e0S8xZS0vWvc0RKEhNzu9CGYfyVennQQAfsGA3Nvi/D0XukaPs4LaMDsDnOuZS rexQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="gELg/ZYV"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id z3-20020a1709028f8300b001c752da513asi1606731plo.405.2023.10.19.02.15.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 02:15:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="gELg/ZYV"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 6900E8135CF0; Thu, 19 Oct 2023 02:15:27 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232846AbjJSJPQ (ORCPT + 99 others); Thu, 19 Oct 2023 05:15:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229830AbjJSJPP (ORCPT ); Thu, 19 Oct 2023 05:15:15 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46EB8FE for ; Thu, 19 Oct 2023 02:15:13 -0700 (PDT) Received: from pps.filterd (m0353727.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 39J97Jo7032585; Thu, 19 Oct 2023 09:15:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : content-transfer-encoding : mime-version; s=pp1; bh=ABt7ADKK8tsXYCvzjlIR2O1T/6z1I1a3pW7ZzNN+dV0=; b=gELg/ZYVZOCDvUy6GAi62Bo7pOSF4DHB1lFZ9CCMvQ9PREVD2/Q7TwQ0gaXKSaeIaERf 0sfaMx/uXIARfGo2B7IWOzNZ5ZpETwmEXt5Ci86WmnaxT+LQho6GrFKL7XtrblITqvXb tFu9m+pptuKN4dLFtV0cUOHZes52AatZ2A0BbHJ0sXrcoaxwFujRpE6VhExwsgl3S4SZ RQxV2Odr7VofoiTEbsq73Bsx19feTLStLByHw+2w6y4EC4wwshVamyCCoSwFmK3vzgo8 Gmxpmy7OTFbFq7zCL/FDcVGajTg4JJPQRVTnANYuc+Xffjeukk23AMwd0z4eUG0HUJc0 4w== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3tu1hx08um-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 19 Oct 2023 09:15:01 +0000 Received: from m0353727.ppops.net (m0353727.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 39J97gcK002996; Thu, 19 Oct 2023 09:15:01 GMT 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 3tu1hx08tb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 19 Oct 2023 09:15:00 +0000 Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 39J88Pch020150; Thu, 19 Oct 2023 09:14:59 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3tr6anfr3t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 19 Oct 2023 09:14:59 +0000 Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 39J9Ev9927460236 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 Oct 2023 09:14:57 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 19EF020040; Thu, 19 Oct 2023 09:14:57 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B6F7420043; Thu, 19 Oct 2023 09:14:54 +0000 (GMT) Received: from sapthagiri.in.ibm.com (unknown [9.109.198.63]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 19 Oct 2023 09:14:54 +0000 (GMT) From: Srikar Dronamraju To: Michael Ellerman Cc: linuxppc-dev , Srikar Dronamraju , Aboorva Devarajan , Shrikanth Hegde , Ajay Kaher , Alexey Makhalov , VMware PV-Drivers Reviewers , Nicholas Piggin , Christophe Leroy , virtualization@lists.linux-foundation.org, x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4] powerpc/paravirt: Improve vcpu_is_preempted Date: Thu, 19 Oct 2023 14:44:52 +0530 Message-ID: <20231019091452.95260-1-srikar@linux.vnet.ibm.com> X-Mailer: git-send-email 2.41.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: zIHbyWlsj8PSg93w1BZId6u8pJalyHr6 X-Proofpoint-ORIG-GUID: PTeXcGKCHRu49IjQ_8I_63TZW_iZncgV Content-Transfer-Encoding: 8bit X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-10-19_06,2023-10-18_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 clxscore=1015 adultscore=0 spamscore=0 malwarescore=0 phishscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 suspectscore=0 impostorscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2309180000 definitions=main-2310190077 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 19 Oct 2023 02:15:27 -0700 (PDT) PowerVM Hypervisor dispatches on a whole core basis. In a shared LPAR, a CPU from a core that is CEDED or preempted may have a larger latency. In such a scenario, its preferable to choose a different CPU to run. If one of the CPUs in the core is active, i.e neither CEDED nor preempted, then consider this CPU as not preempted. Also if any of the CPUs in the core has yielded but OS has not requested CEDE or CONFER, then consider this CPU to be preempted. Correct detection of preempted CPUs is important for detecting idle CPUs/cores in task scheduler. Tested-by: Aboorva Devarajan Reviewed-by: Shrikanth Hegde Signed-off-by: Srikar Dronamraju --- Changelog: v2 (http://lkml.kernel.org/r/20231018155838.2332822-1-srikar@linux.vnet.ibm.com) -> v4: Resolved comments from Michael Ellerman v1 (http://lkml.kernel.org/r/20231009051740.17683-1-srikar@linux.vnet.ibm.com) -> v2: Handle lppaca_of(cpu) in !PPC_SPLPAR case. 1. Fixed some compilation issues reported by kernelbot a. https://lore.kernel.org/oe-kbuild-all/202310102341.K0sgoqQL-lkp@intel.com/ b. https://lore.kernel.org/oe-kbuild-all/202310091636.lElmJkYV-lkp@intel.com/ 2. Resolved comments from Shrikanth --- arch/powerpc/include/asm/paravirt.h | 47 +++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/include/asm/paravirt.h b/arch/powerpc/include/asm/paravirt.h index e08513d73119..ac4279208d63 100644 --- a/arch/powerpc/include/asm/paravirt.h +++ b/arch/powerpc/include/asm/paravirt.h @@ -71,6 +71,11 @@ static inline void yield_to_any(void) { plpar_hcall_norets_notrace(H_CONFER, -1, 0); } + +static inline bool is_vcpu_idle(int vcpu) +{ + return lppaca_of(vcpu).idle; +} #else static inline bool is_shared_processor(void) { @@ -100,6 +105,10 @@ static inline void prod_cpu(int cpu) ___bad_prod_cpu(); /* This would be a bug */ } +static inline bool is_vcpu_idle(int vcpu) +{ + return false; +} #endif #define vcpu_is_preempted vcpu_is_preempted @@ -121,9 +130,23 @@ static inline bool vcpu_is_preempted(int cpu) if (!is_shared_processor()) return false; + /* + * If the hypervisor has dispatched the target CPU on a physical + * processor, then the target CPU is definitely not preempted. + */ + if (!(yield_count_of(cpu) & 1)) + return false; + + /* + * If the target CPU has yielded to Hypervisor but OS has not + * requested idle then the target CPU is definitely preempted. + */ + if (!is_vcpu_idle(cpu)) + return true; + #ifdef CONFIG_PPC_SPLPAR if (!is_kvm_guest()) { - int first_cpu; + int first_cpu, i; /* * The result of vcpu_is_preempted() is used in a @@ -149,11 +172,29 @@ static inline bool vcpu_is_preempted(int cpu) */ if (cpu_first_thread_sibling(cpu) == first_cpu) return false; + + /* + * If any of the threads of the target CPU's core are not + * preempted or ceded, then consider target CPU to be + * non-preempted. + */ + first_cpu = cpu_first_thread_sibling(cpu); + for (i = first_cpu; i < first_cpu + threads_per_core; i++) { + if (i == cpu) + continue; + if (!(yield_count_of(i) & 1)) + return false; + if (!is_vcpu_idle(i)) + return true; + } } #endif - if (yield_count_of(cpu) & 1) - return true; + /* + * None of the threads in target CPU's core are running but none of + * them were preempted too. Hence assume the target CPU to be + * non-preempted. + */ return false; } base-commit: eddc90ea2af5933249ea1a78119f2c8ef8d07156 -- 2.31.1