From patchwork Tue May 3 10:22:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Russell King X-Patchwork-Id: 9002411 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 9B2019F372 for ; Tue, 3 May 2016 10:25:54 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B990120222 for ; Tue, 3 May 2016 10:25:53 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id AEF562022A for ; Tue, 3 May 2016 10:25:52 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1axXUo-0001MH-TA; Tue, 03 May 2016 10:24:02 +0000 Received: from pandora.arm.linux.org.uk ([78.32.30.218]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1axXTj-0007mF-Vy for linux-arm-kernel@lists.infradead.org; Tue, 03 May 2016 10:23:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=arm.linux.org.uk; s=pandora-2014; h=Date:Sender:Message-Id:Content-Type:Content-Transfer-Encoding:MIME-Version:Subject:Cc:To:From:References:In-Reply-To; bh=tjahdjGW/wP4IrzXrbAXarzRc6WBQoDlEV08NNfwQs8=; b=p85YI1csritcRb8/47Qc5sOd23me/zcQVaJvK744ncpqt04BzxKnqFnanbAXZM1q/+LXb6MTVx5dWmi9787zExJ2sf4l4Ldsk1jvnvH12r62asWLVdzKLGaTmplD3DgR7gLow4mDCTiP+oj4LlalWcUqAMHLrE1wBmd60vF4kGU=; Received: from [2001:4d48:ad52:3201:452d:3855:1c55:6d46] (port=48556 helo=e0050434b2927.dyn.arm.linux.org.uk) by pandora.arm.linux.org.uk with esmtpsa (TLSv1:DHE-RSA-AES128-SHA:128) (Exim 4.82_1-5b7a7c0-XX) (envelope-from ) id 1axXT7-0004x3-HI; Tue, 03 May 2016 11:22:18 +0100 Received: from rmk by e0050434b2927.dyn.arm.linux.org.uk with local (Exim 4.82) (envelope-from ) id 1axXSv-0004i3-Gi; Tue, 03 May 2016 11:22:05 +0100 In-Reply-To: References: From: Russell King To: kexec@lists.infradead.org,linux-arm-kernel@lists.infradead.org Subject: [PATCH kexec-tools 12/32] kexec: add helper to exlude a region from a set of memory ranges MIME-Version: 1.0 Content-Disposition: inline Message-Id: Date: Tue, 03 May 2016 11:22:05 +0100 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160503_032257_074977_177760BB X-CRM114-Status: GOOD ( 15.60 ) X-Spam-Score: -3.0 (---) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pratyush Anand Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a helper to exclude a region from a set of memory ranges. Signed-off-by: Russell King --- kexec/mem_regions.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ kexec/mem_regions.h | 4 ++++ 2 files changed, 59 insertions(+) diff --git a/kexec/mem_regions.c b/kexec/mem_regions.c index 804984a..c6ba942 100644 --- a/kexec/mem_regions.c +++ b/kexec/mem_regions.c @@ -29,6 +29,61 @@ void mem_regions_sort(struct memory_ranges *ranges) } /** + * mem_regions_exclude() - excludes a memory region from a set of memory ranges + * @ranges: memory ranges to exclude the region from + * @range: memory range to exclude + * + * Exclude a memory region from a set of memory ranges. We assume that + * the region to be excluded is either wholely located within one of the + * memory ranges, or not at all. + */ +int mem_regions_exclude(struct memory_ranges *ranges, + const struct memory_range *range) +{ + int i; + + for (i = 0; i < ranges->size; i++) { + struct memory_range *r = ranges->ranges + i; + + /* + * We assume that crash area is fully contained in + * some larger memory area. + */ + if (r->start <= range->start && r->end >= range->end) { + if (r->start == range->start) { + /* Shrink the start of this memory range */ + r->start = range->end + 1; + } else if (r->end == range->end) { + /* Shrink the end of this memory range */ + r->end = range->start - 1; + } else { + struct memory_range *new; + + /* + * Split this area into 2 smaller ones and + * remove excluded range from between. First + * create new entry for the remaining area. + */ + if (ranges->size >= ranges->max_size) + return -1; + + new = ranges->ranges + ranges->size++; + new->start = range->end + 1; + new->end = r->end; + + /* + * Update this area to end before excluded + * range. + */ + r->end = range->start - 1; + break; + } + } + } + return 0; +} + +/** * mem_regions_add() - add a memory region to a set of ranges * @ranges: ranges to add the memory region to * @max: maximum number of entries in memory region diff --git a/kexec/mem_regions.h b/kexec/mem_regions.h index da7b5e8..ae9e972 100644 --- a/kexec/mem_regions.h +++ b/kexec/mem_regions.h @@ -2,9 +2,13 @@ #define MEM_REGIONS_H struct memory_ranges; +struct memory_range; void mem_regions_sort(struct memory_ranges *ranges); +int mem_regions_exclude(struct memory_ranges *ranges, + const struct memory_range *range); + int mem_regions_add(struct memory_ranges *ranges, unsigned long long base, unsigned long long length, int type);