Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp5556691pxj; Wed, 26 May 2021 13:30:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysGqNXpmpHvjCtuUgrRzemKffQ5VRAaiyVfSIAXfjRnSt+7rwHul6nwMiCYVuYPivbnMNX X-Received: by 2002:aa7:ccc6:: with SMTP id y6mr44064edt.303.1622061022062; Wed, 26 May 2021 13:30:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622061022; cv=none; d=google.com; s=arc-20160816; b=WcrBO58AHD9DSa7EnaIyjK8kHzqjQb4m+t0344xeZmEttTBBsI96i5Q28a+QIin28r mQ9r3ueTq7200ajOj2/EcZNhXYsSL90wVawlveI06ek0GB0actiMjN8ihcmhPwWoigP/ 0XS+HZnST+jcsjat/CVRLIDrdP6EtknjOPUtiz6lMOhZj8H5TySDTtF6qQljx91wY6UG 78n1V5P1mIsSqzX9gjWEp3sT6T0v25RMr4OiT3bwq42PYx4MsLgrh4HCoIHnoFn7eGtI q5pNCkB6bSgkJL3VpognZXOhfhOZgseLJvx0RPWDtcRXwlJ9e6kO3JKYTVrDOHWVAR1L ltNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=wzX4yGe+mWH/mf8RwGGDuKKLM/2harmyWGP30aGUzao=; b=ee22wzMIWApcMDnts8U6nHbprw9pmTYzR0gbsIHxhn7z2+TrXESFFcbkuxgLKq5mNG 0kPBuOGk44pzOfpHJ5v+wyf5zF+Ot5/+5bRp7IAuOE3s06kJS2NB5H+BGSTvjrmrBSiK ECgeKCv6CZTsEAuFMiBvTbfhaLsPaItF3TidBHe3nWmWTZ28jj+KJBivW85LOlBerirf VjIdMisbTPfdxDkZ6v553h/bA1hwhbz3zeWkcAHCNB8M8HijN/0ErvXYDR9tJ7fdLYdK QrHESeihcQ/nCpaE+VQI6NwbbdWiQjxIGDb37LkIz8mOHdSrKwr04Kq/H8FNcgW7vdST dDRQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dn20si121114ejc.545.2021.05.26.13.29.39; Wed, 26 May 2021 13:30:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235348AbhEZTHa (ORCPT + 99 others); Wed, 26 May 2021 15:07:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:42848 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234245AbhEZTHS (ORCPT ); Wed, 26 May 2021 15:07:18 -0400 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 807CB613CC; Wed, 26 May 2021 19:05:46 +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 1llyqe-003o26-RV; Wed, 26 May 2021 20:05:44 +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 , Moritz Fischer , kernel-team@android.com Subject: [PATCH 3/4] kernel/resource: Add walk_excluding_child_res() helper Date: Wed, 26 May 2021 20:05:30 +0100 Message-Id: <20210526190531.62751-4-maz@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210526190531.62751-1-maz@kernel.org> References: <20210526190531.62751-1-maz@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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, 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Once we have obtained a resource of a certain type from find_next_iomem_res(), it doesn't necessarily mean that the whole resource is usable, and we have cases where a child resource denotes an exclusion in the initial resource. Provide a new walker that deals with this exact case, and calls a callback on each resource fragment that doesn't have a child. Signed-off-by: Marc Zyngier --- include/linux/ioport.h | 4 +++ kernel/resource.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/include/linux/ioport.h b/include/linux/ioport.h index 8359c50f9988..526314a42ad2 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -323,6 +323,10 @@ extern int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start, u64 end, void *arg, int (*func)(struct resource *, void *)); +extern int +walk_excluding_child_res(struct resource *res, void *arg, + int (*func)(struct resource *, void *)); + struct resource *devm_request_free_mem_region(struct device *dev, struct resource *base, unsigned long size); struct resource *request_free_mem_region(struct resource *base, diff --git a/kernel/resource.c b/kernel/resource.c index 311b8d2c9957..1d9b5f653938 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -318,6 +318,86 @@ int release_resource(struct resource *old) EXPORT_SYMBOL(release_resource); +/** + * walk_excluding_child_res - call a callback function on each fragment of + * a resource that do not have a child resource + * + * @res: the root resource containing the initial range + * @arg: function argument for the callback @func + * @func: callback function that is called for each qualifying resource area + * + * For a given resource, remove all the child resources and feed the + * resulting fragments to kexec_locate_mem_hole_callback(). + */ +int walk_excluding_child_res(struct resource *res, void *arg, + int (*func)(struct resource *, void *)) +{ + struct resource *tmp, cursor; + int ret = 0; + + cursor = *res; + + /* Use .child for the head of the list, .sibling for the tail */ + cursor.child = cursor.sibling = NULL; + + read_lock(&resource_lock); + + for (tmp = res->child; tmp; tmp = tmp->sibling) { + struct resource *new; + + if (cursor.start < tmp->start) { + new = kmalloc(sizeof(*new), GFP_KERNEL); + if (!new) + goto cleanup; + + *new = (struct resource) { + .start = cursor.start, + .end = tmp->start - 1, + .flags = res->flags, + .desc = res->desc, + .parent = res->parent, + }; + + if (!cursor.child) + cursor.child = new; + if (cursor.sibling) + cursor.sibling->sibling = new; + cursor.sibling = new; + } + + /* + * This may result in a resource with a negative size + * at the very end of the loop. + */ + cursor.start = tmp->end + 1; + } + + read_unlock(&resource_lock); + + /* + * At this stage, the list pointed to by cursor.child contains + * every non-reserved blocks, completed by 'cursor' which + * contains the potential last block (may be empty). + */ + for (tmp = cursor.child; tmp; tmp = tmp->sibling) { + ret = func(tmp, arg); + if (ret) + break; + } + + if (!ret && cursor.start <= cursor.end) + ret = func(&cursor, tmp); + +cleanup: + while (cursor.child) { + tmp = cursor.child; + cursor.child = cursor.child->sibling; + kfree(tmp); + } + + return ret; +} + /** * find_next_iomem_res - Finds the lowest iomem resource that covers part of * [@start..@end]. -- 2.30.2