From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sender-pp-o91.zoho.in (sender-pp-o91.zoho.in [103.117.158.91]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AA2B3845BD for ; Tue, 3 Mar 2026 11:13:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=103.117.158.91 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772536382; cv=pass; b=Luy9uMi1uu+8z7vLIJa0Jsa50wv7nF1w8RKM4R1D1JHgWShK49OAqR2FR0wcEgK+8OSgRnJrqutNw5ste8ZoTnJ7OaXRJrOBDVRE+siXFR6BpF60/JDw3j1MRs5mzyPK/iIEzKQIEMtnjLEQFvVkhvvB0WbZirs1qVz6nUVYlY4= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772536382; c=relaxed/simple; bh=MUeydrsfmel5MsyJov10vzvKJ6YObuYcQOUy0u0F9TA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=chkIFRqbcTBKWAvjh/zjWLg9gROWj3y0lx3hwU9jLLDMZtct79qx5FPWIdPP2un7nV5iiJG1P4Nm69uGLumw0VDFv5XxGGG5dBWYuYcoIHJ2vJ5YEnr71XrZ6haj5pPxb9dUVfiW/U3sWfVa+1xOfnd8vAeme1EpXpcNHyGEQGA= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=zohomail.in; spf=pass smtp.mailfrom=zohomail.in; dkim=pass (1024-bit key) header.d=zohomail.in header.i=shivamkalra98@zohomail.in header.b=ykPP5llD; arc=pass smtp.client-ip=103.117.158.91 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=zohomail.in Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zohomail.in Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=zohomail.in header.i=shivamkalra98@zohomail.in header.b="ykPP5llD" ARC-Seal: i=1; a=rsa-sha256; t=1772536353; cv=none; d=zohomail.in; s=zohoarc; b=Xk8XOS4NcXpcY8i28EJdCkkRNf9BHS0cGIW9LBUun6Z+/BGaWGSJkG76BgU9sxAUodKdzqfXC1L26l7hNatTm4gBwX2OTnKZBWyNufTbkmu8hMbIndMOwhtwnbD70u8zQQhWTPUnoO0GVpFOeo0OXkfj7O+rxfoD6Y9ldEMLjFE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.in; s=zohoarc; t=1772536353; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=KWwpTG4ziQIkxPyMAvmgDBqeguwQv3n8C1cOCmmRHTY=; b=Aa0TfMVFfmq252EoPEIXCBl6vdBZH+RzAYkX7uMUMCfwsTkKpwspY5d0Z/jV83SsxzHr+ILg5cZ1ZgrcFvGuVy7/XfUmTBpPxCJBJfe/8jxKkG8+Xeryc4XisE5CXXWeha9FYYYrFIQH685jj7q0GGT+HPAcJWjMiR3aIRaT6YY= ARC-Authentication-Results: i=1; mx.zohomail.in; dkim=pass header.i=zohomail.in; spf=pass smtp.mailfrom=shivamkalra98@zohomail.in; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1772536353; s=zoho; d=zohomail.in; i=shivamkalra98@zohomail.in; h=Message-ID:Date:Date:MIME-Version:Subject:Subject:To:To:Cc:Cc:References:From:From:In-Reply-To:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To; bh=KWwpTG4ziQIkxPyMAvmgDBqeguwQv3n8C1cOCmmRHTY=; b=ykPP5llD9FiY+Gv8m7kQnW77XE/UaukiN67+3es5091EmZzPczIY5NKD1ByiZmFF C361lxIw8uppOygNK0V7GUf2cRt7vbIxlp3EXObSM7obEE8L9/q3XpSZSeE9h9Ndc8W tQEZVYYHPIdLAsMVwBdolRSivGrx1CojzLzeGrmk= Received: by mx.zoho.in with SMTPS id 1772536350446260.9400578620058; Tue, 3 Mar 2026 16:42:30 +0530 (IST) Message-ID: <57b0ef75-59c2-44b4-90fc-d1b50cc1e9ed@zohomail.in> Date: Tue, 3 Mar 2026 16:42:29 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] mm/vmalloc: free unused pages on vrealloc() shrink To: Alice Ryhl Cc: Andrew Morton , Uladzislau Rezki , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20260302-vmalloc-shrink-v1-0-46deff465b7e@zohomail.in> <20260302-vmalloc-shrink-v1-2-46deff465b7e@zohomail.in> Content-Language: en-US From: Shivam Kalra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-ZohoMailClient: External On 03/03/26 14:40, Alice Ryhl wrote: > On Mon, Mar 2, 2026 at 3:03 PM Shivam Kalra via B4 Relay > wrote: >> >> From: Shivam Kalra >> >> When vrealloc() shrinks an allocation and the new size crosses a page >> boundary, unmap and free the tail pages that are no longer needed. This >> reclaims physical memory that was previously wasted for the lifetime >> of the allocation. >> >> The heuristic is simple: always free when at least one full page becomes >> unused. Huge page allocations (page_order > 0) are skipped, as partial >> freeing would require splitting. >> >> The virtual address reservation (vm->size / vmap_area) is intentionally >> kept unchanged, preserving the address for potential future grow-in-place >> support. >> >> Fix the grow-in-place check to compare against vm->nr_pages rather than >> get_vm_area_size(), since the latter reflects the virtual reservation >> which does not shrink. Without this fix, a grow after shrink would >> access freed pages. >> >> Signed-off-by: Shivam Kalra >> --- >> mm/vmalloc.c | 25 ++++++++++++++++++++----- >> 1 file changed, 20 insertions(+), 5 deletions(-) >> >> diff --git a/mm/vmalloc.c b/mm/vmalloc.c >> index 54e76a47e995..7a4c59422638 100644 >> --- a/mm/vmalloc.c >> +++ b/mm/vmalloc.c >> @@ -4327,14 +4327,29 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align >> goto need_realloc; >> } >> >> - /* >> - * TODO: Shrink the vm_area, i.e. unmap and free unused pages. What >> - * would be a good heuristic for when to shrink the vm_area? >> - */ >> if (size <= old_size) { >> + unsigned int new_nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; >> + >> /* Zero out "freed" memory, potentially for future realloc. */ >> if (want_init_on_free() || want_init_on_alloc(flags)) >> memset((void *)p + size, 0, old_size - size); >> + >> + /* Free tail pages when shrink crosses a page boundary. */ >> + if (new_nr_pages < vm->nr_pages && >> + !vm_area_page_order(vm)) { >> + unsigned long addr = (unsigned long)p; >> + >> + vunmap_range(addr + (new_nr_pages << PAGE_SHIFT), >> + addr + (vm->nr_pages << PAGE_SHIFT)); >> + >> + kasan_poison_vmalloc( >> + (void *)(addr + (new_nr_pages << PAGE_SHIFT)), >> + (vm->nr_pages - new_nr_pages) << PAGE_SHIFT); > > There is a kasan_poison_vmalloc() call here. > >> + vmalloc_free_pages(vm, new_nr_pages, vm->nr_pages); >> + vm->nr_pages = new_nr_pages; >> + } >> + >> vm->requested_size = size; >> kasan_poison_vmalloc(p + size, old_size - size); > > And there is a kasan_poison_vmalloc() call here. > > Furthermore, they seem to touch overlapping ranges. Perhaps the first > call can be dropped? > > Alice Thanks for the feedback Alice. I will do two things for v2. 1. Update the base-commit to the recent most commit on char-misc-next 2. Keep only a single `kasan_vrealloc` call after the rebase.