Re: [PATCH 5/5] iommufd: Flush CPU caches on DMA pages in non-coherent domains

Yan Zhao
2024-05-15T07:07:41Z

On Tue, May 14, 2024 at 12:11:19PM -0300, Jason Gunthorpe wrote:
> On Mon, May 13, 2024 at 03:43:45PM +0800, Yan Zhao wrote:
> > On Fri, May 10, 2024 at 10:29:28AM -0300, Jason Gunthorpe wrote:
> > > On Fri, May 10, 2024 at 04:03:04PM +0800, Yan Zhao wrote:
> > > > > > @@ -1358,10 +1377,17 @@ int iopt_area_fill_domain(struct iopt_area *area, struct iommu_domain *domain)
> > > > > > {
> > > > > > unsigned long done_end_index;
> > > > > > struct pfn_reader pfns;
> > > > > > + bool cache_flush_required;
> > > > > > int rc;
> > > > > >
> > > > > > lockdep_assert_held(&area->pages->mutex);
> > > > > >
> > > > > > + cache_flush_required = area->iopt->noncoherent_domain_cnt &&
> > > > > > + !area->pages->cache_flush_required;
> > > > > > +
> > > > > > + if (cache_flush_required)
> > > > > > + area->pages->cache_flush_required = true;
> > > > > > +
> > > > > > rc = pfn_reader_first(&pfns, area->pages, iopt_area_index(area),
> > > > > > iopt_area_last_index(area));
> > > > > > if (rc)
> > > > > > @@ -1369,6 +1395,9 @@ int iopt_area_fill_domain(struct iopt_area *area, struct iommu_domain *domain)
> > > > > >
> > > > > > while (!pfn_reader_done(&pfns)) {
> > > > > > done_end_index = pfns.batch_start_index;
> > > > > > + if (cache_flush_required)
> > > > > > + iopt_cache_flush_pfn_batch(&pfns.batch);
> > > > > > +
> > > > >
> > > > > This is a bit unfortunate, it means we are going to flush for every
> > > > > domain, even though it is not required. I don't see any easy way out
> > > > > of that :(
> > > > Yes. Do you think it's possible to add an op get_cache_coherency_enforced
> > > > to iommu_domain_ops?
> > >
> > > Do we need that? The hwpt already keeps track of that? the enforced could be
> > > copied into the area along side storage_domain
> > >
> > > Then I guess you could avoid flushing in the case the page came from
> > > the storage_domain...
> > >
> > > You'd want the storage_domain to preferentially point to any
> > > non-enforced domain.
> > >
> > > Is it worth it? How slow is this stuff?
> > Sorry, I might have misunderstood your intentions in my previous mail.
> > In iopt_area_fill_domain(), flushing CPU caches is only performed when
> > (1) noncoherent_domain_cnt is non-zero and
> > (2) area->pages->cache_flush_required is false.
> > area->pages->cache_flush_required is also set to true after the two are met, so
> > that the next flush to the same "area->pages" in filling phase will be skipped.
> >
> > In my last mail, I thought you wanted to flush for every domain even if
> > area->pages->cache_flush_required is true, because I thought that you were
> > worried about that checking area->pages->cache_flush_required might results in
> > some pages, which ought be flushed, not being flushed.
> > So, I was wondering if we could do the flush for every non-coherent domain by
> > checking whether domain enforces cache coherency.
> >
> > However, as you said, we can check hwpt instead if it's passed in
> > iopt_area_fill_domain().
> >
> > On the other side, after a second thought, looks it's still good to check
> > area->pages->cache_flush_required?
> > - "area" and "pages" are 1:1. In other words, there's no such a condition that
> > several "area"s are pointing to the same "pages".
> > Is this assumption right?
>
> copy can create new areas that point to shared pages. That is why
> there are two structs.
Oh, thanks for explanation and glad to learn that!!
Though in this case, new area is identical to the old area.
>
> > - Once area->pages->cache_flush_required is set to true, it means all pages
> > indicated by "area->pages" has been mapped into a non-coherent
> > domain
>
> Also not true, the multiple area's can take sub slices of the pages,
Ah, right, e.g. after iopt_area_split().

> so two hwpts' can be mapping disjoint sets of pages, and thus have
> disjoint cachability.
Indeed.
>
> So it has to be calculated on closer to a page by page basis (really a
> span by span basis) if flushing of that span is needed based on where
> the pages came from. Only pages that came from a hwpt that is
> non-coherent can skip the flushing.
Is area by area basis also good?
Isn't an area either not mapped to any domain or mapped into all domains?

But, yes, considering the limited number of non-coherent domains, it appears
more robust and clean to always flush for non-coherent domain in
iopt_area_fill_domain().
It eliminates the need to decide whether to retain the area flag during a split.