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=-9.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 1B056C433E5 for ; Fri, 21 Aug 2020 10:35:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D179B204EA for ; Fri, 21 Aug 2020 10:35:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FrULWNGU" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D179B204EA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 79C808D002F; Fri, 21 Aug 2020 06:35:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6FF888D0008; Fri, 21 Aug 2020 06:35:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5ED018D002F; Fri, 21 Aug 2020 06:35:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0077.hostedemail.com [216.40.44.77]) by kanga.kvack.org (Postfix) with ESMTP id 4356A8D0008 for ; Fri, 21 Aug 2020 06:35:03 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 03CA42C14 for ; Fri, 21 Aug 2020 10:35:03 +0000 (UTC) X-FDA: 77174218086.27.rest94_39181d427038 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin27.hostedemail.com (Postfix) with ESMTP id C966C3D663 for ; Fri, 21 Aug 2020 10:35:02 +0000 (UTC) X-HE-Tag: rest94_39181d427038 X-Filterd-Recvd-Size: 7695 Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by imf28.hostedemail.com (Postfix) with ESMTP for ; Fri, 21 Aug 2020 10:35:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598006101; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G68oLT4JVMoIG5DqaYBfCZBI9EqNtAD2sZmp6ATBUeA=; b=FrULWNGUo4md0VuZQnM9ccGnoG1bCbfjiS10prNeVMG1jZJcfWOHOQp2SojYbRZ/jqDhRz C23r2E074N3A9wlN4jJ3WBEA/AHaLAAfaPvN0kobbB+PqCnBG9JwugXDn87cdbxFZyQog7 jv/eNDQ+cSqQy3JTqAmhqocPO0epvzg= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-561-i2qdkbQ-NhGC9y6PSLFkzg-1; Fri, 21 Aug 2020 06:34:57 -0400 X-MC-Unique: i2qdkbQ-NhGC9y6PSLFkzg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E7F3A8030D0; Fri, 21 Aug 2020 10:34:54 +0000 (UTC) Received: from t480s.redhat.com (ovpn-114-87.ams2.redhat.com [10.36.114.87]) by smtp.corp.redhat.com (Postfix) with ESMTP id 05299756D8; Fri, 21 Aug 2020 10:34:47 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: virtualization@lists.linux-foundation.org, linux-mm@kvack.org, linux-hyperv@vger.kernel.org, xen-devel@lists.xenproject.org, David Hildenbrand , Andrew Morton , Michal Hocko , Dan Williams , Jason Gunthorpe , Kees Cook , Ard Biesheuvel , Thomas Gleixner , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Boris Ostrovsky , Juergen Gross , Stefano Stabellini , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Julien Grall , Pankaj Gupta , Baoquan He , Wei Yang Subject: [PATCH v1 2/5] kernel/resource: merge_system_ram_resources() to merge resources after hotplug Date: Fri, 21 Aug 2020 12:34:28 +0200 Message-Id: <20200821103431.13481-3-david@redhat.com> In-Reply-To: <20200821103431.13481-1-david@redhat.com> References: <20200821103431.13481-1-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Rspamd-Queue-Id: C966C3D663 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Some add_memory*() users add memory in small, contiguous memory blocks. Examples include virtio-mem, hyper-v balloon, and the XEN balloon. This can quickly result in a lot of memory resources, whereby the actual resource boundaries are not of interest (e.g., it might be relevant for DIMMs, exposed via /proc/iomem to user space). We really want to merge added resources in this scenario where possible. Let's provide an interface to trigger merging of applicable child resources. It will be, for example, used by virtio-mem to trigger merging of system ram resources it added to its resource container, but also by XEN and Hyper-V to trigger merging of system ram resources in iomem_resource. Note: We really want to merge after the whole operation succeeded, not directly when adding a resource to the resource tree (it would break add_memory_resource() and require splitting resources again when the operation failed - e.g., due to -ENOMEM). Cc: Andrew Morton Cc: Michal Hocko Cc: Dan Williams Cc: Jason Gunthorpe Cc: Kees Cook Cc: Ard Biesheuvel Cc: Thomas Gleixner Cc: "K. Y. Srinivasan" Cc: Haiyang Zhang Cc: Stephen Hemminger Cc: Wei Liu Cc: Boris Ostrovsky Cc: Juergen Gross Cc: Stefano Stabellini Cc: Roger Pau Monn=C3=A9 Cc: Julien Grall Cc: Pankaj Gupta Cc: Baoquan He Cc: Wei Yang Signed-off-by: David Hildenbrand --- include/linux/ioport.h | 3 +++ kernel/resource.c | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/include/linux/ioport.h b/include/linux/ioport.h index 52a91f5fa1a36..3bb0020cd6ddc 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -251,6 +251,9 @@ extern void __release_region(struct resource *, resou= rce_size_t, extern void release_mem_region_adjustable(struct resource *, resource_si= ze_t, resource_size_t); #endif +#ifdef CONFIG_MEMORY_HOTPLUG +extern void merge_system_ram_resources(struct resource *res); +#endif =20 /* Wrappers for managed devices */ struct device; diff --git a/kernel/resource.c b/kernel/resource.c index 1dcef5d53d76e..b4e0963edadd2 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -1360,6 +1360,58 @@ void release_mem_region_adjustable(struct resource= *parent, } #endif /* CONFIG_MEMORY_HOTREMOVE */ =20 +#ifdef CONFIG_MEMORY_HOTPLUG +static bool system_ram_resources_mergeable(struct resource *r1, + struct resource *r2) +{ + return r1->flags =3D=3D r2->flags && r1->end + 1 =3D=3D r2->start && + r1->name =3D=3D r2->name && r1->desc =3D=3D r2->desc && + !r1->child && !r2->child; +} + +/* + * merge_system_ram_resources - try to merge contiguous system ram resou= rces + * @parent: parent resource descriptor + * + * This interface is intended for memory hotplug, whereby lots of contig= uous + * system ram resources are added (e.g., via add_memory*()) by a driver,= and + * the actual resource boundaries are not of interest (e.g., it might be + * relevant for DIMMs). Only immediate child resources that are busy and + * don't have any children are considered. All applicable child resource= s + * must be immutable during the request. + * + * Note: + * - The caller has to make sure that no pointers to resources that migh= t + * get merged are held anymore. Callers should only trigger merging of= child + * resources when they are the only one adding system ram resources to= the + * parent (besides during boot). + * - release_mem_region_adjustable() will split on demand on memory hotu= nplug + */ +void merge_system_ram_resources(struct resource *parent) +{ + const unsigned long flags =3D IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; + struct resource *cur, *next; + + write_lock(&resource_lock); + + cur =3D parent->child; + while (cur && cur->sibling) { + next =3D cur->sibling; + if ((cur->flags & flags) =3D=3D flags && + system_ram_resources_mergeable(cur, next)) { + cur->end =3D next->end; + cur->sibling =3D next->sibling; + free_resource(next); + next =3D cur->sibling; + } + cur =3D next; + } + + write_unlock(&resource_lock); +} +EXPORT_SYMBOL(merge_system_ram_resources); +#endif /* CONFIG_MEMORY_HOTPLUG */ + /* * Managed region resource */ --=20 2.26.2