From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (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 961A5606A31C for ; Fri, 27 Nov 2026 10:11:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=LHau4pfz Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4jbTgV264fz2yMK; Fri, 27 Nov 2026 21:10:14 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip=113.46.200.223 ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1795774214; cv=none; b=QW8/hgIILmdS4b6wLEYtxgjTv0B+2nNumpgZ6ObThPu7cHgcl6gVgLkTKgA3YORDn89Iw13caJJrtjoJZrNn4my62xD8kDSUdB9soTQRLdZJlWpwib4XtwuY5tC1kbgQPg4eSI+FI3bV93CjOZafLz+0PqGDdtfDzSTo5av7Lrok2q1p5qFjHvm7orbvyOsmcyXlP5Paor7mbxoi5lBPLMno6zWlD4j91QekeKqwZwHpMXt4ORHVRPeehYeF1hM2VSY1bkd8R5zhLY4UcTJCjEeBmNe2YBFdbtDjFizcLHl2Mwnn2mcnr4czIJ5ZDVhAtsglFIgHf/bY3HRchBGGCw== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1795774214; c=relaxed/relaxed; bh=RD+RhKHkx2ypFB5sfZGqGwGbb8y1IoqlO39za0vFLv4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Qu5bH0EnCObUY/I9AjrisJFuWC28FpLLcWSwEKhaQYi3sBNT+2V6VhLeoAkspDITlRJPW/I4V917Qxi2tT83NoMvtmrnS4R18iljdwWCK7deN0SmxDBU2idYV3q5dOFDE4tpvHyU0xzLXkqMeAiL+djBj79dgzkGAJHfmWbsTyWdJAGeDAv7UIsfJjVa1zHuOhwpyjkoCSMWnhZqNvuhl0eh5X+1Yk93ITT4gahNkCoJNc8RmeV8blBKjH4FpB6xZrWxYUvu0ak38qbXFRj4SdpARIZoHx3ejY/6mUuZpUQ7TJNvxkwmmMXe0RwwuBKvwFeb2gH2j96lbN9JPJ9AyQ== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; dkim=pass (1024-bit key; unprotected) header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=LHau4pfz; dkim-atps=neutral; spf=pass (client-ip=113.46.200.223; helo=canpmsgout08.his.huawei.com; envelope-from=ruanjinjie@huawei.com; receiver=lists.ozlabs.org) smtp.mailfrom=huawei.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=LHau4pfz; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=huawei.com (client-ip=113.46.200.223; helo=canpmsgout08.his.huawei.com; envelope-from=ruanjinjie@huawei.com; receiver=lists.ozlabs.org) Received: from canpmsgout08.his.huawei.com (canpmsgout08.his.huawei.com [113.46.200.223]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4jbTgT1x7tz2yLH for ; Fri, 27 Nov 2026 21:10:12 +1100 (AEDT) dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=RD+RhKHkx2ypFB5sfZGqGwGbb8y1IoqlO39za0vFLv4=; b=LHau4pfz/sVP57FiK2UxBBLN/FUwWdlrRFdKQoAagxZjTMYkKP4CCFom+7r2gExa8cx+uTB6F wM2IgZjUr4H3DMTa89BmZIZ1XA7SPR269+mshFrZgpWGX6xVa85isQLow5DdpnoG5chTDwApz6d kC2XXILcZVAahBiMa/zjgkQ= Received: from mail.maildlp.com (unknown [172.19.163.15]) by canpmsgout08.his.huawei.com (SkyGuard) with ESMTPS id 4jbTfn5QdGzmV63; Fri, 27 Nov 2026 18:09:37 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id B4DBB4058E; Fri, 27 Nov 2026 18:10:02 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 27 Nov 2026 18:10:00 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: Subject: [PATCH v9 11/11] LoongArch: kexec_file: Fix CMA segment placement retries Date: Fri, 27 Nov 2026 18:09:25 +0800 Message-ID: <20261127100925.3019877-12-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261127100925.3019877-1-ruanjinjie@huawei.com> References: <20261127100925.3019877-1-ruanjinjie@huawei.com> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , Precedence: list X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: kwepems500002.china.huawei.com (7.221.188.17) To kwepemk200008.china.huawei.com (7.202.194.74) X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, 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 The file-based kexec system call places segments with kexec_add_buffer(), which takes them from CMA when kexec_alloc_contig() succeeds, to avoid copying the payload at kexec time. When load_other_segments() cannot place the initrd, the segments of the attempt are dropped but their CMA pages are leaked. Release them with kexec_rollback_segments() in the retry loop and in the error path of load_other_segments(). The loop makes progress by advancing buf_min past a segment placed by the walk. kexec_alloc_contig() ignores buf_min, so once the pages of a CMA segment are released, the next attempt can get the same region again and the retry makes no progress. Disable CMA for the retry. A CMA segment can be anywhere in RAM, so moving buf_min to its end can raise the walk's lower bound close to the top of memory and leave the retry with too little space for the initrd, failing with -EADDRNOTAVAIL. Only advance buf_min for segments found by the walk. Tested with a non-crash kexec_file_load() on QEMU virt, 2 GiB RAM and cma=64M@0xe8000000, which leaves only 64 MiB above the CMA area. Before the fix: The retry leaks the CMA pages and kexec_file_load() returns -EADDRNOTAVAIL even though RAM below CMA is free. After the fix: The kernel and initrd are placed below CMA and the second kernel boots. Fixes: 55d990f0084c ("LoongArch: Add EFI binary support for kexec_file") Cc: Huacai Chen Cc: WANG Xuerui Cc: Youling Tang Cc: Mike Rapoport (Microsoft) Cc: Sourabh Jain Cc: Kees Cook Cc: stable@vger.kernel.org Signed-off-by: Jinjie Ruan Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40huawei.com --- arch/loongarch/kernel/kexec_efi.c | 12 ++++++++---- arch/loongarch/kernel/machine_kexec_file.c | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/loongarch/kernel/kexec_efi.c b/arch/loongarch/kernel/kexec_efi.c index 4824c47907ac..1fbd0ee1deb6 100644 --- a/arch/loongarch/kernel/kexec_efi.c +++ b/arch/loongarch/kernel/kexec_efi.c @@ -82,11 +82,15 @@ static void *efi_kexec_load(struct kimage *image, break; /* - * We couldn't find space for the other segments; erase the - * kernel segment and try the next available hole. + * kexec_alloc_contig() ignores buf_min and always returns + * CMA memory, so disable it and keep buf_min. */ - image->nr_segments -= 1; - kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz; + if (image->segment_cma[kernel_segment_number]) + image->no_cma = true; + else + kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz; + + kexec_rollback_segments(image, kernel_segment_number); kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; } diff --git a/arch/loongarch/kernel/machine_kexec_file.c b/arch/loongarch/kernel/machine_kexec_file.c index 00940f20b45e..adf7416ff49f 100644 --- a/arch/loongarch/kernel/machine_kexec_file.c +++ b/arch/loongarch/kernel/machine_kexec_file.c @@ -190,7 +190,7 @@ int load_other_segments(struct kimage *image, return 0; out_err: - image->nr_segments = orig_segments; + kexec_rollback_segments(image, orig_segments); kfree(modified_cmdline); return ret; } -- 2.34.1