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 8DCA9C61DC2 for ; Wed, 26 Aug 2026 09:25:49 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4hVK4p3dH6z30C0; Wed, 26 Aug 2026 19:25:30 +1000 (AEST) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip=113.46.200.221 ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1787736330; cv=none; b=Vc7pPjOFy+cC0G9d+mXLtE/Mku+/4As3pF2DM8nZPqsLgwgNWpqpBXrUAVo839I0lOAvOYB3MzEKDut1rJhH/gP0GE+BjMFnxMQwQBrkTVEVnzapP0DSfaxQZmzIniItCLT5W6eI09uduzAtSmXwKD+eEvFXWCfpf4y1B//7dOigvhQ6OX5fP6EL5IA5eP4EMYengggyD74D6dlMhVlP86saotcurt41qxAUrSKjbZpcbQK3lHCQD9n1omqlpfmBxO4ZccNEqOFgM/VX/Ugz0ZUePvkamf37CKsgs1C5bOGcA3iJ7mtOLxYYEO+OafJBeDY6LxINeqBhJ673kZE6gg== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1787736330; c=relaxed/relaxed; bh=sbSk8OJq/l2XFTPb3OtwS42B6HB337pYFx+UhYhprQM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QPpIgwCiuZLJ6FDlQHx//d+LSBoYv/n3d2Ml1a1VWHMskmpJ9BTE9QBvY8S85epvtu74IH8eWWQaelLw3yp42thWX6+0Nov13U2n/N3du2PNWZvN9jcXR/FlBBy5Nr5nXhs2kNH03xnTFNvIxdSRb5MOAn3GyLd45ObPTKjmf8HdXF3NXYrbH3gYlGLExmOPn0p9v65NfoXB6+fgluSqXElmlxokib8EeLZox+c4nBn2wgJuadw1x4lr9fWhfkZ2utu+4DByzfWZ9+1yEojkeTOhSCEuwKpfY7komI6XjhpGmTx6T/DDVeo+COKVvLTq0nxhs8EUIqdNlmGKnfdO+w== 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=T8c8+n0h; dkim-atps=neutral; spf=pass (client-ip=113.46.200.221; helo=canpmsgout06.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=T8c8+n0h; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=huawei.com (client-ip=113.46.200.221; helo=canpmsgout06.his.huawei.com; envelope-from=ruanjinjie@huawei.com; receiver=lists.ozlabs.org) Received: from canpmsgout06.his.huawei.com (canpmsgout06.his.huawei.com [113.46.200.221]) (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 4hVK4h3bPzz2y8F for ; Wed, 26 Aug 2026 19:25:23 +1000 (AEST) dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=sbSk8OJq/l2XFTPb3OtwS42B6HB337pYFx+UhYhprQM=; b=T8c8+n0h22Tq1zVVtz7DjXb5w6+R1aGD/V9ecKTeM2uRCAgFmOcuoeWtKKBKTJhTcoEYufilU nHB7WJmb9Tl9N5EMS7H+1vNtHc3odCNbsM4CaphmYV0tEmTGQs3nEvlZcBkVf3LqODgbGw8yImD nGFusQhKqhqcQ5RieTuHGsg= Received: from mail.maildlp.com (unknown [172.19.162.223]) by canpmsgout06.his.huawei.com (SkyGuard) with ESMTPS id 4hVJr62H8VzRhR0; Wed, 26 Aug 2026 17:14:30 +0800 (CST) Received: from dggpemf500011.china.huawei.com (unknown [7.185.36.131]) by mail.maildlp.com (Postfix) with ESMTPS id ECA5540561; Wed, 26 Aug 2026 17:25:13 +0800 (CST) Received: from huawei.com (10.90.53.73) by dggpemf500011.china.huawei.com (7.185.36.131) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 26 Aug 2026 17:25:11 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: Subject: [PATCH v3 02/17] kexec: Extract kexec_free_segment_cma() from kimage_free_cma() Date: Wed, 26 Aug 2026 17:25:26 +0800 Message-ID: <20260826092541.3905933-3-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260826092541.3905933-1-ruanjinjie@huawei.com> References: <20260826092541.3905933-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 dggpemf500011.china.huawei.com (7.185.36.131) kimage_free_cma() relies on image->nr_segments to iterate over segments. When an architecture loader (e.g., arm64) truncates nr_segments on a mid-way failure, CMA pages allocated beyond the new boundary become unreachable, causing silent memory leaks. Extract the per-segment freeing logic into the exported helper kexec_free_segment_cma(), so that architecture loaders can release individual segments before nr_segments is truncated. Refactor kimage_free_cma() to loop over the new helper, preserving existing behavior. Cc: Andrew Morton Cc: Baoquan He Cc: Mike Rapoport Cc: Pasha Tatashin Cc: Pratyush Yadav Signed-off-by: Jinjie Ruan --- include/linux/kexec.h | 2 ++ kernel/kexec_core.c | 27 +++++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 83c296c0eb6c..d7c83938dfd3 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -533,6 +533,7 @@ extern bool kexec_file_dbg_print; extern void *kimage_map_segment(struct kimage *image, int idx); extern void kimage_unmap_segment(void *buffer); +extern void kexec_free_segment_cma(struct kimage *image, unsigned long idx); #else /* !CONFIG_KEXEC_CORE */ struct pt_regs; struct task_struct; @@ -544,6 +545,7 @@ static inline int kexec_crash_loaded(void) { return 0; } static inline void *kimage_map_segment(struct kimage *image, int idx) { return NULL; } static inline void kimage_unmap_segment(void *buffer) { } +static inline void kexec_free_segment_cma(struct kimage *image, unsigned long idx) { } #define kexec_in_progress false #endif /* CONFIG_KEXEC_CORE */ diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index 611b15bb1369..ee66e8ffd358 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -554,23 +554,26 @@ static void kimage_free_entry(kimage_entry_t entry) kimage_free_pages(page); } -static void kimage_free_cma(struct kimage *image) +void kexec_free_segment_cma(struct kimage *image, unsigned long idx) { - unsigned long i; + unsigned int nr_pages = image->segment_cma_pages[idx]; + struct page *cma = image->segment_cma[idx]; - for (i = 0; i < image->nr_segments; i++) { - struct page *cma = image->segment_cma[i]; - unsigned int nr_pages = image->segment_cma_pages[i]; + if (!cma) + return; - if (!cma) - continue; + arch_kexec_pre_free_pages(page_address(cma), nr_pages); + dma_release_from_contiguous(NULL, cma, nr_pages); + image->segment_cma[idx] = NULL; + image->segment_cma_pages[idx] = 0; +} - arch_kexec_pre_free_pages(page_address(cma), nr_pages); - dma_release_from_contiguous(NULL, cma, nr_pages); - image->segment_cma[i] = NULL; - image->segment_cma_pages[i] = 0; - } +static void kimage_free_cma(struct kimage *image) +{ + unsigned long i; + for (i = 0; i < image->nr_segments; i++) + kexec_free_segment_cma(image, i); } void kimage_free(struct kimage *image) -- 2.34.1