From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80FF232573C for ; Thu, 18 Dec 2025 11:33:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766057642; cv=none; b=TVcOFN+cH5BdrE63zvgnyoe9nAKDwyHV6C343KMwoFcVpvG2PJV+Cb0Q7BTqvPnhF0biuoaefQrMXvHiBshnGqsT4KU/sQx3Y74XwIEC/kq5e0L8Cqxy+V969uHp/UqvpxOCeb0G6AxEvmWeNmUc3Ge2UMOXxrk7XjQLRTHMyow= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766057642; c=relaxed/simple; bh=5rVFXtAOQwHhZVAbJ5uzZCRSaAPQtT5kz6CUewVWilw=; h=From:Date:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=DkJEchx0wSrbBlvcygWngnk8VI5Agw7oi8BEgw76g4lcEvi0pyiA8LkM4G5cXp+zNEAAGCJNeqyn75mrw00AB5g72PNaYdNbK2Szay5m9JbY/EV6aETsVuUaQmyvzeD2IKsPw5fXGsuootG1lpeB7lP3ICBSk/57iILXkjpAJQU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=emmmG10y; arc=none smtp.client-ip=209.85.208.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="emmmG10y" Received: by mail-lj1-f171.google.com with SMTP id 38308e7fff4ca-37fdb95971eso3779241fa.1 for ; Thu, 18 Dec 2025 03:33:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766057637; x=1766662437; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:from:to:cc:subject:date:message-id:reply-to; bh=1x3E1VR0Gpbdp6wnRrDsTcGeQICBEEEiprP37wFkWvI=; b=emmmG10yZOT6sWCnQsJ7vJDdtSBEzze9231EP2Gr2VY7o96HM0q6tX3/IMLmoSzuMf Nj5QE8HxdThp6u9uAHeCh0vsWDy9wVzhekII6NjD8Fh1LIle0vXyw1LgoU1ZWPen2xRJ CZXndg+yZXeJ9dC8xTsQNCHKBTW82QmnaA0MDk+vlBQwQvRFh8ie9eMNFM2MAFA6tZ5M ifsk7Vsbt+lEfemYZhE4YPO2jEW+xpenGlq7JbD9jhB4s5UWUwzmaZc1fVj64gC28vxw 45DOA+8PvdEanZ2f7+NjycOJ8fW0UFYJXIH0UdlOHDiSfWTeJEqZgc58EHwLEDrFGYQ5 5MHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766057637; x=1766662437; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1x3E1VR0Gpbdp6wnRrDsTcGeQICBEEEiprP37wFkWvI=; b=ooW6lsbDg8ldklBCXKvq2wzRv20IIOoaC7SMOnfXumMzurEQBJhIuFjPXcNXyQGfk1 t7gXP/lsHz/Sy19IP3zyDlYL0qWEW+2b6jKoXgn76fHcynOh4+KVITyTq0FhgjkBCJWN r8qpqQkA0l/ysjIo0KCvBs5qxAavYdvWiYvYJ/acMtWHMhwC71dhzexQwui/rVgtyqja zf7TrgLnepwjtMxwvDEtAXqIUwLCnDQ7Zy9NHThzY4JH+EZ5DFoIbeACqmbAlGNzdGAD 06IXt5RA3YDnjwzU+5HzxSZpoMplwcgXwCEchfa9k9jia6FY9pMC2BlLDfLbTRdn26x/ njCQ== X-Forwarded-Encrypted: i=1; AJvYcCUnQTO9Rz3BumzP78CvDdc4jObtAd29TALK5GRdgb6fiY6EKBPAGa7Gzg5LwWRYmc4luUSfdciRejeBCWQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyFHhkjBQBusyByRjfYNYi0bflcai6Px3ykJv7i7dyZGlWEl9Zm HFGXdoUjlc2h0zWCI/eWbMVJq27ARE5EFD74130iSA5olX1+GxLdJXoz X-Gm-Gg: AY/fxX4nmEASdqKqbc3cYU9UCZA9022q3qVkC+J7l+pYEL4iJKwX8Tpee726rk6zVpe K4Zt80m2vGI9di65Q9GF1OrOQcPa5s5tyj7f99ZCkGDfR6SV7rspkeEe/gN8lSX/8WwzZj0+Sn0 Nqm6mFyoEcAQuejXlmAMOtxgAKl4qQg3gREzniam97QFb/Q2FXhwfpN7yaDvvAcm2NOPLodALpD 4a2boNLQpja+Sd3RRpbbAQJmg0ysbScjuJ0/iFwPn1g7+iWce/9mtRkZ8Qh+L2Rna8uwfDLfEqj vEiCoL+OOYrWtddR1oN16862R3M5VSfLvtLwY1zma3Rs5tNFF3MbwvjIEp98gEHPynwy3tcnMpl u8+ruYxgoSyNq/oOBHe7gxtbhvnQ8rFGRbgJ7INnKAei3tlN21iEyMtA45wocZH4= X-Google-Smtp-Source: AGHT+IFoD+L6d3uJMTpeMtYDLCM7M3rSnWjOpPMSstf8SjInvaVPiSlxyhM8o8R8yZAtTFMR0ZJ7ew== X-Received: by 2002:a05:651c:418e:b0:37a:4918:91c3 with SMTP id 38308e7fff4ca-37fd0792d91mr56475861fa.14.1766057637217; Thu, 18 Dec 2025 03:33:57 -0800 (PST) Received: from milan ([2001:9b1:d5a0:a500::24b]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-38113711213sm9602351fa.34.2025.12.18.03.33.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Dec 2025 03:33:56 -0800 (PST) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Thu, 18 Dec 2025 12:33:55 +0100 To: Ryan Roberts Cc: Uladzislau Rezki , linux-mm@kvack.org, Andrew Morton , Vishal Moola , Dev Jain , Baoquan He , LKML Subject: Re: [PATCH 2/2] mm/vmalloc: Add attempt_larger_order_alloc parameter Message-ID: References: <20251216211921.1401147-1-urezki@gmail.com> <20251216211921.1401147-2-urezki@gmail.com> <6ca6e796-cded-4221-b1f8-92176a80513e@arm.com> <0f69442d-b44e-4b30-b11e-793511db9f1e@arm.com> <4a66f13d-318b-4cdb-b168-0c993ff8a309@arm.com> <37efa0a9-99bc-4099-ba64-2474f3f09aa2@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <37efa0a9-99bc-4099-ba64-2474f3f09aa2@arm.com> On Thu, Dec 18, 2025 at 11:12:15AM +0000, Ryan Roberts wrote: > On 17/12/2025 19:22, Uladzislau Rezki wrote: > > On Wed, Dec 17, 2025 at 05:01:19PM +0000, Ryan Roberts wrote: > >> On 17/12/2025 15:20, Ryan Roberts wrote: > >>> On 17/12/2025 12:02, Uladzislau Rezki wrote: > >>>>> On 16/12/2025 21:19, Uladzislau Rezki (Sony) wrote: > >>>>>> Introduce a module parameter to enable or disable the large-order > >>>>>> allocation path in vmalloc. High-order allocations are disabled by > >>>>>> default so far, but users may explicitly enable them at runtime if > >>>>>> desired. > >>>>>> > >>>>>> High-order pages allocated for vmalloc are immediately split into > >>>>>> order-0 pages and later freed as order-0, which means they do not > >>>>>> feed the per-CPU page caches. As a result, high-order attempts tend > >>>>>> to bypass the PCP fastpath and fall back to the buddy allocator that > >>>>>> can affect performance. > >>>>>> > >>>>>> However, when the PCP caches are empty, high-order allocations may > >>>>>> show better performance characteristics especially for larger > >>>>>> allocation requests. > >>>>> > >>>>> I wonder if a better solution would be "allocate order-0 if available in pcp, > >>>>> else try large order, else fallback to order-0" Could that provide the best of > >>>>> all worlds without needing a configuration knob? > >>>>> > >>>> I am not sure, to me it looks like a bit odd. > >>> > >>> Perhaps it would feel better if it was generalized to "first try allocation from > >>> PCP list, highest to lowest order, then try allocation from the buddy, highest > >>> to lowest order"? > >>> > >>>> Ideally it would be > >>>> good just free it as high-order page and not order-0 peaces. > >>> > >>> Yeah perhaps that's better. How about something like this (very lightly tested > >>> and no performance results yet): > >>> > >>> (And I should admit I'm not 100% sure it is safe to call free_frozen_pages() > >>> with a contiguous run of order-0 pages, but I'm not seeing any warnings or > >>> memory leaks when running mm selftests...) > >>> > >>> ---8<--- > >>> commit caa3e5eb5bfade81a32fa62d1a8924df1eb0f619 > >>> Author: Ryan Roberts > >>> Date: Wed Dec 17 15:11:08 2025 +0000 > >>> > >>> WIP > >>> > >>> Signed-off-by: Ryan Roberts > >>> > >>> diff --git a/include/linux/gfp.h b/include/linux/gfp.h > >>> index b155929af5b1..d25f5b867e6b 100644 > >>> --- a/include/linux/gfp.h > >>> +++ b/include/linux/gfp.h > >>> @@ -383,6 +383,8 @@ extern void __free_pages(struct page *page, unsigned int order); > >>> extern void free_pages_nolock(struct page *page, unsigned int order); > >>> extern void free_pages(unsigned long addr, unsigned int order); > >>> > >>> +void free_pages_bulk(struct page *page, int nr_pages); > >>> + > >>> #define __free_page(page) __free_pages((page), 0) > >>> #define free_page(addr) free_pages((addr), 0) > >>> > >>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c > >>> index 822e05f1a964..5f11224cf353 100644 > >>> --- a/mm/page_alloc.c > >>> +++ b/mm/page_alloc.c > >>> @@ -5304,6 +5304,48 @@ static void ___free_pages(struct page *page, unsigned int > >>> order, > >>> } > >>> } > >>> > >>> +static void free_frozen_pages_bulk(struct page *page, int nr_pages) > >>> +{ > >>> + while (nr_pages) { > >>> + unsigned int fit_order, align_order, order; > >>> + unsigned long pfn; > >>> + > >>> + pfn = page_to_pfn(page); > >>> + fit_order = ilog2(nr_pages); > >>> + align_order = pfn ? __ffs(pfn) : fit_order; > >>> + order = min3(fit_order, align_order, MAX_PAGE_ORDER); > >>> + > >>> + free_frozen_pages(page, order); > >>> + > >>> + page += 1U << order; > >>> + nr_pages -= 1U << order; > >>> + } > >>> +} > >>> + > >>> +void free_pages_bulk(struct page *page, int nr_pages) > >>> +{ > >>> + struct page *start = NULL; > >>> + bool can_free; > >>> + int i; > >>> + > >>> + for (i = 0; i < nr_pages; i++, page++) { > >>> + VM_BUG_ON_PAGE(PageHead(page), page); > >>> + VM_BUG_ON_PAGE(PageTail(page), page); > >>> + > >>> + can_free = put_page_testzero(page); > >>> + > >>> + if (!can_free && start) { > >>> + free_frozen_pages_bulk(start, page - start); > >>> + start = NULL; > >>> + } else if (can_free && !start) { > >>> + start = page; > >>> + } > >>> + } > >>> + > >>> + if (start) > >>> + free_frozen_pages_bulk(start, page - start); > >>> +} > >>> + > >>> /** > >>> * __free_pages - Free pages allocated with alloc_pages(). > >>> * @page: The page pointer returned from alloc_pages(). > >>> diff --git a/mm/vmalloc.c b/mm/vmalloc.c > >>> index ecbac900c35f..8f782bac1ece 100644 > >>> --- a/mm/vmalloc.c > >>> +++ b/mm/vmalloc.c > >>> @@ -3429,7 +3429,8 @@ void vfree_atomic(const void *addr) > >>> void vfree(const void *addr) > >>> { > >>> struct vm_struct *vm; > >>> - int i; > >>> + struct page *start; > >>> + int i, nr; > >>> > >>> if (unlikely(in_interrupt())) { > >>> vfree_atomic(addr); > >>> @@ -3455,17 +3456,26 @@ void vfree(const void *addr) > >>> /* All pages of vm should be charged to same memcg, so use first one. */ > >>> if (vm->nr_pages && !(vm->flags & VM_MAP_PUT_PAGES)) > >>> mod_memcg_page_state(vm->pages[0], MEMCG_VMALLOC, -vm->nr_pages); > >>> - for (i = 0; i < vm->nr_pages; i++) { > >>> + > >>> + start = vm->pages[0]; > >>> + BUG_ON(!start); > >>> + nr = 1; > >>> + for (i = 1; 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 > >>> - */ > >>> - __free_page(page); > >>> - cond_resched(); > >>> + > >>> + if (start + nr != page) { > >>> + free_pages_bulk(start, nr); > >>> + start = page; > >>> + nr = 1; > >>> + cond_resched(); > >>> + } else { > >>> + nr++; > >>> + } > >>> } > >>> + free_pages_bulk(start, nr); > >>> + > >>> if (!(vm->flags & VM_MAP_PUT_PAGES)) > >>> atomic_long_sub(vm->nr_pages, &nr_vmalloc_pages); > >>> kvfree(vm->pages); > >>> ---8<--- > >> > >> I tested this on a performance monitoring system and see a huge improvement for > >> the test_vmalloc tests. > >> > >> Both columns are compared to v6.18. 6-19-0-rc1 has Vishal's change to allocate > >> large orders, which I previously reported the regressions for. vfree-high-order > >> adds the above patch to free contiguous order-0 pages in bulk. > >> > >> (R)/(I) means statistically significant regression/improvement. Results are > >> normalized so that less than zero is regression and greater than zero is > >> improvement. > >> > >> +-----------------+----------------------------------------------------------+--------------+------------------+ > >> | Benchmark | Result Class | 6-19-0-rc1 | vfree-high-order | > >> +=================+==========================================================+==============+==================+ > >> | micromm/vmalloc | fix_align_alloc_test: p:1, h:0, l:500000 (usec) | (R) -40.69% | (I) 3.98% | > >> | | fix_size_alloc_test: p:1, h:0, l:500000 (usec) | 0.10% | -1.47% | > >> | | fix_size_alloc_test: p:4, h:0, l:500000 (usec) | (R) -22.74% | (I) 11.57% | > >> | | fix_size_alloc_test: p:16, h:0, l:500000 (usec) | (R) -23.63% | (I) 47.42% | > >> | | fix_size_alloc_test: p:16, h:1, l:500000 (usec) | -1.58% | (I) 106.01% | > >> | | fix_size_alloc_test: p:64, h:0, l:100000 (usec) | (R) -24.39% | (I) 99.12% | > >> | | fix_size_alloc_test: p:64, h:1, l:100000 (usec) | (I) 2.34% | (I) 196.87% | > >> | | fix_size_alloc_test: p:256, h:0, l:100000 (usec) | (R) -23.29% | (I) 125.42% | > >> | | fix_size_alloc_test: p:256, h:1, l:100000 (usec) | (I) 3.74% | (I) 238.59% | > >> | | fix_size_alloc_test: p:512, h:0, l:100000 (usec) | (R) -23.80% | (I) 132.38% | > >> | | fix_size_alloc_test: p:512, h:1, l:100000 (usec) | (R) -2.84% | (I) 514.75% | > >> | | full_fit_alloc_test: p:1, h:0, l:500000 (usec) | 2.74% | 0.33% | > >> | | kvfree_rcu_1_arg_vmalloc_test: p:1, h:0, l:500000 (usec) | 0.58% | 1.36% | > >> | | kvfree_rcu_2_arg_vmalloc_test: p:1, h:0, l:500000 (usec) | -0.66% | 1.48% | > >> | | long_busy_list_alloc_test: p:1, h:0, l:500000 (usec) | (R) -25.24% | (I) 77.95% | > >> | | pcpu_alloc_test: p:1, h:0, l:500000 (usec) | -0.58% | 0.60% | > >> | | random_size_align_alloc_test: p:1, h:0, l:500000 (usec) | (R) -45.75% | (I) 8.51% | > >> | | random_size_alloc_test: p:1, h:0, l:500000 (usec) | (R) -28.16% | (I) 65.34% | > >> | | vm_map_ram_test: p:1, h:0, l:500000 (usec) | -0.54% | -0.33% | > >> +-----------------+----------------------------------------------------------+--------------+------------------+ > >> > >> What do you think? > >> > > You were first :) > > > > Some figures from me: > > > > # Default(3 pages) > > What is Default? I'm guessing it's the state prior to Vishal's patch? > Right. > > fix_size_alloc_test passed: 1 failed: 0 xfailed: 0 repeat: 1 loops: 1000000 avg: 541868 usec > > fix_size_alloc_test passed: 1 failed: 0 xfailed: 0 repeat: 1 loops: 1000000 avg: 542515 usec > > fix_size_alloc_test passed: 1 failed: 0 xfailed: 0 repeat: 1 loops: 1000000 avg: 541561 usec > > fix_size_alloc_test passed: 1 failed: 0 xfailed: 0 repeat: 1 loops: 1000000 avg: 542951 usec > > > > # Patch(3 pages) > > What is Patch? I'm guessing state after applying both Vishal's and my patches? > Right. -- Uladzislau Rezki