From patchwork Mon Jun 6 16:59:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Russell King X-Patchwork-Id: 9158905 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 291FE60572 for ; Mon, 6 Jun 2016 17:14:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 18B56252D5 for ; Mon, 6 Jun 2016 17:14:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0D6922834F; Mon, 6 Jun 2016 17:14:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id 8C1B5252D5 for ; Mon, 6 Jun 2016 17:14:33 +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 1b9y4y-0001fs-DM; Mon, 06 Jun 2016 17:12:44 +0000 Received: from pandora.armlinux.org.uk ([2001:4d48:ad52:3201:214:fdff:fe10:1be6]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1b9y3d-0007qt-Bi for linux-arm-kernel@lists.infradead.org; Mon, 06 Jun 2016 17:11:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.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=boWzsPe14mk4EQOfiZUObZLen6ZoiaLbPdCu3CuKmGo=; b=DsfhT7f4dsPP60DLhOFFRGQNYGY2EMuUR68HxsSN/t7Ogy/dx6EM0a3dTVLIlFDEfOdJRYhvNMkFQ08cQfC2XWOSdPCU6yjFBEJqrsVau+zNTV9R4zjN33e+L2QrImGXQdoVqtGfodXMIwyps1RoJ3Wv/aWIozeoUw54B9zrmoE=; Received: from [2002:4e20:1eda:1:b4eb:e902:622d:f2f0] (port=45136 helo=e0050434b2927.dyn.armlinux.org.uk) by pandora.armlinux.org.uk with esmtpsa (TLSv1:DHE-RSA-AES128-SHA:128) (Exim 4.82_1-5b7a7c0-XX) (envelope-from ) id 1b9xsd-0000H7-JI; Mon, 06 Jun 2016 18:00:00 +0100 Received: from rmk by e0050434b2927.dyn.armlinux.org.uk with local (Exim 4.82) (envelope-from ) id 1b9xsO-0003sa-Sr; Mon, 06 Jun 2016 17:59:44 +0100 In-Reply-To: <20160606164129.GM1041@n2100.armlinux.org.uk> References: <20160606164129.GM1041@n2100.armlinux.org.uk> From: Russell King To: kexec@lists.infradead.org,linux-arm-kernel@lists.infradead.org Subject: [PATCH kexec-tools v2 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: Mon, 06 Jun 2016 17:59:44 +0100 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160606_101122_439406_D5A6BC3A X-CRM114-Status: GOOD ( 16.37 ) 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 , Baoquan He Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.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 Reviewed-by: Pratyush Anand --- kexec/mem_regions.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ kexec/mem_regions.h | 4 +++ 2 files changed, 75 insertions(+) diff --git a/kexec/mem_regions.c b/kexec/mem_regions.c index 804984a..b01a5c8 100644 --- a/kexec/mem_regions.c +++ b/kexec/mem_regions.c @@ -55,3 +55,74 @@ int mem_regions_add(struct memory_ranges *ranges, unsigned long long base, return 0; } +static void mem_regions_remove(struct memory_ranges *ranges, int index) +{ + int tail_entries; + + /* we are assured to have at least one entry */ + ranges->size -= 1; + + /* if we have following entries, shuffle them down one place */ + tail_entries = ranges->size - index; + if (tail_entries) + memmove(ranges->ranges + index, ranges->ranges + index + 1, + tail_entries * sizeof(*ranges->ranges)); + + /* zero the new tail entry */ + memset(ranges->ranges + ranges->size, 0, sizeof(*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, ret; + + 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) { + if (r->end == range->end) + /* Remove this entry */ + mem_regions_remove(ranges, i); + else + /* 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 { + /* + * Split this area into 2 smaller ones and + * remove excluded range from between. First + * create new entry for the remaining area. + */ + ret = mem_regions_add(ranges, range->end + 1, + r->end - range->end, 0); + if (ret < 0) + return ret; + + /* + * Update this area to end before excluded + * range. + */ + r->end = range->start - 1; + break; + } + } + } + return 0; +} 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);