Re: [PATCH v4 5/5] iommu/hyperv: Add page-selective IOTLB flush support

From: Yu Zhang

Date: Thu Sep 03 2026 - 04:20:15 EST


On Fri, Aug 28, 2026 at 05:22:48PM +0000, Michael Kelley wrote:
> From: Yu Zhang <zhangyu1@xxxxxxxxxxxxxxxxxxx> Sent: Friday, August 21, 2026 6:27 AM
> >
> > Add page-selective IOTLB flush using HVCALL_FLUSH_DEVICE_DOMAIN_LIST.
> > This hypercall accepts a list of (page_number, page_mask_shift) entries,
> > enabling finer-grained IOTLB invalidation compared to the domain-wide
> > HVCALL_FLUSH_DEVICE_DOMAIN used by hv_iommu_flush_iotlb_all().
> >
> > hv_iommu_calc_flush_range() computes the smallest power-of-two aligned
> > range that covers the target IOVA region, producing a single flush
> > descriptor. This may over-flush when the range is not naturally aligned,
> > matching the approach used by Intel VT-d PSI. If the page-selective
> > flush fails, the code falls back to a full domain flush.
> >
> > Signed-off-by: Easwar Hariharan <easwar.hariharan@xxxxxxxxxxxxxxxxxxx>
> > Signed-off-by: Yu Zhang <zhangyu1@xxxxxxxxxxxxxxxxxxx>
> > Reviewed-by: Jacob Pan <jacob.pan@xxxxxxxxxxxxxxxxxxx>
> > ---
> > drivers/iommu/hyperv/hv-iommu-guest.c | 76 ++++++++++++++++++++++++++-
> > include/hyperv/hvgdk_mini.h | 1 +
> > include/hyperv/hvhdk_mini.h | 18 +++++++
> > 3 files changed, 94 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/iommu/hyperv/hv-iommu-guest.c b/drivers/iommu/hyperv/hv-iommu-guest.c
> > index 2169333020f4..3e38046bd998 100644
> > --- a/drivers/iommu/hyperv/hv-iommu-guest.c
> > +++ b/drivers/iommu/hyperv/hv-iommu-guest.c
> > @@ -396,11 +396,84 @@ static void hv_iommu_flush_iotlb_all(struct iommu_domain *domain)
> > hv_flush_device_domain(to_hv_iommu_domain(domain));
> > }
> >
> > +/*
> > + * Calculate the minimal power-of-two aligned range that covers [start, end]
> > + * (end is inclusive). Returns a single (page_number, page_mask_shift)
> > + * descriptor that may over-flush when the range is not naturally aligned.
> > + */
> > +static void
> > +hv_iommu_calc_flush_range(unsigned long start, unsigned long end,
> > + union hv_iommu_flush_va *va)
> > +{
> > + unsigned int sz_lg2;
> > +
> > + sz_lg2 = fls_long(start ^ end);
> > + if (sz_lg2 < HV_HYP_PAGE_SHIFT)
> > + sz_lg2 = HV_HYP_PAGE_SHIFT;
>
> Use the max() function here instead of open coding it?
>
> sz_lg2 = max(fls_long(start ^ end), HV_HYP_PAGE_SHIFT);
>
> But maybe that's just more obscure. Your call.
>

The  max()  form looks clear to me. Thanks!

> > +
> > + /*
> > + * A valid IOVA range shall not span bit 63. Use the maximum mask
> > + * so the host can safely perform a full flush.
> > + */
> > + if (WARN_ON_ONCE(sz_lg2 >= BITS_PER_LONG)) {
> > + va->as_uint64 = 0;
> > + va->page_mask_shift =
> > + BITS_PER_LONG - HV_HYP_PAGE_SHIFT;
>
> This case is handled slightly inconsistently with the main case.
> Here the entire 64 bits are cleared, which implicitly sets the
> page_number field to zero and also clears the reserved field.
> The main case sets the page_number but doesn't clear the
> reserved field. The memset() in hv_flush_device_domain_list()
> assumes that the reserved field might not be zeroed. I'd just
> suggest making everything consistent and not doing any
> duplicate zero'ing.
>
Agreed. The caller already clears the fixed header and the range entry.
I'd like to remove the duplicate as_uint64 assignment and explicitly set
page_number to 0 here(for the overflow case), leaving the reserved bits
consistently initialized by the caller.

> > + return;
> > + }
> > +
> > + va->page_number =
> > + (start & GENMASK(BITS_PER_LONG - 1, sz_lg2)) >>
> > + HV_HYP_PAGE_SHIFT;
> > + va->page_mask_shift = sz_lg2 - HV_HYP_PAGE_SHIFT;
> > +}
> > +
> > +static void hv_flush_device_domain_list(struct hv_iommu_domain *hv_domain,
> > + struct iommu_iotlb_gather *iotlb_gather)
> > +{
> > + u64 status;
> > + unsigned long flags;
> > + struct hv_input_flush_device_domain_list *input;
> > +
> > + local_irq_save(flags);
> > +
> > + input = *this_cpu_ptr(hyperv_pcpu_input_arg);
> > + /* Clear the fixed header and the single range entry. */
> > + memset(input, 0, struct_size(input, iova_list, 1));
> > +
> > + input->device_domain = hv_domain->device_domain;
> > + input->flags |= HV_FLUSH_DEVICE_DOMAIN_LIST_IOMMU_FORMAT;
> > + hv_iommu_calc_flush_range(iotlb_gather->start, iotlb_gather->end,
> > + &input->iova_list[0]);
> > +
> > + status = hv_do_rep_hypercall(HVCALL_FLUSH_DEVICE_DOMAIN_LIST,
> > + 1, 0, input, NULL);
> > +
> > + if (WARN_ON_ONCE(!hv_result_success(status))) {
>
> I'd suggest having a message here with the status. The list flush really
> should not be failing, so outputting the status might provide a clue
> as to what's going on if it does fail.
>

Sure. Thanks!

B.R.
Yu