From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 0377825D1E9 for ; Wed, 13 May 2026 13:08:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778677688; cv=none; b=ABhNfd867nawcLSukvrfg4bZEOA9/6SwXp+1sh8ImSMUQtHHQVGgUjdfqRAFoiM2JjPrfhonloEhHynlOZqtwt55lwljzUaZKSoxBamWa5FtsVsEFzjt5/qFlCHkj1WZzCWj4r6EpOO4iqe7lCHUPGX4qKTXILPJfhY17Z2M2A8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778677688; c=relaxed/simple; bh=1SPxgSRObVGaK8griK5Hm7ybkone8ondt+x70TyBxdA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ezKTr1zeyg6w5g3NWb1FMk0BzbEkmt2mwkKvl3G69M8uPnPT1eUYu9t9UIEMS8QHQBT9crkOAh8w/zJixo8ktr5qjcdaUlwNtz+Mjm0EGbvvcbgaHnvy2OnNdK4UuLnW8oDAZbX3xVnGQOEKqLOsXB1plTkDl4HyugJZbiiYWJU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=LOKQmZEO; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="LOKQmZEO" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2b9e9a6802aso27696255ad.3 for ; Wed, 13 May 2026 06:08:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1778677686; x=1779282486; 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; bh=4FGAY5sLEWcL6nfZSRVARbstApRC9v1d8J6Ir2cANyU=; b=LOKQmZEOJ1BXKhrlw72Mg05QEKqQin61r5t2AQmd7gn2KPkqoGLJ8K/HXyGnFnGvBm rYUFPXh7yCMRXaHhqG/5Z5u7pN2x1qPjwqaqEEyZ/1fFfSbvkc0oIMrgpR67GuJ3JcT9 J+oHPZnrvqrvEamMfXFFOUXyrWg7q9KeoSmdJapU3UwW90uyVibizxKUOX2oYmbSswhw ZU6SNVUiUzM4rFepy3DDP3hX2xxefYFQaFk4iwO5QhORxGOI3wVUUTnUo3skChBjRWj9 GERAaLAOxQkBIY+sg0lUiPjw4f+uFAOhNyA6w2u8J441Ypjj2oZAi+tSzvcUey+tqZ6r aNxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778677686; x=1779282486; 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; bh=4FGAY5sLEWcL6nfZSRVARbstApRC9v1d8J6Ir2cANyU=; b=IAFWIu1Nj0vVvHd9QU++M/6qmEwzahcGyrpamb5P6Qq75A8Vj3JrRhNBIaEDNKSxZQ 21tesc22bDOR18bbp4dm1dtlTE+wpsbkr8O6CwaR8aq2w+p0+fPN3sSX4rTrAo7R9Gj8 T9F7IAF8wnWSjrP8yGr4asgOXedkYqSqxeFwR7IB/AudzbsdMYEQHLp3eubWkMgVhA1Z ZrFzPBrAlVUIiStKLvacCKXuSJxKZD6mMVnPyoy+UsaFwXwHQHlX0/4Lirz/iFGOjSD8 +JIBBw2kE/Usacu72A4yb7dpK9OvPJF9b87K1n8SVnLFltbPek87R/36W2k78xanX6cK DvZA== X-Forwarded-Encrypted: i=1; AFNElJ9Q5a4rG7XM0TyBuqAzmFJScIRsyB8mOKZn4615+iI5Qh1ksnN8QxL5I06+kGh4o77JaapoajnSYodA1IY=@vger.kernel.org X-Gm-Message-State: AOJu0Yy9CLzcXaFNHINZDe/qyLMMJOcsBqVnJ+3yjOg4F4WMiwGB+NWE 8gD0h5zsbHIDgRuR9T5+bkvZkXY1NXFmOAtbx3kX8Wur2GAElGybiiZgyDBUuHpnzH0= X-Gm-Gg: Acq92OHxiJYEWs0lki6k4e/fcNCdZ7SJPZJ0MHiabeL6gua4myu2MO1QrWC7Ho2Sh+0 VpV9r5wXD+bDR9TA0B/qVGBaY8/x64R+DQ1mbRgb/wIFnr8AHJnGIICMBS4uUA/YJEK851iCedR 5qUL+5uElRERtYIP6zfFajAExxc6zHWeoOeTTmQh+SBH5Ui3XiaeC5E00Cv5bwzNscC9C6hiW1C 6sPZzLv8sj0b/A4Qb3a8t0/UAuPdje73Akk8GdtRWWI6QougvpWPli3T/v/zctqG8j3NLq8wgls lzE7AbnctJEdSV1VyQnZJfzQ8khXcMN1BNtlKV1k/CdjgbQ2ooafzKyJB9DagaCqZezO4s1Z60w 7WZ3UaZBC+p/WSnqqrg4VYrNc+u47vPtYKanVq0N0HOF1wO01tE20k9/99PY6rovW+QmHNNkw/N N9Cpq4mIcwTAEKeNvqqqt3h84NBGErEQq0kH8mDOOkP+5OlaDzHsfIBuBaLqM= X-Received: by 2002:a17:903:bca:b0:2b0:ac1e:9737 with SMTP id d9443c01a7336-2bd27121cc3mr28132885ad.12.1778677685927; Wed, 13 May 2026 06:08:05 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([61.213.176.6]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2baf1e90854sm166641925ad.66.2026.05.13.06.08.00 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 13 May 2026 06:08:05 -0700 (PDT) From: Muchun Song To: Andrew Morton , David Hildenbrand , Muchun Song , Oscar Salvador , Michael Ellerman , Madhavan Srinivasan Cc: Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Nicholas Piggin , Christophe Leroy , Ackerley Tng , Frank van der Linden , aneesh.kumar@linux.ibm.com, joao.m.martins@oracle.com, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH v2 06/69] mm/sparse: Panic on memmap and usemap allocation failure Date: Wed, 13 May 2026 21:04:34 +0800 Message-ID: <20260513130542.35604-7-songmuchun@bytedance.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260513130542.35604-1-songmuchun@bytedance.com> References: <20260513130542.35604-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When vmemmap or usemap allocation fails, sparse_init_nid() currently marks the section non-present and continues. Later boot-time code can still walk PFNs in that section without checking for this partial setup, which leads to invalid accesses. subsection_map_init() can also touch an unallocated usemap. Auditing and fixing all early PFN walkers for this case is not worth the complexity. These allocation failures are expected to be fatal anyway, and other memory models already treat them that way. Make memmap and usemap allocation failures panic immediately instead of trying to recover and crashing later in less obvious ways. This is also consistent with how other memory model configurations handle memmap allocation failures. Signed-off-by: Muchun Song Acked-by: Mike Rapoport (Microsoft) --- v1->v2: - Add Acked-by from Mike Rapoport - I refrained from adding panic() to memmap_alloc() as it wouldn't simplify the code. However, panic() is still required in sparse_init_nid() because the architecture-specific vmemmap_populate() bypasses memmap_alloc(). --- mm/sparse.c | 44 +++++++++----------------------------------- 1 file changed, 9 insertions(+), 35 deletions(-) diff --git a/mm/sparse.c b/mm/sparse.c index 16ac6df3c89f..c92bbc3f3aa3 100644 --- a/mm/sparse.c +++ b/mm/sparse.c @@ -239,15 +239,8 @@ struct page __init *__populate_section_memmap(unsigned long pfn, struct dev_pagemap *pgmap) { unsigned long size = section_map_size(); - struct page *map; - phys_addr_t addr = __pa(MAX_DMA_ADDRESS); - map = memmap_alloc(size, size, addr, nid, false); - if (!map) - panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa\n", - __func__, size, PAGE_SIZE, nid, &addr); - - return map; + return memmap_alloc(size, size, __pa(MAX_DMA_ADDRESS), nid, false); } #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ @@ -300,17 +293,14 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin, unsigned long map_count) { unsigned long pnum; - struct page *map; - struct mem_section *ms; - if (sparse_usage_init(nid, map_count)) { - pr_err("%s: node[%d] usemap allocation failed", __func__, nid); - goto failed; - } + if (sparse_usage_init(nid, map_count)) + panic("Failed to allocate usemap for node %d\n", nid); sparse_vmemmap_init_nid_early(nid); for_each_present_section_nr(pnum_begin, pnum) { + struct mem_section *ms; unsigned long pfn = section_nr_to_pfn(pnum); if (pnum >= pnum_end) @@ -318,34 +308,18 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin, ms = __nr_to_section(pnum); if (!preinited_vmemmap_section(ms)) { + struct page *map; + map = __populate_section_memmap(pfn, PAGES_PER_SECTION, - nid, NULL, NULL); - if (!map) { - pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.", - __func__, nid); - pnum_begin = pnum; - sparse_usage_fini(); - goto failed; - } + nid, NULL, NULL); + if (!map) + panic("Failed to allocate memmap for section %lu\n", pnum); memmap_boot_pages_add(DIV_ROUND_UP(PAGES_PER_SECTION * sizeof(struct page), PAGE_SIZE)); sparse_init_early_section(nid, map, pnum, 0); } } sparse_usage_fini(); - return; -failed: - /* - * We failed to allocate, mark all the following pnums as not present, - * except the ones already initialized earlier. - */ - for_each_present_section_nr(pnum_begin, pnum) { - if (pnum >= pnum_end) - break; - ms = __nr_to_section(pnum); - if (!preinited_vmemmap_section(ms)) - ms->section_mem_map = 0; - } } /* -- 2.54.0