From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) (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 CE9244921AD for ; Wed, 6 May 2026 18:27:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778092075; cv=none; b=OsSdqbOsViogkHwaXrV+H5Pe7z8hbcLXlzXanehISgnTXV2QOMWrf0e38uelRlb7IUZSLrUGqjkgxT2M7XjwuBnTPiW0+tiJh/4eX3j5+J8lFuRzIysuBXbpBcHqpt9NMxosACwVmR4sVbmb/rqV2RDKhMPoThSw4v9e4MlKfQE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778092075; c=relaxed/simple; bh=gmCtt12rpxxRuQ3BH6grr0VwTxAYOIJVSc7xK8dHSys=; h=From:Date:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=GEyaz1WWsPoDDS6FlLJ6RMK3M5WMZ1q+/lvCcqRqOcx/A4ymfAxm0ZAI4OEYZUlqE57NrwzrDktiRsnGN5CXuonNIXnd3ZnW2Tu4ZXDmhQNg8qP7obu+CUUkGbG/o9J7+umEojbNZdwmykm2LJXm/T7UNR+XDb6pIlPURV9xu5Y= 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=XSBFmu5u; arc=none smtp.client-ip=209.85.167.44 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="XSBFmu5u" Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-5a8721851e2so3616726e87.0 for ; Wed, 06 May 2026 11:27:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778092072; x=1778696872; 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=6ahWUXqPYeaMsd00zZP6mrvWm2cueyZ5IdvRpeyyBBs=; b=XSBFmu5uoo+/eyCDFcVeJDe4Ny+8ygh7vE2B/7+oqEOkOla+5exGMstN1hdn2iu8gN BODRvh8DOwigZ9FCpJDaYY6PxyZMm9eEeL2nyyzKvrjOW09xJJVT+QEDjN9wYQjconl8 OP/4qWwSooPgIyL9Z0En/7Zc5tzKP7yyMrVSq2MzzFRfIazoKtuBC9WldKa/Te3d6om/ /v/gb+sGcIOzB5c21f40MsUTihmstgKiEPitzYr2DKad3JpzeM/YJQT+2lpwtxcs57jD sH+LDogZaQ+xaY6GQ8+/pcvHzEbD7bHWBVyybiQgOXlzyXlIw0tOIVSf0WQtD2xodf1q 75MA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778092072; x=1778696872; 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=6ahWUXqPYeaMsd00zZP6mrvWm2cueyZ5IdvRpeyyBBs=; b=CRyWMnDkxYG+Eja2bUiyAWhZbJ8bcEcLQ9ZjDT+F+0vvpe9CUNzDyTCfyPxUi3TjA2 /MAWrPMLYlKCq359pde6oCnZpCK8vIRhFXhr/AFaCb5fNlGPR3pOZPdScrgincHIVGvE jvj4+Id7SlAi6QQw1jqMDNR24Gwgf7PvaFX+MCKQuu+TWB9qbw0ndEpaXiXWr4PiyTXm X+pTrBdKl4np/c6AbQvZa6hENqHFheLSmqUt+k5I/e5FTAHgj3s+sso+lDxgV2ZJiWwm DykicFLEzhjc574ZTFRrsPxVQXUBPm8AridTSXWgaVMYEG26ftXja/WEnbMcCu4hoFkX XxAg== X-Forwarded-Encrypted: i=1; AFNElJ/awoQX1u2lqrf9qp+/G3TRJw94doJDjMyzlQfaxnOwLmrROIM7Z/bqCok6/VvrNatSnyh05kSB9WHpF0A=@vger.kernel.org X-Gm-Message-State: AOJu0YxhCvIFutTRG0w1a9Jc6mpUIejtne6jiNmtjv5jBDW+in6pV2kJ HcTtbxPQ2IfjHzagO6/fgMZ0KYEgsZ7rVj4IcR8ENY0k4dfnUUJfJr3H X-Gm-Gg: AeBDietMMXceyKgvC82ObasTvwnVeKDTOSBmu7AuFCqt/VQPyaQPg0oKNU9vw3I61SC oxXZKsHTebKcXliwzEyx0IMx1fZGrSLGYnvY/gYTtDcZeJS20egZIsgkRt4FLm6jXmeoUXOej7L 5elN8ILE8QPUDr1MTdjUYuSnsmdal8u/DC9fJ4a7wWDDm5b40plKbYQ70WQi+oguYHhOSB2ewOb 8Lm4YcNUFMmw3wQ96yW8tQbP8E0CJesFw3uwwWL4nGjFbMOfVcWlmy8ATS3vWvmjVDsR00MvGhG lBPKgk38rqkoM9u+4Q6V0LKttpKhxFSS3uh09dQCwFVxZcoYeNEPy2YmDmNRd+FpnirJBeDl/OR wEzwGudNI9tLLg2BjVQZoVm8B3PU8pwq53K0KsFHAIi7KPEinUFNltcZpVUA+NTpLemdKh0UyQh rLetdnQ2/5/+I5jDoOrtQE+6tAr4DZng== X-Received: by 2002:a05:6512:304b:b0:5a8:6e64:a932 with SMTP id 2adb3069b0e04-5a887cea7efmr2029835e87.32.1778092071653; Wed, 06 May 2026 11:27:51 -0700 (PDT) Received: from pc636 ([2001:9b1:d5a0:a500:de96:9acf:5dca:ede4]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5a86d599974sm3611601e87.49.2026.05.06.11.27.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 11:27:51 -0700 (PDT) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Wed, 6 May 2026 20:27:49 +0200 To: shivamkalra98@zohomail.in Cc: Andrew Morton , Uladzislau Rezki , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Alice Ryhl , Danilo Krummrich Subject: Re: [PATCH v12 4/5] mm/vmalloc: free unused pages on vrealloc() shrink Message-ID: References: <20260428-vmalloc-shrink-v12-0-3c18c9172eb1@zohomail.in> <20260428-vmalloc-shrink-v12-4-3c18c9172eb1@zohomail.in> 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: <20260428-vmalloc-shrink-v12-4-3c18c9172eb1@zohomail.in> On Tue, Apr 28, 2026 at 01:54:19AM +0530, 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. Allocations with VM_FLUSH_RESET_PERMS > are also skipped, as their direct-map permissions must be reset before > pages are returned to the page allocator, which is handled by > vm_reset_perms() during vfree(). > > Additionally, allocations with VM_USERMAP are skipped because > remap_vmalloc_range_partial() validates mapping requests against the > unchanged vm->size; freeing tail pages would cause vmalloc_to_page() > to return NULL for the unmapped range. > > To protect concurrent readers, the shrink path uses Node lock to > synchronize before freeing the pages. > > Finally, we notify kmemleak of the reduced allocation size using > kmemleak_free_part() to prevent the kmemleak scanner from faulting on > the newly unmapped virtual addresses. > > The virtual address reservation (vm->size / vmap_area) is intentionally > kept unchanged, preserving the address for potential future grow-in-place > support. > > Suggested-by: Danilo Krummrich > Signed-off-by: Shivam Kalra > --- > mm/vmalloc.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- > 1 file changed, 52 insertions(+), 4 deletions(-) > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > index 65e0a23efb3b..9f810d306db9 100644 > --- a/mm/vmalloc.c > +++ b/mm/vmalloc.c > @@ -4346,14 +4346,62 @@ 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. > + * > + * Skip huge page allocations (page_order > 0) as partial > + * freeing would require splitting. > + * > + * Skip VM_FLUSH_RESET_PERMS, as direct-map permissions must > + * be reset before pages are returned to the allocator. > + * > + * Skip VM_USERMAP, as remap_vmalloc_range_partial() validates > + * mapping requests against the unchanged vm->size; freeing > + * tail pages would cause vmalloc_to_page() to return NULL for > + * the unmapped range. > + * > + * Skip if either GFP_NOFS or GFP_NOIO are used. > + * kmemleak_free_part() internally allocates with > + * GFP_KERNEL, which could trigger a recursive deadlock > + * if we are under filesystem or I/O reclaim. > + */ > + if (new_nr_pages < vm->nr_pages && !vm_area_page_order(vm) && > + !(vm->flags & (VM_FLUSH_RESET_PERMS | VM_USERMAP)) && > + gfp_has_io_fs(flags)) { > + unsigned long addr = (unsigned long)kasan_reset_tag(p); > + unsigned int old_nr_pages = vm->nr_pages; > + > + /* > + * Use the node lock to synchronize with concurrent > + * readers (vmalloc_info_show). > + */ > + struct vmap_node *vn = addr_to_node(addr); > + > + spin_lock(&vn->busy.lock); > + vm->nr_pages = new_nr_pages; > + spin_unlock(&vn->busy.lock); > + > + /* Notify kmemleak of the reduced allocation size before unmapping. */ > + kmemleak_free_part( > + (void *)addr + ((unsigned long)new_nr_pages > + << PAGE_SHIFT), > + (unsigned long)(old_nr_pages - new_nr_pages) > + << PAGE_SHIFT); > + > + vunmap_range(addr + ((unsigned long)new_nr_pages > + << PAGE_SHIFT), > + addr + ((unsigned long)old_nr_pages > + << PAGE_SHIFT)); > + > + vm_area_free_pages(vm, new_nr_pages, old_nr_pages); > + } > vm->requested_size = size; > kasan_vrealloc(p, old_size, size); > return (void *)p; > > -- > 2.43.0 > > Reviewed-by: Uladzislau Rezki (Sony) -- Uladzislau Rezki