On Fri, May 09, 2025 at 07:05:56PM +0900, Harry Yoo wrote:
- while (nr_total) {
nr_pages = min(nr_total, PAGE_SIZE / sizeof(data.pages[0]));
nr_populated = alloc_pages_bulk(GFP_KERNEL, nr_pages, data.pages);
if (nr_populated != nr_pages) {
free_pages_bulk(data.pages, nr_populated);
free_page((unsigned long)data.pages);
return -ENOMEM;
}
data.start = start;
ret = apply_to_page_range(&init_mm, start, nr_pages * PAGE_SIZE,
kasan_populate_vmalloc_pte, &data);
free_pages_bulk(data.pages, nr_pages);
A minor suggestion:
I think this free_pages_bulk() can be moved outside the loop (but with PAGE_SIZE / sizeof(data.pages[0]) instead of nr_pages),
Because we know the number of populated pages I think we could use it instead of maximal (PAGE_SIZE / sizeof(data.pages[0])).
because alloc_pages_bulk() simply skips allocating pages for any non-NULL entries.
If some pages in the array were not used, it doesn't have to be freed; on the next iteration of the loop alloc_pages_bulk() can skip allocating pages for the non-NULL entries.
Thanks for the suggestion! I will send an updated version.