From patchwork Wed Nov 15 13:00:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 165340 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp2517795vqg; Wed, 15 Nov 2023 05:01:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IFZmJSqQp/E/IgC0ui1E16Ks4RCZAZfOTwqohr7rivPOYsWTSH6Q9q5AIl4XThBoqnMW42M X-Received: by 2002:a05:6808:2a42:b0:3af:75d5:e10b with SMTP id fa2-20020a0568082a4200b003af75d5e10bmr12768610oib.25.1700053284649; Wed, 15 Nov 2023 05:01:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700053284; cv=none; d=google.com; s=arc-20160816; b=MmuARAX9RxQGkrSJQsDt6/6+AQ8Inz3n/SdW7FzNlx5MOJbjso6n2v7Hiy5odW946d YzhkXJhesK71QTuvlcknGs/XArYmtk+ygzUyd3cmbU/0GB06LGDjlw+JaMi+jvPSUWSv enTPyINDd6XHtHv4K48Skf7fUbDBrKvZIWC++0+COkvJNjXJ2wTI8op448MiK63dyiEB Fgb5zuxiSc+/B9Xj/AynxvhGAGCT6y43N7zM6bXicnPpwlpLNxvor08ecOsJ0bDY/fMb mQpE1fCHIdS3fgGVNZamqKRgfDuMEr6YDvC8JhemcplAd4vN1bIxLcoP38kY/SKaDiLS sEng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=MsmoLDm3Vg05W8pfD2SPdYXQ50SnWlB0SUHodkqBzuQ=; fh=j/OeJoB5VTCimedaTDm208C6AWDsg1qwBUcOYckLF1o=; b=wpF7mzzEOiuVKLiNpRy/Yj7F5lm3kKRnVwpW5jw0iJfBVc909mDhI4eAxJSPw6ZdjJ 57B31E5L7W3gP1WX1RSMrdCrl2xTjczs6fwCK1fMrmBNhW35tfN64iGOGrwjonoqSpes 4gqfuZ5SKO1zdNfoR5CdnJGpcF4O+tO+XIviIdmQIXyNYKNIXex3IvlngcG6DtQKkNwJ bppzBovJpDz7pruAk5uOLSmMrc5MnpM73i5xdbOQyhhYzMouxd5Sxn305EsdFp69LxSL 5ViP4mIGZ6pfEZoJBIrb0A3GT8SkNevGHDWfsJlb9Qd69+sJkOj0VbIsvfSEiQEgg1Cz QN3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eY7g9BvI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id v8-20020a056830090800b006c647feb84asi3881268ott.132.2023.11.15.05.01.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 05:01:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eY7g9BvI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id A94FE8090255; Wed, 15 Nov 2023 05:01:00 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343863AbjKONAq (ORCPT + 28 others); Wed, 15 Nov 2023 08:00:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343873AbjKONAl (ORCPT ); Wed, 15 Nov 2023 08:00:41 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A93D3193 for ; Wed, 15 Nov 2023 05:00:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700053235; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=MsmoLDm3Vg05W8pfD2SPdYXQ50SnWlB0SUHodkqBzuQ=; b=eY7g9BvIoMfROfu7bsPjyD2e3cHmOrpyMNnKTJ4ZkTzPjpJhfKwWyzWlDYTio4nY+lT8NM b8TjHcxKrKl3JRUOa40SHkqwUNaS29s+MldWGe/Hea5NvF9D9BAQ7zT8yEJwjQ6w1fkcUD /n6pYcTPbLjsCxnruGTbxGGfqvBtT8U= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-625-1eLEqSY3OeWVW86v4Tny6A-1; Wed, 15 Nov 2023 08:00:32 -0500 X-MC-Unique: 1eLEqSY3OeWVW86v4Tny6A-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C3AC4810FC1; Wed, 15 Nov 2023 13:00:31 +0000 (UTC) Received: from localhost (unknown [10.72.112.9]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BA6FF492BFD; Wed, 15 Nov 2023 13:00:30 +0000 (UTC) Date: Wed, 15 Nov 2023 21:00:27 +0800 From: Baoquan He To: linux-kernel@vger.kernel.org Cc: kexec@lists.infradead.org, x86@kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, takahiro.akashi@linaro.org Subject: [PATCH v2 1/2] resource: add walk_system_ram_res_rev() Message-ID: References: <20231114091658.228030-1-bhe@redhat.com> <20231114091658.228030-2-bhe@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20231114091658.228030-2-bhe@redhat.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 15 Nov 2023 05:01:00 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782530389720805935 X-GMAIL-MSGID: 1782635072708500912 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 to load kernel, initrd etc when preparing kexec reboot. Signed-off-by: AKASHI Takahiro Signed-off-by: Baoquan He --- v1->v2: - Use kvrealloc() to reallocate memory instead of kvcalloc(), this simplifies code. Suggested by Andrew. include/linux/ioport.h | 3 +++ kernel/resource.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/include/linux/ioport.h b/include/linux/ioport.h index 14f5cfabbbc8..db7fe25f3370 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -331,6 +331,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 866ef3663a0b..e8a244300e5b 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include #include @@ -429,6 +431,61 @@ int walk_system_ram_res(u64 start, u64 end, void *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, *rams; + int rams_size = 16, i; + unsigned long flags; + int ret = -1; + + /* create a list */ + rams = kvcalloc(rams_size, sizeof(struct resource), GFP_KERNEL); + if (!rams) + return ret; + + flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; + i = 0; + while ((start < end) && + (!find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res))) { + if (i >= rams_size) { + /* re-alloc */ + struct resource *rams_new; + + rams_new = kvrealloc(rams, rams_size * sizeof(struct resource), + (rams_size + 16) * sizeof(struct resource), + GFP_KERNEL); + if (!rams_new) + goto out; + + rams = rams_new; + rams_size += 16; + } + + rams[i].start = res.start; + rams[i++].end = res.end; + + start = res.end + 1; + } + + /* go reverse */ + for (i--; i >= 0; i--) { + ret = (*func)(&rams[i], arg); + if (ret) + break; + } + +out: + kvfree(rams); + return ret; +} + /* * This function calls the @func callback against all memory ranges, which * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.