Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6586425rdb; Tue, 2 Jan 2024 06:50:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IG1TKzAOeTGnKAeqozlztywz0cr2K9h1uAlUBtQqDYb+DqvtgKPx6lHmtqWLi/+VzjeIwMJ X-Received: by 2002:a17:902:e892:b0:1d3:fe01:17ec with SMTP id w18-20020a170902e89200b001d3fe0117ecmr25058480plg.18.1704207046383; Tue, 02 Jan 2024 06:50:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704207046; cv=none; d=google.com; s=arc-20160816; b=e83F1OvOwbW36sl9jr3g4fs9eTda4DPsbleYSI5s1SCoO5zXxx+TBBPMS8MivOGe+N uIn5iBYdOV+KFnxHjhdCHwaXMWNULpetGCUDlx7l43FMIzeeFsuK7ZXlonHXz+5mcRWD fZ94AaDR1iVgva7x+xtAwbeoCijiIBh0540GpmngVE953VHnuUwOoGa4BqimeoqVLA7L Lckqj8S2jEmBNkYCbn/mvYGVBGJatYC1jqS01+mcBSS5gjZttVD5rSUAiT691y1KsjoN SUke+d79DcjOPTCLSn5rY28VczYz+cH4hAwN4/cu5ybBCA6jIl8hg9skTN2kAspD2DHc i2sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=LCgO/9ttUPkNH7K3G/yJnpUY2imlxWqPy44q0o7/7cg=; fh=4lcCx3EStIuyjDBLpfSWCe2rIG2Vh/dFYjiMisk3+GQ=; b=WCXZrhFytMviBvss5tzfudN+XCzhbGgnFblUje2hW7FcMFPy93IarmJ79OP24MaFLj roWU7CibJWQi9kcSk+TGU2ULDcM/fMWrzbYoqYwy+T9eVMHcJJ3T0PmT7B1YZ4lIRT/x +8/Q1WnnbpsPLIRmZ8QqiF2GG8kV080Nupdaz2EGSTinWUxMi2Mx8PVG/3c7nSVHLA9g EIRXYgiEJaUpD0fxviLt8BPwH2tsvE3c4pGFRT3A9ugJk5KLwl6PWvrpK1T0KG95Mta6 brEBhWAJLed4uSEimzjIbjUP5c+VLNeSKf++Cif0C7QdHPOiuUjulrXX3nzy8SCx7/aF rH0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=bM3HUjIj; spf=pass (google.com: domain of linux-kernel+bounces-14509-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14509-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id z16-20020a170903019000b001d364196057si17782969plg.486.2024.01.02.06.50.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 06:50:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14509-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=bM3HUjIj; spf=pass (google.com: domain of linux-kernel+bounces-14509-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14509-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id E9033283789 for ; Tue, 2 Jan 2024 14:50:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 660EF14F89; Tue, 2 Jan 2024 14:50:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bM3HUjIj" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (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 4CE3614F62 for ; Tue, 2 Jan 2024 14:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-28bc870c540so7315983a91.2 for ; Tue, 02 Jan 2024 06:50:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704207027; x=1704811827; 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=LCgO/9ttUPkNH7K3G/yJnpUY2imlxWqPy44q0o7/7cg=; b=bM3HUjIjdkKuziGFOvXltDwZisIBomDfioRKmjqsY9RlP6Fd4Nz/EsRAro9S4Dcj/r 3WeQA7enlJk2n3voOhzq3bKaRNQYtz83aNvXTBOAk7Df8cyRhkWvrfc4T4P4XYggMt5d Tobd5t1iq2eVa6XsXNrDRvVllUbGDO0f1h/dHPHex539uPleK2IT41xTve0Xy1GWOBJh I7VrKAjrpcb7BOp56UpBxjyJ3NnzUNvGesfsxFk9Rt1Ox8ja7C9MSi01bD7YQUr3Gw/A XXit4g53IyRfcNbWdUVosITLKaVCvr59NktFRPvrodJICkdqSAYO8WQd2bfTXFUOOyPO vcOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704207027; x=1704811827; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LCgO/9ttUPkNH7K3G/yJnpUY2imlxWqPy44q0o7/7cg=; b=n3yKr5rGr8Z7n+Yruy7EANNwS8O2cmLu7ZkLang2tLK5bg+2oCcIUJwQUbUZxvxidd gsXitSqlBUFQ2r5oH/zL0jmOx8DQw+o0KV5cEOsg+cpI1DBgrV5nfJSj+e+a/RlrM9AK nxpes4w2lz1Zym2pmS3qpx7WZggXZOZhfPUNYDqFhs9G6xrwUJJCWYjJXc71itdURD5d cvkclUKqs79GwKNSdrl5GfmfdqYpM+4XjumWFFCQ/odNbD6FwdESKYDdVQwH6BQVwSZ8 PkS6afrQJm6KnAGM+svx2grsN8UI3TnqPjX9wkAYq9HMpHUm4yGg39sOkmrYOAeJzUxC pnwQ== X-Gm-Message-State: AOJu0YzwDy6l8qrXIuhyUGXU4OnvifV4WBnUMBiRkf86++9KpY80Wj/6 3+rdRqovuqZiJhMmLGQPD8RRiMjOqCe19w== X-Received: by 2002:a17:90b:4b52:b0:28b:e79e:f8d9 with SMTP id mi18-20020a17090b4b5200b0028be79ef8d9mr8228754pjb.84.1704207027515; Tue, 02 Jan 2024 06:50:27 -0800 (PST) Received: from code.. ([144.202.108.46]) by smtp.gmail.com with ESMTPSA id 19-20020a17090a005300b0028be216595csm25836878pjb.4.2024.01.02.06.50.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 06:50:27 -0800 (PST) From: Yuntao Wang To: linux-kernel@vger.kernel.org, kexec@lists.infradead.org, x86@kernel.org Cc: Andrew Morton , Baoquan He , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Vivek Goyal , Dave Young , Hari Bathini , Sourabh Jain , Takashi Iwai , Yuntao Wang Subject: [PATCH v2 3/3] crash_core: fix and simplify the logic of crash_exclude_mem_range() Date: Tue, 2 Jan 2024 22:49:05 +0800 Message-ID: <20240102144905.110047-4-ytcoode@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240102144905.110047-1-ytcoode@gmail.com> References: <20240102144905.110047-1-ytcoode@gmail.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 The purpose of crash_exclude_mem_range() is to remove all memory ranges that overlap with [mstart-mend]. However, the current logic only removes the first overlapping memory range. Commit a2e9a95d2190 ("kexec: Improve & fix crash_exclude_mem_range() to handle overlapping ranges") attempted to address this issue, but it did not fix all error cases. Let's fix and simplify the logic of crash_exclude_mem_range(). Signed-off-by: Yuntao Wang --- kernel/crash_core.c | 80 ++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 51 deletions(-) diff --git a/kernel/crash_core.c b/kernel/crash_core.c index efe87d501c8c..c51d0a54296b 100644 --- a/kernel/crash_core.c +++ b/kernel/crash_core.c @@ -565,9 +565,8 @@ int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map, int crash_exclude_mem_range(struct crash_mem *mem, unsigned long long mstart, unsigned long long mend) { - int i, j; + int i; unsigned long long start, end, p_start, p_end; - struct range temp_range = {0, 0}; for (i = 0; i < mem->nr_ranges; i++) { start = mem->ranges[i].start; @@ -575,72 +574,51 @@ int crash_exclude_mem_range(struct crash_mem *mem, p_start = mstart; p_end = mend; - if (mstart > end || mend < start) + if (p_start > end) continue; + /* + * Because the memory ranges in mem->ranges are stored in + * ascending order, when we detect `p_end < start`, we can + * immediately exit the for loop, as the subsequent memory + * ranges will definitely be outside the range we are looking + * for. + */ + if (p_end < start) + break; + /* Truncate any area outside of range */ - if (mstart < start) + if (p_start < start) p_start = start; - if (mend > end) + if (p_end > end) p_end = end; /* Found completely overlapping range */ if (p_start == start && p_end == end) { - mem->ranges[i].start = 0; - mem->ranges[i].end = 0; - if (i < mem->nr_ranges - 1) { - /* Shift rest of the ranges to left */ - for (j = i; j < mem->nr_ranges - 1; j++) { - mem->ranges[j].start = - mem->ranges[j+1].start; - mem->ranges[j].end = - mem->ranges[j+1].end; - } - - /* - * Continue to check if there are another overlapping ranges - * from the current position because of shifting the above - * mem ranges. - */ - i--; - mem->nr_ranges--; - continue; - } + memmove(&mem->ranges[i], &mem->ranges[i + 1], + (mem->nr_ranges - (i + 1)) * sizeof(mem->ranges[i])); + i--; mem->nr_ranges--; - return 0; - } - - if (p_start > start && p_end < end) { + } else if (p_start > start && p_end < end) { /* Split original range */ + if (mem->nr_ranges >= mem->max_nr_ranges) + return -ENOMEM; + + memmove(&mem->ranges[i + 2], &mem->ranges[i + 1], + (mem->nr_ranges - (i + 1)) * sizeof(mem->ranges[i])); + mem->ranges[i].end = p_start - 1; - temp_range.start = p_end + 1; - temp_range.end = end; + mem->ranges[i + 1].start = p_end + 1; + mem->ranges[i + 1].end = end; + + i++; + mem->nr_ranges++; } else if (p_start != start) mem->ranges[i].end = p_start - 1; else mem->ranges[i].start = p_end + 1; - break; - } - - /* If a split happened, add the split to array */ - if (!temp_range.end) - return 0; - - /* Split happened */ - if (i == mem->max_nr_ranges - 1) - return -ENOMEM; - - /* Location where new range should go */ - j = i + 1; - if (j < mem->nr_ranges) { - /* Move over all ranges one slot towards the end */ - for (i = mem->nr_ranges - 1; i >= j; i--) - mem->ranges[i + 1] = mem->ranges[i]; } - mem->ranges[j].start = temp_range.start; - mem->ranges[j].end = temp_range.end; - mem->nr_ranges++; return 0; } -- 2.43.0