Received: by 10.213.65.68 with SMTP id h4csp1315244imn; Sat, 24 Mar 2018 09:14:59 -0700 (PDT) X-Google-Smtp-Source: AG47ELsDV9Bxy6bRyYEH2om1y0oP0cMVXTJweiqMuGF2sZlwcA/W7ScgeG9rXgdRcrZ1icJffH0R X-Received: by 2002:a17:902:b408:: with SMTP id x8-v6mr27892516plr.7.1521908099218; Sat, 24 Mar 2018 09:14:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521908099; cv=none; d=google.com; s=arc-20160816; b=oodF+RfavRLAsUg7kL97xURQDTer0LX/3zKhR5U1GsxFXFsWIIFecLqTvIN+OK1rtN vphNcFSNqVXU8hcq0FCPOpAOyA+AAn5gC8Wsez2GSmJGb/ubryjz1T4GI5/UIEFNjsdT f52q3y2hDJnW+AsuFwWVyRwnrgRLccutL47mB5dlzOd+uFqK13Lf56xjYw7pU6cpNMku 9xBPjp5D8RwWGdMGlpYeejboEeqZLktSqm+GI+NFlg0MhFcEDf35DQ+Os1guLfuWu4jt EaNEHU4qOLu52shx9xZKlAbhJWt5pORhDaQr5OGfvyGV2FaTT8b9YcVQpErHst2vtLQe mtZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature:arc-authentication-results; bh=IXg1ViJuxbJo4kpb7bmZp0p5TDDlR91QiT3y9DY1QIk=; b=EvMJh4+N/n2DPQSfw6lv5dbAalFvvor1hwsZ7nf08ONaMb4RMxDMBdWEctYObKJ3kA GDBs5Vje/drKNydvXaGISawIKWZJIRN5gJvkVZVrWqRMguiE2BtPsp6CZ5WE9J7bFkkx gAMu31O4bTcHpVF7qbaNQ8NwaTL7CCmDobE9CQWdUCKjpHkGfKcWfpH+JuZ+qgyNsYOe fUORMxedrFWwgbNK04XThfm4YNxO5gw1x8nTSpp48G/7SQUPFXpt6ZXNIwuwlUE12sDw V8siZUggIYkabp79Qbf4uEY+gsYOwnfSESmLpuMRbd/TCJl7P/5QGEZDArWRmiF6vIhf k2rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=M37kXPPJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f11-v6si8292212plj.164.2018.03.24.09.14.44; Sat, 24 Mar 2018 09:14:59 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=M37kXPPJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752517AbeCXQNw (ORCPT + 99 others); Sat, 24 Mar 2018 12:13:52 -0400 Received: from mail-pl0-f66.google.com ([209.85.160.66]:40136 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752139AbeCXQNv (ORCPT ); Sat, 24 Mar 2018 12:13:51 -0400 Received: by mail-pl0-f66.google.com with SMTP id x4-v6so9268650pln.7 for ; Sat, 24 Mar 2018 09:13:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:reply-to:references:mime-version :content-disposition:in-reply-to:user-agent; bh=IXg1ViJuxbJo4kpb7bmZp0p5TDDlR91QiT3y9DY1QIk=; b=M37kXPPJv+/3VpHPnLBVnCdISXsSWcgBumE2DC3As1/oM0jdZW+1otlWz1MDwIlEoG XmZpi2TBfFG4EsD8EyTc4GapMsRuTLsph1ctqp4FHyhbxHo8pM4n5DzfcKXrG1bSWFDm mcz+4lgBr8gTdXw79xkBidmjV6lXz/jklK3MwxAS8UiLf7CJKH9CqcpcjVdzBmiwoRmq tuTMVJkKLBfvkKKxZIIYiYOR+v48ZdegIiLge0M8dfJHXRjHr+xG3ZQkzL/XdArbjQgq YTC3WcJnI9jn9iuXq4DMraTP6b9HVw/a2IIFvPPF5Ev7nvKLK3luS60okGvwkEh42/H1 f/qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:reply-to :references:mime-version:content-disposition:in-reply-to:user-agent; bh=IXg1ViJuxbJo4kpb7bmZp0p5TDDlR91QiT3y9DY1QIk=; b=aJKQ/neM5zu5ZDWLOUHMuoa2EZq14HSJ4m9VYmciJA0NNDiUKFaMX/4C1RZJ+EEm3R 4cnHy1nP7GyjX3Rul/D53gkqpARPsaswOINWkmdMVh/s/LFKGaokJQutRNrCXxao+kJf Ee+FaToxvhydb4fYJN+ZEwbmdIfU3KzoFBlesOTPKCcZrFglUOGaEj49rM3UAI9vsdOO 1eNYQv28qzGAjrKbQ8ETZ9OuPA5gkG8O23jsAVTkRT2HpkkljE8AwLKlC/H2KrzROI6T ugMn944RF4hO7rq9DzWhBNFSQkDmEP8m7PMZMtQ3pA/iSpk3qS8uqB6e4olHp7qCrITg de4w== X-Gm-Message-State: AElRT7GpuwTaG+Gh4ptYMQke/3OiVIthB6vbBjft25QqusNZqAm8guzn PUBIXsvdfWL5YMDf1mJHTc4= X-Received: by 2002:a17:902:770f:: with SMTP id n15-v6mr18911966pll.92.1521908031038; Sat, 24 Mar 2018 09:13:51 -0700 (PDT) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id z14sm7875343pgv.26.2018.03.24.09.13.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 24 Mar 2018 09:13:50 -0700 (PDT) Date: Sun, 25 Mar 2018 00:13:43 +0800 From: Wei Yang To: Baoquan He Cc: Andrew Morton , prudo@linux.vnet.ibm.com, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, takahiro.akashi@linaro.org, ebiederm@xmission.com, dyoung@redhat.com, vgoyal@redhat.com Subject: Re: [PATCH 1/2] resource: add walk_system_ram_res_rev() Message-ID: <20180324161343.GA58414@WeideMacBook-Pro.local> Reply-To: Wei Yang References: <20180322033722.9279-1-bhe@redhat.com> <20180322033722.9279-2-bhe@redhat.com> <20180322152929.9b421af2f66cc819ad691207@linux-foundation.org> <20180323005845.GA25740@localhost.localdomain> <20180322190606.859a0f1c7e2d1b2958daeb9f@linux-foundation.org> <20180323031013.GB11150@localhost.localdomain> <20180323130620.7d60fc442463ed5c21898387@linux-foundation.org> <20180324133330.GD25740@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180324133330.GD25740@localhost.localdomain> User-Agent: Mutt/1.9.1 (2017-09-22) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Mar 24, 2018 at 09:33:30PM +0800, Baoquan He wrote: >> >> Yes. That sounds perfectly acceptable. >> >> It would be interesting to see what this approach looks like, if you >> have time to toss something together? > >OK, will make patches for reviewing. Thanks! Hi, Baoquan, Andrew I have come up with an implementation for top-down search the ram resources. Hope this would meet your need. From b36d50487f1d4e4d6a5103965a27101b3121e0ea Mon Sep 17 00:00:00 2001 From: Wei Yang Date: Sat, 24 Mar 2018 23:25:46 +0800 Subject: [PATCH] kernel/resource: add walk_system_ram_res_rev() As discussed on https://patchwork.kernel.org/patch/10300819/, this patch comes up with a variant implementation of walk_system_ram_res_rev(), which uses iteration instead of allocating array to store those resources. Signed-off-by: Wei Yang --- include/linux/ioport.h | 3 ++ kernel/resource.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/include/linux/ioport.h b/include/linux/ioport.h index da0ebaec25f0..473f1d9cb97e 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -277,6 +277,9 @@ extern int walk_system_ram_res(u64 start, u64 end, void *arg, int (*func)(struct resource *, void *)); extern int +walk_system_ram_res_rev(u64 start, u64 end, void *arg, + int (*func)(struct resource *, void *)); +extern int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start, u64 end, void *arg, int (*func)(struct resource *, void *)); diff --git a/kernel/resource.c b/kernel/resource.c index 769109f20fb7..ddf6b4c41498 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -73,6 +73,38 @@ static struct resource *next_resource(struct resource *p, bool sibling_only) return p->sibling; } +static struct resource *prev_resource(struct resource *p, bool sibling_only) +{ + struct resource *prev; + if (NULL == iomem_resource.child) + return NULL; + + if (p == NULL) { + prev = iomem_resource.child; + while (prev->sibling) + prev = prev->sibling; + } else { + if (p->parent->child == p) { + return p->parent; + } + + for (prev = p->parent->child; prev->sibling != p; + prev = prev->sibling) {} + } + + /* Caller wants to traverse through siblings only */ + if (sibling_only) + return prev; + + for (;prev->child;) { + prev = prev->child; + + while (prev->sibling) + prev = prev->sibling; + } + return prev; +} + static void *r_next(struct seq_file *m, void *v, loff_t *pos) { struct resource *p = v; @@ -401,6 +433,47 @@ static int find_next_iomem_res(struct resource *res, unsigned long desc, return 0; } +/* + * Finds the highest iomem resource existing within [res->start.res->end). + * The caller must specify res->start, res->end, res->flags, and optionally + * desc. If found, returns 0, res is overwritten, if not found, returns -1. + * This function walks the whole tree and not just first level children until + * and unless first_level_children_only is true. + */ +static int find_prev_iomem_res(struct resource *res, unsigned long desc, + bool first_level_children_only) +{ + struct resource *p; + + BUG_ON(!res); + BUG_ON(res->start >= res->end); + + read_lock(&resource_lock); + + for (p = prev_resource(NULL, first_level_children_only); p; + p = prev_resource(p, first_level_children_only)) { + if ((p->flags & res->flags) != res->flags) + continue; + if ((desc != IORES_DESC_NONE) && (desc != p->desc)) + continue; + if (p->end < res->start) { + p = NULL; + break; + } + if ((p->end >= res->start) && (p->start < res->end)) + break; + } + + read_unlock(&resource_lock); + if (!p) + return -1; + /* copy data */ + resource_clip(res, p->start, p->end); + res->flags = p->flags; + res->desc = p->desc; + return 0; +} + static int __walk_iomem_res_desc(struct resource *res, unsigned long desc, bool first_level_children_only, void *arg, @@ -422,6 +495,27 @@ static int __walk_iomem_res_desc(struct resource *res, unsigned long desc, return ret; } +static int __walk_iomem_res_rev_desc(struct resource *res, unsigned long desc, + bool first_level_children_only, + void *arg, + int (*func)(struct resource *, void *)) +{ + u64 orig_start = res->start; + int ret = -1; + + while ((res->start < res->end) && + !find_prev_iomem_res(res, desc, first_level_children_only)) { + ret = (*func)(res, arg); + if (ret) + break; + + res->end = res->start - 1; + res->start = orig_start; + } + + return ret; +} + /* * Walks through iomem resources and calls func() with matching resource * ranges. This walks through whole tree and not just first level children. @@ -468,6 +562,25 @@ int walk_system_ram_res(u64 start, u64 end, void *arg, arg, func); } +/* + * This function, being a variant of walk_system_ram_res(), calls the @func + * callback against all memory ranges of type System RAM which are marked as + * IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY in reversed order, i.e., from + * higher to lower. + */ +int walk_system_ram_res_rev(u64 start, u64 end, void *arg, + int (*func)(struct resource *, void *)) +{ + struct resource res; + + res.start = start; + res.end = end; + res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; + + return __walk_iomem_res_rev_desc(&res, IORES_DESC_NONE, true, + arg, func); +} + /* * This function calls the @func callback against all memory ranges, which * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY. -- 2.15.1 -- Wei Yang Help you, Help me