Received: by 10.213.65.68 with SMTP id h4csp1102571imn; Thu, 22 Mar 2018 15:31:02 -0700 (PDT) X-Google-Smtp-Source: AG47ELswxBzYrw+azrf7jOgU9y+/Ja77j2v+xAvMrsILIz9pRLxgZfebBN0OGZzvp7TKXjvJsN0T X-Received: by 2002:a17:902:a2:: with SMTP id a31-v6mr26704497pla.204.1521757862261; Thu, 22 Mar 2018 15:31:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521757862; cv=none; d=google.com; s=arc-20160816; b=u26fi9RJsIgCVjt2j7mx2wfEu/QdnAsnuBrRCnkVARzMvi8t74V4urfWGRHkOXWnjB LECsbPJVOe6/5YpmnCmrgtGE/HuGx9zY7jGXUiX2mXewWS0/HPRjFM4edLhNAYPbOldV /0pdz45GJvrgWiRGwz2FKDHXaNbq2DqsuEuSbmTGeNrDuTg+hdGaBeiLTH3fFfHvlphS vESK6eKMuko8ziKqlElwPy69f5aclf3Z2Tv9ha5j4uF/lSgZ90gfQEy4yRcuxRxhe3LC 65yz/TaYuXJNepDGaOXB4rAvBBMg8dDK8ZwIFIAZaPM+Lww64x27rFlAXZMeLVqyBLZm sbgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :arc-authentication-results; bh=oBfU9l78BQQIc2zTs1qiOGV7VeBuSqvIUEM3g9zTQww=; b=rrbBuzilPeSxAI30YtfH2ykCiMFlaZgqzTKVqeFI4wkIKuIDyqmPoLoAvN1c7lSb9E ACQBUJaYP2gQeUwRsx9Znp01kIBME7vYLfF2EXJoht+Uu/QSVzAEqewiFNVVJrJ8AMbK BWz7b6d4SehbtZb7gTYXgQFcvyQzjR1/QI8sQpxq6XC4GGhuuSbTqm7fgbyGAO+tTiXn G3ZggjxVdNsbcuS3RKgDbNdx6Oh5Acacks2VlHacZNmH7Rmeu/HEigC6vzB/xWVrEokP g25ajVX4+ED5qOVaZw+t2magvq7ITJmPQQlUuiJ8UHNpgxgmnaLqpGHcq9C8Z6Iql5rD pPdA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v4si5520289pfb.284.2018.03.22.15.30.47; Thu, 22 Mar 2018 15:31:02 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752041AbeCVW3c (ORCPT + 99 others); Thu, 22 Mar 2018 18:29:32 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:49374 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751541AbeCVW3b (ORCPT ); Thu, 22 Mar 2018 18:29:31 -0400 Received: from akpm3.svl.corp.google.com (unknown [104.133.9.71]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 8679C1134; Thu, 22 Mar 2018 22:29:30 +0000 (UTC) Date: Thu, 22 Mar 2018 15:29:29 -0700 From: Andrew Morton To: Baoquan He Cc: linux-kernel@vger.kernel.org, kexec@lists.infradead.org, takahiro.akashi@linaro.org, ebiederm@xmission.com, vgoyal@redhat.com, dyoung@redhat.com, prudo@linux.vnet.ibm.com Subject: Re: [PATCH 1/2] resource: add walk_system_ram_res_rev() Message-Id: <20180322152929.9b421af2f66cc819ad691207@linux-foundation.org> In-Reply-To: <20180322033722.9279-2-bhe@redhat.com> References: <20180322033722.9279-1-bhe@redhat.com> <20180322033722.9279-2-bhe@redhat.com> X-Mailer: Sylpheed 3.6.0 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 22 Mar 2018 11:37:21 +0800 Baoquan He wrote: > From: AKASHI Takahiro > > This function, being a variant of walk_system_ram_res() introduced in > commit 8c86e70acead ("resource: provide new functions to walk through > resources"), walks through a list of all the resources of System RAM > in reversed order, i.e., from higher to lower. > > It will be used in kexec_file code. > > ... > > --- a/kernel/resource.c > +++ b/kernel/resource.c > @@ -23,6 +23,8 @@ > #include > #include > #include > +#include > +#include > #include > > > @@ -470,6 +472,67 @@ int walk_system_ram_res(u64 start, u64 end, void *arg, > } > > /* > + * 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. > + */ This should document the return value, as should walk_system_ram_res(). Why does it return -1 on error rather than an errno (ENOMEM)? > +int walk_system_ram_res_rev(u64 start, u64 end, void *arg, > + int (*func)(struct resource *, void *)) > +{ > + struct resource res, *rams; > + int rams_size = 16, i; > + int ret = -1; > + > + /* create a list */ > + rams = vmalloc(sizeof(struct resource) * rams_size); > + if (!rams) > + return ret; > + > + res.start = start; > + res.end = end; > + res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; > + i = 0; > + while ((res.start < res.end) && > + (!find_next_iomem_res(&res, IORES_DESC_NONE, true))) { > + if (i >= rams_size) { > + /* re-alloc */ > + struct resource *rams_new; > + int rams_new_size; > + > + rams_new_size = rams_size + 16; > + rams_new = vmalloc(sizeof(struct resource) > + * rams_new_size); > + if (!rams_new) > + goto out; > + > + memcpy(rams_new, rams, > + sizeof(struct resource) * rams_size); > + vfree(rams); > + rams = rams_new; > + rams_size = rams_new_size; > + } > + > + rams[i].start = res.start; > + rams[i++].end = res.end; > + > + res.start = res.end + 1; > + res.end = end; > + } > + > + /* go reverse */ > + for (i--; i >= 0; i--) { > + ret = (*func)(&rams[i], arg); > + if (ret) > + break; > + } erk, this is pretty nasty. Isn't there a better way :( > +out: > + vfree(rams); > + return ret; > +}