[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v2 36/39] xen/riscv: wake up a descheduled vCPU on a guest external interrupt


  • To: Oleksii Kurochko <oleksii.kurochko@xxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Mon, 21 Sep 2026 17:08:30 +0200
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:Content-Type:In-Reply-To:Autocrypt:From:Content-Language:References:Cc:To:Subject:User-Agent:MIME-Version:Date:Message-ID"
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Romain Caritey <Romain.Caritey@xxxxxxxxxxxxx>, Baptiste Le Duc <baptiste.le-duc@xxxxxxxxxx>, Zheng Zhang <zhangzheng@xxxxxxxxxxx>, Alistair Francis <alistair.francis@xxxxxxx>, Connor Davis <connojdavis@xxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Julien Grall <julien@xxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Mon, 21 Sep 2026 15:08:37 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 21.09.2026 16:01, Oleksii Kurochko wrote:
> On 9/18/26 2:52 PM, Jan Beulich wrote:
>> On 27.08.2026 17:21, Oleksii Kurochko wrote:
>>> --- a/xen/arch/riscv/domain.c
>>> +++ b/xen/arch/riscv/domain.c
>>> @@ -136,6 +136,8 @@ static void vcpu_csr_init(struct vcpu *v)
>>>           v->arch.hstateen0 = (hstateen0 & csr_masks.hstateen0) |
>>>                               csr_masks.ro_one.hstateen0;
>>>       }
>>> +
>>> +    v->arch.hie = MIP_SGEIP;
>>
>> Neither part of the rhs identifier has anything to do with the CSR
>> having its default value set here. That's perhaps again a piece of
>> RISC-V I'm missing, but I can't make sense of this.
> 
> All interrupt pending/enable CSRs share one bit layout (bit N 
> corresponds to interrupt cause N), which is why MIP_SGEIP happened to be 
> numerically right. I'll use BIT(IRQ_S_GEXT, UL) instead and add a 
> comment: hie.SGEIE is what allows the SGEI raised through hgeie to be 
> taken by Xen at all, while hie's VS-level bits alias the guest's vsie 
> and are left cleared. I will apply the following change:
> 
> -    v->arch.hie = MIP_SGEIP;
> +    /*
> +     * Enable SGEIs, so that a guest interrupt file marked in HGEIE while
> +     * the vCPU is descheduled can raise an interrupt to Xen.
> +     *
> +     * The VS-level bits of hie alias the guest's vsie, which is saved and
> +     * restored separately, so they are left clear here.
> +     */
> +    v->arch.hie = BIT(IRQ_S_GEXT, UL);

It's somewhat better this way, yes. And as said - in not looking great to
me (doc-wise) is likely an issue of mine, not of yours.

>>> --- a/xen/arch/riscv/imsic.c
>>> +++ b/xen/arch/riscv/imsic.c
>>> @@ -510,12 +510,31 @@ void cf_check imsic_ctxt_switch_from(struct vcpu *v)
>>>   
>>>       write_lock_irqsave(&imsic_state->vsfile_lock, flags);
>>>       imsic_state->vsfile_cpu = v->processor;
>>> +    /*
>>> +     * Start to observe the VS-file from HS-mode: while the vCPU isn't
>>> +     * running an interrupt pending in its VS-file is reported through 
>>> HGEIP
>>> +     * instead of being delivered to VS-mode, which lets Xen wake the vCPU 
>>> up.
>>> +     */
>>> +    csr_set(CSR_HGEIE, BIT(imsic_state->guest_file_id, UL));
>>>       write_unlock_irqrestore(&imsic_state->vsfile_lock, flags);
>>>   }
>>
>> It is suspicious for the HGEIE write to be the last step. How's this free
>> of a window where an interrupt is lost. (Sorry, likely another blind spot
>> of mine wrt RISC-V.)
> 
> There's no window: a guest interrupt file's hgeip bit is 
> level-sensitive, i.e. it reflects whether the file currently has a 
> pending-and-enabled interrupt (the MSI itself stays latched in the 
> file's eip[] until the guest claims it), and hip.SGEIP is simply (hgeip 
> & hgeie) != 0. So an MSI arriving after the vCPU stopped running but 
> before hgeie is set raises an SGEI as soon as the bit is set, taken once 
> interrupts are re-enabled. I'll extend the comment to say so:
> 
>      /*
>       * Start to observe the VS-file from HS-mode: while the vCPU isn't
>       * running an interrupt pending in its VS-file is reported through 
> HGEIP
>       * instead of being delivered to VS-mode, which lets Xen wake the 
> vCPU up.
>       *
>       * HGEIP is level-sensitive, reflecting the VS-file's current state, so
>       * an interrupt that became pending before this point raises an SGEI as
>       * soon as the bit is set in HGEIE; nothing is lost in between.
>       */
> 
> Does it make sense?

I think I get what you're trying to explain, but the term "level-sensitive"
here doesn't really help. I'm unconvinced you actually mean that, as it
requires pins / physical signals, which don't exist with MSI. It feels like
you may mean "sticky" instead.

>>>   void cf_check imsic_ctxt_switch_to(struct vcpu *v)
>>>   {
>>> -    /* Nothing to do */
>>> +    struct vimsic_state *imsic_state = v->arch.vimsic_state;
>>> +    unsigned long flags;
>>> +
>>> +    /* A s/w VS-file is never observed through HGEIP. */
>>> +    if ( !vcpu_guest_file_id(v) )
>>> +        return;
>>> +
>>> +    /*
>>> +     * The vCPU is about to run, so hstatus.VGEIN delivers the VS-file's
>>> +     * interrupts to it directly and there is nothing left for Xen to 
>>> observe.
>>> +     */
>>> +    read_lock_irqsave(&imsic_state->vsfile_lock, flags);
>>> +    csr_clear(CSR_HGEIE, BIT(imsic_state->guest_file_id, UL));
>>> +    read_unlock_irqrestore(&imsic_state->vsfile_lock, flags);
>>>   }
>>
>> How can this be a read-lock when you write a CSR?
> 
> But here is a protection of ->guest_file_id not of write to a CSR and we 
> want to not have a change of ->guest_file_id during an update of CSR_HGEIE.
> 
>> Or else - why is locking
>> here necessary in the firt place?
> 
> Strictly speaking no locking is needed with the current implementation: 
> HGEIE is local to this pCPU, and guest_file_id can't change 
> concurrently. It's updated either by this very pCPU ahead of switching 
> the vCPU in (imsic_migrate_vcpu() called from schedule(), 
> imsic_vsfile_attach() right after), or while the vCPU can't be 
> scheduled: sched_unit_migrate_finish() defers to unit_context_saved()
> while the unit is running, and sched_move_domain() pauses the domain.
> 
> (and the similar are true for imsic_ctxt_switch_from())
> 
> But IMO we should keep around ->vsfile_lock to not miss the case where 
> some case will update ->guest_file_id in parallel with 
> imsic_ctxt_switch_to().
> 
> Does it make to continue to have read_lock_irqsave(...->vsfile_lock, 
> ...) here just for potential future cases?

You get to judge. If the lock typically is uncontended, keeping things
as-is may indeed be fine. Introducing a bottleneck "just for potential
future cases" otoh wouldn't look overly nice to me.

Jan



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.