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 X-Spam-Level: X-Spam-Status: No, score=-17.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F22BEC47083 for ; Mon, 31 May 2021 10:00:32 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B90E5610E7 for ; Mon, 31 May 2021 10:00:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B90E5610E7 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=pJZbaw35iwDwcSCHcExw1qeKEBw68wHybXUVQXTARyA=; b=0BQDuL5oVoGjcp bzfSWUJrnNJ8h+p90Q5cQKf0RAuoxdM408QT22bljPqbCYBWsxNrHdU17EzHrcoFHUZly4E8/6qbB xOZ95jZFbOviixmBQH4I4a7eD2Kbi+iLO1iq1ims9mfXdtLe+ZHKPA2bQhR6avnqxHI29ymG8j5EQ sXxflmQfrQWEnRQs9gbUqki0FLW7Lqcnxk1Vf1YrocYXntgNoPA9ZGNzxDg1lT8nPoLCFT0izdj1o 0ZPm4rCQB30+MCnAjFtl/I9MMF+NrOreaGRwkzVZHtrB+F1v+vPeeLEBfHYWDVz0+we4y3ZZVjqx4 BEX0R+zzz/neR/e8/7+Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lnegw-00BkaZ-Rv; Mon, 31 May 2021 09:58:39 +0000 Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lnegM-00BkQ8-BI; Mon, 31 May 2021 09:58:04 +0000 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E24F8611EE; Mon, 31 May 2021 09:58:01 +0000 (UTC) Received: from 78.163-31-62.static.virginmediabusiness.co.uk ([62.31.163.78] helo=why.lan) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1lnegJ-004Z7v-UA; Mon, 31 May 2021 10:58:00 +0100 From: Marc Zyngier To: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Catalin Marinas , Will Deacon , Ard Biesheuvel , Mark Rutland , James Morse , Lorenzo Pieralisi , Hanjun Guo , Sudeep Holla , Eric Biederman , Bhupesh SHARMA , AKASHI Takahiro , Dave Young , Andrew Morton , Moritz Fischer , kernel-team@android.com Subject: [PATCH v2 3/5] kernel/resource: Allow find_next_iomem_res() to exclude overlapping child resources Date: Mon, 31 May 2021 10:57:18 +0100 Message-Id: <20210531095720.77469-4-maz@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210531095720.77469-1-maz@kernel.org> References: <20210531095720.77469-1-maz@kernel.org> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 62.31.163.78 X-SA-Exim-Rcpt-To: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, catalin.marinas@arm.com, will@kernel.org, ardb@kernel.org, mark.rutland@arm.com, james.morse@arm.com, lorenzo.pieralisi@arm.com, guohanjun@huawei.com, sudeep.holla@arm.com, ebiederm@xmission.com, bhupesh.sharma@linaro.org, takahiro.akashi@linaro.org, dyoung@redhat.com, akpm@linux-foundation.org, mdf@kernel.org, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210531_025802_473753_5C0B042C X-CRM114-Status: GOOD ( 19.41 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org find_next_iomem_res() returns the first resource that matches the input parameters (range, flags, desc). It however ignores any sub-resource that may invalidate the usefulness of such resource. Allow find_next_iomem_res() to filter out such sub-resources and wire it into the callers. As nobody is interested in this type of filtering yet, there shouldn't be any functional change. Acked-by: Catalin Marinas Reviewed-by: Ard Biesheuvel Signed-off-by: Marc Zyngier --- kernel/resource.c | 67 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/kernel/resource.c b/kernel/resource.c index ca9f5198a01f..0e4d2ca763cd 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -318,6 +318,42 @@ int release_resource(struct resource *old) EXPORT_SYMBOL(release_resource); +static int exclude_overlapping_child_res(struct resource *res, + struct resource *child) +{ + struct resource cursor = *res; + + for (; child; child = child->sibling) { + if (!resource_overlaps(&cursor, child)) + continue; + + if (cursor.start < child->start) { + *res = (struct resource) { + .start = cursor.start, + .end = child->start - 1, + .flags = res->flags, + .desc = res->desc, + .parent = res->parent, + }; + + return 0; + } + + /* + * This may result in a resource with a negative size + * at the very end of the loop. + */ + cursor.start = child->end + 1; + } + + if (cursor.start <= cursor.end) { + *res = cursor; + return 0; + } + + return -ENODEV; +} + /** * find_next_iomem_res - Finds the lowest iomem resource that covers part of * [@start..@end]. @@ -330,6 +366,7 @@ EXPORT_SYMBOL(release_resource); * @end: end address of same resource * @flags: flags which the resource must have * @desc: descriptor the resource must have + * @exclude_child_res: exclude parts of resource that have overlapping children * @res: return ptr, if resource found * * The caller must specify @start, @end, @flags, and @desc @@ -337,7 +374,7 @@ EXPORT_SYMBOL(release_resource); */ static int find_next_iomem_res(resource_size_t start, resource_size_t end, unsigned long flags, unsigned long desc, - struct resource *res) + bool exclude_child_res, struct resource *res) { struct resource *p; @@ -348,7 +385,7 @@ static int find_next_iomem_res(resource_size_t start, resource_size_t end, return -EINVAL; read_lock(&resource_lock); - +again: for (p = iomem_resource.child; p; p = next_resource(p)) { /* If we passed the resource we are looking for, stop */ if (p->start > end) { @@ -378,6 +415,15 @@ static int find_next_iomem_res(resource_size_t start, resource_size_t end, .desc = p->desc, .parent = p->parent, }; + + if (exclude_child_res && + exclude_overlapping_child_res(res, p->child)) { + start = res->end + 1; + if (start >= end) + p = NULL; + else + goto again; + } } read_unlock(&resource_lock); @@ -386,6 +432,7 @@ static int find_next_iomem_res(resource_size_t start, resource_size_t end, static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end, unsigned long flags, unsigned long desc, + bool exclude_child_res, void *arg, int (*func)(struct resource *, void *)) { @@ -393,7 +440,8 @@ static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end, int ret = -EINVAL; while (start < end && - !find_next_iomem_res(start, end, flags, desc, &res)) { + !find_next_iomem_res(start, end, flags, desc, + exclude_child_res, &res)) { ret = (*func)(&res, arg); if (ret) break; @@ -424,7 +472,7 @@ static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end, int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start, u64 end, void *arg, int (*func)(struct resource *, void *)) { - return __walk_iomem_res_desc(start, end, flags, desc, arg, func); + return __walk_iomem_res_desc(start, end, flags, desc, false, arg, func); } EXPORT_SYMBOL_GPL(walk_iomem_res_desc); @@ -440,8 +488,8 @@ int walk_system_ram_res(u64 start, u64 end, void *arg, { unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; - return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg, - func); + return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, + false, arg, func); } /* @@ -453,8 +501,8 @@ int walk_mem_res(u64 start, u64 end, void *arg, { unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY; - return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg, - func); + return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, + false, arg, func); } /* @@ -475,7 +523,8 @@ int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages, end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1; flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; while (start < end && - !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) { + !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, + false, &res)) { pfn = PFN_UP(res.start); end_pfn = PFN_DOWN(res.end + 1); if (end_pfn > pfn) -- 2.30.2 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel