From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5255606A31A for ; Wed, 2 Dec 2026 09:26:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=leHhHAWU; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=p/ILE6iZ DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=pIAuXWvqPSLpmatT8Z28WTvmr+uEa7NCi9hccso0Zgk=; b=leHhHAWUgsHux4 3nLgQL8WANPDVeySzMbOt9hcjWAWZOKMuh7JwKe4q2QjVQLlcXmzKg00qH549X1rL3VjRo2W8lbTx yWZGpKjl7xZRnO5Ijs3+BnRQIemwhi6uWGE1tlQKsj+is6QN1XEhth5DdoKdDuoVGVa+w3ReEb4sG r/7YE4dxajaNEjARtvfmExQQUSLq9rpFSFPfy+LHVbUUH0YCGK2Nc/qoXmc09Yzfyekz6HDffWTch +xScdZX+sbYX5g4RJkGnaB7SNzLR1gosSJz/uctsVSiomFBU1JORTMwlfeymUfHvEs//DHLbWol0i HnIM1bl6kVd+19ySkx8g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYgOq-0000000AmU2-45B9; Wed, 02 Dec 2026 09:13:16 +0000 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYgOp-0000000AmSC-0dyQ for linux-arm-kernel@lists.infradead.org; Wed, 02 Dec 2026 09:13:16 +0000 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-3b439e37813so6857952a91.3 for ; Wed, 02 Dec 2026 01:13:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796202794; x=1796807594; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=pIAuXWvqPSLpmatT8Z28WTvmr+uEa7NCi9hccso0Zgk=; b=p/ILE6iZCW3PFaz7UOOit7+nKC2KT+wtAFLyFhEFSl6n1NRmkdaQIYiPIhsHXua2Dk uSs/TIINdVeMDKa+6eLgcjQv0EpMQU6z7EokIiyN23V1nQHwtqJZb9Y1dD6GdWdbxzaQ CGw8qsB/ckNJKsd3aiFGS/sm/6pS/+SCRitFPjnI93VlYhAjk+zcS3H2fstcmgVGnrXM j44uHz8fJ8bI/Io4rvPKgB/VXoM5ZSiD6548gOACb/0qj6XUNdTBe6AOnEvETtr/zj2H ll3q0Yg0D2K54LSQVY2A6RSYur798Q5oJUUMhR66W2QMpE44kaJOtXmk5S1nOQD6k5io L/0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796202794; x=1796807594; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=pIAuXWvqPSLpmatT8Z28WTvmr+uEa7NCi9hccso0Zgk=; b=ry17YP+cUbj1/r24618SySZaFdnM+5uduHv3vIrldjrZjh4p2zE9UYh52Z0/60IWOi eA97BgBKJb2VzJnJqpR6vCwiHt3ctk/8oDu1TKpwsjtHI0QCOnUns9OgQpjpED+OT+ye KZsTcBAk7VqtTczgA3D03PdG3UuuezFkFRPKWHvzeKc26WSVH11M8RBC7HjAkOvyo1Qv DSJ1nipjCy5htyBfW5lpBIyUu0QNWfT/L31te75YMjWXilfMGiOv1lXXE71uWySTu97E q8K9EKd7ZPkODey721tvCiB2xosjnXnjN6KFzvvQJuGnruYUUG4HJW0lQBYZ43oR0q2h XRGQ== X-Forwarded-Encrypted: i=1; AL+hErH0dIIZGZYxHxSoxKGwMTaIOtJLtwsLUzSkqLuTi0Ux0KO2gqasb/pVxQwI8T3hxKov4IU+BAy0+WNmzadugrE6@lists.infradead.org X-Gm-Message-State: APuU4Z8RdlxcmKYMVbhnrV3vo+pj6+DTPrm3D9ig1Y/bt5q62QuA3zsK gahYMZT46b4hxcP/lzKB/7IgpSivOvRnhtczxuMJDDnGWJmRWg8INsHY X-Gm-Gg: AZFdTZcj7QsGqwqP5GgNjwyef/510qXoH4fw1TpKUb/DCVzJC+1kaedS8ZApCqdtz+Q wLu4N8W2IFp9FQ65XdUgb+LZWljgzRBNoKfT2NDI3yMJgTS4470/kS023ixNYgepG43pu8/XY57 0YK6j6yMNYp+qEq0ZqmGEG/x6wNN1up5wo2yUsXsqy1aUtf4sgE4DJY7QEZiQ90wOfGKHkvWLax MlpRl9KOEjLErkXFRHaLFQW2/pG32UwLfPClt2SHV9Y40LxyGVjICtQQLfh2E9WJc1uZXuNI4Fc TeB1ILHAsE2r5OgHqe8I6MvoVruV+dH/R7MMyzGZnfqYztO2ZpqmDGuoKpxL2//GgHF/IAZkGw0 cnD/G52STQiSyKVZnOZKfHuAMYphj9PbHxeCVLQL12jnU5v3yAgJ45rdyaZHGaF2KXJOSzNtKSs Sjw6PguQoRbi/C1N7/nz29qrwfY59j4NeMYUJk6u02efmIuGYf6ZVVMrmjz6PHLJWH/q3o6osG2 jSIvZ2lISgJNHIHqbR7JW1dDzk+CM7i1dLhBqGYewZDqrwS0xEBoyJ0IpNUXAa+l4IWpUbUWtYf 7af58aPiOLtBwtUpGwHT9HBH6YWhx3x3J98MTMyiJ6S6DvzhVFLiC0jj2w== X-Received: by 2002:a17:90b:3810:b0:3b4:ac3:8af2 with SMTP id 98e67ed59e1d1-3b41c9cf6famr37406494a91.27.1796202794470; Wed, 02 Dec 2026 01:13:14 -0800 (PST) Received: from mi-OptiPlex-7060.mioffice.cn ([43.224.245.234]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b41adcafe0sm65347467a91.16.2026.12.02.01.13.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 01:13:14 -0800 (PST) From: Wen Jiang To: Andrew Morton , Uladzislau Rezki , linux-mm@kvack.org Cc: Barry Song , Ryan Roberts , Dev Jain , Anshuman Khandual , David Hildenbrand , Catalin Marinas , Will Deacon , Leo Yan , Xueyuan Chen , Mike Rapoport , Pasha Tatashin , Pratyush Yadav , Alexander Graf , kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Wen Jiang Subject: [RFC PATCH 3/4] mm/vmalloc: try smaller block sizes before base pages Date: Wed, 2 Dec 2026 17:12:11 +0800 Message-Id: <20261202091212.363755-4-jiangwenxiaomi@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261202091212.363755-1-jiangwenxiaomi@gmail.com> References: <20261202091212.363755-1-jiangwenxiaomi@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261202_011315_201619_A227B26A X-CRM114-Status: GOOD ( 25.02 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Wen Jiang When a huge vmalloc allocation (VM_ALLOW_HUGE_VMAP) cannot get enough pages of the requested order, __vmalloc_node_range_noprof() frees everything it got, including the huge pages that did succeed, and starts over with base pages. Sizes in between, such as arm64's CONT_PTE, are never tried, so under fragmentation the area ends up fully mapped with PTEs even though much of it could have been covered by larger blocks. Instead, allocate the area level by level, from the requested mapping size down through the smaller sizes the architecture can map, e.g. PMD -> CONT_PTE -> PAGE, keeping whatever was already obtained. Each level asks vm_area_alloc_pages() for as many whole blocks of its size as fit the remainder, and maps what it got with its own shift; the rest is left to the next level. The next level is picked from what is left or, if the level could not be filled, is the next smaller size. VM_REQUIRE_HUGE_VMAP does not fall back below the requested level. Since the level only goes down, each level starts at an index that is a multiple of its block size, and the area is aligned to the requested mapping size, so every run is suitably aligned for its shift. An area that is fully backed at the requested level is mapped in one go, as before. page_order is set to the smallest level that was used, so the page array can still be walked in steps of 1 << page_order. Since it is set after each run, VM_HAS_HUGE_VMAP gets set as soon as one run is mapped with a shift larger than PAGE_SHIFT. __vmalloc_node_range_noprof() no longer retries the whole allocation with base pages when __vmalloc_area_node() fails. For a page allocation failure the fallback has already ended at base pages. Mapping with base pages needs more page table pages than with huge pages, so retrying after a mapping failure is unlikely to help either, and the page array would only shrink by the rounding up of the size, which the next change removes. __GFP_NOFAIL is still honoured by the order-0 level, the page array allocation and the mapping. The retry is kept for the case where the vm area cannot be found with the huge alignment. Suggested-by: Barry Song Assisted-by: LLM Signed-off-by: Wen Jiang --- mm/vmalloc.c | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 595b07a6cc3d1..5edc6bd20969b 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -4112,14 +4112,12 @@ memalloc_restore_scope(unsigned int flags) } static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, - pgprot_t prot, unsigned int page_shift, - int node) + pgprot_t prot, unsigned int shift, int node) { const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; unsigned long size = get_vm_area_size(area); unsigned long array_size; unsigned long nr_small_pages = size >> PAGE_SHIFT; - unsigned int page_order = page_shift - PAGE_SHIFT; array_size = nr_small_pages * sizeof(struct page *); @@ -4146,12 +4144,34 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, * potentially dangerous (pre-mature OOM, disruptive reclaim * and compaction etc. * - * Please note, the __vmalloc_node_range_noprof() falls-back - * to order-0 pages if high-order attempt is unsuccessful. + * Please note, the allocation falls back to smaller orders and + * finally to order-0 pages if high-order attempt is unsuccessful. */ - area->nr_pages = vm_area_alloc_pages( - vmalloc_gfp_adjust(gfp_mask, page_order), node, - page_order, nr_small_pages, area->pages); + while (area->nr_pages < nr_small_pages) { + unsigned int order = shift - PAGE_SHIFT; + unsigned long left = nr_small_pages - area->nr_pages; + unsigned long nr; + + nr = vm_area_alloc_pages(vmalloc_gfp_adjust(gfp_mask, order), + node, order, round_down(left, 1UL << order), + area->pages + area->nr_pages); + if (nr) { + area->nr_pages += nr; + if (vmalloc_area_map_range(area, area->nr_pages - nr, + area->nr_pages, shift, prot, gfp_mask)) + goto fail; + set_vm_area_page_order(area, order); + } + + /* No smaller size left, or huge pages are required. */ + if (shift == PAGE_SHIFT || (area->flags & VM_REQUIRE_HUGE_VMAP)) + break; + + if (left - nr < 1UL << order) + shift = vm_shift(prot, (left - nr) << PAGE_SHIFT); + else /* this level failed, step down */ + shift = vm_shift(prot, 1UL << (shift - 1)); + } /* * If not enough pages were obtained to accomplish an @@ -4165,21 +4185,17 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, * - a pending fatal signal * - insufficient huge page-order pages * - * Since we always retry allocations at order-0 in the huge page - * case a warning for either is spurious. + * Huge page-order allocations only fail here when huge pages + * are required, so a warning for either is spurious. */ - if (!fatal_signal_pending(current) && page_order == 0) + if (!fatal_signal_pending(current) && + !(area->flags & VM_REQUIRE_HUGE_VMAP)) warn_alloc(gfp_mask, NULL, "vmalloc error: size %lu, failed to allocate pages", nr_small_pages * PAGE_SIZE); goto fail; } - if (vmalloc_area_map_range(area, 0, nr_small_pages, page_shift, prot, - gfp_mask)) - goto fail; - - set_vm_area_page_order(area, page_order); return area->addr; fail: @@ -4335,7 +4351,7 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align, /* Allocate physical pages and map them into vmalloc space. */ ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node); if (!ret) - goto fail; + return NULL; /* * Mark the pages as accessible, now that they are mapped. -- 2.34.1