From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794479956; b=YH2lItcaKmjgVcqcIF50jVwwSi2gnwbl+WNyQDOoFxbdQE4TNpaRDb+vRID1VK2nZkBZ reLQwXh7fOcCp1Wft30p55Ln/0UayDPQm5qul7BUHbt+HkPAU48RKMOKdHzz+OXRYJNKv esqy4CGupFj5pd/Rf/ydlU2ooT7KuI4x3rpuTm/4Dbf9FGfVOg9GD6mlkQueKryhW/VmV xn+jE+NiRmd19MDksSg/CqpA6qZLCjc0fwmhQYXPuIpYS7ljkr2zRWgG/9arhK2PJUDjs OsLSBQ4EDrvdLSLOcS5ElqRio+7hW4p8R/+lwnYix+OYLXoFymAIMJfBQ4PdNH4rEKw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794479956; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer; bh=bWRG9mq5c0zudy8yzUdur6pFFwDbg0IZ+6kaZgluVLQ=; b=e656nvDLIk7f6ZlRWmT6tNdkkYx6YGoyTI5ZAjiUsADKcXo562wd1b0w1SfAKheq9YoO If+YVFjowYLnpFDxh7tVKBCqHjEJDASyIuYnpVu/QaZLM0RfxvEdJ2r62MT3RXvX5/5tn Lya9B+833pO3hFWGmja49Al3V4e1m3lcZXCW2cU6rjS2PJLcqq3lfXRDj7yZLEVhunGLl J97VxqdjX1JSFnTE3sNLO5m18TUwgJcLEXCdzrXfuiXZ+NhO95KVle5wFfqx36trnQG2Z bAxmKd40cTzjHVu7CzrT6goGjxFfsR5pvLe917Va87uZgGZBN0IXxhSWl6VvhNO/41g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YiiROZ3c; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A01E4606A327; Thu, 12 Nov 2026 10:39:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YiiROZ3c Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66C061F00A05; Thu, 12 Nov 2026 10:39:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794479956; bh=bWRG9mq5c0zudy8yzUdur6pFFwDbg0IZ+6kaZgluVLQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=YiiROZ3cKFwEsmv/gaH+0ZzA+ApxCkE8LSgohRoc7zn2sawnCN40m+wmKbMhtJW+L prDkmksozAzOpV+vtBhenY31ISzHgT3ZecPebQXQPbINiS5S+0RFXEV8ZYpPExIAvv bSo7f/aSMTDacB3UwsQivWCoQRjvWpxqqnGC5NrZD5Jmw3PCTy69/qdjHGdIEJZOia 9F07LE+lVOBP9RBWHXNeNRrbaX45LTJp6mdI6lvw+n43VlNjzeU+7UEWBu1NfqyUYj 5VNa3SGPrgd6M88pnXeh98m2BO+9THUqVRGuoD/gnxO+2DDyQm5vPEQwGjnxDE7Vlh juAuFAxk2t6HA== From: "Mike Rapoport (Microsoft)" Date: Thu, 12 Nov 2026 12:38:55 +0200 Subject: [PATCH v3 3/5] kho: skip zero-sized lowmem and global bootmem areas 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261112-kho-bootmem-sizing-v3-3-f0c8d00295d5@kernel.org> References: <20261112-kho-bootmem-sizing-v3-0-f0c8d00295d5@kernel.org> In-Reply-To: <20261112-kho-bootmem-sizing-v3-0-f0c8d00295d5@kernel.org> To: Jonathan Corbet , Mike Rapoport , Pasha Tatashin , Pratyush Yadav Cc: Alexander Graf , Andrew Morton , Chen Pei , George Guo , Kexin Liu , Randy Dunlap , Shuah Khan , Sourabh Jain , kexec@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, "Jose A. Perez de Azpillaga" , Mukesh Pilaniya X-Mailer: b4 0.16.1-dev The lowmem and the global bootmem areas can have zero size, for example when none or all of the memory is below ARCH_LOW_ADDRESS_LIMIT, or when either of those areas is explicitly set to zero with kho_bootmem=. For a zero-sized allocation memblock returns an address without reserving anything, and that empty area is recorded in kho_bootmem. If there is no memory below ARCH_LOW_ADDRESS_LIMIT, allocation of the empty lowmem area fails and KHO gets disabled. Extract a helper that checks for zero size and allocates memory from memblock, and use it to skip empty areas the same way as empty per-node areas are skipped. Fixes: 3dc92c311498 ("kexec: add Kexec HandOver (KHO) generation helpers") Reported-by: Chen Pei Closes: https://lore.kernel.org/all/20261026131851.9942-1-cp0613@linux.alibaba.com Reviewed-by: Pasha Tatashin Reviewed-by: Jose A. Perez de Azpillaga Reviewed-by: Pratyush Yadav Reviewed-by: Mukesh Pilaniya Signed-off-by: Mike Rapoport (Microsoft) --- kernel/liveupdate/kexec_handover.c | 66 +++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c index df4a972a860da..bbc9e644addc9 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -835,6 +835,26 @@ static int __init kho_bootmem_cmp(const void *_a, const void *_b) return cmp_int(a->addr, b->addr); } +static int __init kho_reserve_bootmem_area(phys_addr_t size, phys_addr_t end, + int nid) +{ + phys_addr_t addr; + + if (!size) + return 0; + + addr = memblock_alloc_range_nid(size, BOOTMEM_ALIGNMENT_BYTES, 0, end, + nid, numa_valid_node(nid)); + if (!addr) + return -ENOMEM; + + kho_bootmem[kho_bootmem_cnt].addr = addr; + kho_bootmem[kho_bootmem_cnt].size = size; + kho_bootmem_cnt++; + + return 0; +} + /** * kho_reserve_bootmem - Reserve a contiguous chunk of memory for kexec * @@ -846,8 +866,8 @@ static int __init kho_bootmem_cmp(const void *_a, const void *_b) */ static void __init kho_reserve_bootmem(void) { - int nid, i = 0, max_nr_bootmem; - phys_addr_t addr, size; + int nid, max_nr_bootmem; + phys_addr_t size; if (!kho_enable) return; @@ -867,66 +887,40 @@ static void __init kho_reserve_bootmem(void) * reserve bootmem area in low memory for lowmem allocations in the * next kernel */ - size = bootmem_size_lowmem; - addr = memblock_phys_alloc_range(size, BOOTMEM_ALIGNMENT_BYTES, 0, - ARCH_LOW_ADDRESS_LIMIT); - if (!addr) { + if (kho_reserve_bootmem_area(bootmem_size_lowmem, ARCH_LOW_ADDRESS_LIMIT, + NUMA_NO_NODE)) { pr_err("Failed to reserve lowmem bootmem\n"); goto err_free_bootmem_desc; } - kho_bootmem[i].addr = addr; - kho_bootmem[i].size = size; - i++; - /* reserve large contiguous area for allocations without nid */ - size = bootmem_size_global; - addr = memblock_phys_alloc(size, BOOTMEM_ALIGNMENT_BYTES); - if (!addr) { + if (kho_reserve_bootmem_area(bootmem_size_global, + MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE)) { pr_err("Failed to reserve global bootmem\n"); goto err_free_bootmem_areas; } - kho_bootmem[i].addr = addr; - kho_bootmem[i].size = size; - i++; - /* * Loop over nodes that have both memory and are online. Skip * memoryless nodes, as we can not allocate bootmem areas there. */ for_each_node_state(nid, N_MEMORY) { - size = bootmem_size_node(nid); - if (!size) - continue; - - addr = memblock_alloc_range_nid(size, BOOTMEM_ALIGNMENT_BYTES, - 0, MEMBLOCK_ALLOC_ACCESSIBLE, - nid, true); - if (!addr) { + if (kho_reserve_bootmem_area(bootmem_size_node(nid), + MEMBLOCK_ALLOC_ACCESSIBLE, nid)) { pr_err("Failed to reserve nid %d bootmem\n", nid); goto err_free_bootmem_areas; } - - kho_bootmem[i].addr = addr; - kho_bootmem[i].size = size; - i++; } - /* - * Nodes that needed no bootmem were skipped, so fewer areas than - * initially estimated may have been reserved. - */ - kho_bootmem_cnt = i; - sort(kho_bootmem, kho_bootmem_cnt, sizeof(*kho_bootmem), kho_bootmem_cmp, NULL); return; err_free_bootmem_areas: - for (i--; i >= 0; i--) + for (int i = 0; i < kho_bootmem_cnt; i++) memblock_phys_free(kho_bootmem[i].addr, kho_bootmem[i].size); + kho_bootmem_cnt = 0; err_free_bootmem_desc: memblock_free(kho_bootmem, max_nr_bootmem * sizeof(*kho_bootmem)); err_disable_kho: -- 2.53.0