Catalin Marinas <[email protected]> writes: > Hi Aneesh, > > I'm replying on this patch rather than on the one introducing the API. I > think it's more obvious here why I think we can make things simpler. > > On Mon, Nov 16, 2026 at 02:41:27PM +0530, Aneesh Kumar K.V (Arm) wrote: >> static struct page *its_alloc_pages_node(int node, gfp_t gfp, >> unsigned int order) >> { >> - struct page *page; >> - int ret = 0; >> + struct cc_shared_pages mem; >> >> if (node == NUMA_NO_NODE) >> node = numa_mem_id(); >> >> - page = alloc_pages_node(node, gfp | gfp_flags_quirk, order); >> - >> - if (!page) >> - return NULL; >> - >> - ret = set_memory_decrypted((unsigned long)page_address(page), >> - 1 << order); >> - /* >> - * 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. > >> 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. > > What's wrong with a simpler API: > > struct page *alloc_cc_shared_pages_node(int nid, gfp_t gfp, unsigned int > order); > struct page *alloc_cc_shared_pages(gfp_t gfp, unsigned int order); > void free_cc_shared_pages(struct page *page, unsigned int order); > > Both alloc and free take the original request order and perform the > round up internally without exposing the details to the caller. Alloc > shares the whole resulting buddy block and free makes the same block > private before releasing it. We can document that free takes the > original requested order and releases the whole allocation. Partial > frees aren't supported. > > 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. > >> @@ -278,7 +267,8 @@ static void *itt_alloc_pool(int node, int size) >> if (!page) >> break; >> >> - gen_pool_add(itt_pool, (unsigned long)page_address(page), >> PAGE_SIZE, node); >> + gen_pool_add(itt_pool, (unsigned long)page_address(page), >> + cc_shared_granule_size(), node); > > Here we can introduce: > > unsigned int cc_shared_page_order(unsigned int order); > > and just use (PAGE_SIZE << cc_shared_page_order(0)) for gen_pool_add(). > > Was there a caller you had in mind that needs the common allocator to > share less than the whole buddy block? >
No, I don't have a caller that needs to share less than the whole buddy block. Taking Jason's feedback into account, we could have the allocator request compound pages and recover the actual allocation order with compound_order(). That would avoid passing an order to free: void *alloc_cc_shared_node(int nid, gfp_t gfp, unsigned int order); void *alloc_cc_shared(gfp_t gfp, unsigned int order); void free_cc_shared(void *addr); Alloc would round up internally and share the entire buddy block; free would recover its order, make the whole block private, and release it. As you noted, dma-buf would need virt_to_page() to obtain the pages for its scatterlist. -aneesh
