From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 333382550D7; Mon, 24 Nov 2025 07:50:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763970644; cv=none; b=IElIQavBQeXyRZEUbJac3XOb9gS6XQwMvmRIpETBBYORTqKwV0uLMbUjxh98amswounRt/cq1pnttknTLIHcuY7hhiVL9sgUPbZYaxgg0og1WREXUa3mM+KbIJerH6cUt90yTfcv/kkMW2y3OWRJuhK3/7FqDZFMiixo/0Fgt+s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763970644; c=relaxed/simple; bh=w41fTTwzx8VuAOb8fbLjiB8IGgG71yXkBEsBxzqQ1SQ=; h=Subject:To:Cc:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=M8RrGuLWe0Omr9/w1M2d013Xf2relS88d5+xoLcLNiEtqcWFGbnc/sUQHlVh/7nrw+WBP/tbF16uGACzJM8K8bivp0GnqosmnzAT7XRSxSFIvYFxExDv2vfExPAWlFMXWEzrbjjEYlblTWeSgh0Q7qZ0ZSQLCSGqv2o5FBDcx4c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.20.42.62]) by gateway (Coremail) with SMTP id _____8Bx379ODiRpoWsnAA--.17217S3; Mon, 24 Nov 2025 15:50:38 +0800 (CST) Received: from [10.20.42.62] (unknown [10.20.42.62]) by front1 (Coremail) with SMTP id qMiowJAxecBLDiRpe309AQ--.13235S3; Mon, 24 Nov 2025 15:50:37 +0800 (CST) Subject: Re: [PATCH v2 2/3] LoongArch: Add paravirt support with vcpu_is_preempted() in guest side To: Huacai Chen Cc: Paolo Bonzini , WANG Xuerui , Juergen Gross , Ajay Kaher , Alexey Makhalov , Broadcom internal kernel review list , kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, x86@kernel.org References: <20251124035402.3817179-1-maobibo@loongson.cn> <20251124035402.3817179-3-maobibo@loongson.cn> <5d80c452-bbc3-539a-fb8d-14dbe353f8cb@loongson.cn> From: Bibo Mao Message-ID: <718b5b5d-2bb1-5d59-409e-54f54516a6b7@loongson.cn> Date: Mon, 24 Nov 2025 15:48:09 +0800 User-Agent: Mozilla/5.0 (X11; Linux loongarch64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-CM-TRANSID:qMiowJAxecBLDiRpe309AQ--.13235S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxZw4fJFWkWw17Jr1rGF1ktFc_yoWrAw4xpF yUCF1vva18GryxA39Fqw1qkrn8tr1kW3WIqa47Ga45Ar1q9FnrJr4v9ryj9Fyvqwn7WF10 qr97GF4S9Fyrt3gCm3ZEXasCq-sJn29KB7ZKAUJUUUU7529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUPab4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWU AVWUtwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcVAKI4 8JMxk0xIA0c2IEe2xFo4CEbIxvr21lc7CjxVAaw2AFwI0_JF0_Jw1l42xK82IYc2Ij64vI r41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_GFv_Wrylx2IqxVAqx4xG67 AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIY rxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14 v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWx JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUxYiiDU UUU On 2025/11/24 下午3:13, Huacai Chen wrote: > On Mon, Nov 24, 2025 at 3:03 PM Bibo Mao wrote: >> >> >> >> On 2025/11/24 下午2:33, Huacai Chen wrote: >>> Hi, Bibo, >>> >>> On Mon, Nov 24, 2025 at 11:54 AM Bibo Mao wrote: >>>> >>>> Function vcpu_is_preempted() is used to check whether vCPU is preempted >>>> or not. Here add implementation with vcpu_is_preempted() when option >>>> CONFIG_PARAVIRT is enabled. >>>> >>>> Signed-off-by: Bibo Mao >>>> --- >>>> arch/loongarch/include/asm/qspinlock.h | 5 +++++ >>>> arch/loongarch/kernel/paravirt.c | 16 ++++++++++++++++ >>>> 2 files changed, 21 insertions(+) >>>> >>>> diff --git a/arch/loongarch/include/asm/qspinlock.h b/arch/loongarch/include/asm/qspinlock.h >>>> index e76d3aa1e1eb..9a5b7ba1f4cb 100644 >>>> --- a/arch/loongarch/include/asm/qspinlock.h >>>> +++ b/arch/loongarch/include/asm/qspinlock.h >>>> @@ -34,6 +34,11 @@ static inline bool virt_spin_lock(struct qspinlock *lock) >>>> return true; >>>> } >>>> >>>> +#ifdef CONFIG_SMP >>>> +#define vcpu_is_preempted vcpu_is_preempted >>>> +bool vcpu_is_preempted(int cpu); >>> In V1 there is a build error because you reference mp_ops, so in V2 >>> you needn't put it in CONFIG_SMP. >> The compile failure problem is that vcpu_is_preempted() is redefined in >> both arch/loongarch/kernel/paravirt.c and include/linux/sched.h > But other archs don't define vcpu_is_preempted() under CONFIG_SMP, and so what is advantage to implement this function if CONFIG_SMP is disabled? > you can consider to inline the whole vcpu_is_preempted() here. Defining the function vcpu_is_preempted() as inlined is not so easy for me, it beyond my ability now :( With static key method, the static key need be exported, all modules need apply the jump label, that is dangerous and I doubt whether it is deserved. Regards Bibo Mao > >> >> The problem is that is not included by sched.h, if >> CONFIG_SMP is disabled. Here is part of file include/linux/spinlock.h >> #ifdef CONFIG_SMP >> # include >> #else >> # include >> #endif >> >>> On the other hand, even if you really build a UP guest kernel, when >>> multiple guests run together, you probably need vcpu_is_preemtped. >> It is not relative with multiple VMs. When vcpu_is_preempted() is >> called, it is to detect whether dest CPU is preempted or not, the cpu >> from smp_processor_id() should not be preempted. So in generic >> vcpu_is_preempted() works on multiple vCPUs. > OK, I'm wrong here. > > > Huacai > >> >> Regards >> Bibo Mao >>> >>> >>> Huacai >>> >>>> +#endif >>>> + >>>> #endif /* CONFIG_PARAVIRT */ >>>> >>>> #include >>>> diff --git a/arch/loongarch/kernel/paravirt.c b/arch/loongarch/kernel/paravirt.c >>>> index b1b51f920b23..d4163679adc4 100644 >>>> --- a/arch/loongarch/kernel/paravirt.c >>>> +++ b/arch/loongarch/kernel/paravirt.c >>>> @@ -246,6 +246,7 @@ static void pv_disable_steal_time(void) >>>> } >>>> >>>> #ifdef CONFIG_SMP >>>> +DEFINE_STATIC_KEY_FALSE(virt_preempt_key); >>>> static int pv_time_cpu_online(unsigned int cpu) >>>> { >>>> unsigned long flags; >>>> @@ -267,6 +268,18 @@ static int pv_time_cpu_down_prepare(unsigned int cpu) >>>> >>>> return 0; >>>> } >>>> + >>>> +bool notrace vcpu_is_preempted(int cpu) >>>> +{ >>>> + struct kvm_steal_time *src; >>>> + >>>> + if (!static_branch_unlikely(&virt_preempt_key)) >>>> + return false; >>>> + >>>> + src = &per_cpu(steal_time, cpu); >>>> + return !!(src->preempted & KVM_VCPU_PREEMPTED); >>>> +} >>>> +EXPORT_SYMBOL(vcpu_is_preempted); >>>> #endif >>>> >>>> static void pv_cpu_reboot(void *unused) >>>> @@ -308,6 +321,9 @@ int __init pv_time_init(void) >>>> pr_err("Failed to install cpu hotplug callbacks\n"); >>>> return r; >>>> } >>>> + >>>> + if (kvm_para_has_feature(KVM_FEATURE_PREEMPT)) >>>> + static_branch_enable(&virt_preempt_key); >>>> #endif >>>> >>>> static_call_update(pv_steal_clock, paravt_steal_clock); >>>> -- >>>> 2.39.3 >>>> >> >>