On Mon, Nov 23, 2026 at 11:00:41AM -0400, Jason Gunthorpe wrote:
> On Mon, Nov 23, 2026 at 11:05:23AM +0000, Catalin Marinas wrote:
> > > - /*
> > > -  * If set_memory_decrypted() fails then we don't know what state the
> > > -  * page is in, so we can't free it. Instead we leak it.
> > > -  * set_memory_decrypted() will already have WARNed.
> > > -  */
> > > - if (ret)
> > > + if (alloc_cc_shared_pages_node(node, gfp | gfp_flags_quirk,
> > > +                                PAGE_SIZE << order, &mem))
> > >           return NULL;
> > >  
> > > - return page;
> > > + return mem.page;
> > 
> > This part is mostly fine but I do wonder why not make this alloc
> > function the same as buddy alloc_pages_node() - return a struct page
> > pointer.
> 
> I'd argue we shouldn't even be returning struct page here, do many
> places even use the struct page? What for?

I just suggested it for similarity with alloc_pages_node(). It can be
made better indeed.

> Exposing struct page to places that just want a kernel addr means they
> have to call page_address and we are back to the highmem weirdness.

dma-buf system heap needs struct page for scatterlist, so we need the
virt_to_page() conversion if we return a VA.

> > >  static void its_free_pages(void *addr, unsigned int order)
> > >  {
> > > - /*
> > > -  * If the memory cannot be encrypted again then we must leak the pages.
> > > -  * set_memory_encrypted() will already have WARNed.
> > > -  */
> > > - if (set_memory_encrypted((unsigned long)addr, 1 << order))
> > > + struct cc_shared_layout layout;
> > > + struct cc_shared_pages mem;
> > > +
> > > + if (WARN_ON(cc_shared_calc_layout(PAGE_SIZE << order, &layout)))
> > >           return;
> > > - free_pages((unsigned long)addr, order);
> > > +
> > > + mem.page = virt_to_page(addr);
> > > + mem.shared_size = layout.shared_size;
> > > + free_cc_shared_pages(&mem);
> > >  }
> > 
> > Here it gets weird and I don't see why we can't have just a buddy-like
> > interface. The kernel-doc for alloc says that shared_size must be
> > retained for the corresponding free but without reading the source code,
> > you can't tell that cc_shared_calc_layout() gives you the same
> > shared_size populated by alloc. We have a similar pattern in dma-buf but
> > using the compound page order.
> 
> This makes sense, we should not be passing size to a free function,
> make a compount page/high order folio instead if the API is going to
> be struct page based.
> 
> > We can get rid of layout and mem structures (or at least not as
> > part of the API). Of course, this assumes that the shared granule size
> > remains constant for a guest's lifetime but I don't think that's a
> > problem.
> 
> It should be encoded in the struct page meta data, as the order.

I agree.

> > Was there a caller you had in mind that needs the common allocator to
> > share less than the whole buddy block?
> 
> If we want this it definitely needs to be a void * based API not
> struct page one though..

For whole-block sharing, either a VA or a folio works for me. If the
allocator takes an order, the requested size and the sharing granule are
both powers of two. Rounding up to the granule already gives a valid
buddy block size, so there’s no need for a separate shared_size.

-- 
Catalin

Reply via email to