From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4C8DAC982E1 for ; Mon, 21 Sep 2026 09:05:05 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4hpHNK543tz3bcy; Mon, 21 Sep 2026 19:04:17 +1000 (AEST) 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=1789981457; cv=none; b=lMpVQ9yNYnSdm4DvFbDnShsy5YzkW619V8/rHaDXsX7uz2ADpx7x4mpfB+IQnBBvcR7/BCJbnrNB4zUVNjVeIPFUlTE36QRH8H2c7l7CjR4/YFDvppsSXsQAX19LwhFqUky65h7gnFpc0vJXIX7NOYe0ZjOcZ5efacTdhtaErmEvuAYfe/64yhk5hLV5wRePBvEOiEft9plmVzi/pheAmRkpU/DzE54gtNDFQxaT1SZWMgJ62x2nPKxCVw/w9FV5YA2lfy8vFonv8Xg0S9iDL6XAcoA7TyTGLCYOeDaFeGd97Re2PXXFvr/+A+7UjwI6mTLrb/1JZgBjWuNodeq16Q== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1789981457; c=relaxed/relaxed; bh=C/N27OffVwB9HknmerMZN+vGpNQOngX4+yhYYxhlpgs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=T6HVrA/cJ/Bd4AJVdskImROdQ5+Iqtp2solaLUxxDTBhjjN6HMi6MAyXqvNxkt1QGA727ktSogmMP7uH8XavYc5R4vy2PyQJf7Pv/H21vGuFpYb8ZUWB8q6gBPf9VxIo8ZuvaoUYirfVFGudxpa0T0RJ14d836LMOISwQ3Ltj9yj+RInWmxKJfFkSTrapQToLFr8bvp29Jce/msmUSywnCN0qVrDXuYbrQzgegBu59XsxzxS6tBD+tjpSsv6clGGVLYJ38vtNkQITZQ0igBNSogKBR3F7DG+UGOsunvE9CUJL0j3ZOSve5FOTrnUugcAFXphspxPcASpC3aIfrFD7w== 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=xq1YbTvD; 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=xq1YbTvD; 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) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4hpHNJ59Dfz3bZR for ; Mon, 21 Sep 2026 19:04:16 +1000 (AEST) dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=C/N27OffVwB9HknmerMZN+vGpNQOngX4+yhYYxhlpgs=; b=xq1YbTvD3XNNnXqFEYm+rNDxG6EdsqcQH0HTMN1s5TayGIm1XkNSyQvmkbCWJmLw2RC3Hg9MW 03OTT0taTlXRmwul+dn+StNpv/it+wBQ3leQZDkbShT3eaE7XAglReIYtuKqLTKOKW4s7jvAaSh i66n7enwbsFGUXQhFmGkLKQ= Received: from mail.maildlp.com (unknown [172.19.163.15]) by canpmsgout08.his.huawei.com (SkyGuard) with ESMTPS id 4hpH7Q1fbDzmV8f; Mon, 21 Sep 2026 16:53:06 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 212A040586; Mon, 21 Sep 2026 17:04:08 +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; Mon, 21 Sep 2026 17:04:06 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: Subject: [PATCH v6 06/14] LoongArch: kexec_file: Fix CMA page leaks in segment placement retry loops Date: Mon, 21 Sep 2026 17:04:42 +0800 Message-ID: <20260921090450.807575-7-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260921090450.807575-1-ruanjinjie@huawei.com> References: <20260921090450.807575-1-ruanjinjie@huawei.com> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , List-Unsubscribe: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.90.53.73] X-ClientProxiedBy: kwepems500002.china.huawei.com (7.221.188.17) To kwepemk200008.china.huawei.com (7.202.194.74) During kexec image placement retry loops, any midway failure causes the loader to truncate `image->nr_segments` back to its initial state to purge the failed segments. However, this truncation introduces a memory leak. The CMA pages allocated via kexec_add_buffer() during the failed attempt are tracked in the `image->segment_cma` array. Because the subsequent cleanup paths only iterate up to the truncated `nr_segments` boundary, these allocated CMA pages outside the new boundary are permanently leaked. Fix this by explicitly releasing the associated CMA buffers in the failure paths before `image->nr_segments` is reduced. Cc: Huacai Chen Cc: WANG Xuerui Cc: Youling Tang Cc: "Mike Rapoport (Microsoft)" Cc: Sourabh Jain Cc: Kees Cook Cc: stable@vger.kernel.org Link: https://sashiko.dev/#/patchset/20260729031235.2840255-1-ruanjinjie%40huawei.com Fixes: 55d990f0084c ("LoongArch: Add EFI binary support for kexec_file") Signed-off-by: Jinjie Ruan --- arch/loongarch/kernel/kexec_efi.c | 1 + arch/loongarch/kernel/machine_kexec_file.c | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/loongarch/kernel/kexec_efi.c b/arch/loongarch/kernel/kexec_efi.c index 5ee78ebb1546..15fd797ff3de 100644 --- a/arch/loongarch/kernel/kexec_efi.c +++ b/arch/loongarch/kernel/kexec_efi.c @@ -86,6 +86,7 @@ static void *efi_kexec_load(struct kimage *image, * We couldn't find space for the other segments; erase the * kernel segment and try the next available hole. */ + kexec_free_segment_cma(image, kernel_segment_number); image->nr_segments -= 1; kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz; 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 5412aa9f3568..62a5be102065 100644 --- a/arch/loongarch/kernel/machine_kexec_file.c +++ b/arch/loongarch/kernel/machine_kexec_file.c @@ -217,7 +217,11 @@ int load_other_segments(struct kimage *image, return 0; out_err: - image->nr_segments = orig_segments; + while (image->nr_segments > orig_segments) { + kexec_free_segment_cma(image, image->nr_segments - 1); + image->nr_segments--; + } + kfree(modified_cmdline); return ret; } -- 2.34.1