From: Sean Christopherson <seanjc@google.com>
To: Atish Kumar Patra <atishp@rivosinc.com>
Cc: Anup Patel <anup@brainfault.org>, Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Mayuresh Chitale <mchitale@ventanamicro.com>,
linux-riscv@lists.infradead.org,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
kvm-riscv@lists.infradead.org
Subject: Re: [PATCH v5 6/9] KVM: Add a helper function to check if a gpa is in writable memselot
Date: Fri, 5 Sep 2025 01:23:17 -0700 [thread overview]
Message-ID: <aLqd9bKB6ucarR3e@google.com> (raw)
In-Reply-To: <CAHBxVyHFkNtFdX-vciPvYnTOH=GXvHVW7hjFrLA4MFr9wqWVvQ@mail.gmail.com>
On Wed, Sep 03, 2025, Atish Kumar Patra wrote:
> On Fri, Aug 29, 2025 at 1:47 PM Sean Christopherson <seanjc@google.com> wrote:
> >
> > On Fri, Aug 29, 2025, Atish Patra wrote:
> > > +static inline bool kvm_is_gpa_in_writable_memslot(struct kvm *kvm, gpa_t gpa)
> > > +{
> > > + bool writable;
> > > + unsigned long hva = gfn_to_hva_prot(kvm, gpa_to_gfn(gpa), &writable);
> > > +
> > > + return !kvm_is_error_hva(hva) && writable;
> >
> > I don't hate this API, but I don't love it either. Because knowing that the
> > _memslot_ is writable doesn't mean all that much. E.g. in this usage:
> >
> > hva = kvm_vcpu_gfn_to_hva_prot(vcpu, shmem >> PAGE_SHIFT, &writable);
> > if (kvm_is_error_hva(hva) || !writable)
> > return SBI_ERR_INVALID_ADDRESS;
> >
> > ret = kvm_vcpu_write_guest(vcpu, shmem, &zero_sta, sizeof(zero_sta));
> > if (ret)
> > return SBI_ERR_FAILURE;
> >
> > the error code returned to the guest will be different if the memslot is read-only
> > versus if the VMA is read-only (or not even mapped!). Unless every read-only
> > memslot is explicitly communicated as such to the guest, I don't see how the guest
> > can *know* that a memslot is read-only, so returning INVALID_ADDRESS in that case
> > but not when the underlying VMA isn't writable seems odd.
> >
> > It's also entirely possible the memslot could be replaced with a read-only memslot
> > after the check, or vice versa, i.e. become writable after being rejected. Is it
> > *really* a problem to return FAILURE if the guest attempts to setup steal-time in
> > a read-only memslot? I.e. why not do this and call it good?
> >
>
> Reposting the response as gmail converted my previous response as
> html. Sorry for the spam.
>
> From a functionality pov, that should be fine. However, we have
> explicit error conditions for read only memory defined in the SBI STA
> specification[1].
> Technically, we will violate the spec if we return FAILURE instead of
> INVALID_ADDRESS for read only memslot.
But KVM is already violating the spec, as kvm_vcpu_write_guest() redoes the
memslot lookup and so could encounter a read-only memslot (if it races with
a memslot update), and because the underlying memory could be read-only even if
the memslot is writable.
Why not simply return SBI_ERR_INVALID_ADDRESS on kvm_vcpu_write_guest() failure?
The only downside of that is KVM will also return SBI_ERR_INVALID_ADDRESS if the
userspace mapping is completely missing, but AFAICT that doesn't seem to be an
outright spec violation.
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
WARNING: multiple messages have this Message-ID (diff)
From: Sean Christopherson <seanjc@google.com>
To: Atish Kumar Patra <atishp@rivosinc.com>
Cc: Anup Patel <anup@brainfault.org>, Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Mayuresh Chitale <mchitale@ventanamicro.com>,
linux-riscv@lists.infradead.org,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
kvm-riscv@lists.infradead.org
Subject: Re: [PATCH v5 6/9] KVM: Add a helper function to check if a gpa is in writable memselot
Date: Fri, 5 Sep 2025 01:23:17 -0700 [thread overview]
Message-ID: <aLqd9bKB6ucarR3e@google.com> (raw)
In-Reply-To: <CAHBxVyHFkNtFdX-vciPvYnTOH=GXvHVW7hjFrLA4MFr9wqWVvQ@mail.gmail.com>
On Wed, Sep 03, 2025, Atish Kumar Patra wrote:
> On Fri, Aug 29, 2025 at 1:47 PM Sean Christopherson <seanjc@google.com> wrote:
> >
> > On Fri, Aug 29, 2025, Atish Patra wrote:
> > > +static inline bool kvm_is_gpa_in_writable_memslot(struct kvm *kvm, gpa_t gpa)
> > > +{
> > > + bool writable;
> > > + unsigned long hva = gfn_to_hva_prot(kvm, gpa_to_gfn(gpa), &writable);
> > > +
> > > + return !kvm_is_error_hva(hva) && writable;
> >
> > I don't hate this API, but I don't love it either. Because knowing that the
> > _memslot_ is writable doesn't mean all that much. E.g. in this usage:
> >
> > hva = kvm_vcpu_gfn_to_hva_prot(vcpu, shmem >> PAGE_SHIFT, &writable);
> > if (kvm_is_error_hva(hva) || !writable)
> > return SBI_ERR_INVALID_ADDRESS;
> >
> > ret = kvm_vcpu_write_guest(vcpu, shmem, &zero_sta, sizeof(zero_sta));
> > if (ret)
> > return SBI_ERR_FAILURE;
> >
> > the error code returned to the guest will be different if the memslot is read-only
> > versus if the VMA is read-only (or not even mapped!). Unless every read-only
> > memslot is explicitly communicated as such to the guest, I don't see how the guest
> > can *know* that a memslot is read-only, so returning INVALID_ADDRESS in that case
> > but not when the underlying VMA isn't writable seems odd.
> >
> > It's also entirely possible the memslot could be replaced with a read-only memslot
> > after the check, or vice versa, i.e. become writable after being rejected. Is it
> > *really* a problem to return FAILURE if the guest attempts to setup steal-time in
> > a read-only memslot? I.e. why not do this and call it good?
> >
>
> Reposting the response as gmail converted my previous response as
> html. Sorry for the spam.
>
> From a functionality pov, that should be fine. However, we have
> explicit error conditions for read only memory defined in the SBI STA
> specification[1].
> Technically, we will violate the spec if we return FAILURE instead of
> INVALID_ADDRESS for read only memslot.
But KVM is already violating the spec, as kvm_vcpu_write_guest() redoes the
memslot lookup and so could encounter a read-only memslot (if it races with
a memslot update), and because the underlying memory could be read-only even if
the memslot is writable.
Why not simply return SBI_ERR_INVALID_ADDRESS on kvm_vcpu_write_guest() failure?
The only downside of that is KVM will also return SBI_ERR_INVALID_ADDRESS if the
userspace mapping is completely missing, but AFAICT that doesn't seem to be an
outright spec violation.
WARNING: multiple messages have this Message-ID (diff)
From: Sean Christopherson <seanjc@google.com>
To: Atish Kumar Patra <atishp@rivosinc.com>
Cc: Anup Patel <anup@brainfault.org>, Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Mayuresh Chitale <mchitale@ventanamicro.com>,
linux-riscv@lists.infradead.org,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
kvm-riscv@lists.infradead.org
Subject: Re: [PATCH v5 6/9] KVM: Add a helper function to check if a gpa is in writable memselot
Date: Fri, 5 Sep 2025 01:23:17 -0700 [thread overview]
Message-ID: <aLqd9bKB6ucarR3e@google.com> (raw)
In-Reply-To: <CAHBxVyHFkNtFdX-vciPvYnTOH=GXvHVW7hjFrLA4MFr9wqWVvQ@mail.gmail.com>
On Wed, Sep 03, 2025, Atish Kumar Patra wrote:
> On Fri, Aug 29, 2025 at 1:47 PM Sean Christopherson <seanjc@google.com> wrote:
> >
> > On Fri, Aug 29, 2025, Atish Patra wrote:
> > > +static inline bool kvm_is_gpa_in_writable_memslot(struct kvm *kvm, gpa_t gpa)
> > > +{
> > > + bool writable;
> > > + unsigned long hva = gfn_to_hva_prot(kvm, gpa_to_gfn(gpa), &writable);
> > > +
> > > + return !kvm_is_error_hva(hva) && writable;
> >
> > I don't hate this API, but I don't love it either. Because knowing that the
> > _memslot_ is writable doesn't mean all that much. E.g. in this usage:
> >
> > hva = kvm_vcpu_gfn_to_hva_prot(vcpu, shmem >> PAGE_SHIFT, &writable);
> > if (kvm_is_error_hva(hva) || !writable)
> > return SBI_ERR_INVALID_ADDRESS;
> >
> > ret = kvm_vcpu_write_guest(vcpu, shmem, &zero_sta, sizeof(zero_sta));
> > if (ret)
> > return SBI_ERR_FAILURE;
> >
> > the error code returned to the guest will be different if the memslot is read-only
> > versus if the VMA is read-only (or not even mapped!). Unless every read-only
> > memslot is explicitly communicated as such to the guest, I don't see how the guest
> > can *know* that a memslot is read-only, so returning INVALID_ADDRESS in that case
> > but not when the underlying VMA isn't writable seems odd.
> >
> > It's also entirely possible the memslot could be replaced with a read-only memslot
> > after the check, or vice versa, i.e. become writable after being rejected. Is it
> > *really* a problem to return FAILURE if the guest attempts to setup steal-time in
> > a read-only memslot? I.e. why not do this and call it good?
> >
>
> Reposting the response as gmail converted my previous response as
> html. Sorry for the spam.
>
> From a functionality pov, that should be fine. However, we have
> explicit error conditions for read only memory defined in the SBI STA
> specification[1].
> Technically, we will violate the spec if we return FAILURE instead of
> INVALID_ADDRESS for read only memslot.
But KVM is already violating the spec, as kvm_vcpu_write_guest() redoes the
memslot lookup and so could encounter a read-only memslot (if it races with
a memslot update), and because the underlying memory could be read-only even if
the memslot is writable.
Why not simply return SBI_ERR_INVALID_ADDRESS on kvm_vcpu_write_guest() failure?
The only downside of that is KVM will also return SBI_ERR_INVALID_ADDRESS if the
userspace mapping is completely missing, but AFAICT that doesn't seem to be an
outright spec violation.
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2025-09-05 9:54 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-29 14:41 [PATCH v5 0/9] Add SBI v3.0 PMU enhancements Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` [PATCH v5 1/9] drivers/perf: riscv: Add SBI v3.0 flag Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` [PATCH v5 2/9] drivers/perf: riscv: Add raw event v2 support Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` [PATCH v5 3/9] RISC-V: KVM: Add support for Raw event v2 Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` [PATCH v5 4/9] drivers/perf: riscv: Implement PMU event info function Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` [PATCH v5 5/9] drivers/perf: riscv: Export " Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` [PATCH v5 6/9] KVM: Add a helper function to check if a gpa is in writable memselot Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 20:47 ` Sean Christopherson
2025-08-29 20:47 ` Sean Christopherson
2025-08-29 20:47 ` Sean Christopherson
2025-09-04 6:24 ` Atish Kumar Patra
2025-09-04 6:24 ` Atish Kumar Patra
2025-09-04 6:24 ` Atish Kumar Patra
2025-09-05 8:23 ` Sean Christopherson [this message]
2025-09-05 8:23 ` Sean Christopherson
2025-09-05 8:23 ` Sean Christopherson
2025-09-05 19:34 ` Atish Kumar Patra
2025-09-05 19:34 ` Atish Kumar Patra
2025-09-05 19:34 ` Atish Kumar Patra
2025-08-29 14:41 ` [PATCH v5 7/9] RISC-V: KVM: Use the new gpa validate helper function Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` [PATCH v5 8/9] RISC-V: KVM: Implement get event info function Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` [PATCH v5 9/9] RISC-V: KVM: Upgrade the supported SBI version to 3.0 Atish Patra
2025-08-29 14:41 ` Atish Patra
2025-08-29 14:41 ` Atish Patra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aLqd9bKB6ucarR3e@google.com \
--to=seanjc@google.com \
--cc=anup@brainfault.org \
--cc=atishp@rivosinc.com \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=mark.rutland@arm.com \
--cc=mchitale@ventanamicro.com \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.