From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (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 2843730F552 for ; Mon, 22 Dec 2025 11:47:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766404078; cv=none; b=XMs1fcjAabrp7/K7j6xyKpANhyJgi4K9GoagEDjWGFJ8T33f07MlCpCHzVB7o8Q6bi66Y2ONPDtPccjmJLUIEWBsAGOPfsV5pDb8QbY37o/c31BomvDW6cuV8ycatRDPV8aUprJGYWiL0ZTwPztgeuJS/JPokl6P0TqtRbWdN4M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766404078; c=relaxed/simple; bh=r0H447jfFx4SMEOtqVH1PFIQRCV28BL5dE7kQfZ77VU=; h=From:Date:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=NqR0D19TegRo6s6yjiJFn3YKnvXyGBP0+F65IPM0tKFqp8WFOyW/PFinwgIQ6lanyBLpoTirwGfueDjRVbPiC3+88SiEpTAnSmwGzoleE1xI6RE9qgHhHQDDtjBiQtqkZN60Ax7hjvbf4W7dJB+M5JqWgyGOai7mHCfcCTS2v8s= 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=VVqLvKXs; arc=none smtp.client-ip=209.85.167.43 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="VVqLvKXs" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-598f81d090cso4174468e87.2 for ; Mon, 22 Dec 2025 03:47:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766404074; x=1767008874; 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=/foXhPAufi6uRtrLX7dAC2wGmj4Wzh3PyX8slXMF67w=; b=VVqLvKXseS3VOLpDG3EyBES8+PLqYihkN97E/wIisk9iNUv5JEN6p0T5k7sNO2eJNH dMzSi8tY7DGyB5b6uB5bfiqHpluH6hXn6LHkWk9m5nHfCtMCPHZ/tj6DLdQudygkbfb1 O8lLVz2u3IkEACswmS0bUfz4W4x3Y95VZYGxoY4H8lTVE2PcF4Q89pFc2wbMu0oIZbzk PpikEwy2Owhv1904krJKnqOLu52mc26701uUTq7/lxJIBUTALjMubryRgweUI4on4FMe natMNMZnwnDxKSc1wCUpGfg2GoZpd7L2taXlYRoOGB+xtPnjBgF9Bct5+C+jPEOC0zTN OG/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766404074; x=1767008874; 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=/foXhPAufi6uRtrLX7dAC2wGmj4Wzh3PyX8slXMF67w=; b=pFkUn6gF28Ca+OKxb+gjsp2DUmNQrjya8YCsumHe1PhgwU8Cya7N+TMfhtyEtVcqi5 M95fYperUpZjwwnaOJN5eVafmdRJSyGmMiMN16wgIiVPGrmUJyTl0UbvYjQknZ+5d4Tf AS1DcZ1KoLJXM5vBGjYLk06A+0I27tRy7GZ9xkigx7jB+A5P7cwYxwDCGdFNBq+Reqn7 RF9MmFzuBLOliI8Zy9tbkSCcyIz26vf/EW0FeADM8xquFcN/kyyVYZ2PE0mHM96O/1Gl N/gYwMU5xxznNlnG+uWm2ngrxKC9kMY/6cdTS0FiKREYe+J2yR3gjuk0Bb6EYnXDBQSY 1x6g== X-Forwarded-Encrypted: i=1; AJvYcCXheSiXpVwvB4gZ2nG0ycD2AicqbWL5okVJEgmlvMFFJnB2+QeSyibe98FbdRQxO2KPJRgNRd9+BVxF/a8=@vger.kernel.org X-Gm-Message-State: AOJu0YwiYHkpYc28YQX3DaXuD1X5HhLwIxN1i//HE1yWl0YpphSnMrS2 UjoT5LrkRbjdDdWFJ8Pg8JIITuswUaqkmNEMIA7dH5BRwPeG3C/AWAAY X-Gm-Gg: AY/fxX6iCQpCpHH2hj4nOXBW9AQB7Tt2rg2JOb4H43hy0O9YKJPR8jTzQ6MlbTbspia EDO2Tc046pfbypH5ClXAC36mBdqSocv4kU9LLxUKouuhUGKxqiy1jCU1UJLTxthU9qHelaeMfS1 MhwT9LQVi9AuM2dffa/eMnRHggnV9W6tqy9TEardPsSY+7yTIkmrtzBDVrbluZ5ZeZrohZrQvfH WvNzyg/RiW4LFgF4Jc7QrYa8XVtaZS4N4XR1CzvqQ1qYGANKcgUtEE5z61E64y4IcM2dgLiB7Tt ejJNb3NJvq2AV9kictXNwyas/uAxOrSIqKfjXvwUrhjvdtcfVEqdIumRVr5X+vZ7n2E7wNtwKXB Fz1xm/esYe9rWAQxPewaL0GbUZZisM5tvhsMOnjF7IPJmADaGgagq X-Google-Smtp-Source: AGHT+IEsiLInHUpO0B9wn6rIRScWVJqdnac8pFBEVqqQC42l41Q+lmgzVoCpWhWhGmaRmpj1YynoXQ== X-Received: by 2002:a05:6512:2388:b0:595:7e8e:3bc4 with SMTP id 2adb3069b0e04-59a17d5a4f1mr3593333e87.42.1766404073974; Mon, 22 Dec 2025 03:47:53 -0800 (PST) Received: from milan ([2001:9b1:d5a0:a500::24b]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-59a185d5eccsm3157947e87.14.2025.12.22.03.47.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Dec 2025 03:47:53 -0800 (PST) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Mon, 22 Dec 2025 12:47:51 +0100 To: Dev Jain Cc: catalin.marinas@arm.com, will@kernel.org, urezki@gmail.com, akpm@linux-foundation.org, tytso@mit.edu, adilger.kernel@dilger.ca, cem@kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com, shijie@os.amperecomputing.com, yang@os.amperecomputing.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, npiggin@gmail.com, willy@infradead.org, david@kernel.org, ziy@nvidia.com Subject: Re: [RESEND RFC PATCH 1/2] mm/vmalloc: Do not align size to huge size Message-ID: References: <20251212042701.71993-1-dev.jain@arm.com> <20251212042701.71993-2-dev.jain@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: <20251212042701.71993-2-dev.jain@arm.com> On Fri, Dec 12, 2025 at 09:57:00AM +0530, Dev Jain wrote: > vmalloc() consists of the following: > > (1) find empty space in the vmalloc space -> (2) get physical pages from > the buddy system -> (3) map the pages into the pagetable. > > It turns out that the cost of (1) and (3) is pretty insignificant. Hence, > the cost of vmalloc becomes highly sensitive to physical memory allocation > time. > > Currently, if we decide to use huge mappings, apart from aligning the start > of the target vm_struct region to the huge-alignment, we also align the > size. This does not seem to produce any benefit (apart from simplification > of the code), and there is a clear disadvantage - as mentioned above, the > main cost of vmalloc comes from its interaction with the buddy system, and > thus requesting more memory than was requested by the caller is suboptimal > and unnecessary. > > This change is also motivated due to the next patch ("arm64/mm: Enable > vmalloc-huge by default"). Suppose that some user of vmalloc maps 17 pages, > uses that mapping for an extremely short time, and vfree's it. That patch, > without this patch, on arm64 will ultimately map 16 * 2 = 32 pages in a > contiguous way. Since the mapping is used for a very short time, it is > likely that the extra cost of mapping 15 pages defeats any benefit from > reduced TLB pressure, and regresses that code path. > > Signed-off-by: Dev Jain > --- > mm/vmalloc.c | 38 ++++++++++++++++++++++++++++++-------- > 1 file changed, 30 insertions(+), 8 deletions(-) > > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > index ecbac900c35f..389225a6f7ef 100644 > --- a/mm/vmalloc.c > +++ b/mm/vmalloc.c > @@ -654,7 +654,7 @@ static int vmap_small_pages_range_noflush(unsigned long addr, unsigned long end, > int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, > pgprot_t prot, struct page **pages, unsigned int page_shift) > { > - unsigned int i, nr = (end - addr) >> PAGE_SHIFT; > + unsigned int i, step, nr = (end - addr) >> PAGE_SHIFT; > > WARN_ON(page_shift < PAGE_SHIFT); > > @@ -662,7 +662,8 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, > page_shift == PAGE_SHIFT) > return vmap_small_pages_range_noflush(addr, end, prot, pages); > > - for (i = 0; i < nr; i += 1U << (page_shift - PAGE_SHIFT)) { > + step = 1U << (page_shift - PAGE_SHIFT); > + for (i = 0; i < ALIGN_DOWN(nr, step); i += step) { > int err; > > err = vmap_range_noflush(addr, addr + (1UL << page_shift), > @@ -673,8 +674,9 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, > > addr += 1UL << page_shift; > } > - > - return 0; > + if (IS_ALIGNED(nr, step)) > + return 0; > + return vmap_small_pages_range_noflush(addr, end, prot, pages + i); > } > Can we improve the readability? index 25a4178188ee..14ca019b57af 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -655,6 +655,8 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, pgprot_t prot, struct page **pages, unsigned int page_shift) { unsigned int i, step, nr = (end - addr) >> PAGE_SHIFT; + unsigned int nr_aligned; + unsigned long chunk_size; WARN_ON(page_shift < PAGE_SHIFT); @@ -662,20 +664,24 @@ int __vmap_pages_range_noflush(unsigned long addr, unsigned long end, page_shift == PAGE_SHIFT) return vmap_small_pages_range_noflush(addr, end, prot, pages); - step = 1U << (page_shift - PAGE_SHIFT); - for (i = 0; i < ALIGN_DOWN(nr, step); i += step) { - int err; + step = 1U << (page_shift - PAGE_SHIFT); /* small pages per huge chunk. */ + nr_aligned = ALIGN_DOWN(nr, step); + chunk_size = 1UL << page_shift; - err = vmap_range_noflush(addr, addr + (1UL << page_shift), + for (i = 0; i < nr_aligned; i += step) { + int err = vmap_range_noflush(addr, addr + chunk_size, page_to_phys(pages[i]), prot, page_shift); if (err) return err; - addr += 1UL << page_shift; + addr += chunk_size; } - if (IS_ALIGNED(nr, step)) + + if (i == nr) return 0; + + /* Map the tail using small pages. */ return vmap_small_pages_range_noflush(addr, end, prot, pages + i); } > int vmap_pages_range_noflush(unsigned long addr, unsigned long end, > @@ -3197,7 +3199,7 @@ struct vm_struct *__get_vm_area_node(unsigned long size, > unsigned long requested_size = size; > > BUG_ON(in_interrupt()); > - size = ALIGN(size, 1ul << shift); > + size = PAGE_ALIGN(size); > if (unlikely(!size)) > return NULL; > > @@ -3353,7 +3355,7 @@ static void vm_reset_perms(struct vm_struct *area) > * Find the start and end range of the direct mappings to make sure that > * the vm_unmap_aliases() flush includes the direct map. > */ > - for (i = 0; i < area->nr_pages; i += 1U << page_order) { > + for (i = 0; i < ALIGN_DOWN(area->nr_pages, 1U << page_order); i += (1U << page_order)) { > nr_blocks? > unsigned long addr = (unsigned long)page_address(area->pages[i]); > > if (addr) { > @@ -3365,6 +3367,18 @@ static void vm_reset_perms(struct vm_struct *area) > flush_dmap = 1; > } > } > + for (; i < area->nr_pages; ++i) { > + unsigned long addr = (unsigned long)page_address(area->pages[i]); > + > + if (addr) { > + unsigned long page_size; > + > + page_size = PAGE_SIZE; > + start = min(addr, start); > + end = max(addr + page_size, end); > + flush_dmap = 1; > + } > + } > > /* > * Set direct map to something invalid so that it won't be cached if > @@ -3673,6 +3687,7 @@ vm_area_alloc_pages(gfp_t gfp, int nid, > * more permissive. > */ > if (!order) { > +single_page: > while (nr_allocated < nr_pages) { > unsigned int nr, nr_pages_request; > > @@ -3704,13 +3719,18 @@ vm_area_alloc_pages(gfp_t gfp, int nid, > * If zero or pages were obtained partly, > * fallback to a single page allocator. > */ > - if (nr != nr_pages_request) > + if (nr != nr_pages_request) { > + order = 0; > break; > + } > } > } > > /* High-order pages or fallback path if "bulk" fails. */ > while (nr_allocated < nr_pages) { > + if (nr_pages - nr_allocated < (1UL << order)) { > + goto single_page; > + } > if (!(gfp & __GFP_NOFAIL) && fatal_signal_pending(current)) > break; > Yes, it requires more attention. That "goto single_page" should be eliminated, IMO. We should not jump between blocks, logically the single_page belongs to "order-0 alloc path". Probably it requires more refactoring to simplify it. > > @@ -5179,7 +5199,9 @@ static void show_numa_info(struct seq_file *m, struct vm_struct *v, > > memset(counters, 0, nr_node_ids * sizeof(unsigned int)); > > - for (nr = 0; nr < v->nr_pages; nr += step) > + for (nr = 0; nr < ALIGN_DOWN(v->nr_pages, step); nr += step) > + counters[page_to_nid(v->pages[nr])] += step; > + for (; nr < v->nr_pages; ++nr) > counters[page_to_nid(v->pages[nr])] += step; > Can we fit it into one loop? Last tail loop continuous adding step? -- Uladzislau Rezki