Re: [PATCH v3 2/3] vmalloc: Optimize vfree
From: Usama Anjum
Date: Wed Mar 25 2026 - 10:47:17 EST
<snip>
>> +void free_pages_bulk(struct page **page_array, unsigned long nr_pages)
>> +{
>> + unsigned long start_pfn = 0, pfn;
>> + unsigned long i, nr_contig = 0;
>> +
>> + for (i = 0; i < nr_pages; i++) {
>> + pfn = page_to_pfn(page_array[i]);
>> + if (!nr_contig) {
>> + start_pfn = pfn;
>> + nr_contig = 1;
>> + } else if (start_pfn + nr_contig != pfn) {
>> + __free_contig_range(start_pfn, nr_contig);
>> + start_pfn = pfn;
>> + nr_contig = 1;
>> + cond_resched();
>> + } else {
>> + nr_contig++;
>> + }
>> + }
>> + if (nr_contig)
>> + __free_contig_range(start_pfn, nr_contig);
>> +}
>
> free_pages_bulk() assumes pages in page_array are sorted in PFN ascending order.
> I think it is worth documenting it, since without sorting, it can degrade
> back to the original implementation.
I'll add the kerneldoc comment.
>
>> +
>> /*
>> * This is the 'heart' of the zoned buddy allocator.
>> */
>> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
>> index c607307c657a6..e9b3d6451e48b 100644
>> --- a/mm/vmalloc.c
>> +++ b/mm/vmalloc.c
>> @@ -3459,19 +3459,13 @@ void vfree(const void *addr)
>>
>> if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS))
>> vm_reset_perms(vm);
>> - for (i = 0; i < vm->nr_pages; i++) {
>> - struct page *page = vm->pages[i];
>>
>> - BUG_ON(!page);
>> - /*
>> - * High-order allocs for huge vmallocs are split, so
>> - * can be freed as an array of order-0 allocations
>> - */
>> - if (!(vm->flags & VM_MAP_PUT_PAGES))
>> - mod_lruvec_page_state(page, NR_VMALLOC, -1);
>> - __free_page(page);
>> - cond_resched();
>> + if (!(vm->flags & VM_MAP_PUT_PAGES)) {
>> + for (i = 0; i < vm->nr_pages; i++)
>> + mod_lruvec_page_state(vm->pages[i], NR_VMALLOC, -1);
>> }
>> + free_pages_bulk(vm->pages, vm->nr_pages);
>> +
>
> stats is updated before any page is freed. It is better to mention
> it in the commit message.
I'll mention it.
>
>> kvfree(vm->pages);
>> kfree(vm);
>> }
>> --
>> 2.47.3
>
> Otherwise, LGTM.
>
> Acked-by: Zi Yan <ziy@xxxxxxxxxx>
>
> Best Regards,
> Yan, Zi
>
Thanks,
Usama