Re: [RFC PATCH v2 38/51] KVM: guest_memfd: Split allocator pages for guest_memfd use

From: Yan Zhao
Date: Tue May 27 2025 - 00:41:26 EST


> > +static int kvm_gmem_restructure_folios_in_range(struct inode *inode,
> > + pgoff_t start, size_t nr_pages,
> > + bool is_split_operation)
> > +{
> > + size_t to_nr_pages;
> > + pgoff_t index;
> > + pgoff_t end;
> > + void *priv;
> > + int ret;
> > +
> > + if (!kvm_gmem_has_custom_allocator(inode))
> > + return 0;
> > +
> > + end = start + nr_pages;
> > +
> > + /* Round to allocator page size, to check all (huge) pages in range. */
> > + priv = kvm_gmem_allocator_private(inode);
> > + to_nr_pages = kvm_gmem_allocator_ops(inode)->nr_pages_in_folio(priv);
> > +
> > + start = round_down(start, to_nr_pages);
> > + end = round_up(end, to_nr_pages);
> > +
> > + for (index = start; index < end; index += to_nr_pages) {
> > + struct folio *f;
> > +
> > + f = filemap_get_folio(inode->i_mapping, index);
> > + if (IS_ERR(f))
> > + continue;
> > +
> > + /* Leave just filemap's refcounts on the folio. */
> > + folio_put(f);
> > +
> > + if (is_split_operation)
> > + ret = kvm_gmem_split_folio_in_filemap(inode, f);
> The split operation is performed after kvm_gmem_unmap_private() within
> kvm_gmem_convert_should_proceed(), right?
>
> So, it seems that that it's not necessary for TDX to avoid holding private page
> references, as TDX must have released the page refs after
> kvm_gmem_unmap_private() (except when there's TDX module or KVM bug).
Oops. Please ignore this one.
The unmap does not necessarily cover the entire folio range, so split still
requires TDX not to hold ref count.