From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796202795; b=jDs+vURw4srLaUW9AoULEhguvCgmAZHDFAQ2+6IK5g/i/ItdtnJnB5MoAZfDMyve2wpO fJr/nkg/7L1ZvHWKqwfRBIq299DVnOEBzAM8b73/KrvBYQSIrEtPQvKrkHMAh77A/Gx3i JSFSixAcR6ja7FM56w7vt72vUr7n0bEx9cY2zr9TxubKodx2vL5RX6/tEKglBGQ6UYrxG C4Ljqk+6WN1o3YepRIzjmau6EyoJz7FbQHbsgg2WSvq1HNDSrMpJ3Z6emufxAol+/dEPJ p1HkIMndRnvVmTJLPTnYf8LLMwXKgbUQt6ybhMWAtuAeQ9rS4AMOVNRy/p+Fr49pzFw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796202795; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=pIAuXWvqPSLpmatT8Z28WTvmr+uEa7NCi9hccso0Zgk=; b=Tm4CZNWvRrM4AOep15TvzaZuu3/AImdP5hYMPJBPcL2EjX6qXmHWhbcYOR/22m23td3i nF4mKhoHHfDiSHrVwePl3RlAi6OlkuHaVfaPqAD4XZ1dg0oAdIqLWI6tpTTKiSycVhmUR UKWjzt46CtxZmox3WbOLvo1On5R0C1onuvwWgxC9jfoDOqqxXDcgtaeIfkoNvxh4i1IBy hQB85hUANPG2VU17wv0a8F+G0gP6r8dpUVvilqIVHaD/3fEebKua4yVSsvrjvZGZTgUpC vwnPiO2BIX4QGiEPT8GvX5LybE0juv8DvbS4BKksAGr4wmb4lNvKUVkAvYYGDneC3Pw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=OaFPVuM0; arc=none smtp.remote-ip=209.85.216.53 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 647BA606A322 for ; Wed, 2 Dec 2026 09:13:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 647BA606A322 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.a=rsa-sha256 header.s=20260707 header.b=OaFPVuM0 Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-3b3fb193c03so5527431a91.2 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=vger.kernel.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=OaFPVuM0pG+PqZj2h58OrqqVo479IkLwM62qe8nAbvtAQBgA4ASl4T2I8idftDWKH7 wVvMjM49f/P4qn0EmsWOD9Gqqc6OdDKcO6G9HlLgSR2iwmmVd/MWLDwKtjcDLGiwhQR9 c1JaIpernTMo+u57kIXcdT4yUJp062sMMQaJyLBUgcZX7SR3vqqZRpJtJHjk7GgBTSNJ KVaUC7ZY39MoB4UyYzeDDURg2aia7JD5+0rEPPhNGrm+7u5ycv43mci7o1fYlxFnAS0Y wZlkQoVOMQaRp3kFRa6thG/bWW0wgslNbbvY3h3hFpSfeT5wZVnFnWAn9aKQdadI9jd/ +fhA== 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=P6xs7u8uLN3BTxgd0cWH4Znx/YN9t2pe5YyTUdHY8HzK/mvcw9OOKcYQACNdKf/MkR 5pGuozGksEdBSuJK391GkslUbvB270r5F+qi5J8ddydvYpm+2h1JnsrtnE1ufK+Gq/GC KI1drcbMwkctDtvrimgaxLFqSh3YhACq54FMEbEPcz3SjOV7lxH8T1dLI5l9SKwVHecN OgYZkSHdbJ5BZP5lhUXdYwl9cf2tCH/uSmuZorJ9NVm7sLkPaJYXJGhO9kBfaHbs4+/3 U2kx/FuWCEFHEYslrtNsvRdpuSIJZ/F+OKinriN36o3bAEnDaBBXT2oiw7oNHKRnva03 bCQg== X-Forwarded-Encrypted: i=1; AL+hErHMDxU8qdONdmOSX8uYfk8hSgBMcc8SGOSWubo+16BeoH5CsOwdRrrJZ/hoYoTs/awH8HsEJDeheXvDBLk=@vger.kernel.org X-Gm-Message-State: APuU4Z94++St0mBxv00JniDWv/HK0TjN2ZVYPRMHS/kkRbJBmwk0zOvJ eCkadV+segL5PpymT7zZRCwcwllpqN+yaPTWsKv3Ad08znRzQafyjLra X-Gm-Gg: AZFdTZe76vDG7tVWcFkOOENpMXqJWea82UOBx1UG81RzvKdqFxjMBztLduPJzlYrFTx 8sbWPCThcAywxG2Gii5uXS3MYLahokKO317jOdQWGVBeHPU04wnU0zwI9AWz1Qh3nWvgrCoTcFf B1Zh7OkHeUmRzYRZAGzThW1pz7l20zQy994qOUQiFdb4JJfzH4wdPIJ+nnN3s5LFjDjAqo3zUxe cKViUYprOHZKMt2Ov0TnhbE6yzmH97KH7hLVW53FXdfzu1o8BxuDvaYEXjyaBOj1vfvHLrHxkUa X88Qh4ybeGQhXtL6xx+tk/bEv8sJ31A3C2c+DbqqxD9QjYkZb8PJn8VikCnrw1r+4547SF+pbxR eqQb0gJRcll65Txwg1NGn4jUMboXpf3jvRV0Nh266d8vW610vwF1831DjxEd1flkPpfnfnl8wKA 7DnqEUScFCDlQeoTLcnfzrI2yxVV6/vWR1F5N2gAbrksTrKbzDeF93UsYbiofWd0HU6SNLgUfAc X47DD3ifRcTjyA0c/MsLC78bzcKId8isIUe9HBV1k6EFakzFfP48CD74tqyizKxI1RYS77h7Ppe B+bdCis3381ADePUzJaZ9TzDWSugCqXLC+L/UO+Yy8PTvQvOUckDrK9Huw== 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-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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