From patchwork Thu May 26 08:56:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Russell King (Oracle)" X-Patchwork-Id: 9136841 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 74452607D5 for ; Thu, 26 May 2016 08:59:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 68709281D4 for ; Thu, 26 May 2016 08:59:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D4EA282BB; Thu, 26 May 2016 08:59:07 +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 2A65A281D4 for ; Thu, 26 May 2016 08:59:06 +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 1b5r6Q-00021c-IC; Thu, 26 May 2016 08:57:14 +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 1b5r6L-0001wQ-1d for linux-arm-kernel@lists.infradead.org; Thu, 26 May 2016 08:57:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2014; h=Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date; bh=HOUfONawn+/1gyvESLU5aZqWlVho8r44vBVSGJUPkVM=; b=VNhXI/odK46fvIlfgN6X6GMCPOOuA3RNDW86KhTWdaiPYqSDQyw53k7WmFajbjnPz4JG/dzpJxE7zH/y1LycLaONlJx5hq9Tgy2NpVm+ZiTVdhpahCcdEAsTgBH3Re6a+uozh/vpyHZ6P1I9e9C5X6lP5CJcZtaGqndTzkSqPj4=; Received: from n2100.armlinux.org.uk ([fd8f:7570:feb6:1:214:fdff:fe10:4f86]:48420) by pandora.armlinux.org.uk with esmtpsa (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.82_1-5b7a7c0-XX) (envelope-from ) id 1b5r5u-000549-Lq; Thu, 26 May 2016 09:56:42 +0100 Received: from linux by n2100.armlinux.org.uk with local (Exim 4.76) (envelope-from ) id 1b5r5q-0002tM-L7; Thu, 26 May 2016 09:56:38 +0100 Date: Thu, 26 May 2016 09:56:38 +0100 From: Russell King - ARM Linux To: Pratyush Anand Subject: Re: [PATCH kexec-tools 12/32] kexec: add helper to exlude a region from a set of memory ranges Message-ID: <20160526085638.GF19428@n2100.arm.linux.org.uk> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160526_015709_884039_667BDB05 X-CRM114-Status: GOOD ( 27.88 ) 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: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org 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 On Wed, May 25, 2016 at 01:30:44PM +0530, Pratyush Anand wrote: > On Tue, May 3, 2016 at 3:52 PM, Russell King wrote: > > +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; > > What if r->start == range->start) && (r->end == range->end)? I suppose that could happen, so what about this patch on top (untested). This isn't something I'd be able to test on real hardware, it needs a separate test-suite to be written to do modular testing... kexec/mem_regions.c | 94 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/kexec/mem_regions.c b/kexec/mem_regions.c index c6ba942..4e7061b 100644 --- a/kexec/mem_regions.c +++ b/kexec/mem_regions.c @@ -29,6 +29,50 @@ void mem_regions_sort(struct memory_ranges *ranges) } /** + * 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 + * @base: base address of memory region + * @length: length of memory region in bytes + * @type: type of memory region + * + * Add the memory region to the set of ranges, and return %0 if successful, + * or %-1 if we ran out of space. + */ +int mem_regions_add(struct memory_ranges *ranges, unsigned long long base, + unsigned long long length, int type) +{ + struct memory_range *range; + + if (ranges->size >= ranges->max_size) + return -1; + + range = ranges->ranges + ranges->size++; + range->start = base; + range->end = base + length - 1; + range->type = type; + + 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 @@ -40,7 +84,7 @@ void mem_regions_sort(struct memory_ranges *ranges) int mem_regions_exclude(struct memory_ranges *ranges, const struct memory_range *range) { - int i; + int i, ret; for (i = 0; i < ranges->size; i++) { struct memory_range *r = ranges->ranges + i; @@ -51,25 +95,25 @@ int mem_regions_exclude(struct memory_ranges *ranges, */ 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; + 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 { - 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; + ret = mem_regions_add(ranges, range->end + 1, + r->end, 0); + if (ret < 0) + return ret; /* * Update this area to end before excluded @@ -82,31 +126,3 @@ int mem_regions_exclude(struct memory_ranges *ranges, } 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 - * @base: base address of memory region - * @length: length of memory region in bytes - * @type: type of memory region - * - * Add the memory region to the set of ranges, and return %0 if successful, - * or %-1 if we ran out of space. - */ -int mem_regions_add(struct memory_ranges *ranges, unsigned long long base, - unsigned long long length, int type) -{ - struct memory_range *range; - - if (ranges->size >= ranges->max_size) - return -1; - - range = ranges->ranges + ranges->size++; - range->start = base; - range->end = base + length - 1; - range->type = type; - - return 0; -} -